2011-05-22 18:53:23 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2011-06-13 13:05:31 +00:00
|
|
|
import java.util.Map;
|
2011-05-22 18:53:23 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
|
2011-06-13 13:05:31 +00:00
|
|
|
public class Trade
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-08 01:18:33 +00:00
|
|
|
private final transient String command;
|
2011-06-13 13:05:31 +00:00
|
|
|
private final transient Double money;
|
|
|
|
private final transient ItemStack itemStack;
|
2011-06-08 01:18:33 +00:00
|
|
|
private final transient IEssentials ess;
|
2011-05-22 18:53:23 +00:00
|
|
|
|
2011-06-13 13:05:31 +00:00
|
|
|
public Trade(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-13 13:05:31 +00:00
|
|
|
public Trade(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-13 13:05:31 +00:00
|
|
|
public Trade(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
|
|
|
|
2011-06-13 13:05:31 +00:00
|
|
|
private Trade(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;
|
2011-06-13 13:05:31 +00:00
|
|
|
this.money = money;
|
|
|
|
this.itemStack = item;
|
2011-06-01 10:40:12 +00:00
|
|
|
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();
|
2011-06-13 13:05:31 +00:00
|
|
|
if (getMoney() != null
|
|
|
|
&& mon < getMoney()
|
2011-06-25 20:23:11 +00:00
|
|
|
&& getMoney() > 0
|
2011-06-08 01:18:33 +00:00
|
|
|
&& !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
|
|
|
|
2011-06-13 13:05:31 +00:00
|
|
|
if (getItemStack() != null
|
|
|
|
&& !InventoryWorkaround.containsItem(user.getInventory(), true, itemStack))
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-13 13:05:31 +00:00
|
|
|
throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().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)
|
2011-06-25 20:23:11 +00:00
|
|
|
&& 0 < ess.getSettings().getCommandCost(command.charAt(0) == '/' ? command.substring(1) : command)
|
2011-06-08 01:18:33 +00:00
|
|
|
&& !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-13 13:05:31 +00:00
|
|
|
public void pay(final IUser user)
|
|
|
|
{
|
2011-06-25 20:23:11 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-13 13:05:31 +00:00
|
|
|
if (getMoney() != null)
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-08 01:18:33 +00:00
|
|
|
final double mon = user.getMoney();
|
2011-06-25 20:23:11 +00:00
|
|
|
if (mon < getMoney() && getMoney() > 0 && !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-13 13:05:31 +00:00
|
|
|
user.takeMoney(getMoney());
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-06-13 13:05:31 +00:00
|
|
|
if (getItemStack() != null)
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-13 13:05:31 +00:00
|
|
|
if (!InventoryWorkaround.containsItem(user.getInventory(), true, itemStack))
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-06-13 13:05:31 +00:00
|
|
|
throw new ChargeException(Util.format("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase().replace("_", " ")));
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-06-13 13:05:31 +00:00
|
|
|
InventoryWorkaround.removeItem(user.getInventory(), true, getItemStack());
|
2011-05-22 18:53:23 +00:00
|
|
|
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-06-25 20:23:11 +00:00
|
|
|
if (mon < cost && cost > 0 && !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
|
|
|
}
|
|
|
|
}
|
2011-06-13 13:05:31 +00:00
|
|
|
|
|
|
|
public Double getMoney()
|
|
|
|
{
|
|
|
|
return money;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack getItemStack()
|
|
|
|
{
|
|
|
|
return itemStack;
|
|
|
|
}
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|