fix root command unregister (#389)

This commit is contained in:
Pasqual Koschmieder 2022-08-26 20:10:26 +02:00 committed by Jason
parent 4179bc4290
commit 7c934eccc7
3 changed files with 34 additions and 10 deletions

View file

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

View file

@ -952,23 +952,22 @@ public final class CommandTree<C> {
return null;
}
void deleteRecursively(final @NonNull Node<@Nullable CommandArgument<C, ?>> node) {
void deleteRecursively(final @NonNull Node<@Nullable CommandArgument<C, ?>> node, final boolean root) {
for (final Node<@Nullable CommandArgument<C, ?>> child : new ArrayList<>(node.children)) {
this.deleteRecursively(child);
this.deleteRecursively(child, false);
}
// We need to remove it from the tree.
this.removeNode(node);
this.removeNode(node, root);
}
private boolean removeNode(final @NonNull Node<@Nullable CommandArgument<C, ?>> node) {
if (this.getRootNodes().contains(node)) {
this.internalTree.removeChild(node);
private boolean removeNode(final @NonNull Node<@Nullable CommandArgument<C, ?>> node, final boolean root) {
if (root) {
// root command node - remove it from the root tree
return this.internalTree.removeChild(node);
} else {
// child node - remove it from the parent node
return node.getParent().removeChild(node);
}
return false;
}
/**

View file

@ -146,4 +146,29 @@ class CommandDeletionTest {
assertThat(this.commandManager.commandTree().getRootNodes()).isEmpty();
}
@Test
void deleteCommandWithSameArgumentNameAsRootCommand() {
// Arrange
this.commandManager.command(this.commandManager.commandBuilder("test").build());
this.commandManager.command(this.commandManager.commandBuilder("hello").literal("test").build());
// Pre-assert.
this.commandManager.executeCommand(new TestCommandSender(), "test").join();
this.commandManager.executeCommand(new TestCommandSender(), "hello test").join();
// Act
this.commandManager.deleteRootCommand("hello");
// Assert
this.commandManager.executeCommand(new TestCommandSender(), "test").join();
final CompletionException completionException = assertThrows(
CompletionException.class,
() -> this.commandManager.executeCommand(new TestCommandSender(), "hello").join()
);
assertThat(completionException).hasCauseThat().isInstanceOf(NoSuchCommandException.class);
assertThat(this.commandManager.suggest(new TestCommandSender(), "")).contains("test");
assertThat(this.commandManager.commandTree().getRootNodes()).hasSize(1);
}
}