Skip to content

Commit

Permalink
Added static methods to create ArgumentBuilders directly in the Briga…
Browse files Browse the repository at this point in the history
…dierCommand class (PaperMC#1161)
  • Loading branch information
4drian3d authored and skbeh committed Jan 17, 2024
1 parent bc2fba1 commit d11f13d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}

Expand All @@ -40,7 +43,7 @@ public BrigadierCommand(final LiteralArgumentBuilder<CommandSource> builder) {
*
* @param node the command node
*/
public BrigadierCommand(final LiteralCommandNode<CommandSource> node) {
public BrigadierCommand(final @NotNull LiteralCommandNode<CommandSource> node) {
this.node = Preconditions.checkNotNull(node, "node");
}

Expand All @@ -52,4 +55,34 @@ public BrigadierCommand(final LiteralCommandNode<CommandSource> node) {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.velocitypowered.api.command.BrigadierCommand;
Expand Down Expand Up @@ -349,4 +351,14 @@ void testSuggestCompletesExceptionallyIfRequirementPredicateThrows() {
assertThrows(CompletionException.class, () ->
manager.offerSuggestions(source, "parent ").join());
}

@Test
void testArgumentBuilderCreationUsingStaticFactory() {
assertDoesNotThrow(() -> BrigadierCommand.literalArgumentBuilder("someCommand"));
assertThrows(IllegalArgumentException.class,
() -> BrigadierCommand.literalArgumentBuilder("some random command"));
assertDoesNotThrow(
() -> BrigadierCommand.requiredArgumentBuilder(
"someRequiredArgument", StringArgumentType.word()));
}
}

0 comments on commit d11f13d

Please sign in to comment.