Class Brigadier

java.lang.Object
dev.jorel.commandapi.Brigadier

public final class Brigadier extends Object
The Brigadier class is used to access some of the internals of the CommandAPI so you can use the CommandAPI alongside Mojang's com.mojang.brigadier package
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.mojang.brigadier.builder.RequiredArgumentBuilder
    Constructs a RequiredArgumentBuilder from a given argument
    static com.mojang.brigadier.builder.RequiredArgumentBuilder
    fromArgument(List<Argument> args, String nodeName)
    Constructs a RequiredArgumentBuilder from a given argument within a command declaration.
    static com.mojang.brigadier.Command
    Converts a CommandAPICommand into a Brigadier Command
    static com.mojang.brigadier.builder.LiteralArgumentBuilder
    Creates a new literal argument builder from a CommandAPI LiteralArgument
    static com.mojang.brigadier.RedirectModifier
    fromPredicate(BiPredicate<org.bukkit.command.CommandSender,Object[]> predicate, List<Argument> args)
    Constructs a RedirectModifier from a predicate that uses a command sender and some arguments.
    static Object
    getBrigadierSourceFromCommandSender(org.bukkit.command.CommandSender sender)
    Gets a Brigadier source object (e.g.
    static org.bukkit.command.CommandSender
    getBukkitCommandSenderFromContext(com.mojang.brigadier.context.CommandContext cmdCtx)
    Returns a Bukkit CommandSender from a Brigadier CommandContext
    static com.mojang.brigadier.CommandDispatcher
    Returns the Brigadier CommandDispatcher tree that is used internally by the CommandAPI.
    static com.mojang.brigadier.tree.RootCommandNode
    Returns the root node of the current CommandDispatcher.
    static Object[]
    parseArguments(com.mojang.brigadier.context.CommandContext cmdCtx, List<Argument> args)
    Parses arguments into their respective objects with a given command context.
    static com.mojang.brigadier.suggestion.SuggestionProvider
    toSuggestions(String nodeName, List<Argument> args)
    Converts an argument name and a list of arguments to a Brigadier SuggestionProvider

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getCommandDispatcher

      public static com.mojang.brigadier.CommandDispatcher getCommandDispatcher()
      Returns the Brigadier CommandDispatcher tree that is used internally by the CommandAPI. Modifying this CommandDispatcher tree before the server finishes loading will still keep any changes made to it. For example, adding a new node to this tree will keep the node once the server has finished loading.
      Returns:
      The CommandAPI's internal CommandDispatcher instance
    • getRootNode

      public static com.mojang.brigadier.tree.RootCommandNode getRootNode()
      Returns the root node of the current CommandDispatcher. This is the equivalent of running Brigadier.getCommandDispatcher().getRoot();
      Returns:
      The Brigadier CommandDispatcher's root node
    • fromLiteralArgument

      public static com.mojang.brigadier.builder.LiteralArgumentBuilder fromLiteralArgument(LiteralArgument literalArgument)
      Creates a new literal argument builder from a CommandAPI LiteralArgument
      Parameters:
      literalArgument - the LiteralArgument to convert from
      Returns:
      a LiteralArgumentBuilder that represents the literal
    • fromPredicate

      public static com.mojang.brigadier.RedirectModifier fromPredicate(BiPredicate<org.bukkit.command.CommandSender,Object[]> predicate, List<Argument> args)
      Constructs a RedirectModifier from a predicate that uses a command sender and some arguments. RedirectModifiers can be used with Brigadier's fork() method to invoke other nodes in the CommandDispatcher tree. You would use this method as shown:
       Brigadier.fromPredicate((sender, args) -> {
           ...
       }, arguments);
       
      Parameters:
      predicate - the predicate to test
      args - the arguments that the sender has filled in
      Returns:
      a RedirectModifier that encapsulates the provided predicate
    • fromCommand

      public static com.mojang.brigadier.Command fromCommand(CommandAPICommand command)
      Converts a CommandAPICommand into a Brigadier Command
      Parameters:
      command - the command to convert
      Returns:
      a Brigadier Command object that represents the provided command
    • fromArgument

      public static com.mojang.brigadier.builder.RequiredArgumentBuilder fromArgument(List<Argument> args, String nodeName)
      Constructs a RequiredArgumentBuilder from a given argument within a command declaration. For example:
       List<Argument> arguments = new ArrayList<>();
       arguments.add(new IntegerArgument("hello"));
       
       RequiredArgumentBuilder argBuilder = Brigadier.fromArguments(arguments, "hello");
       
      Parameters:
      args - the List of arguments which you typically declare for commands
      nodeName - the name of the argument you want to specify
      Returns:
      a RequiredArgumentBuilder that represents the provided argument
    • fromArgument

      public static com.mojang.brigadier.builder.RequiredArgumentBuilder fromArgument(Argument argument)
      Constructs a RequiredArgumentBuilder from a given argument
      Parameters:
      argument - the argument to create a RequiredArgumentBuilder from
      Returns:
      a RequiredArgumentBuilder that represents the provided argument
    • toSuggestions

      public static com.mojang.brigadier.suggestion.SuggestionProvider toSuggestions(String nodeName, List<Argument> args)
      Converts an argument name and a list of arguments to a Brigadier SuggestionProvider
      Parameters:
      nodeName - the name (prompt) of the argument as declared by its node name
      args - the list of arguments
      Returns:
      a SuggestionProvider that suggests the overridden suggestions for the argument declared in the List with key argumentName
    • parseArguments

      public static Object[] parseArguments(com.mojang.brigadier.context.CommandContext cmdCtx, List<Argument> args) throws com.mojang.brigadier.exceptions.CommandSyntaxException
      Parses arguments into their respective objects with a given command context. This method effectively performs the "parse" step in an argument's class and returns an Object[] which maps directly to the input List with the values generated via parsing.
      Parameters:
      cmdCtx - the command context used to parse the command arguments
      args - the list of arguments to parse
      Returns:
      an array of Objects which hold the results of the argument parsing step
      Throws:
      com.mojang.brigadier.exceptions.CommandSyntaxException - if there was an error during parsing
    • getBrigadierSourceFromCommandSender

      public static Object getBrigadierSourceFromCommandSender(org.bukkit.command.CommandSender sender)
      Gets a Brigadier source object (e.g. CommandListenerWrapper or CommandSourceStack) from a Bukkit CommandSender. This source object is the same object you would get from a command context.
      Parameters:
      sender - the Bukkit CommandSender to convert into a Brigadier source object
      Returns:
      a Brigadier source object representing the provided Bukkit CommandSender
    • getBukkitCommandSenderFromContext

      public static org.bukkit.command.CommandSender getBukkitCommandSenderFromContext(com.mojang.brigadier.context.CommandContext cmdCtx)
      Returns a Bukkit CommandSender from a Brigadier CommandContext
      Parameters:
      cmdCtx - the command context to get the CommandSender from
      Returns:
      a Bukkit CommandSender from the provided Brigadier CommandContext