-
-
Notifications
You must be signed in to change notification settings - Fork 7
Cookbook
Matthias Ngeo edited this page Feb 23, 2020
·
10 revisions
Partial command execution
Full command execution
import com.karuslabs.commons.command.Executable;
import com.karuslabs.commons.command.tree.nodes.Literal;
import com.karuslabs.commons.command.tree.nodes.Argument;
import com.karuslabs.commons.command.types.PlayersType;
import com.mojang.brigadier.arguments.StringArgumentType;
Argument.Builder<CommandSender, List<Player>> players = Argument.of("players", new PlayersType()); // Inbuilt players type provided by Chimera
Argument.Builder<CommandSender, String> message = Argument.of("message", StringArgumentType.string()) // Inbuilt string type provided by Mojang
.executes(context -> {
CommandSender sender = context.getSource();
List<Player> recipents = context.getArgument("players", List.class);
String entered = context.getArgument("message", String.class);
recipents.forEach(p -> p.sendMessage(sender.getName() + " says: " + entered));
})
.suggests((context, builder) -> builder.suggest("\"Hello World!\"").buildFuture());
Argument<CommandSender, List<Player>> arguments = players.then(message).build();
Literal<CommandSender> tell = Literal.of("tell").alias("t")
.executes(context -> context.getSource().sendMessage("Hello darkness my old friend"))
.then(arguments)
.build();
Dispatcher dispatcher = Dispatcher.of(yourPlugin);
dispatcher.getRoot().addChild(tell);
dispatcher.update(); // Only needed if we added "tell" after YourPlugin.onLoad() and YourPlugin.onEnable() has been called.
var tracer = Literal.of("tracer").then(Argument.of("location", new Position3DType()));
var meaning = Literal.of("meaning_of_life").then(Argument.of("?", IntegerArgumentType.integer(30, 43))
.suggests((context, builder) -> builder.suggest(42, new LiteralMessage("The answer to life the universe and everything")).buildFuture()));
var clientside = Literal.of("clientside").then(Argument.of("entities", StringArgumentType.word()).suggests(ClientsideProvider.ENTITIES));