Enable users to override exception handling and make the velocity test plugin less stupid

This commit is contained in:
Alexander Söderberg 2020-09-19 16:38:58 +02:00
parent 5f48b0a032
commit 1fede2b4c0
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
8 changed files with 285 additions and 100 deletions

View file

@ -256,12 +256,11 @@ public final class CloudBrigadierManager<C, S> {
final LiteralArgumentBuilder<S> literalArgumentBuilder = LiteralArgumentBuilder
.<S>literal(cloudCommand.getArguments().get(0).getName())
.requires(sender -> permissionChecker.test(sender, node.getNodeMeta().getOrDefault("permission", "")));
if (node.isLeaf() && node.getValue() != null) {
literalArgumentBuilder.executes(executor);
}
literalArgumentBuilder.executes(executor);
final LiteralCommandNode<S> constructedRoot = literalArgumentBuilder.build();
for (final CommandTree.Node<CommandArgument<C, ?>> child : node.getChildren()) {
constructedRoot.addChild(this.constructCommandNode(child, permissionChecker, executor, provider).build());
constructedRoot.addChild(this.constructCommandNode(true, child,
permissionChecker, executor, provider).build());
}
return constructedRoot;
}
@ -289,12 +288,14 @@ public final class CloudBrigadierManager<C, S> {
}
final LiteralCommandNode<S> constructedRoot = literalArgumentBuilder.build();
for (final CommandTree.Node<CommandArgument<C, ?>> child : cloudCommand.getChildren()) {
constructedRoot.addChild(this.constructCommandNode(child, permissionChecker, executor, suggestionProvider).build());
constructedRoot.addChild(this.constructCommandNode(false, child, permissionChecker,
executor, suggestionProvider).build());
}
return constructedRoot;
}
private ArgumentBuilder<S, ?> constructCommandNode(@Nonnull final CommandTree.Node<CommandArgument<C, ?>> root,
private ArgumentBuilder<S, ?> constructCommandNode(final boolean forceExecutor,
@Nonnull final CommandTree.Node<CommandArgument<C, ?>> root,
@Nonnull final BiPredicate<S, String> permissionChecker,
@Nonnull final com.mojang.brigadier.Command<S> executor,
@Nonnull final SuggestionProvider<S> suggestionProvider) {
@ -313,11 +314,11 @@ public final class CloudBrigadierManager<C, S> {
.suggests(provider)
.requires(sender -> permissionChecker.test(sender, root.getNodeMeta().getOrDefault("permission", "")));
}
if (root.isLeaf() || !root.getValue().isRequired()) {
if (forceExecutor || root.isLeaf() || !root.getValue().isRequired()) {
argumentBuilder.executes(executor);
}
for (final CommandTree.Node<CommandArgument<C, ?>> node : root.getChildren()) {
argumentBuilder.then(constructCommandNode(node, permissionChecker, executor, suggestionProvider));
argumentBuilder.then(constructCommandNode(forceExecutor, node, permissionChecker, executor, suggestionProvider));
}
return argumentBuilder;
}

View file

@ -47,19 +47,19 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
private static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command. Type \"/help\" for help.";
private final CommandArgument<C, ?> command;
private final BukkitCommandManager<C> bukkitCommandManager;
private final BukkitCommandManager<C> manager;
private final Command<C> cloudCommand;
@SuppressWarnings("unchecked")
BukkitCommand(@Nonnull final Command<C> cloudCommand,
@Nonnull final CommandArgument<C, ?> command,
@Nonnull final BukkitCommandManager<C> bukkitCommandManager) {
@Nonnull final BukkitCommandManager<C> manager) {
super(command.getName(),
cloudCommand.getCommandMeta().getOrDefault("description", ""),
"",
((StaticArgument<C>) command).getAlternativeAliases());
this.command = command;
this.bukkitCommandManager = bukkitCommandManager;
this.manager = manager;
this.cloudCommand = cloudCommand;
}
@ -70,31 +70,52 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
for (final String string : strings) {
builder.append(" ").append(string);
}
this.bukkitCommandManager.executeCommand(this.bukkitCommandManager.getCommandSenderMapper().apply(commandSender),
builder.toString())
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof InvalidSyntaxException) {
commandSender.sendMessage(ChatColor.RED + "Invalid Command Syntax. "
+ "Correct command syntax is: "
+ ChatColor.GRAY + "/"
+ ((InvalidSyntaxException) throwable).getCorrectSyntax());
} else if (throwable instanceof InvalidCommandSenderException) {
commandSender.sendMessage(ChatColor.RED + throwable.getMessage());
} else if (throwable instanceof NoPermissionException) {
commandSender.sendMessage(MESSAGE_NO_PERMS);
} else if (throwable instanceof NoSuchCommandException) {
commandSender.sendMessage(MESSAGE_UNKNOWN_COMMAND);
} else if (throwable instanceof ArgumentParseException) {
commandSender.sendMessage(ChatColor.RED + "Invalid Command Argument: "
+ ChatColor.GRAY + throwable.getCause()
.getMessage());
} else {
commandSender.sendMessage(throwable.getMessage());
throwable.printStackTrace();
}
}
}));
final C sender = this.manager.getCommandSenderMapper().apply(commandSender);
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();
}
}
}));
return true;
}
@ -110,13 +131,13 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
for (final String string : args) {
builder.append(" ").append(string);
}
return this.bukkitCommandManager.suggest(this.bukkitCommandManager.getCommandSenderMapper().apply(sender),
builder.toString());
return this.manager.suggest(this.manager.getCommandSenderMapper().apply(sender),
builder.toString());
}
@Override
public Plugin getPlugin() {
return this.bukkitCommandManager.getOwningPlugin();
return this.manager.getOwningPlugin();
}
@Override

View file

@ -45,19 +45,19 @@ public final class BungeeCommand<C> extends Command implements TabExecutor {
+ "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 final BungeeCommandManager<C> bungeeCommandManager;
private final BungeeCommandManager<C> manager;
private final CommandArgument<C, ?> command;
private final com.intellectualsites.commands.Command<C> cloudCommand;
@SuppressWarnings("unchecked")
BungeeCommand(@Nonnull final com.intellectualsites.commands.Command<C> cloudCommand,
@Nonnull final CommandArgument<C, ?> command,
@Nonnull final BungeeCommandManager<C> bungeeCommandManager) {
@Nonnull final BungeeCommandManager<C> manager) {
super(command.getName(),
cloudCommand.getCommandPermission(),
((StaticArgument<C>) command).getAlternativeAliases().toArray(new String[0]));
this.command = command;
this.bungeeCommandManager = bungeeCommandManager;
this.manager = manager;
this.cloudCommand = cloudCommand;
}
@ -68,43 +68,64 @@ public final class BungeeCommand<C> extends Command implements TabExecutor {
for (final String string : strings) {
builder.append(" ").append(string);
}
this.bungeeCommandManager.executeCommand(this.bungeeCommandManager.getCommandSenderMapper().apply(commandSender),
builder.toString())
.whenComplete(((commandResult, throwable) -> {
if (throwable != null) {
if (throwable instanceof InvalidSyntaxException) {
commandSender.sendMessage(
new ComponentBuilder("Invalid Command Syntax. Correct command syntax is: ")
.color(ChatColor.RED)
.append("/")
.color(ChatColor.GRAY)
.append(((InvalidSyntaxException) throwable).getCorrectSyntax())
.color(ChatColor.GRAY)
.create()
);
} else if (throwable instanceof InvalidCommandSenderException) {
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage())
.color(ChatColor.RED)
.create());
} else if (throwable instanceof NoPermissionException) {
commandSender.sendMessage(new ComponentBuilder(MESSAGE_NO_PERMS)
.color(ChatColor.WHITE)
.create());
} else if (throwable instanceof NoSuchCommandException) {
commandSender.sendMessage(new ComponentBuilder(MESSAGE_UNKNOWN_COMMAND)
.color(ChatColor.WHITE)
.create());
} else if (throwable instanceof ArgumentParseException) {
commandSender.sendMessage(new ComponentBuilder("Invalid Command Argument: ")
.color(ChatColor.GRAY)
.append(throwable.getCause().getMessage())
.create());
} else {
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage()).create());
throwable.printStackTrace();
}
}
}));
final C sender = this.manager.getCommandSenderMapper().apply(commandSender);
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(
new ComponentBuilder("Invalid Command Syntax. Correct command syntax is: ")
.color(ChatColor.RED)
.append("/")
.color(ChatColor.GRAY)
.append(((InvalidSyntaxException) throwable).getCorrectSyntax())
.color(ChatColor.GRAY)
.create()
)
);
} else if (throwable instanceof InvalidCommandSenderException) {
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage())
.color(ChatColor.RED)
.create())
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
NoPermissionException.class,
(NoPermissionException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder(MESSAGE_NO_PERMS)
.color(ChatColor.WHITE)
.create())
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder(MESSAGE_UNKNOWN_COMMAND)
.color(ChatColor.WHITE)
.create())
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(sender,
ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) ->
commandSender.sendMessage(new ComponentBuilder("Invalid Command Argument: ")
.color(ChatColor.GRAY)
.append(throwable.getCause().getMessage())
.create())
);
} else {
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage()).create());
throwable.printStackTrace();
}
}
}));
}
@Override
@ -114,8 +135,8 @@ public final class BungeeCommand<C> extends Command implements TabExecutor {
for (final String string : args) {
builder.append(" ").append(string);
}
return this.bungeeCommandManager.suggest(this.bungeeCommandManager.getCommandSenderMapper().apply(sender),
builder.toString());
return this.manager.suggest(this.manager.getCommandSenderMapper().apply(sender),
builder.toString());
}
}

View file

@ -210,5 +210,10 @@
<artifactId>cloud-velocity</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.intellectualsites</groupId>
<artifactId>cloud-annotations</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View file

@ -24,7 +24,12 @@
package com.intellectualsites.cloudvelocitytest;
import com.google.inject.Inject;
import com.intellectualsites.commands.annotations.AnnotationParser;
import com.intellectualsites.commands.annotations.Argument;
import com.intellectualsites.commands.annotations.CommandMethod;
import com.intellectualsites.commands.annotations.specifier.Range;
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
import com.intellectualsites.commands.meta.SimpleCommandMeta;
import com.intellectualsites.commands.velocity.VelocityCommandManager;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.Subscribe;
@ -68,6 +73,22 @@ public class CloudVelocityTest {
.handler(c -> c.getSender().sendMessage(TextComponent.of("That's right ;)").color(NamedTextColor.GOLD)))
.build()
);
final AnnotationParser<CommandSource> annotationParser = new AnnotationParser<>(m, CommandSource.class,
p -> SimpleCommandMeta.empty());
annotationParser.parse(this);
}
@CommandMethod("test <num> [str]")
private void testCommand(@Nonnull @Argument("str") final String string,
@Nonnull final CommandSource source,
@Argument("num") @Range(max = "33") final int num) {
source.sendMessage(TextComponent.builder()
.append("You wrote: ", NamedTextColor.GOLD)
.append(string, NamedTextColor.LIGHT_PURPLE)
.append(" and ", NamedTextColor.GOLD)
.append(Integer.toString(num), NamedTextColor.LIGHT_PURPLE)
.append("!", NamedTextColor.GOLD)
);
}
}

View file

@ -28,10 +28,17 @@ import com.intellectualsites.commands.arguments.CommandArgument;
import com.intellectualsites.commands.arguments.StaticArgument;
import com.intellectualsites.commands.brigadier.CloudBrigadierManager;
import com.intellectualsites.commands.context.CommandContext;
import com.intellectualsites.commands.exceptions.ArgumentParseException;
import com.intellectualsites.commands.exceptions.InvalidCommandSenderException;
import com.intellectualsites.commands.exceptions.InvalidSyntaxException;
import com.intellectualsites.commands.exceptions.NoPermissionException;
import com.intellectualsites.commands.exceptions.NoSuchCommandException;
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandMeta;
import com.velocitypowered.api.command.CommandSource;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import javax.annotation.Nonnull;
import java.util.HashMap;
@ -40,12 +47,17 @@ import java.util.Map;
final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationHandler {
private static final String MESSAGE_NO_PERMS =
"I'm sorry, but you do not have permission to perform this command. "
+ "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 final Map<CommandArgument<?, ?>, BrigadierCommand> registeredCommands = new HashMap<>();
private CloudBrigadierManager<C, CommandSource> brigadierManager;
private VelocityCommandManager<C> velocityCommandManager;
private VelocityCommandManager<C> manager;
void initialize(@Nonnull final VelocityCommandManager<C> velocityCommandManager) {
this.velocityCommandManager = velocityCommandManager;
this.manager = velocityCommandManager;
this.brigadierManager = new CloudBrigadierManager<>(velocityCommandManager,
() -> new CommandContext<>(
velocityCommandManager.getCommandSenderMapper()
@ -63,22 +75,62 @@ final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationH
final List<String> aliases = ((StaticArgument<C>) argument).getAlternativeAliases();
final BrigadierCommand brigadierCommand = new BrigadierCommand(
this.brigadierManager.createLiteralCommandNode((Command<C>) command,
(c, p) -> this.velocityCommandManager.hasPermission(
this.velocityCommandManager.getCommandSenderMapper()
.apply(c), p),
commandContext -> {
final CommandSource source = commandContext.getSource();
final String input = commandContext.getInput();
this.velocityCommandManager.executeCommand(
this.velocityCommandManager.getCommandSenderMapper()
.apply(source), input);
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
})
(c, p) -> this.manager.hasPermission(
this.manager.getCommandSenderMapper()
.apply(c), p),
commandContext -> {
final CommandSource source = commandContext.getSource();
final String input = commandContext.getInput();
final C sender = this.manager.getCommandSenderMapper().apply(source);
this.manager.executeCommand(sender, input).whenComplete((result, throwable) -> {
if (throwable != null) {
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
(InvalidSyntaxException) throwable, (c, e) ->
source.sendMessage(TextComponent.builder("Invalid Command Syntax. Correct command syntax is: ",
NamedTextColor.RED)
.append(e.getCorrectSyntax(), NamedTextColor.GRAY).build())
);
} else if (throwable instanceof InvalidCommandSenderException) {
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
source.sendMessage(TextComponent.of(throwable.getMessage()).color(NamedTextColor.RED))
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
NoPermissionException.class,
(NoPermissionException) throwable, (c, e) ->
source.sendMessage(TextComponent.of(MESSAGE_NO_PERMS))
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable, (c, e) ->
source.sendMessage(TextComponent.of(MESSAGE_UNKNOWN_COMMAND))
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(sender,
ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) ->
source.sendMessage(TextComponent.builder("Invalid Command Argument: ", NamedTextColor.RED)
.append(throwable.getCause().getMessage(), NamedTextColor.GRAY)
.build())
);
} else {
source.sendMessage(TextComponent.of(throwable.getMessage()).color(NamedTextColor.RED));
throwable.printStackTrace();
}
}
});
return com.mojang.brigadier.Command.SINGLE_SUCCESS;
})
);
final CommandMeta commandMeta = this.velocityCommandManager.getProxyServer().getCommandManager()
.metaBuilder(brigadierCommand)
.aliases(aliases.toArray(new String[0])).build();
this.velocityCommandManager.getProxyServer().getCommandManager().register(commandMeta, brigadierCommand);
final CommandMeta commandMeta = this.manager.getProxyServer().getCommandManager()
.metaBuilder(brigadierCommand)
.aliases(aliases.toArray(new String[0])).build();
this.manager.getProxyServer().getCommandManager().register(commandMeta, brigadierCommand);
this.registeredCommands.put(argument, brigadierCommand);
return true;
}