Class Util

java.lang.Object
de.ellpeck.rockbottom.api.util.Util

public final class Util extends Object
This class provides a large variety of helpful utility methods and variables that can be useful in a multitude of circumstances.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final com.google.gson.Gson
    A global instance of google gson' gson that can be used to serialize and deserialize json objects.
    static final Random
    A global random with a randomized seed that can be used for operations that are not based on the world seed.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    ceil(double value)
    This is a faster implementation of Math.ceil(double), in that it simply typecasts the given value and then adjusts it accordingly.
    static double
    clamp(double num, double min, double max)
    Clamps a value between a minimum and a maximum value.
    static int
    clamp(int num, int min, int max)
    Clamps a value between a minimum and a maximum value.
    static float
    clampf(float num, float min, float max)
    Clamps a value between a minimum and a maximum value.
    static boolean
    Creates a file if it doesn't exist and then opens it with the operating system's default file opening software.
    static Entity
    Creates an entity from its entity registry name and with the given world.
    static boolean[]
    decodeBitVector(int input, int size)
    Separates a bit vector into an array of booleans whose are true if the bit is 1.
    static void
    A helper method to recursively delete a folder and all of its contents.
    static double
    distance(double x1, double y1, double x2, double y2)
    Returns the absolute distance between two points using the Pythagorean theorem.
    static double
    distanceSq(double x1, double y1, double x2, double y2)
    Returns the squared distance between two points using the Pythagorean theorem.
    static int
    encodeBitVector(boolean... inputs)
    Creates an integer representation of a bit vector from an array of booleans Reverse of decodeBitVector(int, int)
    static int
    floor(double value)
    This is a faster implementation of Math.floor(double), in that it simply typecasts the given value and then adjusts it accordingly.
    static long
    Gets the system time in milliseconds.
    static boolean
    Utility method to figure out if a string is a resource name, meaning if it contains a Constants.RESOURCE_SEPARATOR that is not at the beginning or end of the string.
    static double
    lerp(double pos1, double pos2, double factor)
    Computes the Linear Interpolation between the two positions given the factor.
    static List<Integer>
    makeIntList(int start, int end)
    Creates a list of integers starting at the first number and ending at the second number.
    static boolean
    Opens a website in the operating system's default browser.
    static long
    scrambleSeed(int i)
    Scrambles a single value into a seed that can be used for world gen.
    static long
    scrambleSeed(int x, int y)
    Scrambles an x and a y value together into a seed that can be used for world gen.
    static long
    scrambleSeed(int x, int y, long seed)
    Scrambles an x and a y value into a seed based on a different seed.
    static long
    scrambleSeed(int i, long seed)
    Scrambles a value into a seed based on a different seed.
    static int
    seconds(int seconds)
    Converts the amount of seconds into amount of ticks.
    static long
    shiftScramble(long l)
    Scrambles a seed in a predictable, always-equal, but seemingly random way, making the output seem like a completely different number.
    static void
    sleepSafe(long time)
    Causes the current thread to sleep by the given amount of milliseconds, ignoring any interruptions that might be thrown.
    static int
    toGridPos(double worldPos)
    Converts a given position in the world to a chunk grid position.
    static int
    toWorldPos(int gridPos)
    Converts a given position of a chunk into its world position.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • RANDOM

      public static final Random RANDOM
      A global random with a randomized seed that can be used for operations that are not based on the world seed. Note that, when doing something like world generation, the world's IChunkOrWorld.getSeed() should always be used. For that, there are several utility methods at the bottom of this class to help modify the world seed in a predictable pattern.
      See Also:
    • GSON

      public static final com.google.gson.Gson GSON
      A global instance of google gson' gson that can be used to serialize and deserialize json objects. This instance has pretty printing turned on, meaning any JSONs that are created with it will look easily readable and will not be a single line.
  • Constructor Details

    • Util

      public Util()
  • Method Details

    • distance

      public static double distance(double x1, double y1, double x2, double y2)
      Returns the absolute distance between two points using the Pythagorean theorem. Note that, if you want to just compare distances and don't care about the actual, absolute value of the distance, it is a lot faster to use distanceSq(double, double, double, double).
      Parameters:
      x1 - The first point's x
      y1 - The first point's y
      x2 - The second point's x
      y2 - The second point's y
      Returns:
      The distance
      See Also:
    • distanceSq

      public static double distanceSq(double x1, double y1, double x2, double y2)
      Returns the squared distance between two points using the Pythagorean theorem.
      Parameters:
      x1 - The first point's x
      y1 - The first point's y
      x2 - The second point's x
      y2 - The second point's y
      Returns:
      The squared distance
      See Also:
    • lerp

      public static double lerp(double pos1, double pos2, double factor)
      Computes the Linear Interpolation between the two positions given the factor. For example: lerp(0.5, 1.0, 0.2); (1/5 of the distance between 0.5 and 1.0) Returns : 0.6
      Parameters:
      pos1 - The first position.
      pos2 - The seconds position.
      factor - The factor between the first and second position as a decimal percentage
      Returns:
      The Linear Interpolation.
    • clamp

      public static double clamp(double num, double min, double max)
      Clamps a value between a minimum and a maximum value. If the given value is higher than the maximum, the maximum will be returned. If it is lower than the minimum, the minimum will be returned. Else, the actual value will be returned.
      Parameters:
      num - The number to clamp
      min - The minimum
      max - The maximum
      Returns:
      The clamped number
      See Also:
    • clampf

      public static float clampf(float num, float min, float max)
      Clamps a value between a minimum and a maximum value. If the given value is higher than the maximum, the maximum will be returned. If it is lower than the minimum, the minimum will be returned. Else, the actual value will be returned.
      Parameters:
      num - The number to clamp
      min - The minimum
      max - The maximum
      Returns:
      The clamped number
      See Also:
    • clamp

      public static int clamp(int num, int min, int max)
      Clamps a value between a minimum and a maximum value. If the given value is higher than the maximum, the maximum will be returned. If it is lower than the minimum, the minimum will be returned. Else, the actual value will be returned.
      Parameters:
      num - The number to clamp
      min - The minimum
      max - The maximum
      Returns:
      The clamped number
      See Also:
    • floor

      public static int floor(double value)
      This is a faster implementation of Math.floor(double), in that it simply typecasts the given value and then adjusts it accordingly. This results in the integer that is closest to the given value (in the direction of 0) will be returned.
      Parameters:
      value - The value to floor
      Returns:
      The floored value
    • ceil

      public static int ceil(double value)
      This is a faster implementation of Math.ceil(double), in that it simply typecasts the given value and then adjusts it accordingly. This results in the integer that is closest to the given value (in the direction of negative infinity, if the input is negative, or positive infinity otherwise) will be returned.
      Parameters:
      value - The value to ceiling
      Returns:
      The ceilinged value
    • toGridPos

      public static int toGridPos(double worldPos)
      Converts a given position in the world to a chunk grid position. In other words, giving any position in the world to this method will return the position of the chunk that this position would be in.
      Parameters:
      worldPos - The world position
      Returns:
      The chunk position
      See Also:
    • toWorldPos

      public static int toWorldPos(int gridPos)
      Converts a given position of a chunk into its world position. In other words, giving any chunk's grid position to this method will return the position of the chunk in the world.
      Parameters:
      gridPos - The chunk grid position
      Returns:
      The world position
    • deleteFolder

      public static void deleteFolder(File file)
      A helper method to recursively delete a folder and all of its contents. Just calling File.delete() will fail if it is a folder that contains other files, so this method clears all of the files in a folder first before deleting it.
      Parameters:
      file - The folder to delete
    • createEntity

      public static Entity createEntity(ResourceName name, IWorld world)
      Creates an entity from its entity registry name and with the given world.
      Parameters:
      name - The registry name of the entity
      world - The world that the entity should be in
      Returns:
      The newly created entity
      See Also:
    • getTimeMillis

      public static long getTimeMillis()
      Gets the system time in milliseconds. This is merely to shorten the long call to System.currentTimeMillis() to a more sensible amount of characters.
      Returns:
      The system time in milliseconds
    • createAndOpen

      public static boolean createAndOpen(File file)
      Creates a file if it doesn't exist and then opens it with the operating system's default file opening software. This is a utility method as usually, just using Desktop.open(File) would cause an exception if the folder does not exist
      Parameters:
      file - The file to open
      Returns:
      True if it worked, false if some exception was thrown
    • openWebsite

      public static boolean openWebsite(String domain)
      Opens a website in the operating system's default browser. This is a utility method as Desktop.browse(URI) can throw an exception.
      Parameters:
      domain - The domain to open
      Returns:
      True if it worked, false if some exception was thrown
    • shiftScramble

      public static long shiftScramble(long l)
      Scrambles a seed in a predictable, always-equal, but seemingly random way, making the output seem like a completely different number. This can be used for world generation when you need a different seed that, nevertheless, should still be influenced by the world's seed. To scramble a seed based on some other value, see scrambleSeed(int, long).
      Parameters:
      l - The long to scramble
      Returns:
      The scrambled long
      See Also:
    • scrambleSeed

      public static long scrambleSeed(int x, int y)
      Scrambles an x and a y value together into a seed that can be used for world gen. Note that this is not influenced by another seed. Obviously, these values do not have to be actual positions in the world, but can just be two arbitrary numbers.
      Parameters:
      x - The x value
      y - The y value
      Returns:
      The seed
    • scrambleSeed

      public static long scrambleSeed(int i)
      Scrambles a single value into a seed that can be used for world gen. Note that this is not influenced by another seed.
      Parameters:
      i - The value
      Returns:
      The seed
    • scrambleSeed

      public static long scrambleSeed(int x, int y, long seed)
      Scrambles an x and a y value into a seed based on a different seed. This can be used for world gen. Obviously, these values do not have to be actual positions in the world, but can just be two arbitrary numbers.
      Parameters:
      x - The x value
      y - The y value
      seed - The original seed
      Returns:
      The new seed
    • scrambleSeed

      public static long scrambleSeed(int i, long seed)
      Scrambles a value into a seed based on a different seed. This can be used for world gen.
      Parameters:
      i - The value
      seed - The original seed
      Returns:
      The new seed
    • makeIntList

      public static List<Integer> makeIntList(int start, int end)
      Creates a list of integers starting at the first number and ending at the second number. This can be especially useful for creating IFilteredInventory instances with a lot of slots.
      Parameters:
      start - The first number (inclusive)
      end - The second number (exclusive)
      Returns:
      A list of integers
      See Also:
    • isResourceName

      public static boolean isResourceName(String s)
      Utility method to figure out if a string is a resource name, meaning if it contains a Constants.RESOURCE_SEPARATOR that is not at the beginning or end of the string.
      Parameters:
      s - The string to check
      Returns:
      If the string is a resource name
    • encodeBitVector

      public static int encodeBitVector(boolean... inputs)
      Creates an integer representation of a bit vector from an array of booleans Reverse of decodeBitVector(int, int)
      Parameters:
      inputs - The bits as booleans
      Returns:
      The int equivalent of ORing the boolean inputs.
      See Also:
    • decodeBitVector

      public static boolean[] decodeBitVector(int input, int size)
      Separates a bit vector into an array of booleans whose are true if the bit is 1. Reverse of encodeBitVector(boolean...)
      Parameters:
      input - The input to decode
      size - The size of the out array (how many bits to check).
      Returns:
      An array of booleans representing the bits of the input
      See Also:
    • seconds

      public static int seconds(int seconds)
      Converts the amount of seconds into amount of ticks.
      Parameters:
      seconds - The amount of seconds
      Returns:
      The amount of ticks
    • sleepSafe

      public static void sleepSafe(long time)
      Causes the current thread to sleep by the given amount of milliseconds, ignoring any interruptions that might be thrown. This is a helper method to reduce the amount of space that a try-catch statement takes up in code.
      Parameters:
      time - The time to sleep for