diff --git a/patches/minecraft/net/minecraft/command/Commands.java.patch b/patches/minecraft/net/minecraft/command/Commands.java.patch index 427b323de9..6aa0bbb922 100644 --- a/patches/minecraft/net/minecraft/command/Commands.java.patch +++ b/patches/minecraft/net/minecraft/command/Commands.java.patch @@ -1,6 +1,14 @@ --- a/net/minecraft/command/Commands.java +++ b/net/minecraft/command/Commands.java -@@ -202,6 +202,14 @@ +@@ -183,6 +183,7 @@ + if (p_i232148_1_.field_237219_d_) { + PublishCommand.func_198581_a(this.field_197062_b); + } ++ net.minecraftforge.event.ForgeEventFactory.onCommandRegister(this.field_197062_b, p_i232148_1_); + + this.field_197062_b.findAmbiguities((p_201302_1_, p_201302_2_, p_201302_3_, p_201302_4_) -> { + field_197061_a.warn("Ambiguity between arguments {} and {} with inputs: {}", this.field_197062_b.getPath(p_201302_2_), this.field_197062_b.getPath(p_201302_3_), p_201302_4_); +@@ -202,6 +203,14 @@ try { try { diff --git a/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java b/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java index edfa54af65..47ff26671e 100644 --- a/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java +++ b/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java @@ -28,6 +28,7 @@ import net.minecraft.world.server.ServerWorld; import net.minecraftforge.common.loot.LootModifierManager; import net.minecraftforge.common.util.FakePlayerFactory; import net.minecraftforge.event.AddReloadListenerEvent; +import net.minecraftforge.event.RegisterCommandsEvent; import net.minecraftforge.event.entity.EntityJoinWorldEvent; import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.event.world.ChunkEvent; @@ -39,6 +40,8 @@ import net.minecraftforge.event.TickEvent; import net.minecraftforge.event.TickEvent.ClientTickEvent; import net.minecraftforge.event.TickEvent.Phase; import net.minecraftforge.event.TickEvent.ServerTickEvent; +import net.minecraftforge.server.command.ForgeCommand; +import net.minecraftforge.server.command.ConfigCommand; public class ForgeInternalHandler { @@ -112,6 +115,13 @@ public class ForgeInternalHandler ForgeHooks.updateBurns(); } + @SubscribeEvent + public void onCommandsRegister(RegisterCommandsEvent event) + { + new ForgeCommand(event.getDispatcher()); + ConfigCommand.register(event.getDispatcher()); + } + private static LootModifierManager INSTANCE; @SubscribeEvent diff --git a/src/main/java/net/minecraftforge/common/ForgeMod.java b/src/main/java/net/minecraftforge/common/ForgeMod.java index 069146b130..2e76f570df 100644 --- a/src/main/java/net/minecraftforge/common/ForgeMod.java +++ b/src/main/java/net/minecraftforge/common/ForgeMod.java @@ -114,7 +114,6 @@ public class ForgeMod implements WorldPersistenceHooks.WorldPersistenceHook modEventBus.addListener(this::gatherData); modEventBus.register(this); ATTRIBUTES.register(modEventBus); - MinecraftForge.EVENT_BUS.addListener(this::serverStarting); MinecraftForge.EVENT_BUS.addListener(this::serverStopping); MinecraftForge.EVENT_BUS.addGenericListener(SoundEvent.class, this::missingSoundMapping); ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, ForgeConfig.clientSpec); @@ -147,12 +146,6 @@ public class ForgeMod implements WorldPersistenceHooks.WorldPersistenceHook */ } - public void serverStarting(FMLServerStartingEvent evt) - { - new ForgeCommand(evt.getCommandDispatcher()); - ConfigCommand.register(evt.getCommandDispatcher()); - } - public void serverStopping(FMLServerStoppingEvent evt) { WorldWorkerManager.clear(); diff --git a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java index 1b456594ef..2e9fb58727 100644 --- a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java +++ b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java @@ -27,9 +27,12 @@ import javax.annotation.Nullable; import com.mojang.blaze3d.matrix.MatrixStack; +import com.mojang.brigadier.CommandDispatcher; import net.minecraft.block.NetherPortalBlock; import net.minecraft.block.BlockState; import net.minecraft.client.util.ITooltipFlag; +import net.minecraft.command.CommandSource; +import net.minecraft.command.Commands; import net.minecraft.entity.Entity; import net.minecraft.entity.MobEntity; import net.minecraft.entity.SpawnReason; @@ -724,4 +727,10 @@ public class ForgeEventFactory MinecraftForge.EVENT_BUS.post(event); return event.getListeners(); } + + public static void onCommandRegister(CommandDispatcher dispatcher, Commands.EnvironmentType environment) + { + RegisterCommandsEvent event = new RegisterCommandsEvent(dispatcher, environment); + MinecraftForge.EVENT_BUS.post(event); + } } diff --git a/src/main/java/net/minecraftforge/event/RegisterCommandsEvent.java b/src/main/java/net/minecraftforge/event/RegisterCommandsEvent.java new file mode 100644 index 0000000000..07de0c97a0 --- /dev/null +++ b/src/main/java/net/minecraftforge/event/RegisterCommandsEvent.java @@ -0,0 +1,36 @@ +package net.minecraftforge.event; + +import com.mojang.brigadier.CommandDispatcher; +import net.minecraft.command.*; +import net.minecraft.resources.*; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.eventbus.api.Event; + + +/** + * Commands are rebuilt whenever {@link DataPackRegistries} is recreated. + * You can use this event to register your commands whenever the {@link net.minecraft.command.Commands} class in constructed. + * + * The event is fired on the {@link MinecraftForge#EVENT_BUS} + */ +public class RegisterCommandsEvent extends Event +{ + private final CommandDispatcher dispatcher; + private final Commands.EnvironmentType environment; + + public RegisterCommandsEvent(CommandDispatcher dispatcher, Commands.EnvironmentType environment) + { + this.dispatcher = dispatcher; + this.environment = environment; + } + + public CommandDispatcher getDispatcher() + { + return dispatcher; + } + + public Commands.EnvironmentType getEnvironment() + { + return environment; + } +} diff --git a/src/main/java/net/minecraftforge/fml/event/server/FMLServerStartingEvent.java b/src/main/java/net/minecraftforge/fml/event/server/FMLServerStartingEvent.java index 7e7520ac55..1a0856b84d 100644 --- a/src/main/java/net/minecraftforge/fml/event/server/FMLServerStartingEvent.java +++ b/src/main/java/net/minecraftforge/fml/event/server/FMLServerStartingEvent.java @@ -19,14 +19,13 @@ package net.minecraftforge.fml.event.server; -import com.mojang.brigadier.CommandDispatcher; -import net.minecraft.command.CommandSource; import net.minecraft.server.MinecraftServer; /** * Called after {@link FMLServerAboutToStartEvent} and before {@link FMLServerStartedEvent}. - * This event allows for customizations of the server, such as loading custom commands, perhaps customizing recipes or - * other activities. + * This event allows for customizations of the server. + * + * If you need to add commands use {@link net.minecraftforge.event.RegisterCommandsEvent}. * * @author cpw */ @@ -37,7 +36,4 @@ public class FMLServerStartingEvent extends ServerLifecycleEvent super(server); } - public CommandDispatcher getCommandDispatcher() { - return server.getCommandManager().getDispatcher(); - } }