From fa29b4de22d725834686f16ba7292713b952971b Mon Sep 17 00:00:00 2001 From: Christopher Nethercott Date: Sun, 2 Apr 2023 22:47:44 +0100 Subject: [PATCH] Add deregister ability to completions and contexts (#364) * feat: add deregister methods to completions and dependencies * fix: change unregister to deregister * feat: add javadocs to deregisterCompleition * fix: add throws to javadocs for dependency methods * fix: missing negation thanks @JOO200 * fix: change deregister -> unregister * Fix missed rename of deregister to unregister --------- Co-authored-by: chickeneer --- .../co/aikar/commands/CommandCompletions.java | 14 +++++++++++ .../co/aikar/commands/CommandManager.java | 25 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/core/src/main/java/co/aikar/commands/CommandCompletions.java b/core/src/main/java/co/aikar/commands/CommandCompletions.java index c048cf94..158f983a 100644 --- a/core/src/main/java/co/aikar/commands/CommandCompletions.java +++ b/core/src/main/java/co/aikar/commands/CommandCompletions.java @@ -82,6 +82,20 @@ public class CommandCompletions { return this.completionMap.put(prepareCompletionId(id), handler); } + /** + * Unregister a completion handler. + * @param id + * @return + * @throws IllegalStateException If the completion couldn't be found + */ + public CommandCompletionHandler unregisterCompletion(String id) { + if (!this.completionMap.containsKey(id)) { + throw new IllegalStateException("The supplied key " + id + " does not exist in any completions"); + } + + return this.completionMap.remove(id); + } + /** * Registr a completion handler to provide command completions based on the user input. * This handler is declared to be safe to be executed asynchronously. diff --git a/core/src/main/java/co/aikar/commands/CommandManager.java b/core/src/main/java/co/aikar/commands/CommandManager.java index ae4ab8d3..0b15ab98 100644 --- a/core/src/main/java/co/aikar/commands/CommandManager.java +++ b/core/src/main/java/co/aikar/commands/CommandManager.java @@ -532,6 +532,31 @@ public abstract class CommandManager< dependencies.put(clazz, key, instance); } + + /** + * Unregisters an instance of the class, it will no longer be able to be injected + * + * @param clazz the class the injector should look for to remove + * @throws IllegalStateException If the dependency was not found. + */ + public void unregisterDependency(Class clazz) { + unregisterDependency(clazz, clazz.getName()); + } + + /** + * Unregisters an instance of the class, it will no longer be able to be injected + * + * @param clazz the class the injector should look for to remove + * @param key the key which needs to be present if that + * @throws IllegalStateException If the dependency was not found. + */ + public void unregisterDependency(Class clazz, String key) { + if (!dependencies.containsKey(clazz, key)) { + throw new IllegalStateException("Unable to unregister a dependency of " + clazz.getName() + " with the key " + key + " because it wasn't registered"); + } + + dependencies.remove(clazz, key); + } /** * Attempts to inject instances of classes registered with {@link CommandManager#registerDependency(Class, Object)}