Class MathsHelper

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

public final class MathsHelper extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final DecimalFormat
    A decimal format that only preserves two decimal places.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    adjustToRange(int initial, int min, int max)
    Takes an integer value and fits it within a range.
    static Map<net.minecraft.core.Direction,net.minecraft.world.phys.shapes.VoxelShape>
    createHorizontalShapes(double x1, double y1, double z1, double x2, double y2, double z2)
    Creates a rotated variant of a voxel shape for each horizontal direction.
    static int
    getAverage(int first, int second)
    Gets the middle integer between two other integers.
    static net.minecraft.world.phys.AABB
    getBoundsForPixels(int minX, int minY, int minZ, int maxX, int maxY, int maxZ)
    Creates a bounding box using pixel size.
    static double
    getDistanceBetweenPoints(net.minecraft.world.phys.Vec3 firstPos, net.minecraft.world.phys.Vec3 secondPos)
    Calculates the distance between two Vec3 positions.
    static int
    getPercentage(int value, int total)
    Gets the percentage of an integer.
    static double
    getPixelDistance(int pixels)
    Gets the distance in world for an amount of pixels.
    static boolean
    isInRange(double min, double max, double value)
    Checks if a double is within range of two other doubles.
    static int
    multiplyColor(int color, float factor)
    Multiplies an int packed color and returns a new int packed number.
    static int
    nextIntInclusive(Random rand, int min, int max)
    Used to retrieve a random integer between the two provided integers.
    static String
    profileNanoTime(long start, long stop)
     
    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 given range.
    static net.minecraft.core.BlockPos
    randomOffsetHorizontal(net.minecraft.core.BlockPos startPos, net.minecraft.util.RandomSource rng, int range)
    Offsets a position by a random amount horizontally within an upper range.
    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 voxel shape that has been rotated a given direction.
    static double
    round(double value, int places)
    This method can be used to round a double to a certain amount of places.
    static boolean
    tryPercentage(double percent)
    A method which handles the calculating of percentages.
    static net.minecraft.world.phys.Vec3
    vec3(net.minecraft.core.BlockPos pos)
     

    Methods inherited from class java.lang.Object

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

    • DECIMAL_2

      public static final DecimalFormat DECIMAL_2
      A decimal format that only preserves two decimal places.
  • Constructor Details

    • MathsHelper

      public MathsHelper()
  • Method Details

    • isInRange

      public static boolean isInRange(double min, double max, double value)
      Checks if a double is within range of two other doubles.
      Parameters:
      min - : The smallest valid value.
      max - : The largest valid value.
      value - : The value to check.
      Returns:
      boolean: Whether or not the value is within the provided scope.
    • getDistanceBetweenPoints

      public static double getDistanceBetweenPoints(net.minecraft.world.phys.Vec3 firstPos, net.minecraft.world.phys.Vec3 secondPos)
      Calculates the distance between two Vec3 positions.
      Parameters:
      firstPos - : The first position to work with.
      secondPos - : The second position to work with.
      Returns:
      double: The distance between the two provided locations.
    • round

      public static double round(double value, int places)
      This method can be used to round a double to a certain amount of places.
      Parameters:
      value - : The double being round.
      places - : The amount of places to round the double to.
      Returns:
      double: The double entered however being rounded to the amount of places specified.
    • nextIntInclusive

      public static int nextIntInclusive(Random rand, int min, int max)
      Used to retrieve a random integer between the two provided integers. The integers provided are also possible outcomes.
      Parameters:
      rand - An instance of random.
      min - The minimum value which can be returned by this method.
      max - The maximum value which can be returned by this method.
      Returns:
      An integer between the min and max, including the min and max.
    • tryPercentage

      public static boolean tryPercentage(double percent)
      A method which handles the calculating of percentages. While this isn't a particularly difficult piece of code, it has been added for the sake of simplicity.
      Parameters:
      percent - : The percent chance that this method should return true. 1.00 = 100%
      Returns:
      boolean: Returns are randomly true or false, based on the suplied percentage.
    • getAverage

      public static int getAverage(int first, int second)
      Gets the middle integer between two other integers. The order is not important.
      Parameters:
      first - : The first integer.
      second - : The second integer.
      Returns:
      int: The integer that is between the two provided integers.
    • getPercentage

      public static int getPercentage(int value, int total)
      Gets the percentage of an integer. Result is an integer and decimal is lost.
      Parameters:
      value - The value to get the percentage of.
      total - The total/max value.
      Returns:
      The percentage as an integer.
    • getPixelDistance

      public static double getPixelDistance(int pixels)
      Gets the distance in world for an amount of pixels. A basic block is a cubic meter, and each pixel is 1/16th of a block.
      Parameters:
      pixels - The amount of pixels
      Returns:
      The distance in game for those pixels.
    • getBoundsForPixels

      public static net.minecraft.world.phys.AABB getBoundsForPixels(int minX, int minY, int minZ, int maxX, int maxY, int maxZ)
      Creates a bounding box using pixel size.
      Parameters:
      minX - The min X pos.
      minY - The min Y pos.
      minZ - The min Z pos.
      maxX - The max X pos.
      maxY - The max Y pos.
      maxZ - The max Z pos.
      Returns:
      A bounding box that is made to a pixel specific size.
    • adjustToRange

      public static int adjustToRange(int initial, int min, int max)
      Takes an integer value and fits it within a range. If the initial value is less than the minimum it will be set to the minimum. If the initial value is greater than the maximum it will be lowered to the maximum.
      Parameters:
      initial - The initial value.
      min - The minimum value.
      max - The maximum value.
      Returns:
      The adjusted value.
    • multiplyColor

      public static int multiplyColor(int color, float factor)
      Multiplies an int packed color and returns a new int packed number.
      Parameters:
      color - The base color.
      factor - The value to multiply the color by. Less than 1 will darken. Greater than 1 will lighten.
      Returns:
      The resulting color as a packed integer.
    • createHorizontalShapes

      public static Map<net.minecraft.core.Direction,net.minecraft.world.phys.shapes.VoxelShape> createHorizontalShapes(double x1, double y1, double z1, double x2, double y2, double z2)
      Creates a rotated variant of a voxel shape for each horizontal direction. The default/base input is expected to be oriented north.
      Parameters:
      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:
      A map containing rotated shape variants for each horizontal direction.
    • 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 voxel shape that has been rotated a given direction. The default/base input is expected to be oriented north. Thanks to Gigaherz for writing the original version of this code.
      Parameters:
      facing - The direction to rotate the shape.
      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:
      A voxel shape that has been rotated in the specified direction.
    • profileNanoTime

      public static String profileNanoTime(long start, long stop)
    • randomOffsetHorizontal

      public static net.minecraft.core.BlockPos randomOffsetHorizontal(net.minecraft.core.BlockPos startPos, net.minecraft.util.RandomSource rng, int range)
      Offsets a position by a random amount horizontally within an upper range. Movement will be along the X and Z axis.
      Parameters:
      startPos - The starting position to offset from.
      rng - A random number source.
      range - The maximum amount of blocks the position can be offset by in both the positive and negative direction. For example a value of 5 will allow the new position to be -5 to 5 blocks away on either axis. Value should be positive.
      Returns:
      The offset postion.
    • 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 given range.
      Parameters:
      startPos - The starting position to offset from.
      rng - A random number source.
      rangeX - The maximum amount of blocks the position can be offset on the X axis in both positive and negative directions.
      rangeY - The maximum amount of blocks the position can be offset on the Y axis in both positive and negative directions.
      rangeZ - The maximum amount of blocks the position can be offset on the Z axis in both positive and negative directions.
      Returns:
      The offset position.
    • vec3

      public static net.minecraft.world.phys.Vec3 vec3(net.minecraft.core.BlockPos pos)