diff --git a/README.md b/README.md
index 3d721a56..81596d1d 100644
--- a/README.md
+++ b/README.md
@@ -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 [number]", permission = "some.permission.node")
diff --git a/cloud-core/src/main/java/cloud/commandframework/Command.java b/cloud-core/src/main/java/cloud/commandframework/Command.java
index 26b341e6..e0949fee 100644
--- a/cloud-core/src/main/java/cloud/commandframework/Command.java
+++ b/cloud-core/src/main/java/cloud/commandframework/Command.java
@@ -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 {
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 Argument type
+ * @return New builder instance with the command argument inserted into the argument list
+ */
+ public @NonNull Builder argument(final CommandArgument.@NonNull Builder 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 {
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 Argument type
+ * @return New builder instance with the command argument inserted into the argument list
+ */
+ public @NonNull Builder argument(final CommandArgument.@NonNull Builder builder,
+ @NonNull final Description description) {
+ final Map, 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
*
diff --git a/cloud-core/src/main/java/cloud/commandframework/CommandManager.java b/cloud-core/src/main/java/cloud/commandframework/CommandManager.java
index a370fcb3..ac5044de 100644
--- a/cloud-core/src/main/java/cloud/commandframework/CommandManager.java
+++ b/cloud-core/src/main/java/cloud/commandframework/CommandManager.java
@@ -182,6 +182,16 @@ public abstract class CommandManager {
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 command(final Command.@NonNull Builder command) {
+ return this.command(command.manager(this).build());
+ }
+
/**
* Get the command syntax formatter
*
diff --git a/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java b/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java
index 7dbde601..a73f6f1b 100644
--- a/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java
+++ b/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java
@@ -47,34 +47,27 @@ public class CommandSuggestionsTest {
manager.command(manager.commandBuilder("test")
.literal("var")
.argument(StringArgument.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.newBuilder("str")
- .withSuggestionsProvider((c, s) -> Arrays.asList("one", "two"))
- .build())
+ .withSuggestionsProvider((c, s) -> Arrays.asList("one", "two")))
.argument(IntegerArgument.newBuilder("num")
- .withMin(1).withMax(95).asOptional().build())
- .build());
+ .withMin(1).withMax(95).asOptional()));
manager.command(manager.commandBuilder("test")
.literal("alt")
.argument(IntegerArgument.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
diff --git a/cloud-core/src/test/java/cloud/commandframework/CommandTreeTest.java b/cloud-core/src/test/java/cloud/commandframework/CommandTreeTest.java
index d1b5869e..b7b15d6b 100644
--- a/cloud-core/src/test/java/cloud/commandframework/CommandTreeTest.java
+++ b/cloud-core/src/test/java/cloud/commandframework/CommandTreeTest.java
@@ -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 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();
}
diff --git a/cloud-minecraft/cloud-bukkit-test/src/main/java/cloud/commandframework/BukkitTest.java b/cloud-minecraft/cloud-bukkit-test/src/main/java/cloud/commandframework/BukkitTest.java
index 5b82716a..52785795 100644
--- a/cloud-minecraft/cloud-bukkit-test/src/main/java/cloud/commandframework/BukkitTest.java
+++ b/cloud-minecraft/cloud-bukkit-test/src/main/java/cloud/commandframework/BukkitTest.java
@@ -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.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.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.getOptional("uuid").orElse(null))))
- .build())
+ .sendMessage(String.format("UUID: %s\n", c.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")