Class Grenadier

java.lang.Object
net.forthecrown.grenadier.Grenadier

public final class Grenadier extends Object
Grenadier singleton access

Note: When getProvider() is called for the first time, the provider will attempt to determine the plugin that loaded it. If a plugin is found, then it is set as Grenadier's plugin and will be returned in plugin().

If no plugin could be determined then there will be issues. See plugin() for why Grenadier requires a plugin

  • Method Details

    • getProvider

      public static GrenadierProvider getProvider()
      Gets or creates the grenadier provider
      Returns:
      Grenadier API
    • dispatcher

      public static CommandDispatcher<CommandSource> dispatcher()
      Gets the command dispatcher.

      This dispatcher has a restriction on the nodes registered into it, only command nodes created by GrenadierCommand instances can be registered into this dispatcher. More specifically, only GrenadierCommandNodes can be registered into this dispatcher

      Returns:
      Grenadier dispatcher
    • plugin

      public static Plugin plugin()
      Gets the plugin using grenadier.

      If the grenadier singleton was created by a plugin class loader, then this will return the plugin that loaded the Grenadier class.

      A plugin is required to register an internal event listener that ensures clients that connect to the server receive correct command trees, otherwise the default bukkit command tree is sent to clients. The difference between the two is mostly in the color of the text typed in chat.

      Secondly, the listener also ensures that Grenadier is able to register commands into the same dispatcher that the /executes command uses.

      Thirdly, if no plugin is found, then there is a chance suggestions will not function properly, this is mostly in the case of argument types like OptionsArgument that must offset suggestions in a way the base Bukkit system cannot handle

      If you're encountering any of the above-mentioned issues, place the following in your plugin's onEnable method

      
       @Override
       public void onEnable() {
         Grenadier.plugin(this);
       }
       
      Returns:
      Grenadier plugin, null, if no plugin is set
    • plugin

      public static void plugin(@NotNull @NotNull Plugin plugin)
      Sets the plugin using grenadier
      Parameters:
      plugin - Plugin
      See Also:
    • exceptions

      public static ExceptionProvider exceptions()
      Gets the exception factory

      Most, if not all, exceptions created by the returned factory are translatable

      Returns:
      Exception factory
    • createSource

      public static CommandSource createSource(CommandSender sender)
      Creates a command source for the specified sender
      Parameters:
      sender - Sender to wrap
      Returns:
      Created source
    • createSource

      public static CommandSource createSource(CommandSender sender, GrenadierCommandNode node)
      Creates a command source for the specified sender and then calls CommandSource.setCurrentNode(GrenadierCommandNode) with the specified node
      Parameters:
      sender - Sender to wrap
      node - Current command node
      Returns:
      Created source
    • fromMessage

      public static Component fromMessage(Message message)
      Converts a Brigadier message to a component.

      This is preferable to using Message.getString() because the vanilla implementation of the chat component system also implements Message. So the supplied message may be a LiteralMessage or a vanilla component.

      Parameters:
      message - Message to convert
      Returns:
      Component
    • toMessage

      public static Message toMessage(Component component)
      Converts the adventure component to a message.

      Internally this turns the Adventure API chat component to a vanilla chat component

      Parameters:
      component - Component to convert
      Returns:
      Message
    • fallbackPrefix

      public static String fallbackPrefix()
      Gets the command fallback prefix, this is prepended onto command labels to prevent commands from overriding eachother.

      Example:

       Without prefix: command_label
       With prefix:    fallback:command_label
       
      Returns:
      Grenadier fallback prefix
    • suggestAllCommands

      public static SuggestionProvider<CommandSource> suggestAllCommands()
      Gets a suggestion provider that suggests all commands currently registered on the server.

      The returned suggestion provider not only suggests command labels, but also gets suggestions specific to each command

      Returns:
      suggestion provider
    • createCommand

      public static GrenadierCommand createCommand(@NotNull @NotNull String name)
      Creates a grenadier command builder
      Parameters:
      name - Name of the command
      Returns:
      Created command
    • ensureValidLabel

      public static void ensureValidLabel(String label) throws IllegalArgumentException, NullPointerException
      Ensures a specified command label is valid. For a command label to be valid it must be non-null and not empty
      Parameters:
      label - Label to test
      Throws:
      IllegalArgumentException - If the label was empty, or blank
      NullPointerException - If the label was null