From 8ffe17fbe59671e3a69f3132984393dc92fc77c8 Mon Sep 17 00:00:00 2001 From: Mariusz Matyszczak Date: Thu, 1 Aug 2024 02:19:51 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Command=20unregister=20feature.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plus/lib/command/CommandManager.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/com/marcusslover/plus/lib/command/CommandManager.java b/src/main/java/com/marcusslover/plus/lib/command/CommandManager.java index f59723f..6a84bf6 100644 --- a/src/main/java/com/marcusslover/plus/lib/command/CommandManager.java +++ b/src/main/java/com/marcusslover/plus/lib/command/CommandManager.java @@ -77,6 +77,28 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String commandLab return this; } + /** + * Unregister a command. + * @param command the command + */ + public void unregister(@NotNull ICommand command) { + Command commandAnnotation = this.getCommandAnnotation(command); + if (commandAnnotation == null) { + return; + } + String name = commandAnnotation.name(); + CommandMap commandMap = Bukkit.getCommandMap(); + List keys = new ArrayList<>(); + commandMap.getKnownCommands().forEach((key, value) -> { + if (value.getName().equals(name)) { + keys.add(key); + } + }); + for (String key : keys) { + commandMap.getKnownCommands().remove(key); + } + } + @NotNull public Set getCommands() { return this.commandSet;