Fixes #987 and #299 (untested)

Example usage:
claim: 50*{arg}
This commit is contained in:
Jesse Boyd 2016-06-18 13:43:34 +10:00
parent 3ced832b80
commit 29ce4af350
7 changed files with 71 additions and 9 deletions

View File

@ -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<Double> 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) {

View File

@ -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<Double> 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);

View File

@ -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<Plot> 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<Double> 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));

View File

@ -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();

View File

@ -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<Double> 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));

View File

@ -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<T> {
public abstract T evalute(T arg);
public static <U> Expression<U> constant(final U value) {
return new Expression<U>() {
@Override
public U evalute(U arg) {
return value;
}
};
}
public static Expression<Double> linearDouble(final Double value) {
return new Expression<Double>() {
@Override
public Double evalute(Double arg) {
return (arg.doubleValue() * value.doubleValue());
}
};
}
public static Expression<Double> 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<Double>() {
@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;
}
};
}
}

View File

@ -56,7 +56,7 @@ public abstract class PlotArea {
public List<String> SCHEMATICS = null;
public Map<Flag<?>, Object> DEFAULT_FLAGS;
public boolean USE_ECONOMY = false;
public Map<String, Double> PRICES = new HashMap<>();
public Map<String, Expression<Double>> 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");