Class AbstractCommand

java.lang.Object
com.hypixel.hytale.server.core.command.system.AbstractCommand
Direct Known Subclasses:
AbstractAsyncCommand, CommandBase, NPCDebugCommand.PresetsCommand

public abstract class AbstractCommand extends Object
An abstract class that represents a command in the command system.
  • Field Details

    • LOGGER

      @Nonnull public static final HytaleLogger LOGGER
      The logger for this class.
    • EMPTY_STRING_ARRAY

      public static final String[] EMPTY_STRING_ARRAY
      An empty string array constant.
  • Method Details

    • setOwner

      public void setOwner(@Nonnull CommandOwner owner)
      Called to set the owner of this command, and it's sub-commands and variants.
      Parameters:
      owner - owning system or plugin.
    • getPermissionGroups

      @Nullable public List<String> getPermissionGroups()
      Returns:
      a list of permission groups for this command.
    • getPermissionGroupsRecursive

      @Nonnull public Map<String, Set<String>> getPermissionGroupsRecursive()
      Returns:
      a map of permission groups to their associated permissions, including sub-commands.
    • putRecursivePermissionGroups

      public void putRecursivePermissionGroups(@Nonnull Map<String, Set<String>> permissionsByGroup)
      Called to populate the given map with permission groups and their associated permissions.
      Parameters:
      permissionsByGroup - The map to populate with permission groups and their associated permissions.
    • setAllowsExtraArguments

      public void setAllowsExtraArguments(boolean allowsExtraArguments)
      Set whether this command allows additional arguments beyond the required ones.

      When true, the command will accept at least the required number of parameters, but may accept more. This is useful for commands like /su that need to capture the rest of the command string.

      Parameters:
      allowsExtraArguments - true to allow extra arguments, false otherwise
    • matches

      @Nonnull public MatchResult matches(@Nonnull String language, @Nonnull String search, int termDepth)
      Called to check if this command or any of its subcommands/variants match the search string.
      Parameters:
      language - The language to use for translations.
      search - The search string.
      termDepth - The depth of the search term.
      Returns:
      a MatchResult representing the result of the match.
    • completeRegistration

      public void completeRegistration() throws GeneralCommandException
      Handles the initialization of various systems that require the command to be in a state where it should not be modified further in the future.
      Throws:
      GeneralCommandException - if validation of default arguments failed.
    • requirePermission

      public void requirePermission(@Nonnull String permission)
      Set the permission string required to execute this command.
      Parameters:
      permission - the permission string required to execute this command.
    • requirePermission

      public void requirePermission(@Nonnull PermissionQuery permission)
      Set the permission required to execute this command.
      Parameters:
      permission - the lookup for the permission required to execute this command.
    • requireNoPermission

      public void requireNoPermission()
      Declares that anyone may run this command.

      A command that says nothing is given a permission of its own, so this is the only way to have one that everyone can run. Say it for a command that hands out the first permission on a server, or one that guards itself by some other means, and say why.

    • getFullyQualifiedName

      @Nullable public String getFullyQualifiedName()
      The fully qualified name of a command is the name of the command, plus all the parent command names before it that you would use to call it ex: "time set".
      Returns:
      the fully qualified name of this command.
    • countParents

      public int countParents()
      Recursively counts how many parents this command has.
      Returns:
      the amount of parents.
    • addAliases

      public void addAliases(@Nonnull String... aliases)
      Add aliases (alternative names) that can be used in place of the original command name. These do not overwrite the original, they can be used in addition to the original command name.
      Parameters:
      aliases - the aliases (alternative names) that you want to add.
    • addSubCommand

      public void addSubCommand(@Nonnull AbstractCommand command)
      Add a subcommand, which is a nested, named command.

      An example of a subcommand could be "/warp list" where "list" is the subcommand.

      Parameters:
      command - the subcommand, where name must not be null.
    • addUsageVariant

      public void addUsageVariant(@Nonnull AbstractCommand command)
      Add a subcommand that has the same command name, but that has a different (and unique to other variants) amount of required arguments. In programming terms, it's an overloaded method.
      Parameters:
      command - The AbstractCommand to be executed when its number of required args is met.
    • acceptCall

      @Nullable public CompletableFuture<Void> acceptCall(@Nonnull CommandSender sender, @Nonnull ParserContext parserContext, @Nonnull ParseResult parseResult)
      Executes a subcommand, variant, or itself, after accepting a ParserContext from the Tokenizer and converting that raw data into a CommandContext that the executing command can use.
      Parameters:
      sender - The CommandSender that sent the initial command.
      parserContext - A ParserContext created from the Tokenizer.
      parseResult - The non-failed parse result that will store error data occurring during the command.
      Returns:
      A CompletableFuture<Void> to be run asynchronously, or null.
    • hasPermission

      public boolean hasPermission(@Nonnull CommandSender sender)
      Determines whether the given CommandSender has permission to execute this command.
      Parameters:
      sender - The sender to check permission for.
      Returns:
      true if the sender has permission, false otherwise.
    • getUsageString

      @Nonnull public Message getUsageString(@Nonnull CommandSender sender)
      Return the full help text that displays the command and all its parameters
      Parameters:
      sender - The command sender that sent the command.
      Returns:
      the usage message to display.
    • getUsageShort

      @Nonnull public Message getUsageShort(@Nonnull CommandSender sender, boolean fullyQualify)
      Return a near-one-liner, showcasing how to use the command.
      Parameters:
      sender - The command sender that sent the command.
      fullyQualify - Whether to display the fully qualified name of the command.
      Returns:
      The usage message to display.
    • withRequiredArg

      @Nonnull public <D> RequiredArg<D> withRequiredArg(@Nonnull String name, @Nonnull String description, @Nonnull ArgumentType<D> argType)
      Add an argument that is required to be specified at the beginning of a command.
      Type Parameters:
      D - The data type of the argument.
      Parameters:
      name - The name of the argument that displays in the usage and help.
      description - The description that will display in the help message.
      argType - The type of the argument.
      Returns:
      the created and registered arg.
    • withRequiredArg

      public <W extends WrappedArg<D>, D> W withRequiredArg(@Nonnull String name, @Nonnull String description, @Nonnull ArgWrapper<W,D> wrapper)
      Add an argument that is required to be specified at the beginning of a command. This specific method deals with ArgWrappers instead of ArgumentTypes, allowing a deeper level of customization for arguments.
      Type Parameters:
      W - The wrapped arg type.
      D - The data type of the argument.
      Parameters:
      name - The name of the argument that displays in the usage and help.
      description - The description that will display in the help message.
      wrapper - The wrapper to provide the return type and the argument type.
      Returns:
      the wrapped arg.
    • withListRequiredArg

      @Nonnull public <D> RequiredArg<List<D>> withListRequiredArg(@Nonnull String name, @Nonnull String description, @Nonnull ArgumentType<D> argType)
      Add an argument that is required to be specified at the beginning of a command. This specific method allows you to specify multiple values for a single argument.
      Type Parameters:
      D - The data type of the argument.
      Parameters:
      name - The name of the argument that displays in the usage and help.
      description - The description that will display in the help message.
      argType - The type of the argument.
      Returns:
      the created and registered arg.
    • withDefaultArg

      @Nonnull public <D> DefaultArg<D> withDefaultArg(String name, String description, ArgumentType<D> argType, @Nullable D defaultValue, String defaultValueDescription)
      Add an optional argument that will default to a value if not specified or if it is specified without a value.
      Type Parameters:
      D - The data type of the argument.
      Parameters:
      name - The name of the argument that displays in the usage and help.
      description - The description that will display in the help message.
      argType - The type of the argument.
      defaultValue - The *nullable* default value.
      defaultValueDescription - The description of the default value.
      Returns:
      the created and registered arg.
    • withDefaultArg

      public <W extends WrappedArg<D>, D> W withDefaultArg(@Nonnull String name, @Nonnull String description, @Nonnull ArgWrapper<W,D> wrapper, D defaultValue, @Nonnull String defaultValueDescription)
      Add an optional argument that will default to a value if not specified or if it is specified without a value. This specific method deals with ArgWrappers instead of ArgumentTypes, allowing a deeper level of customization for arguments.
      Type Parameters:
      W - The wrapped arg type.
      D - The data type of the argument.
      Parameters:
      name - The name of the argument that displays in the usage and help.
      description - The description that will display in the help message.
      wrapper - The wrapper to provide the return type and the argument type.
      defaultValue - The *nullable* default value.
      defaultValueDescription - The description of the default value.
      Returns:
      the created and registered arg.
    • withListDefaultArg

      @Nonnull public <D> DefaultArg<List<D>> withListDefaultArg(@Nonnull String name, @Nonnull String description, @Nonnull ArgumentType<D> argType, List<D> defaultValue, @Nonnull String defaultValueDescription)
      Add an optional argument that will default to a value if not specified or if it is specified without a value. This specific method allows you to specify multiple values for a single argument.
      Type Parameters:
      D - The data type of the argument.
      Parameters:
      name - The name of the argument that displays in the usage and help.
      description - The description that will display in the help message.
      argType - The type of the argument.
      defaultValue - The *nullable* default value.
      defaultValueDescription - The description of the default value.
      Returns:
      the created and registered arg
    • withOptionalArg

      @Nonnull public <D> OptionalArg<D> withOptionalArg(@Nonnull String name, @Nonnull String description, @Nonnull ArgumentType<D> argType)
      Add a required-value optional argument that is optional to be specified on a command, however, when specified, it must provide a value (ex: "--option=value").
      Type Parameters:
      D - The data type of the argument.
      Parameters:
      name - The name of the argument that displays in the usage and help.
      description - The description that will display in the help message.
      argType - The type of the argument.
      Returns:
      the created and registered arg.
    • withOptionalArg

      public <W extends WrappedArg<D>, D> W withOptionalArg(@Nonnull String name, @Nonnull String description, @Nonnull ArgWrapper<W,D> wrapper)
      Add a required-value optional argument that is optional to be specified on a command, however, when specified, it must provide a value (ex: "--option=value"). This specific method deals with ArgWrappers instead of ArgumentTypes, allowing a deeper level of customization for arguments.
      Type Parameters:
      W - The wrapped arg type.
      D - The data type of the argument.
      Parameters:
      name - The name of the argument that displays in the usage and help.
      description - The description that will display in the help message.
      wrapper - The wrapper to provide the return type and the argument type.
      Returns:
      the created and registered arg.
    • withListOptionalArg

      @Nonnull public <D> OptionalArg<List<D>> withListOptionalArg(@Nonnull String name, @Nonnull String description, @Nonnull ArgumentType<D> argType)
      Add a required-value optional argument that is optional to be specified on a command, however, when specified, it must provide a value (ex: "--option=value"). This specific method allows you to specify multiple values for a single argument.
      Type Parameters:
      D - The data type of the argument.
      Parameters:
      name - The name of the argument that displays in the usage and help.
      description - The description that will display in the help message.
      argType - The type of the argument.
      Returns:
      the created and registered arg.
    • withFlagArg

      @Nonnull public FlagArg withFlagArg(@Nonnull String name, @Nonnull String description)
      Add a flag argument that, when specified on the command is true, and when not specified on the command is false. It does not accept values.
      Parameters:
      name - The name of the argument that displays in the usage and help.
      description - The description that will display in the help message.
      Returns:
      the created and registered arg.
    • isVariant

      public boolean isVariant()
      Returns:
      true if this command is a variant command, false otherwise.
    • getName

      @Nullable public String getName()
      Returns:
      the name of the command.
    • getAliases

      @Nonnull public Set<String> getAliases()
      Returns:
      a set of all aliases for the command.
    • getDescription

      @Nullable public String getDescription()
      Returns:
      the description of the command.
    • getOwner

      @Nullable public CommandOwner getOwner()
      Returns:
      the owner of the command.
    • getPermission

      @Nullable public String getPermission()
      Returns:
      the permission node required to execute the command.
    • getSubCommands

      @Nonnull public Map<String, AbstractCommand> getSubCommands()
      Returns:
      the parent command, or null if this command has no parent.
    • getSubCommand

      @Nullable public AbstractCommand getSubCommand(@Nonnull String name)
      Resolves a subcommand by name, falling back to subcommand aliases. The lookup is case-insensitive.
      Parameters:
      name - the subcommand name or alias.
      Returns:
      the matching subcommand, or null if none matches.
    • getRequiredArguments

      @Nonnull public List<RequiredArg<?>> getRequiredArguments()
    • getOptionalArguments

      @Nonnull public Map<String, AbstractOptionalArg<?,?>> getOptionalArguments()
    • getOptionalArgument

      @Nullable public AbstractOptionalArg<?,?> getOptionalArgument(@Nonnull String nameOrAbbreviation)
      Resolves an optional argument by the name, alias, or unambiguous abbreviation the user typed after --, the same way execution resolves it. Returns null before registration (the abbreviation map is built then) or when nothing matches.
    • getVariantCommands

      @Nonnull public Collection<AbstractCommand> getVariantCommands()
    • getVariantByArgCount

      @Nullable public AbstractCommand getVariantByArgCount(int requiredArgCount)
    • getSuggestionOverrides

      @Nullable public List<AbstractCommand.SuggestionOverrideEntry> getSuggestionOverrides()
    • hasBeenRegistered

      public boolean hasBeenRegistered()
      Returns:
      the parent command, or null if this command has no parent.