11package org .mcphackers .mcp .tools .constants ;
22
3+ import java .util .regex .Pattern ;
4+
35public class MathConstants extends Constants {
46
7+ // Used to prevent strings from being captured, such as "2.0D"
8+ private static final Pattern _CONSTANT_REGEX = Pattern .compile ("(?![\\ \" \\ '][.\\ w\\ s]*)-*\\ d+\\ .*\\ w*(?![.\\ w\\ s]*[\\ \" \\ '])" );
9+
510 protected String replace_constants (String code ) {
11+ return replaceTextOfMatchGroup (code , _CONSTANT_REGEX , match1 -> {
12+ String constant = match1 .group (0 );
613
7- code = replaceValue (code , Math .PI , "Math.PI" );
8- code = replaceValue (code , (float )Math .PI , "(float)Math.PI" );
9- code = replaceValue (code , (float )Math .PI / 2F , "(float)Math.PI / 2F" );
10- code = replaceValue (code , (float )Math .PI / 4.5F , "(float)Math.PI / 4.5F" );
11- code = replaceValue (code , (double )(float )Math .PI , "(double)(float)Math.PI" );
12- code = replaceValue (code , Math .PI * 2D , "Math.PI * 2D" );
13- code = replaceValue (code , Math .PI / 2D , "Math.PI / 2D" );
14- for (int i = 1 ; i <= 100 ; i ++) {
15- double d = i * 0.01D ;
16- if (d != (double )(float )d ) { // if imprecise
17- code = floatCastedToDouble (code , d );
14+ constant = replaceValue (constant , Math .PI , "Math.PI" );
15+ constant = replaceValue (constant , (float )Math .PI , "(float)Math.PI" );
16+ constant = replaceValue (constant , (float )Math .PI / 2F , "(float)Math.PI / 2F" );
17+ constant = replaceValue (constant , (float )Math .PI / 4.5F , "(float)Math.PI / 4.5F" );
18+ constant = replaceValue (constant , (double )(float )Math .PI , "(double)(float)Math.PI" );
19+ constant = replaceValue (constant , Math .PI * 2D , "Math.PI * 2D" );
20+ constant = replaceValue (constant , Math .PI / 2D , "Math.PI / 2D" );
21+ for (int i = 1 ; i <= 100 ; i ++) {
22+ double d = i * 0.01D ;
23+ if (d != (double )(float )d ) { // if imprecise
24+ constant = floatCastedToDouble (constant , (float )d );
25+ }
1826 }
19- }
20- code = floatCastedToDouble (code , 0.0075D );
21- code = replaceValue (code , (double )0.999F , "(double)0.999F" );
22- code = replaceValue (code , 1.0D / 128 , "1.0D / 128" );
23- code = replaceValue (code , (double )0.997F , "(double)0.997F" );
24- code = replaceValue (code , (double )1.62F , "(double)1.62F" );
25- return code ;
27+ constant = floatCastedToDouble (constant , 0.0075F );
28+ constant = floatCastedToDouble (constant , 0.999F );
29+ constant = floatCastedToDouble (constant , 0.997F );
30+ constant = floatCastedToDouble (constant , 1.62F );
31+ constant = replaceValue (constant , 0xFFFFFF , "0xFFFFFF" ); // TODO Might do this in fernflower at some point
32+ constant = replaceValue (constant , 0x20200000 , "0x20200000" );
33+ constant = replaceValue (constant , 0x20400000 , "0x20400000" );
34+ constant = replaceValue (constant , 0xFF000000 , "0xFF000000" );
35+ //brugh
36+ constant = replaceValue (constant , 2.0D / 256D , "2.0D / 256D" );
37+ constant = replaceValue (constant , 6.0D / 256D , "6.0D / 256D" );
38+ constant = replaceValue (constant , 7.0D / 256D , "7.0D / 256D" );
39+ constant = replaceValue (constant , 8.0D / 256D , "8.0D / 256D" );
40+ constant = replaceValue (constant , 9.0D / 256D , "9.0D / 256D" );
41+ return constant ;
42+ });
2643 }
2744
28- private String floatCastedToDouble (String code , double value ) {
29- return code .replace ((double )( float ) value + "D" , "(double)" + ( float ) value + "F" );
45+ private String floatCastedToDouble (String code , float value ) {
46+ return code .replace ((double )value + "D" , "(double)" + value + "F" );
3047 }
3148
3249 private String replaceValue (String code , double value , String replace ) {
@@ -36,4 +53,12 @@ private String replaceValue(String code, double value, String replace) {
3653 private String replaceValue (String code , float value , String replace ) {
3754 return code .replace (value + "F" , replace );
3855 }
56+
57+ private String replaceValue (String code , int value , String replace ) {
58+ return code .replace (value + "" , replace );
59+ }
60+
61+ private String replaceValue (String code , long value , String replace ) {
62+ return code .replace (value + "L" , replace );
63+ }
3964}
0 commit comments