From 600cdd77c0c1bbb79fff9c5ed06d45de2bd0528b Mon Sep 17 00:00:00 2001 From: syeyoung Date: Thu, 17 Nov 2022 22:52:19 +0900 Subject: - Unload commands Signed-off-by: syeyoung --- .../kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'mod/src/main') diff --git a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java index 91541a86..f45164e9 100755 --- a/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java +++ b/mod/src/main/java/kr/syeyoung/dungeonsguide/mod/DungeonsGuide.java @@ -47,6 +47,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.*; import net.minecraft.client.resources.IResourceManager; import net.minecraft.client.resources.IResourcePack; +import net.minecraft.command.ICommand; import net.minecraft.launchwrapper.LaunchClassLoader; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.client.event.GuiOpenEvent; @@ -130,6 +131,12 @@ public class DungeonsGuide implements DGInterface { registeredListeners.add(object); MinecraftForge.EVENT_BUS.register(object); } + private List registeredCommands = new ArrayList<>(); + + public void registerCommands(ICommand command) { + registeredCommands.add(command); + ClientCommandHandler.instance.registerCommand(command); + } public void init(File f) { @@ -194,8 +201,8 @@ public class DungeonsGuide implements DGInterface { CommandDungeonsGuide commandDungeonsGuide = new CommandDungeonsGuide(); CommandDgDebug command = new CommandDgDebug(); - ClientCommandHandler.instance.registerCommand(commandDungeonsGuide); - ClientCommandHandler.instance.registerCommand(command); + registerCommands(commandDungeonsGuide); + registerCommands(command); registerEventsForge(command); registerEventsForge(commandDungeonsGuide); @@ -248,6 +255,15 @@ public class DungeonsGuide implements DGInterface { for (Object registeredListener : registeredListeners) { MinecraftForge.EVENT_BUS.unregister(registeredListener); } + Set commands = ReflectionHelper.getPrivateValue(ClientCommandHandler.class, ClientCommandHandler.instance, "commandSet"); + + for (ICommand registeredCommand : registeredCommands) { + ClientCommandHandler.instance.getCommands().remove(registeredCommand.getCommandName()); + for (String commandAlias : registeredCommand.getCommandAliases()) { + ClientCommandHandler.instance.getCommands().remove(commandAlias); + } + commands.remove(registeredCommand); + } THREAD_GROUP.interrupt(); THREAD_GROUP.stop(); try { -- cgit