11package com .cleanroommc .modularui .utils ;
22
3+ import com .cleanroommc .modularui .utils .math .PostfixPercentOperator ;
4+
35import net .minecraft .util .math .MathHelper ;
46
5- import org .mariuszgromada .math .mxparser .Constant ;
6- import org .mariuszgromada .math .mxparser .Expression ;
7+ import com .ezylang .evalex .BaseException ;
8+ import com .ezylang .evalex .Expression ;
9+ import com .ezylang .evalex .config .ExpressionConfiguration ;
10+ import com .ezylang .evalex .data .EvaluationValue ;
11+ import org .apache .commons .lang3 .tuple .Pair ;
12+
13+ import java .math .BigDecimal ;
714
815public class MathUtils {
916
@@ -12,23 +19,12 @@ public class MathUtils {
1219 public static final float PI_HALF = PI / 2f ;
1320 public static final float PI_QUART = PI / 4f ;
1421
15- // SI prefixes
16- public static final Constant k = new Constant ("k" , 1e3 );
17- public static final Constant M = new Constant ("M" , 1e6 );
18- public static final Constant G = new Constant ("G" , 1e9 );
19- public static final Constant T = new Constant ("T" , 1e12 );
20- public static final Constant P = new Constant ("P" , 1e15 );
21- public static final Constant E = new Constant ("E" , 1e18 );
22- public static final Constant Z = new Constant ("Z" , 1e21 );
23- public static final Constant Y = new Constant ("Y" , 1e24 );
24- public static final Constant m = new Constant ("m" , 1e-3 );
25- public static final Constant u = new Constant ("u" , 1e-6 );
26- public static final Constant n = new Constant ("n" , 1e-9 );
27- public static final Constant p = new Constant ("p" , 1e-12 );
28- public static final Constant f = new Constant ("f" , 1e-15 );
29- public static final Constant a = new Constant ("a" , 1e-18 );
30- public static final Constant z = new Constant ("z" , 1e-21 );
31- public static final Constant y = new Constant ("y" , 1e-24 );
22+ public static final ExpressionConfiguration MATH_CFG = ExpressionConfiguration .builder ()
23+ .arraysAllowed (false )
24+ .structuresAllowed (false )
25+ .stripTrailingZeros (true )
26+ .build ()
27+ .withAdditionalOperators (Pair .of ("%" , new PostfixPercentOperator ()));
3228
3329 public static ParseResult parseExpression (String expression ) {
3430 return parseExpression (expression , Double .NaN , false );
@@ -43,16 +39,19 @@ public static ParseResult parseExpression(String expression, double defaultValue
4339 }
4440
4541 public static ParseResult parseExpression (String expression , double defaultValue , boolean useSiPrefixes ) {
46- if (expression == null || expression .isEmpty ()) return ParseResult .success (defaultValue );
47- Expression e = new Expression (expression );
42+ if (expression == null || expression .isEmpty ()) {
43+ return ParseResult .success (EvaluationValue .numberValue (new BigDecimal (defaultValue )));
44+ }
45+
46+ Expression e = new Expression (expression , MATH_CFG );
4847 if (useSiPrefixes ) {
49- e . addConstants ( k , M , G , T , P , E , Z , Y , m , u , n , p , f , a , z , y );
48+ SIPrefix . addAllToExpression ( e );
5049 }
51- double result = e .calculate ();
52- if (Double .isNaN (result )) {
53- return ParseResult .failure (defaultValue , e .getErrorMessage ());
50+ try {
51+ return ParseResult .success (e .evaluate ());
52+ } catch (BaseException exception ) {
53+ return ParseResult .failure (exception );
5454 }
55- return ParseResult .success (result );
5655 }
5756
5857 public static int clamp (int v , int min , int max ) {
0 commit comments