Fix /dh command being deleted after reloading.

This commit is contained in:
Acuadragon100
2025-11-06 15:19:20 +01:00
parent 41c6b2936b
commit 63acd94fd4
2 changed files with 30 additions and 13 deletions
@@ -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();
@@ -12,25 +12,42 @@ import static net.minecraft.commands.Commands.literal;
*/
public class CommandInitializer
{
private final CommandDispatcher<CommandSourceStack> commandDispatcher;
private boolean serverReady = false;
private CommandDispatcher<CommandSourceStack> commandDispatcher;
/**
* Constructs a new instance of this class.
*
* @param commandDispatcher The dispatcher to use for registering commands.
*/
public CommandInitializer(CommandDispatcher<CommandSourceStack> 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<CommandSourceStack> commandDispatcher)
{
if (!serverReady)
{
this.commandDispatcher = commandDispatcher;
return;
}
LiteralArgumentBuilder<CommandSourceStack> 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);
}
}