Added static methods to create ArgumentBuilders directly in the BrigadierCommand class (#1161)

This commit is contained in:
Adrian
2023-12-27 13:33:44 -05:00
committed by GitHub
parent 9450e6600c
commit 6fd03d6f5c
2 changed files with 47 additions and 2 deletions

View File

@@ -8,8 +8,11 @@
package com.velocitypowered.api.command;
import com.google.common.base.Preconditions;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import org.jetbrains.annotations.NotNull;
/**
* A command that uses Brigadier for parsing the command and
@@ -31,7 +34,7 @@ public final class BrigadierCommand implements Command {
*
* @param builder the {@link LiteralCommandNode} builder
*/
public BrigadierCommand(final LiteralArgumentBuilder<CommandSource> builder) {
public BrigadierCommand(final @NotNull LiteralArgumentBuilder<CommandSource> builder) {
this(Preconditions.checkNotNull(builder, "builder").build());
}
@@ -40,7 +43,7 @@ public final class BrigadierCommand implements Command {
*
* @param node the command node
*/
public BrigadierCommand(final LiteralCommandNode<CommandSource> node) {
public BrigadierCommand(final @NotNull LiteralCommandNode<CommandSource> node) {
this.node = Preconditions.checkNotNull(node, "node");
}
@@ -52,4 +55,34 @@ public final class BrigadierCommand implements Command {
public LiteralCommandNode<CommandSource> getNode() {
return node;
}
/**
* Creates a new LiteralArgumentBuilder of the required name.
*
* @param name the literal name.
* @return a new LiteralArgumentBuilder.
*/
public static LiteralArgumentBuilder<CommandSource> literalArgumentBuilder(
final @NotNull String name) {
Preconditions.checkNotNull(name, "name");
// Validation to avoid beginner's errors in case someone includes a space in the argument name
Preconditions.checkArgument(name.indexOf(' ') == -1, "the argument name cannot contain spaces");
return LiteralArgumentBuilder.literal(name);
}
/**
* Creates a new RequiredArgumentBuilder of the required name and type.
*
* @param name the argument name
* @param argumentType the argument type required
* @param <T> the ArgumentType required type
* @return a new RequiredArgumentBuilder
*/
public static <T> RequiredArgumentBuilder<CommandSource, T> requiredArgumentBuilder(
final @NotNull String name, @NotNull final ArgumentType<T> argumentType) {
Preconditions.checkNotNull(name, "name");
Preconditions.checkNotNull(argumentType, "argument type");
return RequiredArgumentBuilder.argument(name, argumentType);
}
}