2011-05-22 18:53:23 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
|
|
|
|
public class Charge
|
|
|
|
{
|
2011-06-08 01:18:33 +00:00
|
|
|
private final transient String command;
|
|
|
|
private final transient Double costs;
|
|
|
|
private final transient ItemStack items;
|
|
|
|
private final transient IEssentials ess;
|
2011-05-22 18:53:23 +00:00
|
|
|
|
2011-06-08 01:18:33 +00:00
|
|
|
public Charge(final String command, final IEssentials ess)
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-01 10:40:12 +00:00
|
|
|
this(command, null, null, ess);
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 01:18:33 +00:00
|
|
|
public Charge(final double money, final IEssentials ess)
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-01 10:40:12 +00:00
|
|
|
this(null, money, null, ess);
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 01:18:33 +00:00
|
|
|
public Charge(final ItemStack items, final IEssentials ess)
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-01 10:40:12 +00:00
|
|
|
this(null, null, items, ess);
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-06-08 01:18:33 +00:00
|
|
|
|
|
|
|
private Charge(final String command, final Double money, final ItemStack item, final IEssentials ess)
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-01 10:40:12 +00:00
|
|
|
this.command = command;
|
|
|
|
this.costs = money;
|
|
|
|
this.items = item;
|
|
|
|
this.ess = ess;
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 01:18:33 +00:00
|
|
|
public void isAffordableFor(final IUser user) throws ChargeException
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-08 01:18:33 +00:00
|
|
|
final double mon = user.getMoney();
|
|
|
|
if (costs != null
|
|
|
|
&& mon < costs
|
|
|
|
&& !user.isAuthorized("essentials.eco.loan"))
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-08 01:18:33 +00:00
|
|
|
throw new ChargeException(Util.i18n("notEnoughMoney"));
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-06-08 01:18:33 +00:00
|
|
|
|
|
|
|
if (items != null
|
|
|
|
&& !InventoryWorkaround.containsItem(user.getInventory(), true, items))
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-08 01:18:33 +00:00
|
|
|
throw new ChargeException(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " ")));
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-06-08 01:18:33 +00:00
|
|
|
|
|
|
|
if (command != null && !command.isEmpty()
|
|
|
|
&& !user.isAuthorized("essentials.nocommandcost.all")
|
|
|
|
&& !user.isAuthorized("essentials.nocommandcost." + command)
|
|
|
|
&& mon < ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command)
|
|
|
|
&& !user.isAuthorized("essentials.eco.loan"))
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-08 01:18:33 +00:00
|
|
|
throw new ChargeException(Util.i18n("notEnoughMoney"));
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-08 01:18:33 +00:00
|
|
|
public void charge(final IUser user) throws ChargeException
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-05-23 09:55:23 +00:00
|
|
|
if (costs != null)
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-08 01:18:33 +00:00
|
|
|
final double mon = user.getMoney();
|
2011-05-22 18:53:23 +00:00
|
|
|
if (mon < costs && !user.isAuthorized("essentials.eco.loan"))
|
|
|
|
{
|
2011-06-08 01:18:33 +00:00
|
|
|
throw new ChargeException(Util.i18n("notEnoughMoney"));
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
|
|
|
user.takeMoney(costs);
|
|
|
|
}
|
|
|
|
if (items != null)
|
|
|
|
{
|
|
|
|
if (!InventoryWorkaround.containsItem(user.getInventory(), true, items))
|
|
|
|
{
|
2011-06-08 01:18:33 +00:00
|
|
|
throw new ChargeException(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " ")));
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
|
|
|
InventoryWorkaround.removeItem(user.getInventory(), true, items);
|
|
|
|
user.updateInventory();
|
|
|
|
}
|
2011-06-08 01:18:33 +00:00
|
|
|
if (command != null && !command.isEmpty()
|
|
|
|
&& !user.isAuthorized("essentials.nocommandcost.all")
|
|
|
|
&& !user.isAuthorized("essentials.nocommandcost." + command))
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-08 01:18:33 +00:00
|
|
|
final double mon = user.getMoney();
|
|
|
|
final double cost = ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command);
|
2011-05-23 09:55:23 +00:00
|
|
|
if (mon < cost && !user.isAuthorized("essentials.eco.loan"))
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-08 01:18:33 +00:00
|
|
|
throw new ChargeException(Util.i18n("notEnoughMoney"));
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-05-23 09:55:23 +00:00
|
|
|
user.takeMoney(cost);
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|