Compare commits

...
2 Commits
Author SHA1 Message Date
md5sha256 832d8967a3 Remove the use of script engine 2021-08-23 03:51:56 +08:00
md5sha256 c369d23f74 Don't throw an exception if there is an invalid version 2021-08-17 13:09:12 +02:00
6 changed files with 164 additions and 17 deletions

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@ plugins {
}
description = 'AreaShop'
version = '2.6.4'
version = '2.6.5'
dependencies {
// Spigot api
@@ -648,7 +648,8 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface {
if (!matcher0.find()) {
matcher0 = VERSION_MATCHER_2.matcher(latestVersion);
if (!matcher0.find()) {
throw new IllegalArgumentException("Invalid Versions Detected!");
// FIXME log invalid version
return false;
}
dual0 = true;
}
@@ -657,7 +658,8 @@ public final class AreaShop extends JavaPlugin implements AreaShopInterface {
if (!matcher1.find()) {
matcher1 = VERSION_MATCHER_2.matcher(currentVersion);
if (!matcher1.find()) {
throw new IllegalArgumentException("Invalid Versions Detected!");
// FIXME log invalid version
return false;
}
dual1 = true;
}
@@ -0,0 +1,152 @@
package me.wiefferink.areashop.tools;
/**
* Parse math from a string
* Adapted from:
* https://stackoverflow.com/questions/3422673/how-to-evaluate-a-math-expression-given-in-string-form
*/
public final class OperandParser {
private final String str;
private OperandParser(final String str) {
this.str = str;
}
public static double parse(final String str) throws ParseException {
return new OperandParser(str).parse();
}
private int pos = -1, ch;
private void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
private boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
private double parse() throws ParseException {
nextChar();
double x = parseExpression();
if (pos < str.length())
{
throw new ParseException("Unexpected: " + (char) ch);
}
return x;
}
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
// | number | functionName factor | factor `^` factor
private double parseExpression() throws ParseException {
double x = parseTerm();
for (; ; ) {
if (eat('+')) {
// addition
x += parseTerm();
}
else if (eat('-')) {
// subtraction
x -= parseTerm();
}
else return x;
}
}
private double parseTerm() throws ParseException {
double x = parseFactor();
for (; ; ) {
if (eat('*')) {
// multiplication
x *= parseFactor();
}
else if (eat('/')) {
// division
x /= parseFactor();
}
else return x;
}
}
private double parseFactor() throws ParseException {
if (eat('+')) {
// unary plus
return parseFactor();
}
if (eat('-')) {
// unary minus
return -parseFactor();
}
double x;
int startPos = this.pos;
if (eat('(')) {
// parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') {
// numbers
while ((ch >= '0' && ch <= '9') || ch == '.') {
nextChar();
}
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') {
// functions
while (ch >= 'a' && ch <= 'z') {
nextChar();
}
String func = str.substring(startPos, this.pos);
x = parseFactor();
x = switch (func) {
case "sqrt" -> Math.sqrt(x);
case "sin" -> Math.sin(Math.toRadians(x));
case "cos" -> Math.cos(Math.toRadians(x));
case "tan" -> Math.tan(Math.toRadians(x));
default -> throw new ParseException("Unknown function: " + func);
};
} else {
throw new ParseException("Unexpected: " + (char) ch);
}
if (eat('^')) {
// exponentiation
x = Math.pow(x, parseFactor());
}
return x;
}
public static class ParseException extends Exception {
public ParseException() {
}
public ParseException(String message) {
super(message);
}
public ParseException(String message, Throwable cause) {
super(message, cause);
}
public ParseException(Throwable cause) {
super(cause);
}
public ParseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
}
@@ -12,7 +12,10 @@ import me.wiefferink.areashop.regions.RentRegion;
import me.wiefferink.interactivemessenger.Log;
import me.wiefferink.interactivemessenger.processing.Message;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.*;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection;
@@ -22,10 +25,6 @@ import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
@@ -46,7 +45,6 @@ public class Utils {
private static Set<String> weeks;
private static Set<String> months;
private static Set<String> years;
private static ScriptEngine scriptEngine;
private static Map<Double, String> suffixes;
// Not used
@@ -768,16 +766,11 @@ public class Utils {
return Double.parseDouble(input);
}
// Lazy init scriptEngine
if (scriptEngine == null) {
scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
}
// Evaluate expression
Object result;
try {
result = scriptEngine.eval(input);
} catch (ScriptException e) {
result = OperandParser.parse(input);
} catch (OperandParser.ParseException e) {
AreaShop.warn("Price of region", region.getName(), "is set with an invalid expression: '" + input + "', exception:", ExceptionUtils.getStackTrace(e));
return 99999999999.0; // High fallback for safety
}
+1 -1
View File
@@ -3,7 +3,7 @@ plugins {
}
group = "me.wiefferink.areashop"
version = '2.6.4'
version = '2.6.5'
subprojects {
Vendored Regular → Executable
View File
File mode changed.