From 63acd94fd4ca776e4b8dd2d56d285310902b7f9d Mon Sep 17 00:00:00 2001 From: Acuadragon100 <8165958-acuadragon100@users.noreply.gitlab.com> Date: Thu, 6 Nov 2025 15:19:20 +0100 Subject: [PATCH] Fix /dh command being deleted after reloading. --- .../common/AbstractModInitializer.java | 6 +-- .../common/commands/CommandInitializer.java | 37 ++++++++++++++----- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/common/src/main/java/com/seibel/distanthorizons/common/AbstractModInitializer.java b/common/src/main/java/com/seibel/distanthorizons/common/AbstractModInitializer.java index f8eeb62c6..367fd5289 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/AbstractModInitializer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/AbstractModInitializer.java @@ -115,8 +115,8 @@ public abstract class AbstractModInitializer this.initializeModCompat(); LOGGER.info(ModInfo.READABLE_NAME + " server Initialized, adding event subscribers..."); - - this.subscribeRegisterCommandsEvent(dispatcher -> { this.commandInitializer = new CommandInitializer(dispatcher); }); + this.commandInitializer = new CommandInitializer(); + this.subscribeRegisterCommandsEvent(dispatcher -> { commandInitializer.initCommands(dispatcher); }); this.subscribeServerStartingEvent(server -> { @@ -124,7 +124,7 @@ public abstract class AbstractModInitializer this.initConfig(); this.postInit(); - this.commandInitializer.initCommands(); + this.commandInitializer.onServerReady(); this.checkForUpdates(); diff --git a/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java b/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java index 0cf47d98d..d38e7ebab 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/commands/CommandInitializer.java @@ -12,25 +12,42 @@ import static net.minecraft.commands.Commands.literal; */ public class CommandInitializer { - private final CommandDispatcher commandDispatcher; + private boolean serverReady = false; + + private CommandDispatcher commandDispatcher; /** * Constructs a new instance of this class. - * - * @param commandDispatcher The dispatcher to use for registering commands. */ - public CommandInitializer(CommandDispatcher commandDispatcher) - { - this.commandDispatcher = commandDispatcher; + public CommandInitializer() {} + + /** + * Notify the command initializer that the game is ready to accept commands. + * If {@link CommandInitializer#initCommands(CommandDispatcher)} has been fired before it was ready, it will also initialize the commands. + */ + public void onServerReady() { + serverReady = true; + if (commandDispatcher != null) + { + initCommands(commandDispatcher); + commandDispatcher = null; + } } - - /** * Initializes all available commands. + * If the game is not ready yet, it stores the dispatcher to initialize the commands later. + * + * @param commandDispatcher The command dispatcher to register commands to. */ - public void initCommands() + public void initCommands(CommandDispatcher commandDispatcher) { + if (!serverReady) + { + this.commandDispatcher = commandDispatcher; + return; + } + LiteralArgumentBuilder builder = literal("dh") .requires(source -> source.hasPermission(4)); @@ -43,7 +60,7 @@ public class CommandInitializer builder.then(new CrashCommand().buildCommand()); } - this.commandDispatcher.register(builder); + commandDispatcher.register(builder); } }