Remove from command collection on unregister (#408)

This commit is contained in:
Jason 2022-11-23 14:03:54 -07:00
parent ead52ef3aa
commit 025dc974ad
2 changed files with 17 additions and 4 deletions

View file

@ -545,7 +545,7 @@ public abstract class CommandManager<C> {
this.commandRegistrationHandler.unregisterRootCommand((StaticArgument<?>) node.getValue()); this.commandRegistrationHandler.unregisterRootCommand((StaticArgument<?>) node.getValue());
// We then delete it from the tree. // 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. // And lastly we re-build the entire tree.
this.commandTree.verifyAndRegister(); this.commandTree.verifyAndRegister();

View file

@ -58,6 +58,7 @@ import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Queue; import java.util.Queue;
import java.util.Set; import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.apiguardian.api.API; import org.apiguardian.api.API;
@ -952,15 +953,27 @@ public final class CommandTree<C> {
return null; return null;
} }
void deleteRecursively(final @NonNull Node<@Nullable CommandArgument<C, ?>> node, final boolean root) { void deleteRecursively(
final @NonNull Node<@Nullable CommandArgument<C, ?>> node,
final boolean root,
final Consumer<Command<C>> op
) {
for (final Node<@Nullable CommandArgument<C, ?>> child : new ArrayList<>(node.children)) { for (final Node<@Nullable CommandArgument<C, ?>> child : new ArrayList<>(node.children)) {
this.deleteRecursively(child, false); this.deleteRecursively(child, false, op);
} }
final @Nullable CommandArgument<C, ?> value = node.getValue();
final @Nullable Command<C> owner = value == null ? null : value.getOwningCommand();
if (owner != null) {
op.accept(owner);
}
this.removeNode(node, root); this.removeNode(node, root);
} }
private boolean removeNode(final @NonNull Node<@Nullable CommandArgument<C, ?>> node, final boolean root) { private boolean removeNode(
final @NonNull Node<@Nullable CommandArgument<C, ?>> node,
final boolean root
) {
if (root) { if (root) {
// root command node - remove it from the root tree // root command node - remove it from the root tree
return this.internalTree.removeChild(node); return this.internalTree.removeChild(node);