TF-EssentialsX/Essentials/src/com/earth2me/essentials/Trade.java

126 lines
3.5 KiB
Java
Raw Normal View History

package com.earth2me.essentials;
2011-06-13 13:05:31 +00:00
import java.util.Map;
import org.bukkit.inventory.ItemStack;
2011-06-13 13:05:31 +00:00
public class Trade
{
private final transient String command;
2011-06-13 13:05:31 +00:00
private final transient Double money;
private final transient ItemStack itemStack;
private final transient IEssentials ess;
2011-06-13 13:05:31 +00:00
public Trade(final String command, final IEssentials ess)
{
this(command, null, null, ess);
}
2011-06-13 13:05:31 +00:00
public Trade(final double money, final IEssentials ess)
{
this(null, money, null, ess);
}
2011-06-13 13:05:31 +00:00
public Trade(final ItemStack items, final IEssentials ess)
{
this(null, null, items, ess);
}
2011-06-13 13:05:31 +00:00
private Trade(final String command, final Double money, final ItemStack item, final IEssentials ess)
{
this.command = command;
2011-06-13 13:05:31 +00:00
this.money = money;
this.itemStack = item;
this.ess = ess;
}
public void isAffordableFor(final IUser user) throws ChargeException
{
final double mon = user.getMoney();
2011-06-13 13:05:31 +00:00
if (getMoney() != null
&& mon < getMoney()
&& getMoney() > 0
&& !user.isAuthorized("essentials.eco.loan"))
{
throw new ChargeException(Util.i18n("notEnoughMoney"));
}
2011-06-13 13:05:31 +00:00
if (getItemStack() != null
&& !InventoryWorkaround.containsItem(user.getInventory(), true, itemStack))
{
2011-06-13 13:05:31 +00:00
throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase().replace("_", " ")));
}
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)
&& 0 < ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command)
&& !user.isAuthorized("essentials.eco.loan"))
{
throw new ChargeException(Util.i18n("notEnoughMoney"));
}
}
2011-06-13 13:05:31 +00:00
public void pay(final IUser user)
{
if (getMoney() != null && getMoney() > 0)
2011-06-13 13:05:31 +00:00
{
user.giveMoney(getMoney());
}
if (getItemStack() != null)
{
final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack());
for (ItemStack itemStack : leftOver.values())
{
InventoryWorkaround.dropItem(user.getLocation(), itemStack);
}
user.updateInventory();
}
}
public void charge(final IUser user) throws ChargeException
{
2011-06-13 13:05:31 +00:00
if (getMoney() != null)
{
final double mon = user.getMoney();
if (mon < getMoney() && getMoney() > 0 && !user.isAuthorized("essentials.eco.loan"))
{
throw new ChargeException(Util.i18n("notEnoughMoney"));
}
2011-06-13 13:05:31 +00:00
user.takeMoney(getMoney());
}
2011-06-13 13:05:31 +00:00
if (getItemStack() != null)
{
2011-06-13 13:05:31 +00:00
if (!InventoryWorkaround.containsItem(user.getInventory(), true, itemStack))
{
2011-06-13 13:05:31 +00:00
throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase().replace("_", " ")));
}
2011-06-13 13:05:31 +00:00
InventoryWorkaround.removeItem(user.getInventory(), true, getItemStack());
user.updateInventory();
}
if (command != null && !command.isEmpty()
&& !user.isAuthorized("essentials.nocommandcost.all")
&& !user.isAuthorized("essentials.nocommandcost." + command))
{
final double mon = user.getMoney();
final double cost = ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command);
if (mon < cost && cost > 0 && !user.isAuthorized("essentials.eco.loan"))
{
throw new ChargeException(Util.i18n("notEnoughMoney"));
}
user.takeMoney(cost);
}
}
2011-06-13 13:05:31 +00:00
public Double getMoney()
{
return money;
}
public ItemStack getItemStack()
{
return itemStack;
}
}