11package com .cleanroommc .modularui .utils ;
22
3+ import com .cleanroommc .modularui .utils .math .CustomDataAccessor ;
34import com .cleanroommc .modularui .utils .math .PostfixPercentOperator ;
45
6+ import com .cleanroommc .modularui .widgets .textfield .INumberParser ;
7+
8+ import com .google .common .math .LongMath ;
9+
510import net .minecraft .util .MathHelper ;
611
712import com .ezylang .evalex .BaseException ;
1116import org .apache .commons .lang3 .tuple .Pair ;
1217
1318import java .math .BigDecimal ;
19+ import java .util .function .LongSupplier ;
1420
1521public class MathUtils {
1622
@@ -23,30 +29,50 @@ public class MathUtils {
2329 .arraysAllowed (false )
2430 .structuresAllowed (false )
2531 .stripTrailingZeros (true )
32+ .allowOverwriteConstants (true )
33+ .dataAccessorSupplier (() -> new CustomDataAccessor (false ))
2634 .build ()
2735 .withAdditionalOperators (Pair .of ("%" , new PostfixPercentOperator ()));
2836
29- public static ParseResult parseExpression ( String expression ) {
30- return parseExpression ( expression , Double . NaN , false );
31- }
37+ public static final ExpressionConfiguration MATH_CFG_CASE_SENSITIVE = MATH_CFG . toBuilder ()
38+ . dataAccessorSupplier (() -> new CustomDataAccessor ( true ))
39+ . build ();
3240
33- public static ParseResult parseExpression (String expression , boolean useSiPrefixes ) {
34- return parseExpression (expression , Double .NaN , useSiPrefixes );
35- }
41+ public static final INumberParser PARSER_WITH_SI = MathUtils ::parseExpression ;
42+ public static final INumberParser PARSER_WHOLE_NUMBER = MathUtils ::parseExpressionWholeNumber ;
3643
3744 public static ParseResult parseExpression (String expression , double defaultValue ) {
38- return parseExpression (expression , defaultValue , true );
45+ return parseExpression (expression , defaultValue , true , false );
3946 }
4047
41- public static ParseResult parseExpression (String expression , double defaultValue , boolean useSiPrefixes ) {
48+ public static ParseResult parseExpression (String expression , double defaultValue , boolean useSiPrefixes , boolean biggerThanOne ) {
4249 if (expression == null || expression .isEmpty ()) {
4350 return ParseResult .success (EvaluationValue .numberValue (new BigDecimal (defaultValue )));
4451 }
4552
46- Expression e = new Expression (expression , MATH_CFG );
53+ Expression e = new Expression (expression , MATH_CFG_CASE_SENSITIVE );
4754 if (useSiPrefixes ) {
48- SIPrefix .addAllToExpression (e );
55+ SIPrefix .addAllToExpression (e , biggerThanOne );
56+ }
57+ try {
58+ return ParseResult .success (e .evaluate ());
59+ } catch (BaseException exception ) {
60+ return ParseResult .failure (exception );
4961 }
62+ }
63+
64+ public static ParseResult parseExpressionWholeNumber (String expression , double defaultValue ) {
65+ if (expression == null || expression .isEmpty ()) {
66+ return ParseResult .success (EvaluationValue .numberValue (new BigDecimal (defaultValue )));
67+ }
68+
69+ Expression e = new Expression (expression , MATH_CFG );
70+ SIPrefix .Kilo .addToExpression (e );
71+ SIPrefix .Mega .addToExpression (e );
72+ SIPrefix .Giga .addToExpression (e , "b" );
73+ SIPrefix .Tera .addToExpression (e );
74+ e .with ("i" , 144 ); // ingot
75+ e .with ("s" , 64 ); // stack
5076 try {
5177 return ParseResult .success (e .evaluate ());
5278 } catch (BaseException exception ) {
@@ -220,4 +246,46 @@ public static int intPlaces(double x) {
220246 if (Math .pow (10 , d - 1 ) > x ) d --;
221247 return Math .max (d , 1 );
222248 }
249+
250+ public static boolean areBothSmallerOrBiggerThanOne (double a , double b ) {
251+ return a > 1 ? b > 1 : b <= 1 ;
252+ }
253+
254+ public static long percentOrSelf (double value , long maxValue ) {
255+ long rounded = Math .round (value );
256+ if (value > 1 || Math .abs (value - rounded ) < 0.0000001 ) return rounded ;
257+ return Math .round (value * maxValue );
258+ }
259+
260+ public static int castToIntSaturated (long l ) {
261+ if (l >= Integer .MAX_VALUE ) return Integer .MAX_VALUE ;
262+ if (l <= Integer .MIN_VALUE ) return Integer .MIN_VALUE ;
263+ return (int ) l ;
264+ }
265+
266+ public static short castToShortSaturated (long l ) {
267+ if (l >= Short .MAX_VALUE ) return Short .MAX_VALUE ;
268+ if (l <= Short .MIN_VALUE ) return Short .MIN_VALUE ;
269+ return (short ) l ;
270+ }
271+
272+ public static byte castToByteSaturated (long l ) {
273+ if (l >= Byte .MAX_VALUE ) return Byte .MAX_VALUE ;
274+ if (l <= Byte .MIN_VALUE ) return Byte .MIN_VALUE ;
275+ return (byte ) l ;
276+ }
277+
278+ public interface UnaryLongOperator {
279+
280+ UnaryLongOperator IDENTITY = v -> v ;
281+
282+ long apply (long l );
283+ }
284+
285+ public interface UnaryIntOperator {
286+
287+ UnaryIntOperator IDENTITY = v -> v ;
288+
289+ int apply (int l );
290+ }
223291}
0 commit comments