diff --git a/cloud-core/src/main/java/cloud/commandframework/CommandManager.java b/cloud-core/src/main/java/cloud/commandframework/CommandManager.java index 26760e43..07b2510b 100644 --- a/cloud-core/src/main/java/cloud/commandframework/CommandManager.java +++ b/cloud-core/src/main/java/cloud/commandframework/CommandManager.java @@ -545,7 +545,7 @@ public abstract class CommandManager { this.commandRegistrationHandler.unregisterRootCommand((StaticArgument) node.getValue()); // We then delete it from the tree. - this.commandTree.deleteRecursively(node, true); + this.commandTree.deleteRecursively(node, true, this.commands::remove); // And lastly we re-build the entire tree. this.commandTree.verifyAndRegister(); diff --git a/cloud-core/src/main/java/cloud/commandframework/CommandTree.java b/cloud-core/src/main/java/cloud/commandframework/CommandTree.java index aa881bd6..8b901e49 100644 --- a/cloud-core/src/main/java/cloud/commandframework/CommandTree.java +++ b/cloud-core/src/main/java/cloud/commandframework/CommandTree.java @@ -58,6 +58,7 @@ import java.util.Objects; import java.util.Optional; import java.util.Queue; import java.util.Set; +import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apiguardian.api.API; @@ -952,15 +953,27 @@ public final class CommandTree { return null; } - void deleteRecursively(final @NonNull Node<@Nullable CommandArgument> node, final boolean root) { + void deleteRecursively( + final @NonNull Node<@Nullable CommandArgument> node, + final boolean root, + final Consumer> op + ) { for (final Node<@Nullable CommandArgument> child : new ArrayList<>(node.children)) { - this.deleteRecursively(child, false); + this.deleteRecursively(child, false, op); } + final @Nullable CommandArgument value = node.getValue(); + final @Nullable Command owner = value == null ? null : value.getOwningCommand(); + if (owner != null) { + op.accept(owner); + } this.removeNode(node, root); } - private boolean removeNode(final @NonNull Node<@Nullable CommandArgument> node, final boolean root) { + private boolean removeNode( + final @NonNull Node<@Nullable CommandArgument> node, + final boolean root + ) { if (root) { // root command node - remove it from the root tree return this.internalTree.removeChild(node);