diff --git a/Common/src/main/java/com/hypherionmc/craterlib/api/event/server/CraterRegisterCommandEvent.java b/Common/src/main/java/com/hypherionmc/craterlib/api/event/server/CraterRegisterCommandEvent.java new file mode 100644 index 0000000..5fe36e1 --- /dev/null +++ b/Common/src/main/java/com/hypherionmc/craterlib/api/event/server/CraterRegisterCommandEvent.java @@ -0,0 +1,23 @@ +package com.hypherionmc.craterlib.api.event.server; + +import com.hypherionmc.craterlib.core.event.CraterEvent; +import com.mojang.brigadier.CommandDispatcher; +import net.minecraft.commands.CommandSourceStack; + +public class CraterRegisterCommandEvent extends CraterEvent { + + private final CommandDispatcher dispatcher; + + public CraterRegisterCommandEvent(CommandDispatcher dispatcher) { + this.dispatcher = dispatcher; + } + + public CommandDispatcher getDispatcher() { + return dispatcher; + } + + @Override + public boolean canCancel() { + return false; + } +} diff --git a/Common/src/main/java/com/hypherionmc/craterlib/api/event/server/CraterServerLifecycleEvent.java b/Common/src/main/java/com/hypherionmc/craterlib/api/event/server/CraterServerLifecycleEvent.java new file mode 100644 index 0000000..877e86b --- /dev/null +++ b/Common/src/main/java/com/hypherionmc/craterlib/api/event/server/CraterServerLifecycleEvent.java @@ -0,0 +1,45 @@ +package com.hypherionmc.craterlib.api.event.server; + +import com.hypherionmc.craterlib.core.event.CraterEvent; +import net.minecraft.server.MinecraftServer; + +public class CraterServerLifecycleEvent extends CraterEvent { + + public CraterServerLifecycleEvent() {} + + @Override + public boolean canCancel() { + return false; + } + + public static class Starting extends CraterServerLifecycleEvent { + + private final MinecraftServer server; + + public Starting(MinecraftServer server) { + this.server = server; + } + + public MinecraftServer getServer() { + return server; + } + } + + public static class Started extends CraterServerLifecycleEvent { + + public Started() {} + + } + + public static class Stopping extends CraterServerLifecycleEvent { + + public Stopping() {} + + } + + public static class Stopped extends CraterServerLifecycleEvent { + + public Stopped() {} + + } +} diff --git a/Fabric/src/main/java/com/hypherionmc/craterlib/CraterLibInitializer.java b/Fabric/src/main/java/com/hypherionmc/craterlib/CraterLibInitializer.java index 4d32ff1..807565f 100644 --- a/Fabric/src/main/java/com/hypherionmc/craterlib/CraterLibInitializer.java +++ b/Fabric/src/main/java/com/hypherionmc/craterlib/CraterLibInitializer.java @@ -1,13 +1,29 @@ package com.hypherionmc.craterlib; +import com.hypherionmc.craterlib.api.event.server.CraterRegisterCommandEvent; +import com.hypherionmc.craterlib.api.event.server.CraterServerLifecycleEvent; import com.hypherionmc.craterlib.common.FabricCommonPlatform; +import com.hypherionmc.craterlib.core.event.CraterEvent; +import com.hypherionmc.craterlib.core.event.CraterEventBus; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; public class CraterLibInitializer implements ModInitializer { @Override public void onInitialize() { - ServerLifecycleEvents.SERVER_STARTING.register(server -> FabricCommonPlatform.server = server); + CommandRegistrationCallback.EVENT.register( + (dispatcher, registryAccess, environment) -> CraterEventBus.INSTANCE.postEvent(new CraterRegisterCommandEvent(dispatcher))); + + + ServerLifecycleEvents.SERVER_STARTING.register(server -> { + FabricCommonPlatform.server = server; + CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Starting(server)); + }); + + ServerLifecycleEvents.SERVER_STARTED.register(li -> CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Started())); + ServerLifecycleEvents.SERVER_STOPPING.register(server -> CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Stopping())); + ServerLifecycleEvents.SERVER_STOPPED.register(server -> CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Stopped())); } } diff --git a/Forge/src/main/java/com/hypherionmc/craterlib/common/ForgeCommonEvents.java b/Forge/src/main/java/com/hypherionmc/craterlib/common/ForgeCommonEvents.java index 4e4ec41..99a49fb 100644 --- a/Forge/src/main/java/com/hypherionmc/craterlib/common/ForgeCommonEvents.java +++ b/Forge/src/main/java/com/hypherionmc/craterlib/common/ForgeCommonEvents.java @@ -1,9 +1,14 @@ package com.hypherionmc.craterlib.common; import com.hypherionmc.craterlib.CraterConstants; +import com.hypherionmc.craterlib.api.event.server.CraterRegisterCommandEvent; +import com.hypherionmc.craterlib.api.event.server.CraterServerLifecycleEvent; +import com.hypherionmc.craterlib.core.event.CraterEventBus; import com.hypherionmc.craterlib.core.systems.internal.CreativeTabRegistry; import net.minecraft.world.item.CreativeModeTab; import net.minecraftforge.event.BuildCreativeModeTabContentsEvent; +import net.minecraftforge.event.RegisterCommandsEvent; +import net.minecraftforge.event.server.*; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; @@ -19,4 +24,28 @@ public class ForgeCommonEvents { .forEach(itemPair -> event.accept(itemPair.getRight())); } + @SubscribeEvent + public static void serverStarting(ServerStartingEvent event) { + CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Starting(event.getServer())); + } + + @SubscribeEvent + public static void serverStarted(ServerStartedEvent event) { + CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Started()); + } + + @SubscribeEvent + public static void serverStopping(ServerStoppingEvent event) { + CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Stopping()); + } + + @SubscribeEvent + public static void serverStopped(ServerStoppedEvent event) { + CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Stopped()); + } + + @SubscribeEvent + public void onCommandRegister(RegisterCommandsEvent event) { + CraterEventBus.INSTANCE.postEvent(new CraterRegisterCommandEvent(event.getDispatcher())); + } }