Hackily solve issue where Bukkit doesn't create a new Brigadier command per alias, so that command aliases get the full Brigadier treatment

This commit is contained in:
Alexander Söderberg 2020-09-19 22:49:03 +02:00
parent 1fede2b4c0
commit d83690cdcf
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
14 changed files with 213 additions and 82 deletions

View file

@ -25,7 +25,6 @@ package com.intellectualsites.commands.bukkit;
import com.intellectualsites.commands.Command;
import com.intellectualsites.commands.arguments.CommandArgument;
import com.intellectualsites.commands.arguments.StaticArgument;
import com.intellectualsites.commands.exceptions.ArgumentParseException;
import com.intellectualsites.commands.exceptions.InvalidCommandSenderException;
import com.intellectualsites.commands.exceptions.InvalidSyntaxException;
@ -51,13 +50,15 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
private final Command<C> cloudCommand;
@SuppressWarnings("unchecked")
BukkitCommand(@Nonnull final Command<C> cloudCommand,
BukkitCommand(@Nonnull final String label,
@Nonnull final List<String> aliases,
@Nonnull final Command<C> cloudCommand,
@Nonnull final CommandArgument<C, ?> command,
@Nonnull final BukkitCommandManager<C> manager) {
super(command.getName(),
super(label,
cloudCommand.getCommandMeta().getOrDefault("description", ""),
"",
((StaticArgument<C>) command).getAlternativeAliases());
aliases);
this.command = command;
this.manager = manager;
this.cloudCommand = cloudCommand;
@ -74,48 +75,52 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
this.manager.executeCommand(sender,
builder.toString())
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
(InvalidSyntaxException) throwable, (c, e) ->
commandSender.sendMessage(ChatColor.RED + "Invalid Command Syntax. "
+ "Correct command syntax is: "
+ ChatColor.GRAY + "/"
+ ((InvalidSyntaxException) throwable).getCorrectSyntax())
);
} else if (throwable instanceof InvalidCommandSenderException) {
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
commandSender.sendMessage(ChatColor.RED + throwable.getMessage())
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
NoPermissionException.class,
(NoPermissionException) throwable, (c, e) ->
commandSender.sendMessage(MESSAGE_NO_PERMS)
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable, (c, e) ->
commandSender.sendMessage(MESSAGE_UNKNOWN_COMMAND)
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(sender,
ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) ->
commandSender.sendMessage(ChatColor.RED + "Invalid Command Argument: "
+ ChatColor.GRAY + throwable.getCause()
.getMessage())
);
} else {
commandSender.sendMessage(throwable.getMessage());
throwable.printStackTrace();
}
}
}));
if (throwable != null) {
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
(InvalidSyntaxException) throwable, (c, e) ->
commandSender.sendMessage(
ChatColor.RED + "Invalid Command Syntax. "
+ "Correct command syntax is: "
+ ChatColor.GRAY + "/"
+ ((InvalidSyntaxException) throwable)
.getCorrectSyntax())
);
} else if (throwable instanceof InvalidCommandSenderException) {
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
commandSender.sendMessage(
ChatColor.RED + throwable.getMessage())
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
NoPermissionException.class,
(NoPermissionException) throwable, (c, e) ->
commandSender.sendMessage(MESSAGE_NO_PERMS)
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable, (c, e) ->
commandSender.sendMessage(MESSAGE_UNKNOWN_COMMAND)
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(sender,
ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) ->
commandSender.sendMessage(
ChatColor.RED + "Invalid Command Argument: "
+ ChatColor.GRAY + throwable.getCause()
.getMessage())
);
} else {
commandSender.sendMessage(throwable.getMessage());
throwable.printStackTrace();
}
}
}));
return true;
}

View file

@ -50,6 +50,8 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
private final Function<CommandSender, C> commandSenderMapper;
private final Function<C, CommandSender> backwardsCommandSenderMapper;
private boolean splitAliases = false;
/**
* Construct a new Bukkit command manager
*
@ -113,4 +115,12 @@ public class BukkitCommandManager<C> extends CommandManager<C> {
return this.backwardsCommandSenderMapper.apply(sender).hasPermission(permission);
}
protected final void setSplitAliases(final boolean value) {
this.splitAliases = value;
}
protected final boolean getSplitAliases() {
return this.splitAliases;
}
}

View file

@ -25,6 +25,7 @@ package com.intellectualsites.commands.bukkit;
import com.intellectualsites.commands.Command;
import com.intellectualsites.commands.arguments.CommandArgument;
import com.intellectualsites.commands.arguments.StaticArgument;
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
@ -34,7 +35,9 @@ import org.bukkit.help.GenericCommandHelpTopic;
import javax.annotation.Nonnull;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
final class BukkitPluginRegistrationHandler<C> implements CommandRegistrationHandler {
@ -74,14 +77,37 @@ final class BukkitPluginRegistrationHandler<C> implements CommandRegistrationHan
} else {
label = commandArgument.getName();
}
@SuppressWarnings("unchecked") final List<String> aliases = ((StaticArgument<C>) commandArgument).getAlternativeAliases();
@SuppressWarnings("unchecked") final BukkitCommand<C> bukkitCommand = new BukkitCommand<>(
commandArgument.getName(),
(this.bukkitCommandManager.getSplitAliases() ? Collections.<String>emptyList() : aliases),
(Command<C>) command,
(CommandArgument<C, ?>) commandArgument,
this.bukkitCommandManager);
this.registeredCommands.put(commandArgument, bukkitCommand);
this.commandMap.register(commandArgument.getName(), this.bukkitCommandManager.getOwningPlugin().getName().toLowerCase(),
bukkitCommand);
if (this.bukkitCommandManager.getSplitAliases()) {
for (final String alias : aliases) {
if (!this.bukkitCommands.containsKey(alias)) {
@SuppressWarnings("unchecked") final BukkitCommand<C> aliasCommand = new BukkitCommand<>(
alias,
Collections.emptyList(),
(Command<C>) command,
(CommandArgument<C, ?>) commandArgument,
this.bukkitCommandManager);
this.commandMap.register(alias, this.bukkitCommandManager.getOwningPlugin()
.getName().toLowerCase(),
bukkitCommand);
}
}
}
return true;
}
}