java.lang.Object
me.remigio07.chatplugin.api.common.util.Utils
Direct Known Subclasses:
Utils, Utils

public class Utils extends Object
Common utils class.
  • Field Details

    • STRING_NOT_FOUND

      public static final String STRING_NOT_FOUND
      String containing the "string not found" text indicator.

      Content: "string_not_found"

      See Also:
    • NOT_APPLICABLE

      public static final String NOT_APPLICABLE
      String containing the "not applicable" text indicator.

      Content: "N/A"

      See Also:
    • USER_AGENT

      public static final String USER_AGENT
      ChatPlugin's default user agent.

      Content: "Mozilla/5.0 +https://remigio07.me/chatplugin ChatPlugin/" + ChatPlugin.VERSION

    • NIL_UUID

      public static final UUID NIL_UUID
      UUID representing the nil UUID.

      UUID: "00000000-0000-0000-0000-000000000000"

    • MAIN_FOLDER

      public static final File MAIN_FOLDER
      File that indicates the current drive's main folder.

      Path: File.separator

    • IPV4_PATTERN

      public static final Pattern IPV4_PATTERN
      Pattern representing the allowed IPv4s.

      Regex: ^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$

      See Also:
    • SECONDS_IN_A_YEAR

      public static final int SECONDS_IN_A_YEAR
      Seconds in a year.

      Value: 31536000 (0x1e13380)

      See Also:
    • SECONDS_IN_A_MONTH

      public static final int SECONDS_IN_A_MONTH
      Seconds in a month.

      Value: 2592000 (0x278d00)

      See Also:
    • SECONDS_IN_A_WEEK

      public static final int SECONDS_IN_A_WEEK
      Seconds in a week.

      Value: 604800 (0x93a80)

      See Also:
    • SECONDS_IN_A_DAY

      public static final int SECONDS_IN_A_DAY
      Seconds in a day.

      Value: 86400 (0x15180)

      See Also:
    • SECONDS_IN_AN_HOUR

      public static final int SECONDS_IN_AN_HOUR
      Seconds in an hour.

      Value: 3600 (0xe10)

      See Also:
    • SECONDS_IN_A_MINUTE

      public static final int SECONDS_IN_A_MINUTE
      Seconds in a minute.

      Value: 60 (0x3c)

      See Also:
  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • isNumber

      public static boolean isNumber(String number)
      Checks if the specified input is a valid number using Double.parseDouble(String).
      Parameters:
      number - Number to check
      Returns:
      Whether input is a valid number
    • isFloat

      public static boolean isFloat(String number)
      Checks if the specified input is a valid float using Float.parseFloat(String).
      Parameters:
      number - Number to check
      Returns:
      Whether input is a valid float
    • isInteger

      public static boolean isInteger(String number)
      Checks if the specified input is a valid integer using Integer.parseInt(String).
      Parameters:
      number - Number to check
      Returns:
      Whether input is a valid integer
    • isLong

      public static boolean isLong(String number)
      Checks if the specified input is a valid long using Long.parseLong(String).
      Parameters:
      number - Number to check
      Returns:
      Whether input is a valid long
    • isPositiveInteger

      public static boolean isPositiveInteger(String number)
      Checks if the specified input is a positive integer using Integer.parseInt(String).
      Parameters:
      number - Number to check
      Returns:
      Whether input is a positive integer
    • isPositiveLong

      public static boolean isPositiveLong(String number)
      Checks if the specified input is a positive long using Long.parseLong(String).
      Parameters:
      number - Number to check
      Returns:
      Whether input is a positive long
    • truncate

      public static double truncate(double number, int decimalPlaces)
      Truncates the specified double to a certain approximation.
      Parameters:
      number - Number to truncate
      decimalPlaces - Decimal places to keep
      Returns:
      Truncated number
    • kilometersToMiles

      public static long kilometersToMiles(long kilometers)
      Converts the specified kilometers to miles.

      1 mi = 1609.344 m

      Parameters:
      kilometers - Kilometers to convert
      Returns:
      Corresponding miles
    • kilometersToNauticalMiles

      public static long kilometersToNauticalMiles(long kilometers)
      Converts the specified kilometers to nautical miles.

      1 km = 0.53995680345572 nm

      Parameters:
      kilometers - Kilometers to convert
      Returns:
      Corresponding nautical miles
    • getMaxPlayers

      public static int getMaxPlayers()
      Gets the maximum online players' amount.
      Returns:
      Max players' amount
    • isValidIPv4

      public static boolean isValidIPv4(String ipv4)
      Checks if the specified String is a valid IPv4.
      Parameters:
      ipv4 - IPv4's raw representation (xxx.xxx.xxx.xxx)
      Returns:
      Whether the specified IPv4 is valid
      See Also:
    • getInetAddress

      public static InetAddress getInetAddress(String ipv4)
      Evaluates an IP address using InetAddress.getByAddress(byte[]) to avoid pinging it, making this method faster than InetAddress.getByName(String).
      Parameters:
      ipv4 - IPv4's representation
      Returns:
      Corresponding IP address
      Throws:
      IllegalArgumentException - If specified IPv4 !isValidIPv4(String)
    • getOriginalClass

      public static Class<? extends ChatPluginManager> getOriginalClass(ChatPluginManager manager)
      Gets the original (abstract, API) class of the specified manager's instance.
      Parameters:
      manager - Manager's instance
      Returns:
      Manager's original class
    • addAndGet

      public static <T> List<T> addAndGet(Collection<T> input, Collection<T> args)
      Creates a copy of the input collection, adds the specified collection's elements and then returns it.
      Type Parameters:
      T - Collection's type
      Parameters:
      input - Input collection
      args - Collection to add
      Returns:
      Copy of input + args
    • removeAndGet

      public static <T> List<T> removeAndGet(Collection<T> input, Collection<T> args)
      Creates a copy of the input collection, removes the specified collection's elements and then returns it.
      Type Parameters:
      T - Collection's type
      Parameters:
      input - Input collection
      args - Collection to remove
      Returns:
      Copy of input - args
    • getStringFromList

      public static String getStringFromList(List<?> list, boolean translateColorCodes, boolean retainNewLines)
      Creates a string representation of the specified list like the following:
      "[random string, 3, that was a number as a string]"
      Parameters:
      list - List to transform
      translateColorCodes - Whether to translate colors
      retainNewLines - Whether to retain new lines or to replace them with spaces
      Returns:
      String representing the list
    • getListFromString

      public static List<String> getListFromString(String string)
      Creates a list from a string representation obtained using getStringFromList(List, boolean, boolean).
      Parameters:
      string - String representing the list
      Returns:
      List from the string representation
    • reverse

      public static void reverse(char[] array)
      Reverses the specified char array.
      Parameters:
      array - Array to reverse
    • arrayIndexOf

      public static int arrayIndexOf(String[] array, String string, boolean ignoreCase)
      Gets the index of the specified string in the given array.

      Will return -1 if the string is not present.

      Parameters:
      array - Array to check
      string - String to check
      ignoreCase - Whether to ignore case
      Returns:
      String's index in the array
    • arrayContains

      public static boolean arrayContains(String[] array, String string, boolean ignoreCase)
      Parameters:
      array - Array to check
      string - String to check
      ignoreCase - Whether to ignore case
      Returns:
      Whether the array contains the string
    • removeFromArray

      public static String[] removeFromArray(String[] array, String string, boolean ignoreCase)
      Creates a copy of the specified array, removes the given string and then returns it.
      Parameters:
      array - Input array
      string - String to remove
      ignoreCase - Whether to ignore case
      Returns:
      New array
    • capitalizeEveryWord

      public static String capitalizeEveryWord(String input)
      Capitalizes every word in the specified string.
      Parameters:
      input - String to capitalize
      Returns:
      Capitalized string
    • getTime

      public static long getTime(String input, boolean addCurrent, boolean allow0s)
      Calculates the time in milliseconds expressed by an input string.

      Will return -1 if the input is invalid or the time is < 0s or if !allow0s and the time is 0s. Input string must not contain spaces and timestamps must be divided by a comma.



      Recognized time indicators:
      IdentifierValue
      s/SSecond
      mMinute
      h/HHour
      d/DDay
      w/WWeek
      MMonth
      y/YYear

      Example: "23d,7h,39s"

      Parameters:
      input - Input string
      addCurrent - Whether to add current time
      allow0s - Whether to allow the "0s" timestamp
      Returns:
      Time in milliseconds or -1 if invalid input
    • getTime

      public static long getTime(int years, int months, int weeks, int days, int hours, int minutes, int seconds, boolean addCurrent)
      Converts the specified years, months, weeks, days, hours, minutes and seconds to their milliseconds value.
      Parameters:
      years - Years' amount
      months - Months' amount
      weeks - Weeks' amount
      days - Days' amount
      hours - Hours' amount
      minutes - Minutes' amount
      seconds - Seconds' amount
      addCurrent - Whether to add current time
      Returns:
      Time in milliseconds
    • replaceNumericPlaceholders

      public static String replaceNumericPlaceholders(String string, Object... args)
      Replaces numeric placeholders in a string with the given arguments:
      • {0} will be replaced with args[0]
      • {1} will be replaced with args[1]
      • {7} will be replaced with args[7]
      ...and so on.
      Parameters:
      string - String containing placeholders
      args - Optional arguments
      Returns:
      String with placeholders translated
    • replaceCustomPlaceholders

      public static String replaceCustomPlaceholders(String string, String[] placeholders, Object... args)
      Replaces custom placeholders in a string with the given arguments:
      • placeholders[0] will be replaced with args[0]
      • placeholders[1] will be replaced with args[1]
      • placeholders[7] will be replaced with args[7]
      ...and so on.
      Parameters:
      string - String containing placeholders
      placeholders - Placeholders to translate
      args - Optional arguments
      Returns:
      String with placeholders translated
    • getTotalStorage

      public static long getTotalStorage()
      Gets the total storage installed on /, in bytes.
      Returns:
      Total storage
    • getFreeStorage

      public static long getFreeStorage()
      Gets the free storage available on /, in bytes.
      Returns:
      Free storage