diff options
author | KHobbits <rob@khobbits.co.uk> | 2012-08-31 17:47:35 +0100 |
---|---|---|
committer | KHobbits <rob@khobbits.co.uk> | 2012-08-31 17:48:07 +0100 |
commit | 93f5a05f170fc53dc8c46e8c3d61cc7ecb39b12a (patch) | |
tree | 1306f93f08b2155d11a31e01a9adf22222235a08 | |
parent | a39824abb61ac5b087bb362387377ee5ca532f64 (diff) | |
download | Essentials-93f5a05f170fc53dc8c46e8c3d61cc7ecb39b12a.tar Essentials-93f5a05f170fc53dc8c46e8c3d61cc7ecb39b12a.tar.gz Essentials-93f5a05f170fc53dc8c46e8c3d61cc7ecb39b12a.tar.lz Essentials-93f5a05f170fc53dc8c46e8c3d61cc7ecb39b12a.tar.xz Essentials-93f5a05f170fc53dc8c46e8c3d61cc7ecb39b12a.zip |
Reduce the number of permissions checks in command cost lookups
Add support for item number repair costs (repair-123: 120)
Ess charge api supports nested charges.
Slight kit optimization.
4 files changed, 58 insertions, 33 deletions
diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java index 73beb1891..c7a59b763 100644 --- a/Essentials/src/com/earth2me/essentials/Settings.java +++ b/Essentials/src/com/earth2me/essentials/Settings.java @@ -191,6 +191,7 @@ public class Settings implements ISettings } return config.getBoolean("override-" + name.toLowerCase(Locale.ENGLISH), false); } + private ConfigurationSection commandCosts; @Override public double getCommandCost(IEssentialsCommand cmd) @@ -198,15 +199,37 @@ public class Settings implements ISettings return getCommandCost(cmd.getName()); } + public ConfigurationSection _getCommandCosts() + { + if (config.isConfigurationSection("command-costs")) + { + final ConfigurationSection section = config.getConfigurationSection("command-costs"); + final ConfigurationSection newSection = new MemoryConfiguration(); + for (String command : section.getKeys(false)) + { + if (section.isDouble(command)) + { + newSection.set(command.toLowerCase(Locale.ENGLISH), section.getDouble(command)); + } + else if (section.isInt(command)) + { + newSection.set(command.toLowerCase(Locale.ENGLISH), (double)section.getInt(command)); + } + } + return newSection; + } + return null; + } + @Override - public double getCommandCost(String label) + public double getCommandCost(String name) { - double cost = config.getDouble("command-costs." + label, 0.0); - if (cost == 0.0) + name = name.replace('.', '_').replace('/', '_'); + if (commandCosts != null) { - cost = config.getDouble("cost-" + label, 0.0); + return commandCosts.getDouble(name, 0.0); } - return cost; + return 0.0; } private String nicknamePrefix = "~"; @@ -262,7 +285,7 @@ public class Settings implements ISettings public Map<String, Object> getKit(String name) { name = name.replace('.', '_').replace('/', '_'); - if (config.isConfigurationSection("kits")) + if (getKits() != null) { final ConfigurationSection kits = getKits(); if (kits.isConfigurationSection(name)) @@ -431,6 +454,7 @@ public class Settings implements ISettings disablePrefix = _disablePrefix(); disableSuffix = _disableSuffix(); chatRadius = _getChatRadius(); + commandCosts = _getCommandCosts(); warnOnBuildDisallow = _warnOnBuildDisallow(); } private List<Integer> itemSpawnBl = new ArrayList<Integer>(); @@ -512,21 +536,18 @@ public class Settings implements ISettings { return config.getBoolean("spawn-if-no-home", false); } - private boolean warnOnBuildDisallow; private boolean _warnOnBuildDisallow() { return config.getBoolean("protect.disable.warn-on-build-disallow", false); } - + @Override public boolean warnOnBuildDisallow() { return warnOnBuildDisallow; } - - private boolean debug = false; private boolean configDebug = false; diff --git a/Essentials/src/com/earth2me/essentials/Trade.java b/Essentials/src/com/earth2me/essentials/Trade.java index 707665d93..1718852d2 100644 --- a/Essentials/src/com/earth2me/essentials/Trade.java +++ b/Essentials/src/com/earth2me/essentials/Trade.java @@ -20,7 +20,7 @@ import org.bukkit.inventory.ItemStack; public class Trade { private final transient String command; - private final transient String fallbackCommand; + private final transient Trade fallbackTrade; private final transient Double money; private final transient ItemStack itemStack; private final transient Integer exp; @@ -31,7 +31,7 @@ public class Trade this(command, null, null, null, null, ess); } - public Trade(final String command, final String fallback, final IEssentials ess) + public Trade(final String command, final Trade fallback, final IEssentials ess) { this(command, fallback, null, null, null, ess); } @@ -51,10 +51,10 @@ public class Trade this(null, null, null, null, exp, ess); } - private Trade(final String command, final String fallback, final Double money, final ItemStack item, final Integer exp, final IEssentials ess) + private Trade(final String command, final Trade fallback, final Double money, final ItemStack item, final Integer exp, final IEssentials ess) { this.command = command; - this.fallbackCommand = fallback; + this.fallbackTrade = fallback; this.money = money; this.itemStack = item; this.exp = exp; @@ -150,9 +150,14 @@ public class Trade public void charge(final IUser user) throws ChargeException { + if (ess.getSettings().isDebug()) + { + ess.getLogger().log(Level.INFO, "charging user " + user.getName()); + } + if (getMoney() != null) { - if (!user.canAfford(getMoney()) && getMoney() > 0) + if (!user.canAfford(getMoney()) && getMoney() > 0.0d) { throw new ChargeException(_("notEnoughMoney")); } @@ -170,7 +175,7 @@ public class Trade if (command != null) { final double cost = getCommandCost(user); - if (!user.canAfford(cost) && cost > 0) + if (!user.canAfford(cost) && cost > 0.0d) { throw new ChargeException(_("notEnoughMoney")); } @@ -204,19 +209,13 @@ public class Trade public Double getCommandCost(final IUser user) { - double cost = 0d; - if (command != null && !command.isEmpty() - && !user.isAuthorized("essentials.nocommandcost.all") - && !user.isAuthorized("essentials.nocommandcost." + command)) + double cost = 0.0d; + if (command != null && !command.isEmpty()) { cost = ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command); - if (cost == 0.0 && fallbackCommand != null && !fallbackCommand.isEmpty()) + if (cost == 0.0d && fallbackTrade != null) { - if (ess.getSettings().isDebug()) - { - ess.getLogger().log(Level.INFO, "checking fallback command cost (" + fallbackCommand + ") cost for " + user.getName()); - } - cost = ess.getSettings().getCommandCost(fallbackCommand.charAt(0) == '/' ? fallbackCommand.substring(1) : fallbackCommand); + cost = fallbackTrade.getCommandCost(user); } if (ess.getSettings().isDebug()) @@ -224,6 +223,11 @@ public class Trade ess.getLogger().log(Level.INFO, "calculated command (" + command + ") cost for " + user.getName() + " as " + cost); } } + if (cost != 0.0d && (user.isAuthorized("essentials.nocommandcost.all") + || user.isAuthorized("essentials.nocommandcost." + command))) + { + return 0.0d; + } return cost; } private static FileWriter fw = null; diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index c2b2a2754..90a3679c4 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -119,7 +119,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser public void giveMoney(final double value, final CommandSender initiator) { - if (value == 0) + if (value == 0.0d) { return; } @@ -133,7 +133,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser public void payUser(final User reciever, final double value) throws Exception { - if (value == 0) + if (value == 0.0d) { return; } @@ -158,7 +158,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser public void takeMoney(final double value, final CommandSender initiator) { - if (value == 0) + if (value == 0.0d) { return; } @@ -178,7 +178,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser public boolean canAfford(final double cost, final boolean permcheck) { - if (cost <= 0.0) + if (cost <= 0.0d) { return true; } @@ -727,5 +727,5 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser public void setRightClickJump(boolean rightClickJump) { this.rightClickJump = rightClickJump; - } + } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandrepair.java b/Essentials/src/com/earth2me/essentials/commands/Commandrepair.java index d621109f7..4fc428910 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandrepair.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandrepair.java @@ -100,12 +100,12 @@ public class Commandrepair extends EssentialsCommand { for (ItemStack item : items) { - if (item == null) + if (item == null || item.getType().isBlock() || item.getDurability() == 0) { continue; } final String itemName = item.getType().toString().toLowerCase(Locale.ENGLISH); - final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), "repair-item", ess); + final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), new Trade("repair-" + item.getTypeId(), new Trade("repair-item", ess), ess), ess); try { charge.isAffordableFor(user); |