2011-05-22 18:53:23 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
|
|
|
import com.earth2me.essentials.commands.EssentialsCommand;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
|
|
|
|
public class Charge
|
|
|
|
{
|
|
|
|
private String command = null;
|
|
|
|
private Double costs = null;
|
|
|
|
private ItemStack items = null;
|
|
|
|
private Essentials ess = Essentials.getStatic();
|
|
|
|
|
|
|
|
public Charge(String command)
|
|
|
|
{
|
|
|
|
this.command = command;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Charge(double money)
|
|
|
|
{
|
|
|
|
this.costs = money;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Charge(ItemStack items)
|
|
|
|
{
|
|
|
|
this.items = items;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Charge(EssentialsCommand command)
|
|
|
|
{
|
|
|
|
this.command = command.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void isAffordableFor(User user) throws Exception
|
|
|
|
{
|
|
|
|
double mon = user.getMoney();
|
|
|
|
if (costs != null)
|
|
|
|
{
|
|
|
|
if (mon < costs && !user.isAuthorized("essentials.eco.loan"))
|
|
|
|
{
|
|
|
|
throw new Exception(Util.i18n("notEnoughMoney"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (items != null)
|
|
|
|
{
|
|
|
|
if (!InventoryWorkaround.containsItem(user.getInventory(), true, items))
|
|
|
|
{
|
|
|
|
throw new Exception(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " ")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (command != null && !command.isEmpty())
|
|
|
|
{
|
|
|
|
if (user.isAuthorized("essentials.nocommandcost.all")
|
|
|
|
|| user.isAuthorized("essentials.nocommandcost." + command))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
double cost = ess.getSettings().getCommandCost(command.startsWith("/") ? command.substring(1) : command);
|
|
|
|
if (mon < cost && !user.isAuthorized("essentials.eco.loan"))
|
|
|
|
{
|
|
|
|
throw new Exception(Util.i18n("notEnoughMoney"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void charge(User user) throws Exception
|
|
|
|
{
|
|
|
|
double mon = user.getMoney();
|
2011-05-23 09:42:33 +00:00
|
|
|
if (costs != null && costs != 0.0)
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
|
|
|
if (mon < costs && !user.isAuthorized("essentials.eco.loan"))
|
|
|
|
{
|
|
|
|
throw new Exception(Util.i18n("notEnoughMoney"));
|
|
|
|
}
|
|
|
|
user.takeMoney(costs);
|
|
|
|
user.sendMessage(Util.format("moneyTaken", Util.formatCurrency(costs)));
|
|
|
|
}
|
|
|
|
if (items != null)
|
|
|
|
{
|
|
|
|
if (!InventoryWorkaround.containsItem(user.getInventory(), true, items))
|
|
|
|
{
|
|
|
|
throw new Exception(Util.format("missingItems", items.getAmount(), items.getType().toString().toLowerCase().replace("_", " ")));
|
|
|
|
}
|
|
|
|
InventoryWorkaround.removeItem(user.getInventory(), true, items);
|
|
|
|
user.updateInventory();
|
|
|
|
}
|
|
|
|
if (command != null && !command.isEmpty())
|
|
|
|
{
|
|
|
|
if (user.isAuthorized("essentials.nocommandcost.all")
|
|
|
|
|| user.isAuthorized("essentials.nocommandcost." + command))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-23 09:42:33 +00:00
|
|
|
int cost = ess.getSettings().getCommandCost(command.startsWith("/") ? command.substring(1) : command);
|
|
|
|
if (cost != 0)
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-05-23 09:42:33 +00:00
|
|
|
if (mon < cost && !user.isAuthorized("essentials.eco.loan"))
|
|
|
|
{
|
|
|
|
throw new Exception(Util.i18n("notEnoughMoney"));
|
|
|
|
}
|
|
|
|
user.takeMoney(cost);
|
|
|
|
user.sendMessage(Util.format("moneyTaken", Util.formatCurrency(cost)));
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|