1package dev.keii.goldenage.utils;
2
3import org.bukkit.Bukkit;
4import org.bukkit.command.Command;
5import org.bukkit.command.SimpleCommandMap;
6import org.bukkit.plugin.SimplePluginManager;
7
8import java.lang.reflect.Field;
9import java.util.Map;
10
11public class CommandUtility {
12 @SuppressWarnings("unchecked")
13 public static void unregisterCommand(String name) {
14 try {
15 SimplePluginManager pluginManager = (SimplePluginManager) Bukkit.getPluginManager();
16
17 Field commandMapField = SimplePluginManager.class.getDeclaredField("commandMap");
18 commandMapField.setAccessible(true);
19 SimpleCommandMap commandMap = (SimpleCommandMap) commandMapField.get(pluginManager);
20
21 Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
22 knownCommandsField.setAccessible(true);
23 Map<String, Command> knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
24
25 // Remove the command and its aliases from the map
26 Command cmd = knownCommands.get(name);
27 if (cmd != null) {
28 knownCommands.remove(name);
29 for (String alias : cmd.getAliases()) {
30 knownCommands.remove(alias);
31 }
32 }
33
34 } catch (Exception e) {
35 e.printStackTrace();
36 }
37 }
38}