diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Auto.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Auto.java index 4cdb09563..2c8fc8cc1 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Auto.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Auto.java @@ -2,6 +2,7 @@ package com.intellectualcrafters.plot.commands; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; +import com.intellectualcrafters.plot.object.Expression; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotArea; import com.intellectualcrafters.plot.object.PlotId; @@ -116,7 +117,8 @@ public class Auto extends SubCommand { } } if (EconHandler.manager != null && plotarea.USE_ECONOMY) { - double cost = plotarea.PRICES.get("claim"); + Expression costExp = plotarea.PRICES.get("claim"); + double cost = costExp.evalute((double) currentPlots); cost = (size_x * size_z) * cost; if (cost > 0d) { if (EconHandler.manager.getMoney(player) < cost) { diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Claim.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Claim.java index ccb3e5437..301ae171d 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Claim.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Claim.java @@ -2,6 +2,7 @@ package com.intellectualcrafters.plot.commands; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; +import com.intellectualcrafters.plot.object.Expression; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotArea; @@ -48,7 +49,8 @@ public class Claim extends SubCommand { } PlotArea world = plot.getArea(); if ((EconHandler.manager != null) && world.USE_ECONOMY) { - double cost = world.PRICES.get("claim"); + Expression costExr = world.PRICES.get("claim"); + double cost = costExr.evalute((double) currentPlots); if (cost > 0d) { if (EconHandler.manager.getMoney(player) < cost) { return sendMessage(player, C.CANNOT_AFFORD_PLOT, "" + cost); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Delete.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Delete.java index 389771eb1..78697bfd0 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Delete.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Delete.java @@ -1,6 +1,8 @@ package com.intellectualcrafters.plot.commands; import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.config.Settings; +import com.intellectualcrafters.plot.object.Expression; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotArea; @@ -39,6 +41,7 @@ public class Delete extends SubCommand { } final PlotArea plotArea = plot.getArea(); final java.util.Set plots = plot.getConnectedPlots(); + final int currentPlots = Settings.Limit.GLOBAL ? player.getPlotCount() : player.getPlotCount(loc.getWorld()); Runnable run = new Runnable() { @Override public void run() { @@ -52,7 +55,8 @@ public class Delete extends SubCommand { public void run() { plot.removeRunning(); if ((EconHandler.manager != null) && plotArea.USE_ECONOMY) { - double value = plotArea.PRICES.get("sell") * plots.size(); + Expression valueExr = plotArea.PRICES.get("sell"); + double value = plots.size() * valueExr.evalute((double) currentPlots); if (value > 0d) { EconHandler.manager.depositMoney(player, value); sendMessage(player, C.ADDED_BALANCE, String.valueOf(value)); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java index 7c40ab416..644498624 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java @@ -139,7 +139,7 @@ public class MainCommand extends Command { if (EconHandler.manager != null) { PlotArea area = player.getApplicablePlotArea(); if (area != null) { - Double price = area.PRICES.get(cmd.getFullId()); + Double price = area.PRICES.get(cmd.getFullId()).evalute(0d); if (price != null && EconHandler.manager.getMoney(player) < price) { if (failure != null) { failure.run(); @@ -158,7 +158,7 @@ public class MainCommand extends Command { if (EconHandler.manager != null) { PlotArea area = player.getApplicablePlotArea(); if (area != null) { - Double price = area.PRICES.get(cmd.getFullId()); + Double price = area.PRICES.get(cmd.getFullId()).evalute(0d); if (price != null && EconHandler.manager.getMoney(player) < price) { if (failure != null) { failure.run(); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/commands/Merge.java b/Core/src/main/java/com/intellectualcrafters/plot/commands/Merge.java index c1f0aa7e7..99b35ceb5 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/commands/Merge.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/commands/Merge.java @@ -2,6 +2,7 @@ package com.intellectualcrafters.plot.commands; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; +import com.intellectualcrafters.plot.object.Expression; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotArea; @@ -71,12 +72,13 @@ public class Merge extends SubCommand { } } final PlotArea plotArea = plot.getArea(); - final double price = plotArea.PRICES.containsKey("merge") ? plotArea.PRICES.get("merge") : 0; + Expression priceExr = plotArea.PRICES.containsKey("merge") ? plotArea.PRICES.get("merge") : null; + final int size = plot.getConnectedPlots().size(); + double price = priceExr == null ? 0d : priceExr.evalute((double) size); if (EconHandler.manager != null && plotArea.USE_ECONOMY && price > 0d && EconHandler.manager.getMoney(player) < price) { sendMessage(player, C.CANNOT_AFFORD_MERGE, String.valueOf(price)); return false; } - final int size = plot.getConnectedPlots().size(); final int maxSize = Permissions.hasPermissionRange(player, "plots.merge", Settings.Limit.MAX_PLOTS); if (size - 1 > maxSize) { MainUtil.sendMessage(player, C.NO_PERMISSION, "plots.merge." + (size + 1)); diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/Expression.java b/Core/src/main/java/com/intellectualcrafters/plot/object/Expression.java new file mode 100644 index 000000000..feb577591 --- /dev/null +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/Expression.java @@ -0,0 +1,52 @@ +package com.intellectualcrafters.plot.object; + +import com.intellectualcrafters.plot.PS; +import com.intellectualcrafters.plot.commands.DebugExec; +import com.intellectualcrafters.plot.commands.MainCommand; +import javax.script.ScriptException; + +public abstract class Expression { + public abstract T evalute(T arg); + + public static Expression constant(final U value) { + return new Expression() { + @Override + public U evalute(U arg) { + return value; + } + }; + } + + public static Expression linearDouble(final Double value) { + return new Expression() { + @Override + public Double evalute(Double arg) { + return (arg.doubleValue() * value.doubleValue()); + } + }; + } + + public static Expression doubleExpression(final String expression) { + try { + return constant(Double.parseDouble(expression)); + } catch (Exception ignore) {} + if (expression.endsWith("*{arg}")) { + try { + return linearDouble(Double.parseDouble(expression.substring(0, expression.length() - 8))); + } catch (Exception ignore) {} + } + return new Expression() { + @Override + public Double evalute(Double arg) { + DebugExec exec = (DebugExec) MainCommand.getInstance().getCommand(DebugExec.class); + try { + return (Double) exec.getEngine().eval(expression.replace("{arg}", "" + arg)); + } catch (ScriptException e) { + PS.debug("Invalid Expression: " + expression); + e.printStackTrace(); + } + return 0d; + } + }; + } +} diff --git a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java index 16bd991a7..d9bb65fe5 100644 --- a/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java +++ b/Core/src/main/java/com/intellectualcrafters/plot/object/PlotArea.java @@ -56,7 +56,7 @@ public abstract class PlotArea { public List SCHEMATICS = null; public Map, Object> DEFAULT_FLAGS; public boolean USE_ECONOMY = false; - public Map PRICES = new HashMap<>(); + public Map> PRICES = new HashMap<>(); public boolean SPAWN_EGGS = false; public boolean SPAWN_CUSTOM = true; public boolean SPAWN_BREEDING = false; @@ -225,7 +225,7 @@ public abstract class PlotArea { if (this.USE_ECONOMY) { this.PRICES = new HashMap<>(); for (String key : priceSection.getKeys(false)) { - this.PRICES.put(key, priceSection.getDouble(key)); + this.PRICES.put(key, Expression.doubleExpression(priceSection.getString(key))); } } this.PLOT_CHAT = config.getBoolean("chat.enabled");