diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 2cd1a0a..30da3ec 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -33,9 +33,9 @@ import java.util.logging.Level; public abstract class SWCommand { - private boolean enabled = true; - private final Set commandSet = new HashSet<>(); - private final Set commandHelpSet = new HashSet<>(); + private final Command command; + private final LinkedList commandSet = new LinkedList<>(); + private final LinkedList commandHelpSet = new LinkedList<>(); private final Map> localTypeMapper = new HashMap<>(); protected SWCommand(String command) { @@ -43,10 +43,9 @@ public abstract class SWCommand { } protected SWCommand(String command, String... aliases) { - SWCommandUtils.commandMap.register("steamwar", new Command(command, "", "/" + command, Arrays.asList(aliases)) { + this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) { @Override public boolean execute(CommandSender sender, String alias, String[] args) { - if (!enabled) return false; for (SubCommand subCommand : commandSet) { if (subCommand.invoke(sender, args)) { return false; @@ -62,7 +61,6 @@ public abstract class SWCommand { @Override public List tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { - if (!enabled) return Collections.emptyList(); List strings = new ArrayList<>(); for (SubCommand subCommand : commandSet) { List tabCompletes = subCommand.tabComplete(sender, args); @@ -78,7 +76,8 @@ public abstract class SWCommand { } return strings; } - }); + }; + SWCommandUtils.commandMap.register("steamwar", this.command); for (Method method : getClass().getDeclaredMethods()) { addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> { @@ -103,7 +102,7 @@ public abstract class SWCommand { Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); return; } - commandHelpSet.add(new SubCommand(this, method, anno.value())); + commandHelpSet.addLast(new SubCommand(this, method, anno.value())); }); } for (Method method : getClass().getDeclaredMethods()) { @@ -130,7 +129,7 @@ public abstract class SWCommand { return; } } - commandSet.add(new SubCommand(this, method, anno.value())); + commandSet.addLast(new SubCommand(this, method, anno.value())); }); } } @@ -169,12 +168,12 @@ public abstract class SWCommand { }); } - protected boolean isEnabled() { - return enabled; - } - - protected void setEnabled(boolean enabled) { - this.enabled = enabled; + protected void unregister() { + SWCommandUtils.knownCommandMap.remove(command.getName()); + for (String alias : command.getAliases()) { + SWCommandUtils.knownCommandMap.remove(alias); + } + command.unregister(SWCommandUtils.commandMap); } @Retention(RetentionPolicy.RUNTIME) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 2ddf34d..b97db6d 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -21,6 +21,7 @@ package de.steamwar.command; import org.bukkit.Bukkit; import org.bukkit.GameMode; +import org.bukkit.command.Command; import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -77,6 +78,7 @@ public class SWCommandUtils { } static final CommandMap commandMap; + static final Map knownCommandMap; static { try { @@ -87,6 +89,14 @@ public class SWCommandUtils { Bukkit.shutdown(); throw new SecurityException("Oh shit. Commands cannot be registered.", exception); } + try { + final Field knownCommandsField = commandMap.getClass().getDeclaredField("knownCommands"); + knownCommandsField.setAccessible(true); + knownCommandMap = (Map) knownCommandsField.get(commandMap); + } catch (NoSuchFieldException | IllegalAccessException exception) { + Bukkit.shutdown(); + throw new SecurityException("Oh shit. Commands cannot be registered.", exception); + } } static Object[] generateArgumentArray(TypeMapper[] parameters, String[] args, boolean varArgs, String[] subCommand) {