Fixed Chat Calc
This commit is contained in:
@ -15,6 +15,7 @@ import org.spongepowered.asm.mixin.injection.ModifyArg;
|
|||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||||
import wtf.hak.survivalfabric.commands.SpectatorCommand;
|
import wtf.hak.survivalfabric.commands.SpectatorCommand;
|
||||||
import wtf.hak.survivalfabric.config.ConfigManager;
|
import wtf.hak.survivalfabric.config.ConfigManager;
|
||||||
|
import wtf.hak.survivalfabric.utils.MathUtils;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@ -56,10 +57,10 @@ public abstract class PlayerManagerMixin {
|
|||||||
|
|
||||||
String processedMessage = rawMessage;
|
String processedMessage = rawMessage;
|
||||||
|
|
||||||
if (isCalcEnabled) {
|
if (isCalcEnabled && MathUtils.hasSupportedOperator(rawMessage)) {
|
||||||
String expression = rawMessage.endsWith("=") ? rawMessage.substring(0, rawMessage.length() - 1).trim() : rawMessage;
|
String expression = rawMessage.endsWith("=") ? rawMessage.substring(0, rawMessage.length() - 1).trim() : rawMessage;
|
||||||
try {
|
try {
|
||||||
String result = String.valueOf(evaluateExpression(expression));
|
String result = String.valueOf(MathUtils.evaluateExpression(expression));
|
||||||
StringBuilder sb = new StringBuilder(rawMessage).append("§6");
|
StringBuilder sb = new StringBuilder(rawMessage).append("§6");
|
||||||
|
|
||||||
if (rawMessage.contains(" ")) sb.append(" ");
|
if (rawMessage.contains(" ")) sb.append(" ");
|
||||||
@ -83,47 +84,4 @@ public abstract class PlayerManagerMixin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private double evaluateExpression(String expression) {
|
|
||||||
return evaluate(expression.replaceAll("\\s", ""), new int[]{0});
|
|
||||||
}
|
|
||||||
|
|
||||||
private double evaluate(String expr, int[] index) {
|
|
||||||
double value = parseTerm(expr, index);
|
|
||||||
while (index[0] < expr.length()) {
|
|
||||||
char op = expr.charAt(index[0]);
|
|
||||||
if (op != '+' && op != '-') break;
|
|
||||||
index[0]++;
|
|
||||||
double nextTerm = parseTerm(expr, index);
|
|
||||||
value = (op == '+') ? value + nextTerm : value - nextTerm;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
private double parseTerm(String expr, int[] index) {
|
|
||||||
double value = parseFactor(expr, index);
|
|
||||||
while (index[0] < expr.length()) {
|
|
||||||
char op = expr.charAt(index[0]);
|
|
||||||
if (op != '*' && op != '/') break;
|
|
||||||
index[0]++;
|
|
||||||
double nextFactor = parseFactor(expr, index);
|
|
||||||
value = (op == '*') ? value * nextFactor : value / nextFactor;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
private double parseFactor(String expr, int[] index) {
|
|
||||||
if (expr.charAt(index[0]) == '(') {
|
|
||||||
index[0]++;
|
|
||||||
double value = evaluate(expr, index);
|
|
||||||
index[0]++; // Skip closing ')'
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
int start = index[0];
|
|
||||||
while (index[0] < expr.length() && (Character.isDigit(expr.charAt(index[0])) || expr.charAt(index[0]) == '.')) {
|
|
||||||
index[0]++;
|
|
||||||
}
|
|
||||||
return Double.parseDouble(expr.substring(start, index[0]));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
52
src/main/java/wtf/hak/survivalfabric/utils/MathUtils.java
Normal file
52
src/main/java/wtf/hak/survivalfabric/utils/MathUtils.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package wtf.hak.survivalfabric.utils;
|
||||||
|
|
||||||
|
public class MathUtils {
|
||||||
|
|
||||||
|
|
||||||
|
public static boolean hasSupportedOperator(String msg) {
|
||||||
|
return msg.contains("+") || msg.contains("-") || msg.contains("*") || msg.contains("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double evaluateExpression(String expression) {
|
||||||
|
return evaluate(expression.replaceAll("\\s", ""), new int[]{0});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double evaluate(String expr, int[] index) {
|
||||||
|
double value = parseTerm(expr, index);
|
||||||
|
while (index[0] < expr.length()) {
|
||||||
|
char op = expr.charAt(index[0]);
|
||||||
|
if (op != '+' && op != '-') break;
|
||||||
|
index[0]++;
|
||||||
|
double nextTerm = parseTerm(expr, index);
|
||||||
|
value = (op == '+') ? value + nextTerm : value - nextTerm;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double parseTerm(String expr, int[] index) {
|
||||||
|
double value = parseFactor(expr, index);
|
||||||
|
while (index[0] < expr.length()) {
|
||||||
|
char op = expr.charAt(index[0]);
|
||||||
|
if (op != '*' && op != '/') break;
|
||||||
|
index[0]++;
|
||||||
|
double nextFactor = parseFactor(expr, index);
|
||||||
|
value = (op == '*') ? value * nextFactor : value / nextFactor;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double parseFactor(String expr, int[] index) {
|
||||||
|
if (expr.charAt(index[0]) == '(') {
|
||||||
|
index[0]++;
|
||||||
|
double value = evaluate(expr, index);
|
||||||
|
index[0]++; // Skip closing ')'
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
int start = index[0];
|
||||||
|
while (index[0] < expr.length() && (Character.isDigit(expr.charAt(index[0])) || expr.charAt(index[0]) == '.')) {
|
||||||
|
index[0]++;
|
||||||
|
}
|
||||||
|
return Double.parseDouble(expr.substring(start, index[0]));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user