Class ArgumentUtils

java.lang.Object
de.eldoria.eldoutilities.utils.ArgumentUtils

public final class ArgumentUtils extends Object
This class contains helpful methods for handling of command arguments.
Since:
1.0.0
  • Method Details

    • getOrDefault

      public static String getOrDefault(String[] arguments, int index, String defaultValue)
      Get a string value or a default value when the index does not exists.
      Parameters:
      arguments - array of string arguments.
      index - index of the requested parameter
      defaultValue - default value which will be returned when the index does not exists.
      Returns:
      string at index or default value if the index does not exists.
    • getOrDefault

      public static <T> T getOrDefault(String[] arguments, int index, Function<String,T> parse, T defaultValue)
      Get a value from string or a default value when the index does not exists or the parse result is null.
      Type Parameters:
      T - return type
      Parameters:
      arguments - array of string arguments.
      index - index of the requested parameter
      parse - Function to parse the string to value. Returns null if not parseable.
      defaultValue - default value which will be returned when the index does not exists.
      Returns:
      string at index or default value if the index does not exists.
    • get

      public static String get(String[] arguments, int index)
      Get the index from the string array .
      Parameters:
      arguments - array of string arguments.
      index - index of the requested parameter
      Returns:
      string or null if the index does not exists
    • get

      public static <T> Optional<T> get(String[] arguments, int index, Function<String,T> parse)
      Get the index parsed.
      Type Parameters:
      T - type of optional return value
      Parameters:
      arguments - array of string arguments.
      index - index of the requested parameter
      parse - function to parse the string.
      Returns:
      the string of the index after the parse function was applied or a empty optional when the index was not found.
    • getOptionalParameter

      public static <T> T getOptionalParameter(String[] arguments, int index, T defaultValue, Function<String,T> parse)
      Gets a optional parameter. Will return the default value when the index is not inside the array.
      Type Parameters:
      T - type of the returned parameter.
      Parameters:
      arguments - array of string arguments.
      index - index of the requested parameter
      defaultValue - default value which will be delivered when the index does not exists.
      parse - function to parse the string.
      Returns:
      parsed string value at index or default value if the index does not exists
    • getDefaultFromPlayerOrArg

      public static <T> T getDefaultFromPlayerOrArg(String[] args, int index, CommandSender sender, Function<Player,T> playerFunction, Function<String,T> argFunction)
    • getRangeAsString

      public static String getRangeAsString(String delimiter, String[] source, int from, int to)
      Returns a range of a string array as string.
      Parameters:
      delimiter - delimiter for string join
      source - source array
      from - start index (included). Use negative counts to count from the last index.
      to - end index (excluded). Use negative counts to count from the last index.
      Returns:
      range as string
    • getRangeAsString

      public static String getRangeAsString(String[] strings, int from)
      Get a array as sublist from 'from' to array.length().
      Parameters:
      strings - arguments
      from - start index included
      Returns:
      string array delimited with ' '
    • unescapeWorldName

      public static String unescapeWorldName(String world)
    • unescapeWorldName

      public static String unescapeWorldName(String world, String replace)
    • escapeWorldName

      public static String escapeWorldName(String world)
    • escapeWorldName

      public static String escapeWorldName(String world, String replace)
    • escapeWorldName

      public static String escapeWorldName(World world)
    • escapeWorldName

      public static String escapeWorldName(World world, String replace)
    • getWorld

      public static World getWorld(String world)
    • parseQuotedArgs

      public static String[] parseQuotedArgs(String args, boolean strip)
    • parseQuotedArgs

      public static String[] parseQuotedArgs(String[] args)
      Parse arguments and keep quotes together
      Parameters:
      args - args to parse
      Returns:
      parsed args
    • getMessage

      public static String getMessage(String[] strings, int from)
      Get a message from string array from 'from' to array.length().
      Parameters:
      strings - array of strings.
      from - start index (included). Use negative counts to count from the last index.
      Returns:
      array sequence as string
    • getMessage

      public static String getMessage(List<String> strings, int from)
      Get a message from string array from 'from' to array.length().
      Parameters:
      strings - array of strings.
      from - start index (included). Use negative counts to count from the last index.
      Returns:
      array sequence as string
    • getMessage

      public static String getMessage(String[] strings, int from, int to)
      Get a message from string array from 'from' to 'to'.
      Parameters:
      strings - array of strings.
      from - start index (included). Use negative counts to count from the last index.
      to - end index (excluded). Use negative counts to count from the last index.
      Returns:
      array sequence as string
    • getMessage

      public static String getMessage(List<String> strings, int from, int to)
      Get a message from string array from 'from' to 'to'.
      Parameters:
      strings - array of strings.
      from - start index (included). Use negative counts to count from the last index.
      to - end index (excluded). Use negative counts to count from the last index.
      Returns:
      array sequence as string
    • getRangeAsList

      public static <T> List<T> getRangeAsList(T[] objects, int from)
      Get a array as sublist from 'from' to array.length().
      Type Parameters:
      T - Type of objects
      Parameters:
      objects - arguments
      from - start index included
      Returns:
      sublist
    • getRangeAsList

      public static <T> List<T> getRangeAsList(T[] objects, int from, int to)
      Get a sublist from array from 'from' to 'to'.
      Type Parameters:
      T - Type of objects
      Parameters:
      objects - list of objects.
      from - start index (included). Use negative counts to count from the last index.
      to - end index (excluded). Use negative counts to count from the last index.
      Returns:
      sublist.
    • getRangeAsList

      public static <T> List<T> getRangeAsList(List<T> objects, int from)
      Get a sublist from 'from' to list.size()
      Type Parameters:
      T - Type of objects
      Parameters:
      objects - list of objects.
      from - start index (included). Use negative counts to count from the last index.
      Returns:
      sublist.
    • getRangeAsList

      public static <T> List<T> getRangeAsList(List<T> objects, int from, int to)
      Get a sublist of a list.
      Type Parameters:
      T - Type of objects
      Parameters:
      objects - list of objects.
      from - start index (included). Use negative counts to count from the last index.
      to - end index (excluded). Use negative counts to count from the last index.
      Returns:
      sublist.