Class MathsHelper

java.lang.Object
net.darkhax.bookshelf.common.api.util.MathsHelper

public class MathsHelper extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final DecimalFormat
    A decimal format that will only preserve two decimal places.
    static final Random
    A RNG source that can be used in contexts where a more suitable RNG source is not available.
    static final net.minecraft.util.RandomSource
    A RandomSource that can be used in contexts where a more suitable RNG source is not available.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static float
    average(int... values)
    Calculates the average of many integers.
    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.
    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 double
    distance(net.minecraft.world.phys.Vec3 first, net.minecraft.world.phys.Vec3 second)
    Calculates the distance between two points.
    static boolean
    inRange(double min, double max, double value)
    Checks if a double is within the given range.
    static int
    nextInt(Random rng, int min, int max)
    Generates a pseudorandom number within a given range of values.
    static int
    nextInt(net.minecraft.util.RandomSource rng, int min, int max)
    Generates a pseudorandom number within a given range of values.
    static float
    percentage(int value, int total)
    Calculates the percentage out of a total.
    static boolean
    percentChance(double chance)
    Performs an RNG check that has a percent chance to succeed.
    static double
    pixelSize(int pixels)
    Converts a standard pixel measurement to a world-space measurement.
    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.
    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.
    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.
    static double
    round(double value, int places)
    Rounds a double with a certain amount of precision.

    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 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_SOURCE
      A RandomSource that can be used in contexts where a more suitable RNG source is not available.
    • DECIMAL_2

      public static final DecimalFormat 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

      public static int nextInt(Random 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.
    • 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.