Class MathUtils

java.lang.Object
com.ninjaguild.dragoneggdrop.utils.math.MathUtils

public final class MathUtils
extends Object
A powerful utility class to assist in the parsing and evaluation of arithmetic functions with custom variables and operations through a recursive algorithm.
Author:
Parker Hawke - Choco
  • Method Details

    • parseExpression

      public static MathExpression parseExpression​(String expression, ParticleParameterContext parameters)
      Evaluate a mathematical expression with given variables.
      Parameters:
      expression - the string to parse
      parameters - the parameter context containing necessary variables for this expression. To modify variables at any given time between evaluations, update values in the context
      Returns:
      The mathematical expression
    • parseExpression

      public static MathExpression parseExpression​(String expression)
      Evaluate a basic mathematical expression. Variables are not permitted in expressions parsed by this method. For variable-based functions, see parseExpression(String, ParticleParameterContext).
      Parameters:
      expression - the string to parse
      Returns:
      The mathematical expression
    • injectMathematicalOperator

      public static boolean injectMathematicalOperator​(String functionName, DoubleUnaryOperator operator)
      Inject a custom mathematical operation into the expression parser.
      Parameters:
      functionName - the name of the function to inject (i.e. "sqrt")
      operator - the operation to perform when parsing this function
      Returns:
      true if successful. false if operator already exists
    • parseRespawnSeconds

      public static int parseRespawnSeconds​(String value, int defaultSeconds)
      Parse a timestamp value (i.e. 1w2d3h4m5s) and return its value in seconds.
      Parameters:
      value - the value to parse
      defaultSeconds - the value to return if "value" is null (i.e. from a config)
      Returns:
      the amount of time in seconds represented by the supplied value
    • parseRespawnSeconds

      public static int parseRespawnSeconds​(String value)
      Parse a timestamp value (i.e. 1w2d3h4m5s) and return its value in seconds.
      Parameters:
      value - the value to parse
      Returns:
      the amount of time in seconds represented by the supplied value
    • getFormattedTime

      public static String getFormattedTime​(int timeInSeconds)
      Get a formatted time String from a time in seconds. Formatted time should be in the format, "x hours, y minutes, z seconds". Alternatively, if time is 0, "now", or "invalid seconds" otherwise.
      Parameters:
      timeInSeconds - the time in seconds
      Returns:
      the formatted time
    • clamp

      public static double clamp​(double value, double min, double max)
      Clamp a value between a minimum and maximum value.
      Parameters:
      value - the value to clamp
      min - the minimum value (inclusive)
      max - the maximum value (inclusive)
      Returns:
      the clamped value