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

@ -211,7 +211,16 @@ public final class AnnotationParser<C> {
} catch (final Exception e) { } catch (final Exception e) {
throw new RuntimeException("Failed to construct command execution handler", e); throw new RuntimeException("Failed to construct command execution handler", e);
} }
commands.add(builder.build()); final Command<C> builtCommand = builder.build();
commands.add(builtCommand);
/* Check if we need to construct a proxy */
if (method.isAnnotationPresent(ProxiedBy.class)) {
final String proxy = method.getAnnotation(ProxiedBy.class).value();
if (proxy.contains(" ")) {
throw new IllegalArgumentException("@ProxiedBy proxies may only contain single literals");
}
manager.command(manager.commandBuilder(proxy, builtCommand.getCommandMeta()).proxies(builtCommand).build());
}
} }
return commands; return commands;
} }

View file

@ -0,0 +1,47 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package com.intellectualsites.commands.annotations;
import javax.annotation.Nonnull;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Creates a command proxy for the command. This is similar to
* {@link com.intellectualsites.commands.Command.Builder#proxies(com.intellectualsites.commands.Command)}.
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ProxiedBy {
/**
* Syntax of the proxying command
*
* @return Proxy syntax
*/
@Nonnull String value();
}

View file

@ -54,13 +54,15 @@ class AnnotationParserTest {
void testMethodConstruction() { void testMethodConstruction() {
final Collection<Command<TestCommandSender>> commands = annotationParser.parse(this); final Collection<Command<TestCommandSender>> commands = annotationParser.parse(this);
Assertions.assertFalse(commands.isEmpty()); Assertions.assertFalse(commands.isEmpty());
manager.executeCommand(new TestCommandSender(), "test 10").join(); manager.executeCommand(new TestCommandSender(), "test literal 10").join();
manager.executeCommand(new TestCommandSender(), "t 10 o").join(); manager.executeCommand(new TestCommandSender(), "t literal 10 o").join();
manager.executeCommand(new TestCommandSender(), "proxycommand 10").join();
Assertions.assertThrows(CompletionException.class, () -> Assertions.assertThrows(CompletionException.class, () ->
manager.executeCommand(new TestCommandSender(), "test 101").join()); manager.executeCommand(new TestCommandSender(), "test 101").join());
} }
@CommandMethod("test|t <int> [string]") @ProxiedBy("proxycommand")
@CommandMethod("test|t literal <int> [string]")
public void testCommand(@Nonnull final TestCommandSender sender, public void testCommand(@Nonnull final TestCommandSender sender,
@Argument("int") @Range(max = "100") final int argument, @Argument("int") @Range(max = "100") final int argument,
@Nonnull @Argument(value = "string", defaultValue = "potato", parserName = "potato") @Nonnull @Argument(value = "string", defaultValue = "potato", parserName = "potato")

View file

@ -410,6 +410,33 @@ public class Command<C> {
this.commandExecutionHandler, Permission.of(permission)); 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 * Build a command using the builder instance
* *

View file

@ -281,6 +281,27 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
return this.valueType; 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 * Mutable builder for {@link CommandArgument} instances
* *

View file

@ -24,6 +24,7 @@
package com.intellectualsites.commands; package com.intellectualsites.commands;
import com.intellectualsites.commands.arguments.standard.IntegerArgument; import com.intellectualsites.commands.arguments.standard.IntegerArgument;
import com.intellectualsites.commands.arguments.standard.StringArgument;
import com.intellectualsites.commands.context.CommandContext; import com.intellectualsites.commands.context.CommandContext;
import com.intellectualsites.commands.exceptions.NoPermissionException; import com.intellectualsites.commands.exceptions.NoPermissionException;
import com.intellectualsites.commands.meta.SimpleCommandMeta; import com.intellectualsites.commands.meta.SimpleCommandMeta;
@ -56,6 +57,15 @@ class CommandTreeTest {
.optional("num", EXPECTED_INPUT_NUMBER)) .optional("num", EXPECTED_INPUT_NUMBER))
.build()) .build())
.command(manager.commandBuilder("req").withSenderType(SpecificCommandSender.class).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 @Test
@ -120,6 +130,12 @@ class CommandTreeTest {
.executeCommand(new TestCommandSender(), "invalid test").join()); .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 { public static final class SpecificCommandSender extends TestCommandSender {
} }