Class ArgType<T>

java.lang.Object
redempt.redlib.commandmanager.ArgType<T>
Type Parameters:
T - The type this ArgType converts to
Direct Known Subclasses:
ArgSubtype

public class ArgType<T> extends Object
A command argument type, which converts a String argument to another type
  • Constructor Details

    • ArgType

      protected ArgType(String name, ArgType<?> parent, ArgType.ArgConverter<T,?> convert)
    • ArgType

      public ArgType(String name, Function<String,T> convert)
      Create an ArgType from a name and converter
      Parameters:
      name - The name of this command argument type, to be used in the command file
      convert - The Function to convert from a String to whatever type this converts to
    • ArgType

      public ArgType(String name, BiFunction<org.bukkit.command.CommandSender,String,T> convert)
      Create an ArgType from a name and converter
      Parameters:
      name - The name of this command argument type, to be used in the command file
      convert - The BiFunction to convert from a String to whatever type this converts to
  • Method Details

    • getDefault

      public static ArgType<?> getDefault(String name)
      Gets a default ArgType by name
      Parameters:
      name - The name of the default ArgType
      Returns:
      The ArgType, or null if there is not a default one by that name
    • of

      public static <T extends Enum> ArgType<T> of(String name, Class<T> clazz)
      Creates an ArgType for an enum, which will accept all of the enum's values as arguments and offer all enum values as tab completions
      Type Parameters:
      T - The enum type
      Parameters:
      name - The name of the ArgType
      clazz - The enum class to make an ArgType from
      Returns:
      An ArgType for the given enum
    • of

      public static ArgType<String> of(String name, String... values)
      Creates an ArgType for a set of possible string inputs
      Parameters:
      name - The name of the ArgType
      values - The list of possible inputs
      Returns:
      An ArgType for the given inputs, which will offer tab completion and accept any of the supplied strings, or return null if the given argument does not match any of them
    • of

      public static <T> ArgType<T> of(String name, Map<String,T> map)
      Creates an ArgType for a map of a String to another type
      Type Parameters:
      T - The type this ArgType will provide
      Parameters:
      name - The name of the ArgType
      map - The map from String to the type this ArgType will provide
      Returns:
      The constructed ArgType
    • getParent

      public ArgType<?> getParent()
      Returns:
      The parent type of this ArgType, or null
    • setTab

      public ArgType<T> setTab(Function<org.bukkit.command.CommandSender,List<String>> tab)
      Sets the tab completer for this type
      Parameters:
      tab - The function returning a List of all completions for this sender
      Returns:
      itself
    • constraint

      public ArgType<T> constraint(Function<String,Constraint<T>> constraint)
      Set the handler to check constraints for this type
      Parameters:
      constraint - A predicate to check constraints - return false to fail
      Returns:
      itself
    • checkConstraint

      public boolean checkConstraint(org.bukkit.command.CommandSender sender, Constraint<T> constraint, T value)
    • setTab

      public ArgType<T> setTab(BiFunction<org.bukkit.command.CommandSender,String[],List<String>> tab)
      Sets the tab completer for this type
      Parameters:
      tab - The function returning a List of all completions for this sender
      Returns:
      itself
    • setTab

      protected ArgType<T> setTab(ArgType.TabCompleter<?> tab)
    • tabStream

      public ArgType<T> tabStream(Function<org.bukkit.command.CommandSender,Stream<String>> tab)
      Sets the tab completer for this type, can be used instead of tab
      Parameters:
      tab - The function returning a Stream of all completions for this sender
      Returns:
      itself
    • tabStream

      public ArgType<T> tabStream(BiFunction<org.bukkit.command.CommandSender,String[],Stream<String>> tab)
      Sets the tab completer for this type, can be used instead of tab
      Parameters:
      tab - The function returning a Stream of all completions for this sender
      Returns:
      itself
    • tabComplete

      public List<String> tabComplete(org.bukkit.command.CommandSender sender, String[] args, Object prev)
      Gets tab completions
      Parameters:
      sender - The sender of the command
      args - The previous arguments
      prev - The previous value
      Returns:
      The tab completions
    • getName

      public String getName()
      Returns:
      The name of this argument type
    • convert

      public T convert(org.bukkit.command.CommandSender sender, Object previous, String argument)
      Converts an argument to another type
      Parameters:
      sender - The sender of the command
      previous - The value of the previous argument
      argument - The argument to be converted
      Returns:
      The converted argument for use in a method hook
    • getConstraint

      public Constraint<T> getConstraint(String constraintContents)
    • map

      public <K> ArgType<K> map(String name, Function<T,K> func)
      Creates a new ArgType based on this one which converts from this type to another
      Type Parameters:
      K - The type of the resulting ArgType
      Parameters:
      name - The name of the ArgType being created
      func - The function to convert from the type this ArgType returns to the type the new one will
      Returns:
      The resulting ArgType
    • map

      public <K> ArgType<K> map(String name, BiFunction<org.bukkit.command.CommandSender,T,K> func)
      Creates a new ArgType based on this one which converts from this type to another
      Type Parameters:
      K - The type of the resulting ArgType
      Parameters:
      name - The name of the ArgType being created
      func - The function to convert from the type this ArgType returns to the type the new one will
      Returns:
      The resulting ArgType
    • subType

      public <K> ArgSubtype<K,T> subType(String name, BiFunction<String,T,K> convert)
      Creates a new ArgSubtype with this ArgType as its parent. ArgSubtypes are argument types which must follow another argument type, and use info from the previous argument to determine their values for conversion and tab completion.
      Type Parameters:
      K - The type the new ArgSubtype will convert to
      Parameters:
      name - The name of the new ArgSubtype
      convert - The function to convert using the previous argument value
      Returns:
      The created ArgSubtype
    • subType

      public <K> ArgSubtype<K,T> subType(String name, ArgType.ArgConverter<K,T> convert)
      Creates a new ArgSubtype with this ArgType as its parent. ArgSubtypes are argument types which must follow another argument type, and use info from the previous argument to determine their values for conversion and tab completion.
      Type Parameters:
      K - The type the new ArgSubtype will convert to
      Parameters:
      name - The name of the new ArgSubtype
      convert - The function to convert using the previous argument value
      Returns:
      The created ArgSubtype