✨ 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);
|
||||
((Player) c.getSender()).getInventory().addItem(itemStack);
|
||||
c.getSender().sendMessage("You've been given stuff, bro.");
|
||||
})
|
||||
.build())
|
||||
}));
|
||||
```
|
||||
|
||||
or using annoted methods, like this:
|
||||
or using annotated methods, like this:
|
||||
```java
|
||||
@Description("Test cloud command using @CommandMethod")
|
||||
@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.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
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -134,11 +134,10 @@ public final class BukkitTest extends JavaPlugin {
|
|||
suggestions.add("dog");
|
||||
suggestions.add("cat");
|
||||
return suggestions;
|
||||
}).build())
|
||||
}))
|
||||
.handler(c -> ((Player) c.getSender())
|
||||
.setGameMode(c.<GameMode>getOptional("gamemode")
|
||||
.orElse(GameMode.SURVIVAL)))
|
||||
.build())
|
||||
.orElse(GameMode.SURVIVAL))))
|
||||
.command(mgr.commandBuilder("kenny", "k")
|
||||
.literal("sux", "s")
|
||||
.argument(IntegerArgument
|
||||
|
|
@ -149,11 +148,9 @@ public final class BukkitTest extends JavaPlugin {
|
|||
"Kenny sux %d%%",
|
||||
context.<Integer>getOptional("perc").orElse(PERC_MIN)
|
||||
));
|
||||
})
|
||||
.build())
|
||||
}))
|
||||
.command(mgr.commandBuilder("uuidtest")
|
||||
.handler(c -> c.getSender().sendMessage("Hey yo dum, provide a UUID idiot. Thx!"))
|
||||
.build())
|
||||
.handler(c -> c.getSender().sendMessage("Hey yo dum, provide a UUID idiot. Thx!")))
|
||||
.command(mgr.commandBuilder("uuidtest")
|
||||
.argument(UUID.class, "uuid", builder -> builder
|
||||
.asRequired()
|
||||
|
|
@ -166,10 +163,9 @@ public final class BukkitTest extends JavaPlugin {
|
|||
} catch (final Exception e) {
|
||||
return ArgumentParseResult.failure(e);
|
||||
}
|
||||
}).build())
|
||||
}))
|
||||
.handler(c -> c.getSender()
|
||||
.sendMessage(String.format("UUID: %s\n", c.<UUID>getOptional("uuid").orElse(null))))
|
||||
.build())
|
||||
.sendMessage(String.format("UUID: %s\n", c.<UUID>getOptional("uuid").orElse(null)))))
|
||||
.command(mgr.commandBuilder("give")
|
||||
.withSenderType(Player.class)
|
||||
.argument(EnumArgument.of(Material.class, "material"))
|
||||
|
|
@ -180,30 +176,25 @@ public final class BukkitTest extends JavaPlugin {
|
|||
final ItemStack itemStack = new ItemStack(material, amount);
|
||||
((Player) c.getSender()).getInventory().addItem(itemStack);
|
||||
c.getSender().sendMessage("You've been given stuff, bro.");
|
||||
})
|
||||
.build())
|
||||
}))
|
||||
.command(mgr.commandBuilder("worldtp", BukkitCommandMetaBuilder.builder()
|
||||
.withDescription("Teleport to a world")
|
||||
.build())
|
||||
.withDescription("Teleport to a world"))
|
||||
.argument(WorldArgument.of("world"))
|
||||
.handler(c -> {
|
||||
final World world = c.get("world");
|
||||
((Player) c.getSender()).teleport(world.getSpawnLocation());
|
||||
c.getSender().sendMessage("Teleported.");
|
||||
})
|
||||
.build())
|
||||
}))
|
||||
.command(mgr.commandBuilder("brigadier")
|
||||
.argument(FloatArgument.of("float"))
|
||||
.argument(DoubleArgument.of("double"))
|
||||
.argument(IntegerArgument.of("int"))
|
||||
.argument(BooleanArgument.of("bool"))
|
||||
.argument(StringArgument.of("string"))
|
||||
.handler(c -> c.getSender().sendMessage("Executed the command"))
|
||||
.build())
|
||||
.handler(c -> c.getSender().sendMessage("Executed the command")))
|
||||
.command(mgr.commandBuilder("annotationass")
|
||||
.handler(c -> c.getSender()
|
||||
.sendMessage(ChatColor.YELLOW + "Du e en ananas!"))
|
||||
.build())
|
||||
.sendMessage(ChatColor.YELLOW + "Du e en ananas!")))
|
||||
.command(mgr.commandBuilder("cloud")
|
||||
.literal("confirm")
|
||||
.handler(confirmationManager.createConfirmationExecutionHandler()).build())
|
||||
|
|
@ -240,8 +231,7 @@ public final class BukkitTest extends JavaPlugin {
|
|||
final Vector vector = context.get("coords");
|
||||
((Player) context.getSender()).teleport(vector.toLocation(world));
|
||||
});
|
||||
})
|
||||
.build());
|
||||
}));
|
||||
}
|
||||
|
||||
@CommandDescription("Test cloud command using @CommandMethod")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue