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.
This commit is contained in:
KHobbits 2012-08-31 17:47:35 +01:00
parent a39824abb6
commit 93f5a05f17
4 changed files with 59 additions and 34 deletions

View File

@ -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());
}
@Override
public double getCommandCost(String label)
public ConfigurationSection _getCommandCosts()
{
double cost = config.getDouble("command-costs." + label, 0.0);
if (cost == 0.0)
if (config.isConfigurationSection("command-costs"))
{
cost = config.getDouble("cost-" + label, 0.0);
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 cost;
return null;
}
@Override
public double getCommandCost(String name)
{
name = name.replace('.', '_').replace('/', '_');
if (commandCosts != null)
{
return commandCosts.getDouble(name, 0.0);
}
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;

View File

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

View File

@ -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;
}
}
}

View File

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