Class MathsHelper
java.lang.Object
net.darkhax.bookshelf.common.api.util.MathsHelper
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DecimalFormatA decimal format that will only preserve two decimal places.static final RandomA RNG source that can be used in contexts where a more suitable RNG source is not available.static final net.minecraft.util.RandomSourceA RandomSource that can be used in contexts where a more suitable RNG source is not available. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic floataverage(int... values) Calculates the average of many integers.static net.minecraft.world.phys.AABBboundsForPixels(int minX, int minY, int minZ, int maxX, int maxY, int maxZ) Creates an Axis Aligned Bounding Box from a series of pixel measurements.static Map<net.minecraft.core.Direction, net.minecraft.world.phys.shapes.VoxelShape> createHorizontalShapes(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) Creates horizontally rotated variants of a VoxelShape.static doubledistance(net.minecraft.world.phys.Vec3 first, net.minecraft.world.phys.Vec3 second) Calculates the distance between two points.static booleaninRange(double min, double max, double value) Checks if a double is within the given range.static intGenerates a pseudorandom number within a given range of values.static intnextInt(net.minecraft.util.RandomSource rng, int min, int max) Generates a pseudorandom number within a given range of values.static floatpercentage(int value, int total) Calculates the percentage out of a total.static booleanpercentChance(double chance) Performs an RNG check that has a percent chance to succeed.static doublepixelSize(int pixels) Converts a standard pixel measurement to a world-space measurement.static net.minecraft.core.BlockPosrandomOffset(net.minecraft.core.BlockPos startPos, net.minecraft.util.RandomSource rng, int rangeX, int rangeY, int rangeZ) Offsets a position by a random amount within a limited range.static net.minecraft.core.BlockPosrandomOffsetHorizontal(net.minecraft.core.BlockPos startPos, net.minecraft.util.RandomSource rng, int range) Offsets a position horizontally by a random amount.static net.minecraft.world.phys.shapes.VoxelShaperotateShape(net.minecraft.core.Direction facing, double x1, double y1, double z1, double x2, double y2, double z2) Creates a VoxelShape that has been rotated to face a given direction.static doubleround(double value, int places) Rounds a double with a certain amount of precision.
-
Field Details
-
RANDOM
A RNG source that can be used in contexts where a more suitable RNG source is not available. -
RANDOM_SOURCE
public static final net.minecraft.util.RandomSource RANDOM_SOURCEA RandomSource that can be used in contexts where a more suitable RNG source is not available. -
DECIMAL_2
A decimal format that will only preserve two decimal places.
-
-
Constructor Details
-
MathsHelper
public MathsHelper()
-
-
Method Details
-
inRange
public static boolean inRange(double min, double max, double value) Checks if a double is within the given range.- Parameters:
min- The smallest value that is valid.max- The largest value that is valid.value- The value to check.- Returns:
- If the value is within the defined range.
-
distance
public static double distance(net.minecraft.world.phys.Vec3 first, net.minecraft.world.phys.Vec3 second) Calculates the distance between two points.- Parameters:
first- The first position.second- The second position.- Returns:
- The distance between the first and second position.
-
round
public static double round(double value, int places) Rounds a double with a certain amount of precision.- Parameters:
value- The value to round.places- The amount of decimal places to preserve.- Returns:
- The rounded value.
-
nextInt
Generates a pseudorandom number within a given range of values. The range of values is inclusive of the minimum and maximum value.- Parameters:
rng- The RNG source to generate the number.min- The minimum value to generate.max- The maximum value to generate.- Returns:
- A pseudorandom number within the provided range.
-
nextInt
public static int nextInt(net.minecraft.util.RandomSource rng, int min, int max) Generates a pseudorandom number within a given range of values. The range of values is inclusive of the minimum and maximum value.- Parameters:
rng- The RNG source to generate the number.min- The minimum value to generate.max- The maximum value to generate.- Returns:
- A pseudorandom number within the provided range.
-
percentChance
public static boolean percentChance(double chance) Performs an RNG check that has a percent chance to succeed.- Parameters:
chance- The chance that the check will succeed.- Returns:
- Returns true when the RNG check is successful.
-
average
public static float average(int... values) Calculates the average of many integers.- Parameters:
values- The values to average.- Returns:
- The average of the input values.
-
percentage
public static float percentage(int value, int total) Calculates the percentage out of a total.- Parameters:
value- The value that is available.total- The largest possible value.- Returns:
- The calculated percentage.
-
pixelSize
public static double pixelSize(int pixels) Converts a standard pixel measurement to a world-space measurement. This assumes one block in the world represents 16 pixels.- Parameters:
pixels- The amount of pixels.- Returns:
- The size of the pixels in the world-space.
-
boundsForPixels
public static net.minecraft.world.phys.AABB boundsForPixels(int minX, int minY, int minZ, int maxX, int maxY, int maxZ) Creates an Axis Aligned Bounding Box from a series of pixel measurements.- Parameters:
minX- The start on the X axis.minY- The start on the Y axis.minZ- The start on the Z axis.maxX- The end on the X axis.maxY- The end on the Y axis.maxZ- The end on the Z axis.- Returns:
- An AABB that represents a series of block pixel measurements.
-
createHorizontalShapes
public static Map<net.minecraft.core.Direction,net.minecraft.world.phys.shapes.VoxelShape> createHorizontalShapes(double minX, double minY, double minZ, double maxX, double maxY, double maxZ) Creates horizontally rotated variants of a VoxelShape. The input values are considered to be rotated north.- Parameters:
minX- The min X of the shape.minY- The min Y of the shape.minZ- The min Z of the shape.maxX- The max X of the shape.maxY- The max Y of the shape.maxZ- The max Z of the shape.- Returns:
- A map of rotated VoxelShape.
-
rotateShape
public static net.minecraft.world.phys.shapes.VoxelShape rotateShape(net.minecraft.core.Direction facing, double x1, double y1, double z1, double x2, double y2, double z2) Creates a VoxelShape that has been rotated to face a given direction. The input sizes are considered to be rotated north already. The up/down rotations are not supported yet.- Parameters:
facing-x1- The min x coordinate.y1- The min y coordinate.z1- The min z coordinate.x2- The max x coordinate.y2- The max y coordinate.z2- The max z coordinate.- Returns:
- The rotated VoxelShape.
-
randomOffsetHorizontal
public static net.minecraft.core.BlockPos randomOffsetHorizontal(net.minecraft.core.BlockPos startPos, net.minecraft.util.RandomSource rng, int range) Offsets a position horizontally by a random amount.- Parameters:
startPos- The starting position to offset from.rng- The RNG source.range- The maximum amount of blocks to offset the position by. This range applies to both the positive and negative directions.- Returns:
- The randomly offset position.
-
randomOffset
public static net.minecraft.core.BlockPos randomOffset(net.minecraft.core.BlockPos startPos, net.minecraft.util.RandomSource rng, int rangeX, int rangeY, int rangeZ) Offsets a position by a random amount within a limited range.- Parameters:
startPos- The starting position to offset from.rng- The RNG source.rangeX- The maximum amount of blocks to offset on the X axis.rangeY- The maximum amount of blocks to offset on the Y axis.rangeZ- The maximum amount of blocks to offset on the Z axis.- Returns:
- The randomly offset position.
-