Class Utils

java.lang.Object
emu.grasscutter.utils.Utils

public final class Utils extends Object
  • Field Details

    • random

      public static final Random random
  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • randomRange

      public static int randomRange(int min, int max)
    • randomFloatRange

      public static float randomFloatRange(float min, float max)
    • getDist

      public static double getDist(Position pos1, Position pos2)
    • getCurrentSeconds

      public static int getCurrentSeconds()
    • lowerCaseFirstChar

      public static String lowerCaseFirstChar(String s)
    • toString

      public static String toString(InputStream inputStream) throws IOException
      Throws:
      IOException
    • logByteArray

      public static void logByteArray(byte[] array)
    • bytesToHex

      public static String bytesToHex(byte[] bytes)
    • bytesToHex

      public static String bytesToHex(io.netty.buffer.ByteBuf buf)
    • byteBufToArray

      public static byte[] byteBufToArray(io.netty.buffer.ByteBuf buf)
    • abilityHash

      public static int abilityHash(String str)
    • toFilePath

      public static String toFilePath(String path)
      Creates a string with the path to a file.
      Parameters:
      path - The path to the file.
      Returns:
      A path using the operating system's file separator.
    • fileExists

      public static boolean fileExists(String path)
      Checks if a file exists on the file system.
      Parameters:
      path - The path to the file.
      Returns:
      True if the file exists, false otherwise.
    • createFolder

      public static boolean createFolder(String path)
      Creates a folder on the file system.
      Parameters:
      path - The path to the folder.
      Returns:
      True if the folder was created, false otherwise.
    • copyFromResources

      public static boolean copyFromResources(String resource, String destination)
      Copies a file from the archive's resources to the file system.
      Parameters:
      resource - The path to the resource.
      destination - The path to copy the resource to.
      Returns:
      True if the file was copied, false otherwise.
    • requireNonNullElseGet

      public static <T> T requireNonNullElseGet(T nonNull, T fallback)
      Get object with null fallback.
      Parameters:
      nonNull - The object to return if not null.
      fallback - The object to return if null.
      Returns:
      One of the two provided objects.
    • logObject

      public static void logObject(Object object)
      Logs an object to the console.
      Parameters:
      object - The object to log.
    • startupCheck

      public static void startupCheck()
      Checks for required files and folders before startup.
    • getNextTimestampOfThisHour

      public static int getNextTimestampOfThisHour(int hour, String timeZone, int param)
      Gets the timestamp of the next hour.
      Returns:
      The timestamp in UNIX seconds.
    • getNextTimestampOfThisHourInNextWeek

      public static int getNextTimestampOfThisHourInNextWeek(int hour, String timeZone, int param)
      Gets the timestamp of the next hour in a week.
      Returns:
      The timestamp in UNIX seconds.
    • getNextTimestampOfThisHourInNextMonth

      public static int getNextTimestampOfThisHourInNextMonth(int hour, String timeZone, int param)
      Gets the timestamp of the next hour in a month.
      Returns:
      The timestamp in UNIX seconds.
    • readFromInputStream

      public static String readFromInputStream(@Nullable InputStream stream)
      Retrieves a string from an input stream.
      Parameters:
      stream - The input stream.
      Returns:
      The string.
    • switchPropertiesUpperLowerCase

      public static Map<String,Object> switchPropertiesUpperLowerCase(Map<String,Object> objMap, Class<?> cls)
      Switch properties from upper case to lower case?
    • lerp

      public static int lerp(int x, int[][] xyArray)
      Performs a linear interpolation using a table of fixed points to create an effective piecewise f(x) = y function.
      Parameters:
      x -
      xyArray - Array of points in [[x0,y0], ... [xN, yN]] format
      Returns:
      f(x) = y
    • intInArray

      public static boolean intInArray(int key, int[] array)
      Checks if an int is in an int[]
      Parameters:
      key - int to look for
      array - int[] to look in
      Returns:
      key in array
    • setSubtract

      public static int[] setSubtract(int[] minuend, int[] subtrahend)
      Return a copy of minuend without any elements found in subtrahend.
      Parameters:
      minuend - The array we want elements from
      subtrahend - The array whose elements we don't want
      Returns:
      The array with only the elements we want, in the order that minuend had them
    • getLanguageCode

      public static String getLanguageCode(Locale locale)
      Gets the language code from a given locale.
      Parameters:
      locale - A locale.
      Returns:
      A string in the format of 'XX-XX'.
    • base64Encode

      public static String base64Encode(byte[] toEncode)
      Base64 encodes a given byte array.
      Parameters:
      toEncode - An array of bytes.
      Returns:
      A base64 encoded string.
    • base64Decode

      public static byte[] base64Decode(String toDecode)
      Base64 decodes a given string.
      Parameters:
      toDecode - A base64 encoded string.
      Returns:
      An array of bytes.
    • jsonDecode

      public static <T> T jsonDecode(String jsonData, Class<T> classType)
      Safely JSON decodes a given string.
      Parameters:
      jsonData - The JSON-encoded data.
      Returns:
      JSON decoded data, or null if an exception occurred.