Skip to content

Commit

Permalink
feat(bouquet): server start event
Browse files Browse the repository at this point in the history
  • Loading branch information
hugeblank committed Sep 17, 2024
1 parent 1f73881 commit e870cdd
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.hugeblank.bouquet.mixin.client;
package dev.hugeblank.bouquet.mixin.client.network;

import dev.hugeblank.bouquet.api.lib.ClientEventsLib;
import net.minecraft.client.network.ClientPlayerEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.hugeblank.bouquet.mixin.server.integrated;

import dev.hugeblank.bouquet.api.lib.DefaultEventsLib;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.integrated.IntegratedServer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(IntegratedServer.class)
public class IntegratedServerMixin {
@Inject(at = @At("TAIL"), method = "setupServer")
private void init(CallbackInfoReturnable<Boolean> cir) {
DefaultEventsLib.SERVER_START.invoker().onServerStart((MinecraftServer) (Object) this);
}
}
7 changes: 4 additions & 3 deletions bouquet/src/client/resources/bouquet.client.mixins.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"required": true,
"package": "dev.hugeblank.bouquet.mixin.client",
"package": "dev.hugeblank.bouquet.mixin",
"compatibilityLevel": "JAVA_21",
"client": [
"ClientPlayerEntityMixin",
"gui.hud.InGameHudMixin"
"client.gui.hud.InGameHudMixin",
"client.network.ClientPlayerEntityMixin",
"server.integrated.IntegratedServerMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ public class BouquetModInitializer implements ModInitializer {

@Override
public void onInitialize() {
/* TODO: Missing APIs
/* TODO: Missing/nerfed APIs
- Resource Pack
- Recipes Library
- Config Library (maybe it's better that it's gone?)
- Game Library (some stuff now requires scripts to get the MinecraftServer instance)
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.block.BlockState;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Hand;
Expand Down Expand Up @@ -37,8 +38,12 @@ public interface PlayerBlockInteract {
void onPlayerBlockInteraction(BlockState state, ServerWorld world, BlockPos pos, ServerPlayerEntity player, Hand hand, BlockHitResult hitResult);
}

public interface Tick {
void onServerTick();
public interface ServerTick {
void onServerTick(MinecraftServer server);
}

public interface ServerStart {
void onServerStart(MinecraftServer server);
}

public interface CommandRegistration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public class DefaultEventsLib implements WrappedLuaLibrary {
@LuaWrapped public static final SimpleEventType<CommonEventHandlers.PlayerBlockCollision> PLAYER_BLOCK_COLLISION; // player collides with a block
@LuaWrapped public static final SimpleEventType<CommonEventHandlers.PlayerDeath> PLAYER_DEATH; // player dies
@LuaWrapped public static final SimpleEventType<CommonEventHandlers.PlayerBlockInteract> BLOCK_INTERACT; // player interacts (right clicks) with a block
@LuaWrapped public static final SimpleEventType<CommonEventHandlers.Tick> TICK; // server gets ticked
@LuaWrapped public static final SimpleEventType<CommonEventHandlers.ServerTick> SERVER_TICK; // server gets ticked
@LuaWrapped public static final SimpleEventType<CommonEventHandlers.CommandRegistration> COMMAND_REGISTER; // the result of a registered command
@LuaWrapped public static final SimpleEventType<CommonEventHandlers.ServerStart> SERVER_START; // server has started

static {
CHAT_MESSAGE = new SimpleEventType<>(Identifier.of("allium:chat_message"));
Expand All @@ -27,8 +28,9 @@ public class DefaultEventsLib implements WrappedLuaLibrary {
PLAYER_BLOCK_COLLISION = new SimpleEventType<>(Identifier.of("allium:player_block_collision"));
PLAYER_DEATH = new SimpleEventType<>(Identifier.of("allium:player_death"));
BLOCK_INTERACT = new SimpleEventType<>(Identifier.of("allium:block_interact"));
TICK = new SimpleEventType<>(Identifier.of("allium:server_tick"));
SERVER_TICK = new SimpleEventType<>(Identifier.of("allium:server_tick"));
COMMAND_REGISTER = new SimpleEventType<>(Identifier.of("allium:command_register"));
SERVER_START = new SimpleEventType<>(Identifier.of("allium:server_start"));
}

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
package dev.hugeblank.bouquet.mixin.server;

import com.mojang.datafixers.DataFixer;
import dev.hugeblank.bouquet.api.lib.DefaultEventsLib;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.resource.ResourcePackManager;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.SaveLoader;
import net.minecraft.server.WorldGenerationProgressListenerFactory;
import net.minecraft.util.ApiServices;
import net.minecraft.world.level.storage.LevelStorage;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.net.Proxy;
import java.util.function.BooleanSupplier;

@Mixin(MinecraftServer.class)
public abstract class MinecraftServerMixin {
@Shadow public abstract DynamicRegistryManager.Immutable getRegistryManager();

@Inject(at = @At("TAIL"), method = "<init>")
private void init(Thread serverThread, LevelStorage.Session session, ResourcePackManager dataPackManager, SaveLoader saveLoader, Proxy proxy, DataFixer dataFixer, ApiServices apiServices, WorldGenerationProgressListenerFactory worldGenerationProgressListenerFactory, CallbackInfo ci) {
// TODO: Server start event
}

@Inject(at = @At("TAIL"), method = "tick")
private void tick(BooleanSupplier shouldKeepTicking, CallbackInfo ci) {
DefaultEventsLib.TICK.invoker().onServerTick();
DefaultEventsLib.SERVER_TICK.invoker().onServerTick((MinecraftServer) (Object) this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.hugeblank.bouquet.mixin.server;

import dev.hugeblank.bouquet.api.lib.DefaultEventsLib;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.dedicated.MinecraftDedicatedServer;
import net.minecraft.test.TestServer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(value = {MinecraftDedicatedServer.class, TestServer.class})
public class MinecraftServerSubclassMixin {
@Inject(at = @At("TAIL"), method = "setupServer")
private void init(CallbackInfoReturnable<Boolean> cir) {
DefaultEventsLib.SERVER_START.invoker().onServerStart((MinecraftServer) (Object) this);
}
}
1 change: 1 addition & 0 deletions bouquet/src/main/resources/bouquet.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"entity.EntityMixin",
"resource.ResourcePackManagerMixin",
"server.MinecraftServerMixin",
"server.MinecraftServerSubclassMixin",
"server.PlayerManagerMixin",
"server.network.ServerPlayerEntityMixin"
],
Expand Down

0 comments on commit e870cdd

Please sign in to comment.