Skip to content

Commit cec6201

Browse files
committed
Move to 1.20.4
Refactored the calculator code for clarity. Added Jacoco line to prvent issues with the bigger Material class.
1 parent cfb3590 commit cec6201

File tree

3 files changed

+603
-545
lines changed

3 files changed

+603
-545
lines changed

pom.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<!-- Non-minecraft related dependencies -->
5959
<powermock.version>2.0.9</powermock.version>
6060
<!-- More visible way how to change dependency versions -->
61-
<spigot.version>1.19.4-R0.1-SNAPSHOT</spigot.version>
61+
<spigot.version>1.20.4-R0.1-SNAPSHOT</spigot.version>
6262
<bentobox.version>2.0.0-SNAPSHOT</bentobox.version>
6363
<!-- Warps addon version -->
6464
<warps.version>1.12.0</warps.version>
@@ -410,6 +410,8 @@
410410
<!-- This is required to prevent Jacoco from adding
411411
synthetic fields to a JavaBean class (causes errors in testing) -->
412412
<exclude>**/*Names*</exclude>
413+
<!-- Prevents the Material is too large to mock error -->
414+
<exclude>org/bukkit/Material*</exclude>
413415
</excludes>
414416
</configuration>
415417
<executions>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package world.bentobox.level.calculators;
2+
3+
import java.text.ParseException;
4+
5+
/**
6+
* @author tastybento
7+
*/
8+
public class EquationEvaluator {
9+
10+
private static class Parser {
11+
private final String input;
12+
private int pos = -1;
13+
private int currentChar;
14+
15+
public Parser(String input) {
16+
this.input = input;
17+
moveToNextChar();
18+
}
19+
20+
private void moveToNextChar() {
21+
currentChar = (++pos < input.length()) ? input.charAt(pos) : -1;
22+
}
23+
24+
private boolean tryToEat(int charToEat) {
25+
while (currentChar == ' ')
26+
moveToNextChar();
27+
if (currentChar == charToEat) {
28+
moveToNextChar();
29+
return true;
30+
}
31+
return false;
32+
}
33+
34+
public double evaluate() throws ParseException {
35+
double result = parseExpression();
36+
if (pos < input.length()) {
37+
throw new ParseException("Unexpected character: " + (char) currentChar, pos);
38+
}
39+
return result;
40+
}
41+
42+
private double parseExpression() throws ParseException {
43+
double result = parseTerm();
44+
while (true) {
45+
if (tryToEat('+'))
46+
result += parseTerm();
47+
else if (tryToEat('-'))
48+
result -= parseTerm();
49+
else
50+
return result;
51+
}
52+
}
53+
54+
private double parseFactor() throws ParseException {
55+
if (tryToEat('+'))
56+
return parseFactor(); // unary plus
57+
if (tryToEat('-'))
58+
return -parseFactor(); // unary minus
59+
60+
double x;
61+
int startPos = this.pos;
62+
if (tryToEat('(')) { // parentheses
63+
x = parseExpression();
64+
tryToEat(')');
65+
} else if ((currentChar >= '0' && currentChar <= '9') || currentChar == '.') { // numbers
66+
while ((currentChar >= '0' && currentChar <= '9') || currentChar == '.')
67+
moveToNextChar();
68+
x = Double.parseDouble(input.substring(startPos, this.pos));
69+
} else if (currentChar >= 'a' && currentChar <= 'z') { // functions
70+
while (currentChar >= 'a' && currentChar <= 'z')
71+
moveToNextChar();
72+
String func = input.substring(startPos, this.pos);
73+
x = parseFactor();
74+
switch (func) {
75+
case "sqrt":
76+
x = Math.sqrt(x);
77+
break;
78+
case "sin":
79+
x = Math.sin(Math.toRadians(x));
80+
break;
81+
case "cos":
82+
x = Math.cos(Math.toRadians(x));
83+
break;
84+
case "tan":
85+
x = Math.tan(Math.toRadians(x));
86+
break;
87+
case "log":
88+
x = Math.log(x);
89+
break;
90+
default:
91+
throw new ParseException("Unknown function: " + func, startPos);
92+
}
93+
} else {
94+
throw new ParseException("Unexpected: " + (char) currentChar, startPos);
95+
}
96+
97+
if (tryToEat('^'))
98+
x = Math.pow(x, parseFactor()); // exponentiation
99+
100+
return x;
101+
}
102+
103+
private double parseTerm() throws ParseException {
104+
double x = parseFactor();
105+
for (;;) {
106+
if (tryToEat('*'))
107+
x *= parseFactor(); // multiplication
108+
else if (tryToEat('/'))
109+
x /= parseFactor(); // division
110+
else
111+
return x;
112+
}
113+
}
114+
115+
}
116+
117+
public static double eval(final String equation) throws ParseException {
118+
return new Parser(equation).evaluate();
119+
}
120+
121+
}

0 commit comments

Comments
 (0)