Reduce builder noise.

This commit is contained in:
Alexander Söderberg 2020-10-02 00:16:14 +02:00 committed by Alexander Söderberg
parent ab9860352c
commit 1c56be714c
6 changed files with 66 additions and 56 deletions

View file

@ -38,13 +38,7 @@ import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
@ -325,6 +319,18 @@ public class Command<C> {
return this.argument(StaticArgument.of(main, aliases), description);
}
/**
* Add a new command argument with an empty description to the command
*
* @param builder Argument to add. {@link CommandArgument.Builder#build()} will be invoked
* and the result will be registered in the command.
* @param <T> Argument type
* @return New builder instance with the command argument inserted into the argument list
*/
public <T> @NonNull Builder<C> argument(final CommandArgument.@NonNull Builder<C, T> builder) {
return this.argument(builder.build(), Description.empty());
}
/**
* Add a new command argument with an empty description to the command
*
@ -352,6 +358,23 @@ public class Command<C> {
this.commandExecutionHandler, this.commandPermission);
}
/**
* Add a new command argument to the command
*
* @param builder Argument to add. {@link CommandArgument.Builder#build()} will be invoked
* and the result will be registered in the command.
* @param description Argument description
* @param <T> Argument type
* @return New builder instance with the command argument inserted into the argument list
*/
public <T> @NonNull Builder<C> argument(final CommandArgument.@NonNull Builder<C, T> builder,
@NonNull final Description description) {
final Map<CommandArgument<C, ?>, Description> commandArgumentMap = new LinkedHashMap<>(this.commandArguments);
commandArgumentMap.put(builder.build(), description);
return new Builder<>(this.commandManager, this.commandMeta, this.senderType, commandArgumentMap,
this.commandExecutionHandler, this.commandPermission);
}
/**
* Add a new command argument by interacting with a constructed command argument builder
*

View file

@ -182,6 +182,16 @@ public abstract class CommandManager<C> {
return this;
}
/**
* Register a new command
*
* @param command Command to register. {@link Command.Builder#build()}} will be invoked.
* @return The command manager instance
*/
public @NonNull CommandManager<C> command(final Command.@NonNull Builder<C> command) {
return this.command(command.manager(this).build());
}
/**
* Get the command syntax formatter
*

View file

@ -47,34 +47,27 @@ public class CommandSuggestionsTest {
manager.command(manager.commandBuilder("test")
.literal("var")
.argument(StringArgument.<TestCommandSender>newBuilder("str")
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two"))
.build())
.argument(EnumArgument.of(TestEnum.class, "enum"))
.build());
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two")))
.argument(EnumArgument.of(TestEnum.class, "enum")));
manager.command(manager.commandBuilder("test")
.literal("comb")
.argument(StringArgument.<TestCommandSender>newBuilder("str")
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two"))
.build())
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two")))
.argument(IntegerArgument.<TestCommandSender>newBuilder("num")
.withMin(1).withMax(95).asOptional().build())
.build());
.withMin(1).withMax(95).asOptional()));
manager.command(manager.commandBuilder("test")
.literal("alt")
.argument(IntegerArgument.<TestCommandSender>newBuilder("num")
.withSuggestionsProvider((c, s) -> Arrays.asList("3", "33", "333")).build())
.build());
.withSuggestionsProvider((c, s) -> Arrays.asList("3", "33", "333"))));
manager.command(manager.commandBuilder("com")
.argumentPair("com", Pair.of("x", "y"), Pair.of(Integer.class, TestEnum.class),
Description.empty())
.argument(IntegerArgument.of("int"))
.build());
.argument(IntegerArgument.of("int")));
manager.command(manager.commandBuilder("com2")
.argumentPair("com", Pair.of("x", "enum"),
Pair.of(Integer.class, TestEnum.class), Description.empty())
.build());
Pair.of(Integer.class, TestEnum.class), Description.empty()));
}
@Test

View file

@ -77,12 +77,10 @@ class CommandTreeTest {
manager.command(manager.commandBuilder("command")
.withPermission("command.inner")
.literal("inner")
.handler(c -> System.out.println("Using inner command"))
.build());
.handler(c -> System.out.println("Using inner command")));
manager.command(manager.commandBuilder("command")
.withPermission("command.outer")
.handler(c -> System.out.println("Using outer command"))
.build());
.handler(c -> System.out.println("Using outer command")));
/* Build command for testing compound types */
manager.command(manager.commandBuilder("pos")
@ -92,8 +90,7 @@ class CommandTreeTest {
.handler(c -> {
final Pair<Integer, Integer> pair = c.get("pos");
System.out.printf("X: %d | Y: %d\n", pair.getFirst(), pair.getSecond());
})
.build());
}));
manager.command(manager.commandBuilder("vec")
.argument(ArgumentPair.of(manager, "vec", Pair.of("x", "y"),
Pair.of(Double.class, Double.class))
@ -103,8 +100,7 @@ class CommandTreeTest {
.handler(c -> {
final Vector2 vector2 = c.get("vec");
System.out.printf("X: %f | Y: %f\n", vector2.getX(), vector2.getY());
})
.build());
}));
}
@Test
@ -153,12 +149,11 @@ class CommandTreeTest {
void testDefaultParser() {
manager.command(
manager.commandBuilder("default")
.argument(manager.argumentBuilder(Integer.class, "int").build())
.argument(manager.argumentBuilder(Integer.class, "int"))
.handler(context -> {
final int number = context.get("int");
System.out.printf("Supplied number is: %d\n", number);
})
.build()
);
manager.executeCommand(new TestCommandSender(), "default 5").join();
}