From 2c8a36ed54f346b09839b08fb88c5308b1092bf9 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Mon, 11 Dec 2017 21:55:05 +0100 Subject: [PATCH] CommandsAPI - Implemented CommandsManager to manage registering of commands by both BSB and its addons --- .../api/commands/CompositeCommand.java | 4 -- .../bskyblock/managers/CommandsManager.java | 45 +++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/main/java/us/tastybento/bskyblock/managers/CommandsManager.java diff --git a/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java b/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java index 69e539726..8c4fe8cab 100644 --- a/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java +++ b/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java @@ -23,8 +23,6 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi this.subCommands = new LinkedHashMap<>(); this.setup(); - - plugin.getNMSHandler().getServerCommandMap().register(label, this); } public CompositeCommand(String label, String description, String... aliases) { @@ -35,8 +33,6 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi this.subCommands = new LinkedHashMap<>(); this.setup(); - - plugin.getNMSHandler().getServerCommandMap().register(label, this); } public abstract void setup(); diff --git a/src/main/java/us/tastybento/bskyblock/managers/CommandsManager.java b/src/main/java/us/tastybento/bskyblock/managers/CommandsManager.java new file mode 100644 index 000000000..dd8576459 --- /dev/null +++ b/src/main/java/us/tastybento/bskyblock/managers/CommandsManager.java @@ -0,0 +1,45 @@ +package us.tastybento.bskyblock.managers; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import us.tastybento.bskyblock.BSkyBlock; +import us.tastybento.bskyblock.api.BSModule; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public final class CommandsManager { + + private Map> commands = new LinkedHashMap<>(); + + public void registerCommand(BSModule module, Command command) { + List cmds = new ArrayList<>(); + if (commands.containsKey(module)) { + cmds = commands.get(module); + } + + cmds.add(command); + commands.put(module, cmds); + BSkyBlock.getPlugin().getNMSHandler().getServerCommandMap().register(command.getLabel(), command); + } + + public void unregisterCommand(Command command) { + + } + + public List getCommands(BSModule module) { + return commands.get(module); + } + + public Command getCommand(String label) { + for (List cmds : commands.values()) { + for (Command cmd : cmds) { + if (cmd.getLabel().equalsIgnoreCase(label)) return cmd; + } + } + return null; + } + +}