Add command proxies

This commit is contained in:
Alexander Söderberg 2020-09-25 02:20:04 +02:00
parent d3ed876df6
commit c980adac3b
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
6 changed files with 126 additions and 4 deletions

View file

@ -410,6 +410,33 @@ public class Command<C> {
this.commandExecutionHandler, Permission.of(permission));
}
/**
* Make the current command be a proxy of the supplied command. This means that
* all of the proxied commands variable command arguments will be inserted into this
* builder instance, in the order they are declared in the proxied command. Furthermore,
* the proxied commands command handler will be showed by the command that is currently
* being built. If the current command builder does not have a permission node set, this
* too will be copied.
*
* @param command Command to proxy
* @return New builder that proxies the given command
*/
@Nonnull
public Builder<C> proxies(@Nonnull final Command<C> command) {
Builder<C> builder = this;
for (final CommandArgument<C, ?> argument : command.getArguments()) {
if (argument instanceof StaticArgument) {
continue;
}
final CommandArgument<C, ?> builtArgument = argument.copy();
builder = builder.argument(builtArgument, Description.of(command.getArgumentDescription(argument)));
}
if (this.commandPermission.toString().isEmpty()) {
builder = builder.withPermission(command.getCommandPermission());
}
return builder.handler(command.commandExecutionHandler);
}
/**
* Build a command using the builder instance
*

View file

@ -281,6 +281,27 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
return this.valueType;
}
/**
* Create a copy of the command argument
*
* @return Copied argument
*/
@Nonnull
public CommandArgument<C, T> copy() {
CommandArgument.Builder<C, T> builder = ofType(this.valueType, this.name);
builder = builder.withSuggestionsProvider(this.suggestionsProvider);
builder = builder.withParser(this.parser);
if (this.isRequired()) {
builder = builder.asRequired();
} else if (this.defaultValue.isEmpty()) {
builder = builder.asOptional();
} else {
builder = builder.asOptionalWithDefault(this.defaultValue);
}
return builder.build();
}
/**
* Mutable builder for {@link CommandArgument} instances
*

View file

@ -24,6 +24,7 @@
package com.intellectualsites.commands;
import com.intellectualsites.commands.arguments.standard.IntegerArgument;
import com.intellectualsites.commands.arguments.standard.StringArgument;
import com.intellectualsites.commands.context.CommandContext;
import com.intellectualsites.commands.exceptions.NoPermissionException;
import com.intellectualsites.commands.meta.SimpleCommandMeta;
@ -56,6 +57,15 @@ class CommandTreeTest {
.optional("num", EXPECTED_INPUT_NUMBER))
.build())
.command(manager.commandBuilder("req").withSenderType(SpecificCommandSender.class).build());
final Command<TestCommandSender> toProxy = manager.commandBuilder("test")
.literal("unproxied")
.argument(StringArgument.required("string"))
.argument(IntegerArgument.required("int"))
.literal("anotherliteral")
.handler(c -> {})
.build();
manager.command(toProxy);
manager.command(manager.commandBuilder("proxy").proxies(toProxy).build());
}
@Test
@ -120,6 +130,12 @@ class CommandTreeTest {
.executeCommand(new TestCommandSender(), "invalid test").join());
}
@Test
void testProxy() {
manager.executeCommand(new TestCommandSender(),"test unproxied foo 10 anotherliteral").join();
manager.executeCommand(new TestCommandSender(), "proxy foo 10").join();
}
public static final class SpecificCommandSender extends TestCommandSender {
}