Package de.ellpeck.rockbottom.api.util
Class Util
java.lang.Object
de.ellpeck.rockbottom.api.util.Util
This class provides a large variety of helpful utility methods and variables
that can be useful in a multitude of circumstances.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.google.gson.GsonA global instance of google gson' gson that can be used to serialize and deserialize json objects.static final RandomA global random with a randomized seed that can be used for operations that are not based on the world seed. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intceil(double value) This is a faster implementation ofMath.ceil(double), in that it simply typecasts the given value and then adjusts it accordingly.static doubleclamp(double num, double min, double max) Clamps a value between a minimum and a maximum value.static intclamp(int num, int min, int max) Clamps a value between a minimum and a maximum value.static floatclampf(float num, float min, float max) Clamps a value between a minimum and a maximum value.static booleancreateAndOpen(File file) Creates a file if it doesn't exist and then opens it with the operating system's default file opening software.static EntitycreateEntity(ResourceName name, IWorld world) 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 voiddeleteFolder(File file) A helper method to recursively delete a folder and all of its contents.static doubledistance(double x1, double y1, double x2, double y2) Returns the absolute distance between two points using the Pythagorean theorem.static doubledistanceSq(double x1, double y1, double x2, double y2) Returns the squared distance between two points using the Pythagorean theorem.static intencodeBitVector(boolean... inputs) Creates an integer representation of a bit vector from an array of booleans Reverse ofdecodeBitVector(int, int)static intfloor(double value) This is a faster implementation ofMath.floor(double), in that it simply typecasts the given value and then adjusts it accordingly.static longGets the system time in milliseconds.static booleanUtility method to figure out if a string is a resource name, meaning if it contains aConstants.RESOURCE_SEPARATORthat is not at the beginning or end of the string.static doublelerp(double pos1, double pos2, double factor) Computes the Linear Interpolation between the two positions given the factor.makeIntList(int start, int end) Creates a list of integers starting at the first number and ending at the second number.static booleanopenWebsite(String domain) Opens a website in the operating system's default browser.static longscrambleSeed(int i) Scrambles a single value into a seed that can be used for world gen.static longscrambleSeed(int x, int y) Scrambles an x and a y value together into a seed that can be used for world gen.static longscrambleSeed(int x, int y, long seed) Scrambles an x and a y value into a seed based on a different seed.static longscrambleSeed(int i, long seed) Scrambles a value into a seed based on a different seed.static intseconds(int seconds) Converts the amount of seconds into amount of ticks.static longshiftScramble(long l) Scrambles a seed in a predictable, always-equal, but seemingly random way, making the output seem like a completely different number.static voidsleepSafe(long time) Causes the current thread to sleep by the given amount of milliseconds, ignoring any interruptions that might be thrown.static inttoGridPos(double worldPos) Converts a given position in the world to a chunk grid position.static inttoWorldPos(int gridPos) Converts a given position of a chunk into its world position.
-
Field Details
-
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'sIChunkOrWorld.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. -
GSON
public static final com.google.gson.Gson GSONA 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 usedistanceSq(double, double, double, double).- Parameters:
x1- The first point's xy1- The first point's yx2- The second point's xy2- 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 xy1- The first point's yx2- The second point's xy2- 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 clampmin- The minimummax- 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 clampmin- The minimummax- 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 clampmin- The minimummax- The maximum- Returns:
- The clamped number
- See Also:
-
floor
public static int floor(double value) This is a faster implementation ofMath.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 ofMath.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
A helper method to recursively delete a folder and all of its contents. Just callingFile.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
Creates an entity from its entity registry name and with the given world.- Parameters:
name- The registry name of the entityworld- 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 toSystem.currentTimeMillis()to a more sensible amount of characters.- Returns:
- The system time in milliseconds
-
createAndOpen
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 usingDesktop.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
Opens a website in the operating system's default browser. This is a utility method asDesktop.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, seescrambleSeed(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 valuey- 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 valuey- The y valueseed- 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 valueseed- The original seed- Returns:
- The new seed
-
makeIntList
Creates a list of integers starting at the first number and ending at the second number. This can be especially useful for creatingIFilteredInventoryinstances with a lot of slots.- Parameters:
start- The first number (inclusive)end- The second number (exclusive)- Returns:
- A list of integers
- See Also:
-
isResourceName
Utility method to figure out if a string is a resource name, meaning if it contains aConstants.RESOURCE_SEPARATORthat 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 ofdecodeBitVector(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 ofencodeBitVector(boolean...)- Parameters:
input- The input to decodesize- 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
-