Package dev.majek.pc.util
Class Utils
- java.lang.Object
-
- dev.majek.pc.util.Utils
-
public final class Utils extends Object
Contains various unrelated utility functions.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
canStand(org.bukkit.block.Block block)
If a player would be teleported to the location of this block, confirm they cannot: fall through or take damage unless the block is waterstatic String
capitalize(String x)
Capitalizes each word in the provided string.static int
constrain(int n, int min, int max)
Constrains the given number between the given minimum and maximum value.static String
convertItemStackToJson(org.bukkit.inventory.ItemStack itemStack)
Converts anItemStack
to a Json string for sending withBaseComponent
's.static List<UUID>
deserializeMembers(String members)
static String
doubleToString(double d, int precision)
Converts the given floating point number to a string and then truncates the decimal point to the given precision.static org.bukkit.Location
findSafe(org.bukkit.Location safe, int bottom, int top)
Search for a safe location for a player to stand We search a column at the x z of the origin The algorithm treats this column as being sorted with ground at the bottom and sky above it Because of this we can make assumptions that the ground will always have: 2 non damaging blocks above (air|vines|etc) and 1 block the player can stand on below (non passable and non damaging) Because bottom and top specify search range for Y they should satisfy these ranges:static org.bukkit.Location
findSafeNear(org.bukkit.Location origin, int bottom, int top)
Search the nearby area for a location a player can safely stand before teleporting them Set the location to be in the center of the block ready for teleportation Calculate reasonable bottom and top valuesstatic String
formattedName(Enum e)
Converts the given enumeration element's name (which should be all capitalized with underscores) and replaces the underscores with hyphens and converts the string to lower case.static String
getWorldName(String alias)
Attempts to find the proper world name for the given alias.static int
indexOfDefault(int index, int def)
Returns the given index if it is not equal to negative one, otherwise it returns the default value.static boolean
isSafe(org.bukkit.Location location)
If a player would be teleported to this location, confirm they cannot: take damage from the block at the location (body) take damage from the block above the location (head)static <T> T
safeValueOf(Function<String,T> valueOf, String input)
Returns the value returned by the given function with the given input, or null if the function throws an exception.static String
serializeMembers(List<UUID> members)
static org.bukkit.Location
walk(org.bukkit.Location origin, int dx, int dz)
Search for a safe location using findSafe starting at and including origin in steps of dx dz where these values are multiplied by 16 when there is Liquid at y62 as this usually indicates being in an ocean and oceans provide a lower probability for findSafe to return a non null location.
-
-
-
Field Detail
-
UUID_00
public static final UUID UUID_00
-
-
Method Detail
-
convertItemStackToJson
public static String convertItemStackToJson(org.bukkit.inventory.ItemStack itemStack)
Converts anItemStack
to a Json string for sending withBaseComponent
's.- Parameters:
itemStack
- the item to convert- Returns:
- the Json string representation of the item
-
doubleToString
public static String doubleToString(double d, int precision)
Converts the given floating point number to a string and then truncates the decimal point to the given precision.- Parameters:
d
- the float point to convert to a string.precision
- the number of decimal point the resulting string should have.- Returns:
- a string form of the given floating point number, with the decimal point truncated to the given precision.
-
constrain
public static int constrain(int n, int min, int max)
Constrains the given number between the given minimum and maximum value. If the given number n is outside the given range then the closest bound is returned.- Parameters:
n
- the number to constrain.min
- the minimum bound.max
- the maximum bound.- Returns:
- the constrained number.
- Throws:
IllegalArgumentException
- if the given maximum bound is less than the given minimum bound.
-
getWorldName
public static String getWorldName(String alias)
Attempts to find the proper world name for the given alias. No pattern is necessarily used here, rather common names for the various vanilla worlds are mapped to the correct names. If no world name could be found for the given alias, the given alias is returned.- Parameters:
alias
- the alias to map.- Returns:
- the correct world name for the given alias, or the given alias if no world name could be found.
-
formattedName
public static String formattedName(Enum e)
Converts the given enumeration element's name (which should be all capitalized with underscores) and replaces the underscores with hyphens and converts the string to lower case.- Parameters:
e
- the enumeration element.- Returns:
- the formatted name of the given element as defined above.
-
capitalize
public static String capitalize(String x)
Capitalizes each word in the provided string. A word is defined as a cluster of characters separated on either side by spaces or the end or beginning of a string.- Parameters:
x
- the string to capitalize.- Returns:
- the capitalized string.
-
indexOfDefault
public static int indexOfDefault(int index, int def)
Returns the given index if it is not equal to negative one, otherwise it returns the default value.- Parameters:
index
- the index.def
- the default value.- Returns:
- the given index if it is not equal to negative one, otherwise it returns the default value.
-
safeValueOf
public static <T> T safeValueOf(Function<String,T> valueOf, String input)
Returns the value returned by the given function with the given input, or null if the function throws an exception.- Type Parameters:
T
- the return type.- Parameters:
valueOf
- the value-of function.input
- the input.- Returns:
- the value returned by the given function with the given input, or null if the function throws an exception.
-
isSafe
public static boolean isSafe(org.bukkit.Location location)
If a player would be teleported to this location, confirm they cannot: take damage from the block at the location (body) take damage from the block above the location (head)- Parameters:
location
- the location to check- Returns:
- if a player would be damaged by any of the blocks at this location upon tp ignoring the block below
-
canStand
public static boolean canStand(org.bukkit.block.Block block)
If a player would be teleported to the location of this block, confirm they cannot: fall through or take damage unless the block is water- Parameters:
block
- the block to check- Returns:
- if the block can be stood on without the block damaging the player
-
findSafe
public static org.bukkit.Location findSafe(org.bukkit.Location safe, int bottom, int top)
Search for a safe location for a player to stand We search a column at the x z of the origin The algorithm treats this column as being sorted with ground at the bottom and sky above it Because of this we can make assumptions that the ground will always have: 2 non damaging blocks above (air|vines|etc) and 1 block the player can stand on below (non passable and non damaging) Because bottom and top specify search range for Y they should satisfy these ranges:- Parameters:
safe
- the location to check for safetybottom
- where to set the bottom of the "binary search"top
- where to set the top of the "binary search"- Returns:
- a safe location if found, else null
-
findSafeNear
public static org.bukkit.Location findSafeNear(org.bukkit.Location origin, int bottom, int top)
Search the nearby area for a location a player can safely stand before teleporting them Set the location to be in the center of the block ready for teleportation Calculate reasonable bottom and top values- Parameters:
origin
- location to check for safetybottom
- location of the bottomtop
- location of the top- Returns:
- a safe location near the origin
-
walk
public static org.bukkit.Location walk(org.bukkit.Location origin, int dx, int dz)
Search for a safe location using findSafe starting at and including origin in steps of dx dz where these values are multiplied by 16 when there is Liquid at y62 as this usually indicates being in an ocean and oceans provide a lower probability for findSafe to return a non null location.- Parameters:
origin
- the location to start searching fromdx
- the x offset we tend towardsdz
- the x offset we tend towards- Returns:
- the first safe location we find
-
-