🐛 Fix command registration for Velocity

We need to recreate the Brigadier command node each time a command is extended, and the code was only letting each command get registered once.
This commit is contained in:
Alexander Söderberg 2020-09-26 17:28:35 +02:00 committed by Alexander Söderberg
parent 0d44a8c944
commit 7036beb8ad
4 changed files with 12 additions and 16 deletions

View file

@ -282,7 +282,7 @@ public final class CloudBrigadierManager<C, S> {
final LiteralArgumentBuilder<S> literalArgumentBuilder = LiteralArgumentBuilder.<S>literal(root.getLiteral()) final LiteralArgumentBuilder<S> literalArgumentBuilder = LiteralArgumentBuilder.<S>literal(root.getLiteral())
.requires(sender -> permissionChecker.test(sender, (CommandPermission) cloudCommand.getNodeMeta() .requires(sender -> permissionChecker.test(sender, (CommandPermission) cloudCommand.getNodeMeta()
.getOrDefault("permission", Permission.empty()))); .getOrDefault("permission", Permission.empty())));
if (cloudCommand.isLeaf() && cloudCommand.getValue() != null) { if (cloudCommand.getValue() != null && cloudCommand.getValue().getOwningCommand() != null) {
literalArgumentBuilder.executes(executor); literalArgumentBuilder.executes(executor);
} }
final LiteralCommandNode<S> constructedRoot = literalArgumentBuilder.build(); final LiteralCommandNode<S> constructedRoot = literalArgumentBuilder.build();

View file

@ -78,6 +78,11 @@ public class CloudVelocityTest {
annotationParser.parse(this); annotationParser.parse(this);
} }
@CommandMethod(value = "test", permission = "cloud.root")
private void testRoot(@Nonnull final CommandSource source) {
source.sendMessage(TextComponent.builder("Hello from the root!", NamedTextColor.GOLD));
}
@CommandMethod("test <num> [str]") @CommandMethod("test <num> [str]")
private void testCommand(@Nonnull @Argument(value = "str", defaultValue = "potato") final String string, private void testCommand(@Nonnull @Argument(value = "str", defaultValue = "potato") final String string,
@Nonnull final CommandSource source, @Nonnull final CommandSource source,

View file

@ -67,9 +67,6 @@ public class VelocityCommandManager<C> extends CommandManager<C> {
@Override @Override
public final boolean hasPermission(@Nonnull final C sender, @Nonnull final String permission) { public final boolean hasPermission(@Nonnull final C sender, @Nonnull final String permission) {
if (permission.isEmpty()) {
return true;
}
return this.backwardsCommandSenderMapper.apply(sender).hasPermission(permission); return this.backwardsCommandSenderMapper.apply(sender).hasPermission(permission);
} }

View file

@ -41,9 +41,7 @@ import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionException;
final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationHandler { final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationHandler {
@ -53,7 +51,6 @@ final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationH
+ "Please contact the server administrators if you believe that this is in error."; + "Please contact the server administrators if you believe that this is in error.";
private static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command. Type \"/help\" for help."; private static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command. Type \"/help\" for help.";
private final Map<CommandArgument<?, ?>, BrigadierCommand> registeredCommands = new HashMap<>();
private CloudBrigadierManager<C, CommandSource> brigadierManager; private CloudBrigadierManager<C, CommandSource> brigadierManager;
private VelocityCommandManager<C> manager; private VelocityCommandManager<C> manager;
@ -70,9 +67,6 @@ final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationH
@Override @Override
public boolean registerCommand(@Nonnull final Command<?> command) { public boolean registerCommand(@Nonnull final Command<?> command) {
final CommandArgument<?, ?> argument = command.getArguments().get(0); final CommandArgument<?, ?> argument = command.getArguments().get(0);
if (this.registeredCommands.containsKey(argument)) {
return false;
}
final List<String> aliases = ((StaticArgument<C>) argument).getAlternativeAliases(); final List<String> aliases = ((StaticArgument<C>) argument).getAlternativeAliases();
final BrigadierCommand brigadierCommand = new BrigadierCommand( final BrigadierCommand brigadierCommand = new BrigadierCommand(
this.brigadierManager.createLiteralCommandNode(command.getArguments().get(0).getName(), (Command<C>) command, this.brigadierManager.createLiteralCommandNode(command.getArguments().get(0).getName(), (Command<C>) command,
@ -137,8 +131,8 @@ final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationH
final CommandMeta commandMeta = this.manager.getProxyServer().getCommandManager() final CommandMeta commandMeta = this.manager.getProxyServer().getCommandManager()
.metaBuilder(brigadierCommand) .metaBuilder(brigadierCommand)
.aliases(aliases.toArray(new String[0])).build(); .aliases(aliases.toArray(new String[0])).build();
aliases.forEach(this.manager.getProxyServer().getCommandManager()::unregister);
this.manager.getProxyServer().getCommandManager().register(commandMeta, brigadierCommand); this.manager.getProxyServer().getCommandManager().register(commandMeta, brigadierCommand);
this.registeredCommands.put(argument, brigadierCommand);
return true; return true;
} }