diff --git a/dependencies.gradle b/dependencies.gradle index 4bafa45d5..2c67644cb 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -22,5 +22,5 @@ * For more details, see https://docs.gradle.org/8.0.1/userguide/java_library_plugin.html#sec:java_library_configurations_graph */ dependencies { - + embed 'org.mariuszgromada.math:MathParser.org-mXparser:6.1.0' } diff --git a/src/main/java/com/cleanroommc/modularui/ModularUI.java b/src/main/java/com/cleanroommc/modularui/ModularUI.java index 987b7d415..85f78c2fa 100644 --- a/src/main/java/com/cleanroommc/modularui/ModularUI.java +++ b/src/main/java/com/cleanroommc/modularui/ModularUI.java @@ -3,13 +3,13 @@ import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.SidedProxy; -import net.minecraftforge.fml.common.event.FMLLoadCompleteEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.event.FMLServerStartingEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.mariuszgromada.math.mxparser.License; @Mod(modid = ModularUI.ID, name = ModularUI.NAME, @@ -37,6 +37,11 @@ public class ModularUI { private static boolean blurLoaded = false; private static boolean sorterLoaded = false; + static { + // confirm mXparser license + License.iConfirmNonCommercialUse("CleanroomMC"); + } + @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { blurLoaded = Loader.isModLoaded("blur"); diff --git a/src/main/java/com/cleanroommc/modularui/utils/MathUtils.java b/src/main/java/com/cleanroommc/modularui/utils/MathUtils.java index 95be617f4..6bf77ecdb 100644 --- a/src/main/java/com/cleanroommc/modularui/utils/MathUtils.java +++ b/src/main/java/com/cleanroommc/modularui/utils/MathUtils.java @@ -1,7 +1,53 @@ package com.cleanroommc.modularui.utils; +import org.mariuszgromada.math.mxparser.Constant; +import org.mariuszgromada.math.mxparser.Expression; + public class MathUtils { + // SI prefixes + public static final Constant k = new Constant("k", 1e3); + public static final Constant M = new Constant("M", 1e6); + public static final Constant G = new Constant("G", 1e9); + public static final Constant T = new Constant("T", 1e12); + public static final Constant P = new Constant("P", 1e15); + public static final Constant E = new Constant("E", 1e18); + public static final Constant Z = new Constant("Z", 1e21); + public static final Constant Y = new Constant("Y", 1e24); + public static final Constant m = new Constant("m", 1e-3); + public static final Constant u = new Constant("u", 1e-6); + public static final Constant n = new Constant("n", 1e-9); + public static final Constant p = new Constant("p", 1e-12); + public static final Constant f = new Constant("f", 1e-15); + public static final Constant a = new Constant("a", 1e-18); + public static final Constant z = new Constant("z", 1e-21); + public static final Constant y = new Constant("y", 1e-24); + + public static ParseResult parseExpression(String expression) { + return parseExpression(expression, Double.NaN, false); + } + + public static ParseResult parseExpression(String expression, boolean useSiPrefixes) { + return parseExpression(expression, Double.NaN, useSiPrefixes); + } + + public static ParseResult parseExpression(String expression, double defaultValue) { + return parseExpression(expression, defaultValue, true); + } + + public static ParseResult parseExpression(String expression, double defaultValue, boolean useSiPrefixes) { + if (expression == null || expression.isEmpty()) return ParseResult.success(defaultValue); + Expression e = new Expression(expression); + if (useSiPrefixes) { + e.addConstants(k, M, G, T, P, E, Z, Y, m, u, n, p, f, a, z, y); + } + double result = e.calculate(); + if (Double.isNaN(result)) { + return ParseResult.failure(defaultValue, e.getErrorMessage()); + } + return ParseResult.success(result); + } + public static int clamp(int v, int min, int max) { return Math.max(min, Math.min(v, max)); } diff --git a/src/main/java/com/cleanroommc/modularui/utils/ParseResult.java b/src/main/java/com/cleanroommc/modularui/utils/ParseResult.java new file mode 100644 index 000000000..12cc7bf7f --- /dev/null +++ b/src/main/java/com/cleanroommc/modularui/utils/ParseResult.java @@ -0,0 +1,46 @@ +package com.cleanroommc.modularui.utils; + +import org.jetbrains.annotations.NotNull; + +public class ParseResult { + + private final double result; + private final String error; + + public static ParseResult success(double result) { + return new ParseResult(result, null); + } + + public static ParseResult failure(@NotNull String error) { + return failure(Double.NaN, error); + } + + public static ParseResult failure(double value, @NotNull String error) { + return new ParseResult(value, error); + } + + private ParseResult(double result, String error) { + this.result = result; + this.error = error; + } + + public boolean isSuccess() { + return this.error == null; + } + + public boolean isFailure() { + return this.error != null; + } + + public boolean hasValue() { + return !Double.isNaN(this.result); + } + + public double getResult() { + return result; + } + + public String getError() { + return error; + } +} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/Constant.java b/src/main/java/com/cleanroommc/modularui/utils/math/Constant.java deleted file mode 100644 index dec746b06..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/Constant.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.cleanroommc.modularui.utils.math; - -import com.cleanroommc.modularui.api.IMathValue; - -/** - * Constant class - *

- * This class simply returns supplied in the constructor value - */ -public class Constant implements IMathValue { - - private double doubleValue; - private String stringValue; - - public Constant(double doubleValue) { - this.doubleValue = doubleValue; - } - - public Constant(String stringValue) { - this.stringValue = stringValue; - } - - @Override - public IMathValue get() { - return this; - } - - @Override - public boolean isNumber() { - return this.stringValue == null; - } - - @Override - public void set(double value) { - this.doubleValue = value; - this.stringValue = null; - } - - @Override - public void set(String value) { - this.doubleValue = 0; - this.stringValue = value; - } - - @Override - public double doubleValue() { - return this.doubleValue; - } - - @Override - public boolean booleanValue() { - if (this.isNumber()) { - return Operation.isTrue(this.doubleValue); - } - - return this.stringValue.equalsIgnoreCase("true"); - } - - @Override - public String stringValue() { - return this.stringValue; - } - - @Override - public String toString() { - return this.stringValue == null ? String.valueOf(this.doubleValue) : "\"" + this.stringValue + "\""; - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/Direction.java b/src/main/java/com/cleanroommc/modularui/utils/math/Direction.java deleted file mode 100644 index bfb5f2ef1..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/Direction.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.cleanroommc.modularui.utils.math; - -import com.cleanroommc.modularui.utils.Interpolations; - -public enum Direction { - - TOP(0.5F, 0F), LEFT(0F, 0.5F), BOTTOM(0.5F, 1F), RIGHT(1F, 0.5F); - - public final float anchorX; - public final float anchorY; - public final int factorX; - public final int factorY; - - Direction(float anchorX, float anchorY) { - this.anchorX = anchorX; - this.anchorY = anchorY; - this.factorX = (int) Interpolations.lerp(-1, 1, anchorX); - this.factorY = (int) Interpolations.lerp(-1, 1, anchorY); - } - - public boolean isHorizontal() { - return this == LEFT || this == RIGHT; - } - - public boolean isVertical() { - return this == TOP || this == BOTTOM; - } - - public Direction opposite() { - if (this == TOP) { - return BOTTOM; - } else if (this == BOTTOM) { - return TOP; - } else if (this == LEFT) { - return RIGHT; - } - - /* this == RIGHT */ - return LEFT; - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/Group.java b/src/main/java/com/cleanroommc/modularui/utils/math/Group.java deleted file mode 100644 index f084c19ec..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/Group.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.cleanroommc.modularui.utils.math; - -import com.cleanroommc.modularui.api.IMathValue; - -/** - * Group class - *

- * Simply wraps given {@link IMathValue} into parenthesis in the - * {@link #toString()} method. - */ -public class Group implements IMathValue { - - private final IMathValue value; - - public Group(IMathValue value) { - this.value = value; - } - - @Override - public IMathValue get() { - return this.value.get(); - } - - @Override - public boolean isNumber() { - return this.value.isNumber(); - } - - @Override - public void set(double value) { - this.value.set(value); - } - - @Override - public void set(String value) { - this.value.set(value); - } - - @Override - public double doubleValue() { - return this.value.doubleValue(); - } - - @Override - public boolean booleanValue() { - return this.value.booleanValue(); - } - - @Override - public String stringValue() { - return this.value.stringValue(); - } - - @Override - public String toString() { - return "(" + this.value.toString() + ")"; - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/MathBuilder.java b/src/main/java/com/cleanroommc/modularui/utils/math/MathBuilder.java deleted file mode 100644 index 90d47db08..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/MathBuilder.java +++ /dev/null @@ -1,652 +0,0 @@ -package com.cleanroommc.modularui.utils.math; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.Function; -import com.cleanroommc.modularui.utils.math.functions.classic.*; -import com.cleanroommc.modularui.utils.math.functions.limit.Clamp; -import com.cleanroommc.modularui.utils.math.functions.limit.Max; -import com.cleanroommc.modularui.utils.math.functions.limit.Min; -import com.cleanroommc.modularui.utils.math.functions.rounding.Ceil; -import com.cleanroommc.modularui.utils.math.functions.rounding.Floor; -import com.cleanroommc.modularui.utils.math.functions.rounding.Round; -import com.cleanroommc.modularui.utils.math.functions.rounding.Trunc; -import com.cleanroommc.modularui.utils.math.functions.string.StringContains; -import com.cleanroommc.modularui.utils.math.functions.string.StringEndsWith; -import com.cleanroommc.modularui.utils.math.functions.string.StringStartsWith; -import com.cleanroommc.modularui.utils.math.functions.trig.*; -import com.cleanroommc.modularui.utils.math.functions.utility.*; - -import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; -import org.jetbrains.annotations.Nullable; - -import java.lang.reflect.Constructor; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.regex.Pattern; - -/** - * Math builder - *

- * This class is responsible for parsing math expressions provided by - * user in a string to an {@link IMathValue} which can be used to compute - * some value dynamically using different math operators, variables and - * functions. - *

- * It works by first breaking down given string into a list of tokens - * and then putting them together in a binary tree-like {@link IMathValue}. - *

- * TODO: maybe implement constant pool (to reuse same values)? - * TODO: maybe pre-compute constant expressions? - */ -public class MathBuilder { - - public static final Pattern DECIMAL_PATTERN = Pattern.compile("-?\\d+(\\.\\d+)?([eE]-?\\d+)?"); - private static final Pattern BRACKETS = Pattern.compile("[()]"); - - public static final MathBuilder INSTANCE = new MathBuilder(); - - /** - * Named variables that can be used in math expression by this - * builder - */ - public final Map variables = new Object2ObjectOpenHashMap<>(); - - /** - * Map of functions which can be used in the math expressions - */ - public final Map> functions = new Object2ObjectOpenHashMap<>(); - - /** - * Whether math expression parser should be strict about which characters - * can be used within math expressions - */ - protected boolean strict = true; - - public MathBuilder() { - /* Some default values */ - this.register(new Variable("PI", Math.PI)); - this.register(new Variable("NAPIER", Math.E)); - - /* Rounding functions */ - this.functions.put("floor", Floor.class); - this.functions.put("round", Round.class); - this.functions.put("ceil", Ceil.class); - this.functions.put("trunc", Trunc.class); - - /* Selection and limit functions */ - this.functions.put("clamp", Clamp.class); - this.functions.put("max", Max.class); - this.functions.put("min", Min.class); - - /* Classical functions */ - this.functions.put("abs", Abs.class); - this.functions.put("exp", Exp.class); - this.functions.put("ln", Ln.class); - this.functions.put("sqrt", Sqrt.class); - this.functions.put("mod", Mod.class); - this.functions.put("pow", Pow.class); - - /* Trig functions */ - this.functions.put("cos", Cos.class); - this.functions.put("sin", Sin.class); - this.functions.put("acos", Acos.class); - this.functions.put("asin", Asin.class); - this.functions.put("atan", Atan.class); - this.functions.put("atan2", Atan2.class); - - /* Utility functions */ - this.functions.put("lerp", Lerp.class); - this.functions.put("lerprotate", LerpRotate.class); - this.functions.put("random", Random.class); - this.functions.put("randomi", RandomInteger.class); - this.functions.put("roll", DieRoll.class); - this.functions.put("rolli", DieRollInteger.class); - this.functions.put("hermite", HermiteBlend.class); - - /* String functions */ - this.functions.put("str_contains", StringContains.class); - this.functions.put("str_starts", StringStartsWith.class); - this.functions.put("str_ends", StringEndsWith.class); - } - - public MathBuilder lenient() { - this.strict = false; - - return this; - } - - /** - * Register a variable - */ - public void register(Variable variable) { - this.variables.put(variable.getName(), variable); - } - - /** - * Parse given math expression into a {@link IMathValue} which can be - * used to execute math. - */ - public IMathValue parse(String expression) throws ParseException { - if (!BRACKETS.matcher(expression).find()) { - // Absense of bracket implies it's simple decimal expression maybe with operators, - // so comma is supposed to be representing a thousand separator instead of an argument separator - expression = expression.replace(",", ""); - } - return this.parseSymbols(this.breakdownChars(this.breakdown(expression))); - } - - /** - * Breakdown an expression - */ - public String[] breakdown(String expression) throws ParseException { - /* If given string have illegal characters, then it can't be parsed */ - if (this.strict && !expression.matches("^[\\w\\s_+-/*%^&|<>=!?:.,()\"'@~\\[\\]]+$")) { - throw new ParseException("Given expression '" + expression + "' contains illegal characters!"); - } - - String[] chars = expression.split("(?!^)"); - - int left = 0; - int right = 0; - - for (String s : chars) { - if (s.equals("(")) { - left++; - } else if (s.equals(")")) { - right++; - } - } - - /* Amount of left and right brackets should be the same */ - if (left != right) { - // TODO: auto pre- or append ( or ) - throw new ParseException("Given expression '" + expression + "' has more uneven amount of parenthesis, there are " + left + " open and " + right + " closed!"); - } - - return chars; - } - - /** - * Breakdown characters into a list of math expression symbols. - */ - public List breakdownChars(String[] chars) { - List symbols = new ArrayList<>(); - StringBuilder buffer = new StringBuilder(); - int len = chars.length; - boolean string = false; - - for (int i = 0; i < len; i++) { - String s = chars[i]; - boolean longOperator = i < chars.length - 1 && this.isOperator(s + chars[i + 1]); - - if (s.equals("\"")) { - string = !string; - } - - if (string) { - buffer.append(s); - } else if (this.isOperator(s) || longOperator || s.equals(",")) { - /* Taking care of a special case of using minus sign to - * invert the positive value */ - if (s.equals("-")) { - int size = symbols.size(); - - boolean isEmpty = buffer.toString().trim().isEmpty(); - boolean isFirst = size == 0 && isEmpty; - boolean isOperatorBehind = size > 0 && (this.isOperator(symbols.get(size - 1)) || symbols.get(size - 1).equals(",")) && isEmpty; - - if (isFirst || isOperatorBehind) { - buffer.append(s); - - continue; - } - } - - /* Push buffer and operator */ - if (buffer.length() > 0) { - symbols.add(buffer.toString()); - buffer = new StringBuilder(); - } - - if (longOperator) { - symbols.add(s + chars[i + 1]); - i += 1; - } else { - symbols.add(s); - } - } else if (s.equals("(")) { - /* Push a list of symbols */ - if (buffer.length() > 0) { - symbols.add(buffer.toString()); - buffer = new StringBuilder(); - } - - int counter = 1; - - for (int j = i + 1; j < len; j++) { - String c = chars[j]; - - if (c.equals("(")) { - counter++; - } else if (c.equals(")")) { - counter--; - } - - if (counter == 0) { - symbols.add(this.breakdownChars(buffer.toString().split("(?!^)"))); - - i = j; - buffer = new StringBuilder(); - - break; - } else { - buffer.append(c); - } - } - } else { - /* Accumulate the buffer */ - buffer.append(s); - } - } - - if (buffer.length() > 0) { - symbols.add(buffer.toString()); - } - - return this.trimSymbols(symbols); - } - - /** - * Trims spaces from individual symbols - */ - private List trimSymbols(List symbols) { - List newSymbols = new ArrayList<>(); - - for (Object value : symbols) { - if (value instanceof String string) { - string = string.trim(); - - if (!string.isEmpty()) { - newSymbols.add(string); - } - } else { - newSymbols.add(this.trimSymbols((List) value)); - } - } - - return newSymbols; - } - - /** - * Parse symbols - *

- * This function is the most important part of this class. It's - * responsible for turning list of symbols into {@link IMathValue}. This - * is done by constructing a binary tree-like {@link IMathValue} based on - * {@link Operator} class. - *

- * However, beside parsing operations, it's also can return one or - * two item sized symbol lists. - */ - public IMathValue parseSymbols(List symbols) throws ParseException { - IMathValue ternary = this.tryTernary(symbols); - - if (ternary != null) { - return ternary; - } - - int size = symbols.size(); - - /* Constant, variable or group (parenthesis) */ - if (size == 1) { - return this.valueFromObject(symbols.get(0)); - } - - /* Function */ - if (size == 2) { - Object first = symbols.get(0); - Object second = symbols.get(1); - - if ((this.isVariable(first) || first.equals("-")) && second instanceof List list) { - return this.createFunction((String) first, list); - } - // This can happen with e.g. [15, %] and we cannot process this any further - throw new ParseException(String.format("Couldn't parse symbols: '%s%s'", first, second)); - } - - /* Any other math expression */ - int lastOp = this.seekLastOperator(symbols); - int op = lastOp; - - while (op != -1) { - int leftOp = this.seekLastOperator(symbols, op - 1); - - if (leftOp != -1) { - Operation left = this.operationForOperator((String) symbols.get(leftOp)); - Operation right = this.operationForOperator((String) symbols.get(op)); - - if (right.value > left.value) { - IMathValue leftValue = this.parseSymbols(symbols.subList(0, leftOp)); - IMathValue rightValue = this.parseSymbols(symbols.subList(leftOp + 1, size)); - - return new Operator(left, leftValue, rightValue); - } else if (left.value > right.value) { - Operation initial = this.operationForOperator((String) symbols.get(lastOp)); - - if (initial.value < left.value) { - IMathValue leftValue = this.parseSymbols(symbols.subList(0, lastOp)); - IMathValue rightValue = this.parseSymbols(symbols.subList(lastOp + 1, size)); - - return new Operator(initial, leftValue, rightValue); - } - - IMathValue leftValue = this.parseSymbols(symbols.subList(0, op)); - IMathValue rightValue = this.parseSymbols(symbols.subList(op + 1, size)); - - return new Operator(right, leftValue, rightValue); - } - } - - op = leftOp; - } - - Operation operation = this.operationForOperator((String) symbols.get(lastOp)); - - return new Operator(operation, this.parseSymbols(symbols.subList(0, lastOp)), this.parseSymbols(symbols.subList(lastOp + 1, size))); - } - - protected int seekLastOperator(List symbols) { - return this.seekLastOperator(symbols, symbols.size() - 1); - } - - /** - * Find the index of the first operator - */ - protected int seekLastOperator(List symbols, int offset) { - for (int i = offset; i >= 0; i--) { - Object o = symbols.get(i); - - if (this.isOperator(o)) { - /* - before a group isn't considered an operator per se */ - if (o.equals("-")) { - Object next = i < symbols.size() - 1 ? symbols.get(i + 1) : null; - Object prev = i > 0 ? symbols.get(i - 1) : null; - - if (next instanceof List && (this.isOperator(prev) || prev == null)) { - continue; - } - } - - return i; - } - } - - return -1; - } - - /** - * Try parsing a ternary expression - *

- * From what we know, with ternary expressions, we should have only one ? and :, - * and some elements from beginning till ?, in between ? and :, and also some - * remaining elements after :. - */ - protected IMathValue tryTernary(List symbols) throws ParseException { - int question = -1; - int questions = 0; - int colon = -1; - int colons = 0; - int size = symbols.size(); - - for (int i = 0; i < size; i++) { - Object object = symbols.get(i); - - if (object instanceof String) { - if (object.equals("?")) { - if (question == -1) { - question = i; - } - - questions++; - } else if (object.equals(":")) { - if (colons + 1 == questions && colon == -1) { - colon = i; - } - - colons++; - } - } - } - - if (questions == colons && question > 0 && question + 1 < colon && colon < size - 1) { - return new Ternary( - this.parseSymbols(symbols.subList(0, question)), - this.parseSymbols(symbols.subList(question + 1, colon)), - this.parseSymbols(symbols.subList(colon + 1, size)) - ); - } - - return null; - } - - /** - * Create a function value - *

- * This method in comparison to {@link #valueFromObject(Object)} - * needs the name of the function and list of args (which can't be - * stored in one object). - *

- * This method will construct {@link IMathValue}s from list of args - * mixed with operators, groups, values and commas. And then plug it - * in to a class constructor with given name. - */ - protected IMathValue createFunction(String first, List args) throws ParseException { - /* Handle special cases with negation */ - if (first.equals("!")) { - return new Negate(this.parseSymbols(args)); - } - - if (first.startsWith("!") && first.length() > 1) { - return new Negate(this.createFunction(first.substring(1), args)); - } - - /* Handle inversion of the value */ - if (first.equals("-")) { - return new Negative(new Group(this.parseSymbols(args))); - } - - if (first.startsWith("-") && first.length() > 1) { - return new Negative(this.createFunction(first.substring(1), args)); - } - - if (!this.functions.containsKey(first)) { - throw new ParseException("Function '" + first + "' couldn't be found!"); - } - - List values = new ArrayList<>(); - List buffer = new ArrayList<>(); - - for (Object o : args) { - if (o.equals(",")) { - values.add(this.parseSymbols(buffer)); - buffer.clear(); - } else { - buffer.add(o); - } - } - - if (!buffer.isEmpty()) { - values.add(this.parseSymbols(buffer)); - } - - Class function = this.functions.get(first); - Constructor ctor; - try { - ctor = function.getConstructor(IMathValue[].class, String.class); - } catch (NoSuchMethodException e) { - throw new RuntimeException(e); - } - try { - return ctor.newInstance(values.toArray(new IMathValue[0]), first); - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e); - } - } - - /** - * Get value from an object. - *

- * This method is responsible for creating different sort of values - * based on the input object. It can create constants, variables and - * groups. - */ - @SuppressWarnings("unchecked") - public IMathValue valueFromObject(Object object) throws ParseException { - if (object instanceof String symbol) { - - /* Variable and constant negation */ - if (symbol.startsWith("!")) { - return new Negate(this.valueFromObject(symbol.substring(1))); - } - - if (symbol.startsWith("\"") && symbol.endsWith("\"")) { - return new Constant(symbol.substring(1, symbol.length() - 1)); - } - - symbol = trimThousandSeparator(symbol); - Double triedNumber = tryParseNumber(symbol); - - if (triedNumber != null) { - return new Constant(triedNumber); - } else if (this.isVariable(symbol)) { - /* Need to account for a negative value variable */ - if (symbol.startsWith("-")) { - symbol = symbol.substring(1); - Variable value = this.getVariable(symbol); - - if (value != null) { - return new Negative(value); - } - } else { - IMathValue value = this.getVariable(symbol); - - /* Avoid NPE */ - if (value != null) { - return value; - } - } - } - } else if (object instanceof List list) { - return new Group(this.parseSymbols(list)); - } - - throw new ParseException("Given object couldn't be converted to value! " + object); - } - - protected String trimThousandSeparator(String symbol) { - return symbol.replace(" ", "") - .replace("\u202F", "") // French locale - .replace("_", ""); - } - - protected Double tryParseNumber(String symbol) throws ParseException { - final StringBuilder buffer = new StringBuilder(); - final char[] characters = symbol.toCharArray(); - Double leftNumber = null; - for (char c : characters) { - double multiplier = switch (c) { - case 'k', 'K' -> 1_000; - case 'm', 'M' -> 1_000_000; - case 'g', 'G', 'b', 'B' -> 1_000_000_000; - case 't', 'T' -> 1_000_000_000_000L; - case 's', 'S' -> 64; - case 'i', 'I' -> 144; - default -> 0; - }; - if (multiplier == 0) { - // Not a suffix - if (leftNumber != null) { - // Something like 2k5 cannot be parsed - throw new ParseException(String.format("Symbol %s cannot be parsed!", symbol)); - } - buffer.append(c); - } else { - if (leftNumber == null) { - // We haven't seen number so far, so try parse it - final String buffered = buffer.toString(); - try { - leftNumber = Double.parseDouble(buffered); - } catch (NumberFormatException ignored) { - // Don't throw error here, as it could be variable name - return null; - } - } - // Continue parsing to allow 2kk == 2000k for example - leftNumber *= multiplier; - } - } - if (leftNumber != null) { - return leftNumber; - } - try { - return Double.parseDouble(symbol); - } catch (NumberFormatException ignored) { - throw new ParseException(String.format("Symbol %s cannot be parsed!", symbol)); - } - } - - /** - * Get variable - */ - @Nullable - protected Variable getVariable(String name) { - return this.variables.get(name); - } - - /** - * Get operation for given operator strings - */ - protected Operation operationForOperator(String op) throws ParseException { - for (Operation operation : Operation.values()) { - if (operation.sign.equals(op)) { - return operation; - } - } - - throw new ParseException("There is no such operator '" + op + "'!"); - } - - /** - * Whether given object is a variable - */ - protected boolean isVariable(Object o) { - return o instanceof String string && !this.isDecimal(string) && !this.isOperator(string); - } - - protected boolean isOperator(Object o) { - return o instanceof String string && this.isOperator(string); - } - - /** - * Whether string is an operator - */ - protected boolean isOperator(String s) { - return Operation.OPERATORS.contains(s) || s.equals("?") || s.equals(":"); - } - - /** - * Whether string is numeric (including whether it's a floating - * number) - */ - protected boolean isDecimal(String s) { - return DECIMAL_PATTERN.matcher(s).matches(); - } - - public static class ParseException extends Exception { - - public ParseException(String message) { - super(message); - } - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/Negate.java b/src/main/java/com/cleanroommc/modularui/utils/math/Negate.java deleted file mode 100644 index 55df3ff42..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/Negate.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.cleanroommc.modularui.utils.math; - -import com.cleanroommc.modularui.api.IMathValue; - -/** - * Negate operator class - *

- * This class is responsible for negating given value - */ -public class Negate extends Wrapper { - - public Negate(IMathValue value) { - super(value); - } - - @Override - protected void process() { - this.result.set(this.doubleValue()); - } - - @Override - public double doubleValue() { - return this.booleanValue() ? 1 : 0; - } - - @Override - public boolean booleanValue() { - return !this.value.booleanValue(); - } - - @Override - public String toString() { - return "!" + this.value.toString(); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/Negative.java b/src/main/java/com/cleanroommc/modularui/utils/math/Negative.java deleted file mode 100644 index 5deed1d65..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/Negative.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.cleanroommc.modularui.utils.math; - -import com.cleanroommc.modularui.api.IMathValue; - -/** - * Negative operator class - *

- * This class is responsible for inverting given value - */ -public class Negative extends Wrapper { - - public Negative(IMathValue value) { - super(value); - } - - @Override - protected void process() { - this.result.set(this.doubleValue()); - } - - @Override - public double doubleValue() { - return -this.value.doubleValue(); - } - - @Override - public boolean booleanValue() { - return Operation.isTrue(this.doubleValue()); - } - - @Override - public String toString() { - return "-" + this.value.toString(); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/Operation.java b/src/main/java/com/cleanroommc/modularui/utils/math/Operation.java deleted file mode 100644 index bc2a8d52d..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/Operation.java +++ /dev/null @@ -1,186 +0,0 @@ -package com.cleanroommc.modularui.utils.math; - -import com.cleanroommc.modularui.api.IMathValue; - -import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; - -import java.util.Set; - -/** - * Operation enumeration - *

- * This enumeration provides different hardcoded enumerations of default - * math operators such addition, substraction, multiplication, division, - * modulo and power. - *

- * TODO: maybe convert to classes (for the sake of API)? - */ -public enum Operation { - - ADD("+", 1) { - @Override - public double calculate(double a, double b) { - return a + b; - } - }, - SUB("-", 1) { - @Override - public double calculate(double a, double b) { - return a - b; - } - }, - MUL("*", 2) { - @Override - public double calculate(double a, double b) { - return a * b; - } - }, - DIV("/", 2) { - @Override - public double calculate(double a, double b) { - if (b == 0) { - throw new IMathValue.EvaluateException(String.format("Division by zero: %s / %s", a, b)); - } - return a / b; - } - }, - MOD("%", 2) { - @Override - public double calculate(double a, double b) { - return a % b; - } - }, - POW("^", 3) { - @Override - public double calculate(double a, double b) { - return Math.pow(a, b); - } - }, - E_NOTATION_LOWERCASE("e", 4) { - @Override - public double calculate(double a, double b) { - return a * Math.pow(10, b); - } - }, - E_NOTATION_UPPERCASE("E", 4) { - @Override - public double calculate(double a, double b) { - return a * Math.pow(10, b); - } - }, - AND("&&", -3) { - @Override - public double calculate(double a, double b) { - return isTrue(a) && isTrue(b) ? 1 : 0; - } - }, - OR("||", -3) { - @Override - public double calculate(double a, double b) { - return isTrue(a) || isTrue(b) ? 1 : 0; - } - }, - SHIFT_LEFT("<<", 0) { - @Override - public double calculate(double a, double b) { - return ((int) a) << ((int) b); - } - }, - SHIFT_RIGHT(">>", 0) { - @Override - public double calculate(double a, double b) { - return ((int) a) >> ((int) b); - } - }, - BIT_AND("&", -1) { - @Override - public double calculate(double a, double b) { - return ((int) a) & ((int) b); - } - }, - BIT_OR("|", -1) { - @Override - public double calculate(double a, double b) { - return ((int) a) | ((int) b); - } - }, - BIT_XOR("^^", -1) { - @Override - public double calculate(double a, double b) { - return ((int) a) ^ ((int) b); - } - }, - LESS("<", -2) { - @Override - public double calculate(double a, double b) { - return a < b ? 1 : 0; - } - }, - LESS_THAN("<=", -2) { - @Override - public double calculate(double a, double b) { - return a < b || equals(a, b) ? 1 : 0; - } - }, - GREATER_THAN(">=", -2) { - @Override - public double calculate(double a, double b) { - return a > b || equals(a, b) ? 1 : 0; - } - }, - GREATER(">", -2) { - @Override - public double calculate(double a, double b) { - return a > b ? 1 : 0; - } - }, - EQUALS("==", -2) { - @Override - public double calculate(double a, double b) { - return equals(a, b) ? 1 : 0; - } - }, - NOT_EQUALS("!=", -2) { - @Override - public double calculate(double a, double b) { - return !equals(a, b) ? 1 : 0; - } - }; - - public final static Set OPERATORS = new ObjectOpenHashSet<>(); - - public static boolean equals(double a, double b) { - return Math.abs(a - b) < 0.00001; - } - - public static boolean isTrue(double value) { - return !equals(value, 0); - } - - static { - for (Operation op : values()) { - OPERATORS.add(op.sign); - } - } - - /** - * String-ified name of this operation - */ - public final String sign; - - /** - * Value of this operation in relation to other operations (i.e. - * precedence importance) - */ - public final int value; - - Operation(String sign, int value) { - this.sign = sign; - this.value = value; - } - - /** - * Calculate the value based on given two doubles - */ - public abstract double calculate(double a, double b); -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/Operator.java b/src/main/java/com/cleanroommc/modularui/utils/math/Operator.java deleted file mode 100644 index eacb3c5cb..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/Operator.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.cleanroommc.modularui.utils.math; - -import com.cleanroommc.modularui.api.IMathValue; - -/** - * Operator class - *

- * This class is responsible for performing a calculation of two values - * based on given operation. - */ -public class Operator implements IMathValue { - - public static boolean DEBUG = false; - - public Operation operation; - public IMathValue a; - public IMathValue b; - private final IMathValue result = new Constant(0); - - public Operator(Operation op, IMathValue a, IMathValue b) { - this.operation = op; - this.a = a; - this.b = b; - } - - @Override - public IMathValue get() { - if (!this.isNumber() && this.operation == Operation.ADD) { - this.result.set(this.stringValue()); - } else { - this.result.set(this.doubleValue()); - } - - return this.result; - } - - @Override - public boolean isNumber() { - return this.a.isNumber() || this.b.isNumber(); - } - - @Override - public void set(double value) { - } - - @Override - public void set(String value) { - } - - @Override - public double doubleValue() { - if (!this.isNumber() && this.operation == Operation.EQUALS) { - return this.a.stringValue().equals(this.b.stringValue()) ? 1 : 0; - } - - return this.operation.calculate(this.a.doubleValue(), this.b.doubleValue()); - } - - @Override - public boolean booleanValue() { - return Operation.isTrue(this.doubleValue()); - } - - @Override - public String stringValue() { - if (this.operation == Operation.ADD) { - return this.a.stringValue() + this.b.stringValue(); - } - - return this.a.stringValue(); - } - - @Override - public String toString() { - if (DEBUG) { - return "(" + this.a.toString() + " " + this.operation.sign + " " + this.b.toString() + ")"; - } - - return this.a.toString() + " " + this.operation.sign + " " + this.b.toString(); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/Ternary.java b/src/main/java/com/cleanroommc/modularui/utils/math/Ternary.java deleted file mode 100644 index d3d641cdc..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/Ternary.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.cleanroommc.modularui.utils.math; - -import com.cleanroommc.modularui.api.IMathValue; - -/** - * Ternary operator class - *

- * This value implementation allows to return different values depending on - * given condition value - */ -public class Ternary implements IMathValue { - - public IMathValue condition; - public IMathValue ifTrue; - public IMathValue ifFalse; - - private final IMathValue result = new Constant(0); - - public Ternary(IMathValue condition, IMathValue ifTrue, IMathValue ifFalse) { - this.condition = condition; - this.ifTrue = ifTrue; - this.ifFalse = ifFalse; - } - - @Override - public IMathValue get() { - if (this.isNumber()) { - this.result.set(this.doubleValue()); - } else { - this.result.set(this.stringValue()); - } - - return this.result; - } - - @Override - public boolean isNumber() { - return this.ifFalse.isNumber() || this.ifTrue.isNumber(); - } - - @Override - public void set(double value) { - } - - @Override - public void set(String value) { - } - - @Override - public double doubleValue() { - return Operation.isTrue(this.condition.doubleValue()) ? this.ifTrue.doubleValue() : this.ifFalse.doubleValue(); - } - - @Override - public boolean booleanValue() { - return Operation.isTrue(this.doubleValue()); - } - - @Override - public String stringValue() { - return Operation.isTrue(this.condition.doubleValue()) ? this.ifTrue.stringValue() : this.ifFalse.stringValue(); - } - - @Override - public String toString() { - return this.condition.toString() + " ? " + this.ifTrue.toString() + " : " + this.ifFalse.toString(); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/Variable.java b/src/main/java/com/cleanroommc/modularui/utils/math/Variable.java deleted file mode 100644 index 8a3934c8e..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/Variable.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.cleanroommc.modularui.utils.math; - -import com.cleanroommc.modularui.api.IMathValue; - -/** - * Variable class - *

- * This class is responsible for providing a mutable {@link IMathValue} - * which can be modifier during runtime and still getting referenced in - * the expressions parsed by {@link MathBuilder}. - *

- * But in practice, it's simply returns stored value and provides a - * method to modify it. - */ -public class Variable extends Constant { - - private final String name; - - public Variable(String name, double value) { - super(value); - - this.name = name; - } - - public Variable(String name, String value) { - super(value); - - this.name = name; - } - - public String getName() { - return this.name; - } - - @Override - public String toString() { - return this.name; - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/Wrapper.java b/src/main/java/com/cleanroommc/modularui/utils/math/Wrapper.java deleted file mode 100644 index 2d50193a8..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/Wrapper.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.cleanroommc.modularui.utils.math; - -import com.cleanroommc.modularui.api.IMathValue; - -public abstract class Wrapper implements IMathValue { - - public IMathValue value; - - protected IMathValue result = new Constant(0); - - public Wrapper(IMathValue value) { - this.value = value; - } - - @Override - public IMathValue get() { - this.process(); - - return this.result; - } - - protected abstract void process(); - - @Override - public boolean isNumber() { - return this.value.isNumber(); - } - - @Override - public void set(double value) { - this.value.set(value); - } - - @Override - public void set(String value) { - this.value.set(value); - } - - @Override - public double doubleValue() { - return this.value.doubleValue(); - } - - @Override - public boolean booleanValue() { - return this.value.booleanValue(); - } - - @Override - public String stringValue() { - return this.value.stringValue(); - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/Function.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/Function.java deleted file mode 100644 index 7cbc8c5d2..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/Function.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.Constant; - -/** - * Abstract function class - *

- * This class provides function capability (i.e. giving it arguments and - * upon {@link #get()} method you receive output). - */ -public abstract class Function implements IMathValue { - - protected IMathValue[] args; - protected String name; - - protected IMathValue result = new Constant(0); - - public Function(IMathValue[] values, String name) throws Exception { - if (values.length < this.getRequiredArguments()) { - String message = String.format("Function '%s' requires at least %s arguments. %s are given!", this.getName(), this.getRequiredArguments(), values.length); - - throw new Exception(message); - } - - for (int i = 0; i < values.length; i++) { - this.verifyArgument(i, values[i]); - } - - this.args = values; - this.name = name; - } - - protected void verifyArgument(int index, IMathValue value) { - } - - @Override - public void set(double value) { - } - - @Override - public void set(String value) { - } - - /** - * Get the value of nth argument - */ - public IMathValue getArg(int index) { - if (index < 0 || index >= this.args.length) { - throw new IllegalStateException("Index should be within the argument's length range! Given " + index + ", arguments length: " + this.args.length); - } - - return this.args[index].get(); - } - - @Override - public String toString() { - StringBuilder args = new StringBuilder(); - for (int i = 0; i < this.args.length; i++) { - args.append(this.args[i].toString()); - - if (i < this.args.length - 1) { - args.append(", "); - } - } - return this.getName() + "(" + args + ")"; - } - - /** - * Get name of this function - */ - public String getName() { - return this.name; - } - - /** - * Get minimum count of arguments this function needs - */ - public int getRequiredArguments() { - return 0; - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/NNFunction.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/NNFunction.java deleted file mode 100644 index aec80a3b3..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/NNFunction.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.Operation; - -/** - * Function that expects number input arguments and outputs a number - */ -public abstract class NNFunction extends Function { - - public NNFunction(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - protected void verifyArgument(int index, IMathValue value) { - if (!value.isNumber()) { - throw new IllegalStateException("Function " + this.name + " cannot receive string arguments!"); - } - } - - @Override - public IMathValue get() { - this.result.set(this.doubleValue()); - - return this.result; - } - - @Override - public boolean isNumber() { - return true; - } - - @Override - public boolean booleanValue() { - return Operation.isTrue(this.doubleValue()); - } - - @Override - public String stringValue() { - return ""; - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/NSFunction.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/NSFunction.java deleted file mode 100644 index a37aef0a2..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/NSFunction.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions; - -import com.cleanroommc.modularui.api.IMathValue; - -/** - * Function that expects number input arguments and outputs a string - */ -public abstract class NSFunction extends Function { - - public NSFunction(IMathValue[] values, String name) throws Exception { - super(values, name); - - for (IMathValue value : values) { - if (!value.isNumber()) { - throw new IllegalStateException("Function " + name + " cannot receive string arguments!"); - } - } - } - - @Override - protected void verifyArgument(int index, IMathValue value) { - if (!value.isNumber()) { - throw new IllegalStateException("Function " + this.name + " cannot receive string arguments!"); - } - } - - @Override - public IMathValue get() { - this.result.set(this.stringValue()); - - return this.result; - } - - @Override - public boolean isNumber() { - return false; - } - - @Override - public double doubleValue() { - return 0; - } - - @Override - public boolean booleanValue() { - return this.stringValue().equalsIgnoreCase("true"); - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/SNFunction.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/SNFunction.java deleted file mode 100644 index 22276fde9..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/SNFunction.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.Operation; - -/** - * Function that expects string input arguments and outputs a number - */ -public abstract class SNFunction extends Function { - - public SNFunction(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - protected void verifyArgument(int index, IMathValue value) { - if (value.isNumber()) { - throw new IllegalStateException("Function " + this.name + " cannot receive number arguments!"); - } - } - - @Override - public IMathValue get() { - this.result.set(this.doubleValue()); - - return this.result; - } - - @Override - public boolean isNumber() { - return true; - } - - @Override - public boolean booleanValue() { - return Operation.isTrue(this.doubleValue()); - } - - @Override - public String stringValue() { - return ""; - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/SSFunction.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/SSFunction.java deleted file mode 100644 index a86e436f9..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/SSFunction.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions; - -import com.cleanroommc.modularui.api.IMathValue; - -/** - * Function that expects string input arguments and outputs a string - */ -public abstract class SSFunction extends Function { - - public SSFunction(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - protected void verifyArgument(int index, IMathValue value) { - if (value.isNumber()) { - throw new IllegalStateException("Function " + this.name + " cannot receive number arguments!"); - } - } - - @Override - public IMathValue get() { - this.result.set(this.stringValue()); - - return this.result; - } - - @Override - public boolean isNumber() { - return false; - } - - @Override - public double doubleValue() { - return 0; - } - - @Override - public boolean booleanValue() { - return this.stringValue().equalsIgnoreCase("true"); - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Abs.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Abs.java deleted file mode 100644 index 15f70fcd8..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Abs.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.classic; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -/** - * Absolute value function - */ -public class Abs extends NNFunction { - - public Abs(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - return Math.abs(this.getArg(0).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Exp.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Exp.java deleted file mode 100644 index 5ae78054b..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Exp.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.classic; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Exp extends NNFunction { - - public Exp(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - return Math.exp(this.getArg(0).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Ln.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Ln.java deleted file mode 100644 index abd2b4fc9..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Ln.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.classic; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Ln extends NNFunction { - - public Ln(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - return Math.log(this.getArg(0).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Mod.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Mod.java deleted file mode 100644 index 6dcf6d06a..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Mod.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.classic; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Mod extends NNFunction { - - public Mod(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 2; - } - - @Override - public double doubleValue() { - return this.getArg(0).doubleValue() % this.getArg(1).doubleValue(); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Pow.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Pow.java deleted file mode 100644 index b97d602d9..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Pow.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.classic; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Pow extends NNFunction { - - public Pow(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 2; - } - - @Override - public double doubleValue() { - return Math.pow(this.getArg(0).doubleValue(), this.getArg(1).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Sqrt.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Sqrt.java deleted file mode 100644 index 14df8add9..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/classic/Sqrt.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.classic; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Sqrt extends NNFunction { - - public Sqrt(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - return Math.sqrt(this.getArg(0).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/limit/Clamp.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/limit/Clamp.java deleted file mode 100644 index 7f7aa154e..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/limit/Clamp.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.limit; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -import net.minecraft.util.math.MathHelper; - -public class Clamp extends NNFunction { - - public Clamp(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 3; - } - - @Override - public double doubleValue() { - return MathHelper.clamp(this.getArg(0).doubleValue(), this.getArg(1).doubleValue(), this.getArg(2).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/limit/Max.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/limit/Max.java deleted file mode 100644 index 6ef8c44b7..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/limit/Max.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.limit; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Max extends NNFunction { - - public Max(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 2; - } - - @Override - public double doubleValue() { - return Math.max(this.getArg(0).doubleValue(), this.getArg(1).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/limit/Min.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/limit/Min.java deleted file mode 100644 index 2e42da615..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/limit/Min.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.limit; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Min extends NNFunction { - - public Min(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 2; - } - - @Override - public double doubleValue() { - return Math.min(this.getArg(0).doubleValue(), this.getArg(1).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/rounding/Ceil.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/rounding/Ceil.java deleted file mode 100644 index eb4129505..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/rounding/Ceil.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.rounding; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Ceil extends NNFunction { - - public Ceil(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - return Math.ceil(this.getArg(0).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/rounding/Floor.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/rounding/Floor.java deleted file mode 100644 index 48f454f8b..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/rounding/Floor.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.rounding; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Floor extends NNFunction { - - public Floor(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - return Math.floor(this.getArg(0).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/rounding/Round.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/rounding/Round.java deleted file mode 100644 index f6c604f02..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/rounding/Round.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.rounding; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Round extends NNFunction { - - public Round(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - return Math.round(this.getArg(0).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/rounding/Trunc.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/rounding/Trunc.java deleted file mode 100644 index 53f131537..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/rounding/Trunc.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.rounding; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Trunc extends NNFunction { - - public Trunc(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - double value = this.getArg(0).doubleValue(); - - return value < 0 ? Math.ceil(value) : Math.floor(value); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/string/StringContains.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/string/StringContains.java deleted file mode 100644 index 88178a2c7..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/string/StringContains.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.string; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.SNFunction; - -public class StringContains extends SNFunction { - - public StringContains(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 2; - } - - @Override - public double doubleValue() { - return this.getArg(0).stringValue().contains(this.getArg(1).stringValue()) ? 1 : 0; - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/string/StringEndsWith.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/string/StringEndsWith.java deleted file mode 100644 index ee6ef7412..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/string/StringEndsWith.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.string; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.SNFunction; - -public class StringEndsWith extends SNFunction { - - public StringEndsWith(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 2; - } - - @Override - public double doubleValue() { - return this.getArg(0).stringValue().endsWith(this.getArg(1).stringValue()) ? 1 : 0; - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/string/StringStartsWith.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/string/StringStartsWith.java deleted file mode 100644 index 2507c2490..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/string/StringStartsWith.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.string; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.SNFunction; - -public class StringStartsWith extends SNFunction { - - public StringStartsWith(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 2; - } - - @Override - public double doubleValue() { - return this.getArg(0).stringValue().startsWith(this.getArg(1).stringValue()) ? 1 : 0; - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Acos.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Acos.java deleted file mode 100644 index 34c6f4913..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Acos.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.trig; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Acos extends NNFunction { - - public Acos(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - return Math.acos(this.getArg(0).doubleValue()); - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Asin.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Asin.java deleted file mode 100644 index 60f514a1b..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Asin.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.trig; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Asin extends NNFunction { - - public Asin(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - return Math.asin(this.getArg(0).doubleValue()); - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Atan.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Atan.java deleted file mode 100644 index 3390b0280..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Atan.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.trig; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Atan extends NNFunction { - - public Atan(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - return Math.atan(this.getArg(0).doubleValue()); - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Atan2.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Atan2.java deleted file mode 100644 index a1a7b8167..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Atan2.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.trig; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Atan2 extends NNFunction { - - public Atan2(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 2; - } - - @Override - public double doubleValue() { - return Math.atan2(this.getArg(0).doubleValue(), this.getArg(1).doubleValue()); - } -} diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Cos.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Cos.java deleted file mode 100644 index e045d285c..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Cos.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.trig; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Cos extends NNFunction { - - public Cos(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - return Math.cos(this.getArg(0).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Sin.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Sin.java deleted file mode 100644 index dd4a6d3e0..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/trig/Sin.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.trig; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Sin extends NNFunction { - - public Sin(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - return Math.sin(this.getArg(0).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/DieRoll.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/DieRoll.java deleted file mode 100644 index d31a71871..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/DieRoll.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.utility; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class DieRoll extends NNFunction { - - public static double rollDie(int num, double min, double max) { - double m = Math.max(max, min); - double n = Math.min(max, min); - - double sum = 0; - - for (int i = 0; i < num; i++) { - sum += Math.random() * (m - n) + n; - } - - return sum; - } - - public DieRoll(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 3; - } - - @Override - public double doubleValue() { - return rollDie((int) this.getArg(0).doubleValue(), this.getArg(1).doubleValue(), this.getArg(2).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/DieRollInteger.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/DieRollInteger.java deleted file mode 100644 index 82428d92b..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/DieRollInteger.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.utility; - -import com.cleanroommc.modularui.api.IMathValue; - -public class DieRollInteger extends DieRoll { - - public DieRollInteger(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public double doubleValue() { - return (int) super.doubleValue(); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/HermiteBlend.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/HermiteBlend.java deleted file mode 100644 index 0f62ce5bd..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/HermiteBlend.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.utility; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class HermiteBlend extends NNFunction { - - public HermiteBlend(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 1; - } - - @Override - public double doubleValue() { - double x = this.getArg(0).doubleValue(); - - return 3 * x * x - 2 * x * x * x; - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/Lerp.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/Lerp.java deleted file mode 100644 index 9d83cc11a..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/Lerp.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.utility; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.Interpolations; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Lerp extends NNFunction { - - public Lerp(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 3; - } - - @Override - public double doubleValue() { - return Interpolations.lerp(this.getArg(0).doubleValue(), this.getArg(1).doubleValue(), this.getArg(2).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/LerpRotate.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/LerpRotate.java deleted file mode 100644 index 64dd4ff88..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/LerpRotate.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.utility; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.Interpolations; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class LerpRotate extends NNFunction { - - public LerpRotate(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public int getRequiredArguments() { - return 3; - } - - @Override - public double doubleValue() { - return Interpolations.lerpYaw(this.getArg(0).doubleValue(), this.getArg(1).doubleValue(), this.getArg(2).doubleValue()); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/Random.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/Random.java deleted file mode 100644 index de4a77900..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/Random.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.utility; - -import com.cleanroommc.modularui.api.IMathValue; -import com.cleanroommc.modularui.utils.math.functions.NNFunction; - -public class Random extends NNFunction { - - public java.util.Random random; - - public Random(IMathValue[] values, String name) throws Exception { - super(values, name); - - this.random = new java.util.Random(); - } - - @Override - public double doubleValue() { - double random; - - if (this.args.length >= 3) { - this.random.setSeed((long) this.getArg(2).doubleValue()); - random = this.random.nextDouble(); - } else { - random = Math.random(); - } - - if (this.args.length >= 2) { - double a = this.getArg(0).doubleValue(); - double b = this.getArg(1).doubleValue(); - - double min = Math.min(a, b); - double max = Math.max(a, b); - - random = random * (max - min) + min; - } else if (this.args.length >= 1) { - random = random * this.getArg(0).doubleValue(); - } - - return random; - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/RandomInteger.java b/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/RandomInteger.java deleted file mode 100644 index 69938795d..000000000 --- a/src/main/java/com/cleanroommc/modularui/utils/math/functions/utility/RandomInteger.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.cleanroommc.modularui.utils.math.functions.utility; - -import com.cleanroommc.modularui.api.IMathValue; - -public class RandomInteger extends Random { - - public RandomInteger(IMathValue[] values, String name) throws Exception { - super(values, name); - } - - @Override - public double doubleValue() { - return (int) super.doubleValue(); - } -} \ No newline at end of file diff --git a/src/main/java/com/cleanroommc/modularui/widgets/textfield/TextFieldWidget.java b/src/main/java/com/cleanroommc/modularui/widgets/textfield/TextFieldWidget.java index 2ec56a903..b2ec72c00 100644 --- a/src/main/java/com/cleanroommc/modularui/widgets/textfield/TextFieldWidget.java +++ b/src/main/java/com/cleanroommc/modularui/widgets/textfield/TextFieldWidget.java @@ -1,15 +1,14 @@ package com.cleanroommc.modularui.widgets.textfield; import com.cleanroommc.modularui.ModularUI; -import com.cleanroommc.modularui.api.IMathValue; import com.cleanroommc.modularui.api.ITheme; import com.cleanroommc.modularui.api.drawable.IKey; import com.cleanroommc.modularui.api.value.IStringValue; import com.cleanroommc.modularui.screen.viewport.ModularGuiContext; import com.cleanroommc.modularui.theme.WidgetTextFieldTheme; import com.cleanroommc.modularui.theme.WidgetTheme; -import com.cleanroommc.modularui.utils.math.Constant; -import com.cleanroommc.modularui.utils.math.MathBuilder; +import com.cleanroommc.modularui.utils.MathUtils; +import com.cleanroommc.modularui.utils.ParseResult; import com.cleanroommc.modularui.value.StringValue; import com.cleanroommc.modularui.value.sync.SyncHandler; import com.cleanroommc.modularui.value.sync.ValueSyncHandler; @@ -35,17 +34,14 @@ public class TextFieldWidget extends BaseTextFieldWidget { protected boolean changedMarkedColor = false; - public IMathValue parse(String num) { - try { - IMathValue ret = MathBuilder.INSTANCE.parse(num); - this.mathFailMessage = null; - return ret; - } catch (MathBuilder.ParseException e) { - this.mathFailMessage = e.getMessage(); - } catch (Exception e) { - ModularUI.LOGGER.catching(e); + public double parse(String num) { + ParseResult result = MathUtils.parseExpression(num, this.defaultNumber, true); + double value = result.getResult(); + if (result.isFailure()) { + this.mathFailMessage = result.getError(); + ModularUI.LOGGER.error("Math expression error in {}: {}", this, this.mathFailMessage); } - return new Constant(this.defaultNumber); + return value; } public IStringValue createMathFailMessageValue() { @@ -163,6 +159,10 @@ public boolean canHover() { return true; } + public String getMathFailMessage() { + return mathFailMessage; + } + public TextFieldWidget setMaxLength(int maxLength) { this.handler.setMaxCharacters(maxLength); return this; @@ -197,12 +197,7 @@ public TextFieldWidget setNumbersLong(Function validator) { if (val.isEmpty()) { num = (long) this.defaultNumber; } else { - try { - num = (long) parse(val).doubleValue(); - } catch (IMathValue.EvaluateException e) { - this.mathFailMessage = e.getMessage(); - num = (long) this.defaultNumber; - } + num = (long) parse(val); } return format.format(validator.apply(num)); }); @@ -216,12 +211,7 @@ public TextFieldWidget setNumbers(Function validator) { if (val.isEmpty()) { num = (int) this.defaultNumber; } else { - try { - num = (int) parse(val).doubleValue(); - } catch (IMathValue.EvaluateException e) { - this.mathFailMessage = e.getMessage(); - num = (int) this.defaultNumber; - } + num = (int) parse(val); } return format.format(validator.apply(num)); }); @@ -234,12 +224,7 @@ public TextFieldWidget setNumbersDouble(Function validator) { if (val.isEmpty()) { num = this.defaultNumber; } else { - try { - num = parse(val).doubleValue(); - } catch (IMathValue.EvaluateException e) { - this.mathFailMessage = e.getMessage(); - num = this.defaultNumber; - } + num = parse(val); } return format.format(validator.apply(num)); });