✨ Reduce builder noise.
This commit is contained in:
parent
ab9860352c
commit
1c56be714c
6 changed files with 66 additions and 56 deletions
|
|
@ -24,11 +24,10 @@ mgr.command(mgr.commandBuilder("give")
|
||||||
final ItemStack itemStack = new ItemStack(material, amount);
|
final ItemStack itemStack = new ItemStack(material, amount);
|
||||||
((Player) c.getSender()).getInventory().addItem(itemStack);
|
((Player) c.getSender()).getInventory().addItem(itemStack);
|
||||||
c.getSender().sendMessage("You've been given stuff, bro.");
|
c.getSender().sendMessage("You've been given stuff, bro.");
|
||||||
})
|
}));
|
||||||
.build())
|
|
||||||
```
|
```
|
||||||
|
|
||||||
or using annoted methods, like this:
|
or using annotated methods, like this:
|
||||||
```java
|
```java
|
||||||
@Description("Test cloud command using @CommandMethod")
|
@Description("Test cloud command using @CommandMethod")
|
||||||
@CommandMethod(value = "annotation|a <input> [number]", permission = "some.permission.node")
|
@CommandMethod(value = "annotation|a <input> [number]", permission = "some.permission.node")
|
||||||
|
|
|
||||||
|
|
@ -38,13 +38,7 @@ import io.leangen.geantyref.TypeToken;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
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.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
|
@ -325,6 +319,18 @@ public class Command<C> {
|
||||||
return this.argument(StaticArgument.of(main, aliases), description);
|
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
|
* Add a new command argument with an empty description to the command
|
||||||
*
|
*
|
||||||
|
|
@ -352,6 +358,23 @@ public class Command<C> {
|
||||||
this.commandExecutionHandler, this.commandPermission);
|
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
|
* Add a new command argument by interacting with a constructed command argument builder
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -182,6 +182,16 @@ public abstract class CommandManager<C> {
|
||||||
return this;
|
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
|
* Get the command syntax formatter
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -47,34 +47,27 @@ public class CommandSuggestionsTest {
|
||||||
manager.command(manager.commandBuilder("test")
|
manager.command(manager.commandBuilder("test")
|
||||||
.literal("var")
|
.literal("var")
|
||||||
.argument(StringArgument.<TestCommandSender>newBuilder("str")
|
.argument(StringArgument.<TestCommandSender>newBuilder("str")
|
||||||
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two"))
|
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two")))
|
||||||
.build())
|
.argument(EnumArgument.of(TestEnum.class, "enum")));
|
||||||
.argument(EnumArgument.of(TestEnum.class, "enum"))
|
|
||||||
.build());
|
|
||||||
manager.command(manager.commandBuilder("test")
|
manager.command(manager.commandBuilder("test")
|
||||||
.literal("comb")
|
.literal("comb")
|
||||||
.argument(StringArgument.<TestCommandSender>newBuilder("str")
|
.argument(StringArgument.<TestCommandSender>newBuilder("str")
|
||||||
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two"))
|
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two")))
|
||||||
.build())
|
|
||||||
.argument(IntegerArgument.<TestCommandSender>newBuilder("num")
|
.argument(IntegerArgument.<TestCommandSender>newBuilder("num")
|
||||||
.withMin(1).withMax(95).asOptional().build())
|
.withMin(1).withMax(95).asOptional()));
|
||||||
.build());
|
|
||||||
manager.command(manager.commandBuilder("test")
|
manager.command(manager.commandBuilder("test")
|
||||||
.literal("alt")
|
.literal("alt")
|
||||||
.argument(IntegerArgument.<TestCommandSender>newBuilder("num")
|
.argument(IntegerArgument.<TestCommandSender>newBuilder("num")
|
||||||
.withSuggestionsProvider((c, s) -> Arrays.asList("3", "33", "333")).build())
|
.withSuggestionsProvider((c, s) -> Arrays.asList("3", "33", "333"))));
|
||||||
.build());
|
|
||||||
|
|
||||||
manager.command(manager.commandBuilder("com")
|
manager.command(manager.commandBuilder("com")
|
||||||
.argumentPair("com", Pair.of("x", "y"), Pair.of(Integer.class, TestEnum.class),
|
.argumentPair("com", Pair.of("x", "y"), Pair.of(Integer.class, TestEnum.class),
|
||||||
Description.empty())
|
Description.empty())
|
||||||
.argument(IntegerArgument.of("int"))
|
.argument(IntegerArgument.of("int")));
|
||||||
.build());
|
|
||||||
|
|
||||||
manager.command(manager.commandBuilder("com2")
|
manager.command(manager.commandBuilder("com2")
|
||||||
.argumentPair("com", Pair.of("x", "enum"),
|
.argumentPair("com", Pair.of("x", "enum"),
|
||||||
Pair.of(Integer.class, TestEnum.class), Description.empty())
|
Pair.of(Integer.class, TestEnum.class), Description.empty()));
|
||||||
.build());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
|
|
@ -77,12 +77,10 @@ class CommandTreeTest {
|
||||||
manager.command(manager.commandBuilder("command")
|
manager.command(manager.commandBuilder("command")
|
||||||
.withPermission("command.inner")
|
.withPermission("command.inner")
|
||||||
.literal("inner")
|
.literal("inner")
|
||||||
.handler(c -> System.out.println("Using inner command"))
|
.handler(c -> System.out.println("Using inner command")));
|
||||||
.build());
|
|
||||||
manager.command(manager.commandBuilder("command")
|
manager.command(manager.commandBuilder("command")
|
||||||
.withPermission("command.outer")
|
.withPermission("command.outer")
|
||||||
.handler(c -> System.out.println("Using outer command"))
|
.handler(c -> System.out.println("Using outer command")));
|
||||||
.build());
|
|
||||||
|
|
||||||
/* Build command for testing compound types */
|
/* Build command for testing compound types */
|
||||||
manager.command(manager.commandBuilder("pos")
|
manager.command(manager.commandBuilder("pos")
|
||||||
|
|
@ -92,8 +90,7 @@ class CommandTreeTest {
|
||||||
.handler(c -> {
|
.handler(c -> {
|
||||||
final Pair<Integer, Integer> pair = c.get("pos");
|
final Pair<Integer, Integer> pair = c.get("pos");
|
||||||
System.out.printf("X: %d | Y: %d\n", pair.getFirst(), pair.getSecond());
|
System.out.printf("X: %d | Y: %d\n", pair.getFirst(), pair.getSecond());
|
||||||
})
|
}));
|
||||||
.build());
|
|
||||||
manager.command(manager.commandBuilder("vec")
|
manager.command(manager.commandBuilder("vec")
|
||||||
.argument(ArgumentPair.of(manager, "vec", Pair.of("x", "y"),
|
.argument(ArgumentPair.of(manager, "vec", Pair.of("x", "y"),
|
||||||
Pair.of(Double.class, Double.class))
|
Pair.of(Double.class, Double.class))
|
||||||
|
|
@ -103,8 +100,7 @@ class CommandTreeTest {
|
||||||
.handler(c -> {
|
.handler(c -> {
|
||||||
final Vector2 vector2 = c.get("vec");
|
final Vector2 vector2 = c.get("vec");
|
||||||
System.out.printf("X: %f | Y: %f\n", vector2.getX(), vector2.getY());
|
System.out.printf("X: %f | Y: %f\n", vector2.getX(), vector2.getY());
|
||||||
})
|
}));
|
||||||
.build());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -153,12 +149,11 @@ class CommandTreeTest {
|
||||||
void testDefaultParser() {
|
void testDefaultParser() {
|
||||||
manager.command(
|
manager.command(
|
||||||
manager.commandBuilder("default")
|
manager.commandBuilder("default")
|
||||||
.argument(manager.argumentBuilder(Integer.class, "int").build())
|
.argument(manager.argumentBuilder(Integer.class, "int"))
|
||||||
.handler(context -> {
|
.handler(context -> {
|
||||||
final int number = context.get("int");
|
final int number = context.get("int");
|
||||||
System.out.printf("Supplied number is: %d\n", number);
|
System.out.printf("Supplied number is: %d\n", number);
|
||||||
})
|
})
|
||||||
.build()
|
|
||||||
);
|
);
|
||||||
manager.executeCommand(new TestCommandSender(), "default 5").join();
|
manager.executeCommand(new TestCommandSender(), "default 5").join();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -134,11 +134,10 @@ public final class BukkitTest extends JavaPlugin {
|
||||||
suggestions.add("dog");
|
suggestions.add("dog");
|
||||||
suggestions.add("cat");
|
suggestions.add("cat");
|
||||||
return suggestions;
|
return suggestions;
|
||||||
}).build())
|
}))
|
||||||
.handler(c -> ((Player) c.getSender())
|
.handler(c -> ((Player) c.getSender())
|
||||||
.setGameMode(c.<GameMode>getOptional("gamemode")
|
.setGameMode(c.<GameMode>getOptional("gamemode")
|
||||||
.orElse(GameMode.SURVIVAL)))
|
.orElse(GameMode.SURVIVAL))))
|
||||||
.build())
|
|
||||||
.command(mgr.commandBuilder("kenny", "k")
|
.command(mgr.commandBuilder("kenny", "k")
|
||||||
.literal("sux", "s")
|
.literal("sux", "s")
|
||||||
.argument(IntegerArgument
|
.argument(IntegerArgument
|
||||||
|
|
@ -149,11 +148,9 @@ public final class BukkitTest extends JavaPlugin {
|
||||||
"Kenny sux %d%%",
|
"Kenny sux %d%%",
|
||||||
context.<Integer>getOptional("perc").orElse(PERC_MIN)
|
context.<Integer>getOptional("perc").orElse(PERC_MIN)
|
||||||
));
|
));
|
||||||
})
|
}))
|
||||||
.build())
|
|
||||||
.command(mgr.commandBuilder("uuidtest")
|
.command(mgr.commandBuilder("uuidtest")
|
||||||
.handler(c -> c.getSender().sendMessage("Hey yo dum, provide a UUID idiot. Thx!"))
|
.handler(c -> c.getSender().sendMessage("Hey yo dum, provide a UUID idiot. Thx!")))
|
||||||
.build())
|
|
||||||
.command(mgr.commandBuilder("uuidtest")
|
.command(mgr.commandBuilder("uuidtest")
|
||||||
.argument(UUID.class, "uuid", builder -> builder
|
.argument(UUID.class, "uuid", builder -> builder
|
||||||
.asRequired()
|
.asRequired()
|
||||||
|
|
@ -166,10 +163,9 @@ public final class BukkitTest extends JavaPlugin {
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
return ArgumentParseResult.failure(e);
|
return ArgumentParseResult.failure(e);
|
||||||
}
|
}
|
||||||
}).build())
|
}))
|
||||||
.handler(c -> c.getSender()
|
.handler(c -> c.getSender()
|
||||||
.sendMessage(String.format("UUID: %s\n", c.<UUID>getOptional("uuid").orElse(null))))
|
.sendMessage(String.format("UUID: %s\n", c.<UUID>getOptional("uuid").orElse(null)))))
|
||||||
.build())
|
|
||||||
.command(mgr.commandBuilder("give")
|
.command(mgr.commandBuilder("give")
|
||||||
.withSenderType(Player.class)
|
.withSenderType(Player.class)
|
||||||
.argument(EnumArgument.of(Material.class, "material"))
|
.argument(EnumArgument.of(Material.class, "material"))
|
||||||
|
|
@ -180,30 +176,25 @@ public final class BukkitTest extends JavaPlugin {
|
||||||
final ItemStack itemStack = new ItemStack(material, amount);
|
final ItemStack itemStack = new ItemStack(material, amount);
|
||||||
((Player) c.getSender()).getInventory().addItem(itemStack);
|
((Player) c.getSender()).getInventory().addItem(itemStack);
|
||||||
c.getSender().sendMessage("You've been given stuff, bro.");
|
c.getSender().sendMessage("You've been given stuff, bro.");
|
||||||
})
|
}))
|
||||||
.build())
|
|
||||||
.command(mgr.commandBuilder("worldtp", BukkitCommandMetaBuilder.builder()
|
.command(mgr.commandBuilder("worldtp", BukkitCommandMetaBuilder.builder()
|
||||||
.withDescription("Teleport to a world")
|
.withDescription("Teleport to a world"))
|
||||||
.build())
|
|
||||||
.argument(WorldArgument.of("world"))
|
.argument(WorldArgument.of("world"))
|
||||||
.handler(c -> {
|
.handler(c -> {
|
||||||
final World world = c.get("world");
|
final World world = c.get("world");
|
||||||
((Player) c.getSender()).teleport(world.getSpawnLocation());
|
((Player) c.getSender()).teleport(world.getSpawnLocation());
|
||||||
c.getSender().sendMessage("Teleported.");
|
c.getSender().sendMessage("Teleported.");
|
||||||
})
|
}))
|
||||||
.build())
|
|
||||||
.command(mgr.commandBuilder("brigadier")
|
.command(mgr.commandBuilder("brigadier")
|
||||||
.argument(FloatArgument.of("float"))
|
.argument(FloatArgument.of("float"))
|
||||||
.argument(DoubleArgument.of("double"))
|
.argument(DoubleArgument.of("double"))
|
||||||
.argument(IntegerArgument.of("int"))
|
.argument(IntegerArgument.of("int"))
|
||||||
.argument(BooleanArgument.of("bool"))
|
.argument(BooleanArgument.of("bool"))
|
||||||
.argument(StringArgument.of("string"))
|
.argument(StringArgument.of("string"))
|
||||||
.handler(c -> c.getSender().sendMessage("Executed the command"))
|
.handler(c -> c.getSender().sendMessage("Executed the command")))
|
||||||
.build())
|
|
||||||
.command(mgr.commandBuilder("annotationass")
|
.command(mgr.commandBuilder("annotationass")
|
||||||
.handler(c -> c.getSender()
|
.handler(c -> c.getSender()
|
||||||
.sendMessage(ChatColor.YELLOW + "Du e en ananas!"))
|
.sendMessage(ChatColor.YELLOW + "Du e en ananas!")))
|
||||||
.build())
|
|
||||||
.command(mgr.commandBuilder("cloud")
|
.command(mgr.commandBuilder("cloud")
|
||||||
.literal("confirm")
|
.literal("confirm")
|
||||||
.handler(confirmationManager.createConfirmationExecutionHandler()).build())
|
.handler(confirmationManager.createConfirmationExecutionHandler()).build())
|
||||||
|
|
@ -240,8 +231,7 @@ public final class BukkitTest extends JavaPlugin {
|
||||||
final Vector vector = context.get("coords");
|
final Vector vector = context.get("coords");
|
||||||
((Player) context.getSender()).teleport(vector.toLocation(world));
|
((Player) context.getSender()).teleport(vector.toLocation(world));
|
||||||
});
|
});
|
||||||
})
|
}));
|
||||||
.build());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@CommandDescription("Test cloud command using @CommandMethod")
|
@CommandDescription("Test cloud command using @CommandMethod")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue