Skip to content

Commit

Permalink
Fix bug where the namespaced version of a command registered while Bu…
Browse files Browse the repository at this point in the history
…kkit is enabled would not be sent to the player
  • Loading branch information
willkroboth committed Aug 6, 2023
1 parent 0300cd9 commit 6be5f57
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ public void postCommandRegistration(RegisteredCommand registeredCommand, Literal

// Adding commands to the other (Why bukkit/spigot?!) dispatcher usually happens in `CraftServer#syncCommands`
root.addChild(resultantNode);
root.addChild(namespaceNode(resultantNode));

// Do the same for the aliases
for(LiteralCommandNode<Source> node: aliasNodes) {
Expand All @@ -456,6 +457,7 @@ public void postCommandRegistration(RegisteredCommand registeredCommand, Literal
command.setPermission(permNode);

root.addChild(node);
root.addChild(namespaceNode(node));
}

// Adding the command to the help map usually happens in `CommandAPIBukkit#onEnable`
Expand All @@ -468,6 +470,23 @@ public void postCommandRegistration(RegisteredCommand registeredCommand, Literal
}
}

private LiteralCommandNode<Source> namespaceNode(LiteralCommandNode<Source> original) {
// Adapted from a section of `CraftServer#syncCommands`
LiteralCommandNode<Source> clone = new LiteralCommandNode<>(
"minecraft:" + original.getLiteral(),
original.getCommand(),
original.getRequirement(),
original.getRedirect(),
original.getRedirectModifier(),
original.isFork()
);

for (CommandNode<Source> child : original.getChildren()) {
clone.addChild(child);
}
return clone;
}

@Override
public LiteralCommandNode<Source> registerCommandNode(LiteralArgumentBuilder<Source> node) {
return getBrigadierDispatcher().register(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ void testOnEnableRegisterAndUnregisterCommand() {
assertNotNull(resourcesRoot.getChild("alias1"));
assertNotNull(resourcesRoot.getChild("alias2"));

// Namespaces should be in the resources dispatcher too
assertNotNull(resourcesRoot.getChild("minecraft:command"));
assertNotNull(resourcesRoot.getChild("minecraft:alias1"));
assertNotNull(resourcesRoot.getChild("minecraft:alias2"));


// Check the help topic was added
HelpTopic topic = server.getHelpMap().getHelpTopic("/command");
Expand Down Expand Up @@ -256,6 +261,9 @@ void testOnEnableRegisterAndUnregisterCommand() {
// Command should be removed from resources dispatcher
assertNull(resourcesRoot.getChild("command"));

// Namespace should still be there
assertNotNull(resourcesRoot.getChild("minecraft:command"));


// Help topic should be gone
assertNull(server.getHelpMap().getHelpTopic("/command"));
Expand All @@ -280,9 +288,12 @@ void testOnEnableRegisterAndUnregisterCommand() {
// Update commands should have been called for all players on the server
Mockito.verify(updateCommandsPlayer, Mockito.times(3)).updateCommands();

// Namespace should be gone
// Namespace should be gone from Bukkit's map
assertNull(commandMap.getCommand("minecraft:command"));

// Namespace should be gone from resources dispatcher
assertNull(resourcesRoot.getChild("minecraft:command"));

// Namespace should fail
assertCommandFailsWith(runCommandsPlayer, "minecraft:command argument",
"Unknown or incomplete command, see below for error at position 0: <--[HERE]");
Expand Down

0 comments on commit 6be5f57

Please sign in to comment.