2011-05-22 18:53:23 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2011-12-06 16:42:55 +00:00
|
|
|
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
2011-11-21 01:55:26 +00:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
2011-07-06 00:58:59 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.util.Date;
|
2011-11-21 01:55:26 +00:00
|
|
|
import java.util.Locale;
|
2011-06-13 13:05:31 +00:00
|
|
|
import java.util.Map;
|
2011-07-06 00:58:59 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2011-07-18 02:49:38 +00:00
|
|
|
import org.bukkit.Location;
|
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-12-07 09:31:18 +00:00
|
|
|
private final transient Integer exp;
|
2011-06-08 01:18:33 +00:00
|
|
|
private final transient IEssentials ess;
|
2011-10-25 21:18:28 +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-12-07 09:31:18 +00:00
|
|
|
this(command, null, null, null, ess);
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-10-25 21:18:28 +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-12-07 09:31:18 +00:00
|
|
|
this(null, money, null, null, ess);
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-10-25 21:18:28 +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-12-07 09:31:18 +00:00
|
|
|
this(null, null, items, null, ess);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Trade(final int exp, final IEssentials ess)
|
|
|
|
{
|
2011-12-13 06:35:46 +00:00
|
|
|
//TODO: Revert this change, when exp is fixed in Bukkit
|
|
|
|
this(null, (double)exp, null, null, ess);
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-10-25 21:18:28 +00:00
|
|
|
|
2011-12-07 09:31:18 +00:00
|
|
|
private Trade(final String command, final Double money, final ItemStack item, final Integer exp, 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-12-07 09:31:18 +00:00
|
|
|
this.exp = exp;
|
2011-06-01 10:40:12 +00:00
|
|
|
this.ess = ess;
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-10-25 21:18:28 +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-11-21 01:55:26 +00:00
|
|
|
throw new ChargeException(_("notEnoughMoney"));
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-10-25 21:18:28 +00:00
|
|
|
|
2011-06-13 13:05:31 +00:00
|
|
|
if (getItemStack() != null
|
2011-12-07 13:23:56 +00:00
|
|
|
&& !InventoryWorkaround.containsItem(user.getInventory(), true, true, itemStack))
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
throw new ChargeException(_("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " ")));
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-10-25 21:18:28 +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-11-21 01:55:26 +00:00
|
|
|
throw new ChargeException(_("notEnoughMoney"));
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-12-07 09:31:18 +00:00
|
|
|
|
|
|
|
if (exp != null && exp > 0
|
2011-12-13 04:29:16 +00:00
|
|
|
&& user.getXP() < exp) {
|
2011-12-07 09:31:18 +00:00
|
|
|
throw new ChargeException(_("notEnoughExperience"));
|
|
|
|
}
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-10-25 21:18:28 +00:00
|
|
|
|
2011-06-13 13:05:31 +00:00
|
|
|
public void pay(final IUser user)
|
|
|
|
{
|
2011-10-09 20:25:15 +00:00
|
|
|
pay(user, true);
|
|
|
|
}
|
2011-10-25 21:18:28 +00:00
|
|
|
|
2011-10-09 20:25:15 +00:00
|
|
|
public boolean pay(final IUser user, final boolean dropItems)
|
|
|
|
{
|
|
|
|
boolean success = true;
|
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)
|
|
|
|
{
|
2011-10-09 20:25:15 +00:00
|
|
|
if (dropItems)
|
|
|
|
{
|
|
|
|
final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack());
|
|
|
|
for (ItemStack itemStack : leftOver.values())
|
|
|
|
{
|
|
|
|
InventoryWorkaround.dropItem(user.getLocation(), itemStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2011-06-13 13:05:31 +00:00
|
|
|
{
|
2011-10-09 20:25:15 +00:00
|
|
|
success = InventoryWorkaround.addAllItems(user.getInventory(), true, getItemStack());
|
2011-06-13 13:05:31 +00:00
|
|
|
}
|
|
|
|
user.updateInventory();
|
|
|
|
}
|
2011-12-07 09:31:18 +00:00
|
|
|
if (getExperience() != null)
|
|
|
|
{
|
2011-12-13 04:29:16 +00:00
|
|
|
user.setXP(user.getXP() + getExperience());
|
2011-12-07 09:31:18 +00:00
|
|
|
}
|
2011-10-09 20:25:15 +00:00
|
|
|
return success;
|
2011-06-13 13:05:31 +00:00
|
|
|
}
|
2011-10-25 21:18:28 +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-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-11-21 01:55:26 +00:00
|
|
|
throw new ChargeException(_("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-12-07 13:23:56 +00:00
|
|
|
if (!InventoryWorkaround.containsItem(user.getInventory(), true, true, itemStack))
|
2011-05-22 18:53:23 +00:00
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
throw new ChargeException(_("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " ")));
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-12-07 13:23:56 +00:00
|
|
|
InventoryWorkaround.removeItem(user.getInventory(), true, 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-11-21 01:55:26 +00:00
|
|
|
throw new ChargeException(_("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-12-07 09:31:18 +00:00
|
|
|
if (getExperience() != null)
|
|
|
|
{
|
2011-12-13 04:29:16 +00:00
|
|
|
final int experience = user.getXP();
|
2011-12-07 09:31:18 +00:00
|
|
|
if (experience < getExperience() && getExperience() > 0)
|
|
|
|
{
|
|
|
|
throw new ChargeException(_("notEnoughExperience"));
|
|
|
|
}
|
2011-12-13 04:29:16 +00:00
|
|
|
user.setXP(experience - getExperience());
|
2011-12-07 09:31:18 +00:00
|
|
|
}
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|
2011-10-25 21:18:28 +00:00
|
|
|
|
2011-06-13 13:05:31 +00:00
|
|
|
public Double getMoney()
|
|
|
|
{
|
|
|
|
return money;
|
|
|
|
}
|
2011-10-25 21:18:28 +00:00
|
|
|
|
2011-06-13 13:05:31 +00:00
|
|
|
public ItemStack getItemStack()
|
|
|
|
{
|
|
|
|
return itemStack;
|
|
|
|
}
|
2011-12-07 09:31:18 +00:00
|
|
|
|
|
|
|
public Integer getExperience()
|
|
|
|
{
|
|
|
|
return exp;
|
|
|
|
}
|
2011-07-06 00:58:59 +00:00
|
|
|
private static FileWriter fw = null;
|
2011-10-25 21:18:28 +00:00
|
|
|
|
2011-07-18 02:49:38 +00:00
|
|
|
public static void log(String type, String subtype, String event, String sender, Trade charge, String receiver, Trade pay, Location loc, IEssentials ess)
|
2011-07-06 00:58:59 +00:00
|
|
|
{
|
|
|
|
if (!ess.getSettings().isEcoLogEnabled())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fw == null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
fw = new FileWriter(new File(ess.getDataFolder(), "trade.log"), true);
|
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
|
|
|
Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2011-07-18 02:58:55 +00:00
|
|
|
sb.append(type).append(",").append(subtype).append(",").append(event).append(",\"");
|
2011-07-06 00:58:59 +00:00
|
|
|
sb.append(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date()));
|
|
|
|
sb.append("\",\"");
|
|
|
|
if (sender != null)
|
|
|
|
{
|
|
|
|
sb.append(sender);
|
|
|
|
}
|
|
|
|
sb.append("\",");
|
|
|
|
if (charge == null)
|
|
|
|
{
|
|
|
|
sb.append("\"\",\"\",\"\"");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (charge.getItemStack() != null)
|
|
|
|
{
|
|
|
|
sb.append(charge.getItemStack().getAmount()).append(",");
|
|
|
|
sb.append(charge.getItemStack().getType().toString()).append(",");
|
|
|
|
sb.append(charge.getItemStack().getDurability());
|
|
|
|
}
|
|
|
|
if (charge.getMoney() != null)
|
|
|
|
{
|
|
|
|
sb.append(charge.getMoney()).append(",");
|
|
|
|
sb.append("money").append(",");
|
|
|
|
sb.append(ess.getSettings().getCurrencySymbol());
|
|
|
|
}
|
2011-12-07 09:31:18 +00:00
|
|
|
if (charge.getExperience() != null)
|
|
|
|
{
|
|
|
|
sb.append(charge.getExperience()).append(",");
|
|
|
|
sb.append("exp").append(",");
|
|
|
|
sb.append("\"\"");
|
|
|
|
}
|
2011-07-06 00:58:59 +00:00
|
|
|
}
|
|
|
|
sb.append(",\"");
|
|
|
|
if (receiver != null)
|
|
|
|
{
|
|
|
|
sb.append(receiver);
|
|
|
|
}
|
|
|
|
sb.append("\",");
|
|
|
|
if (pay == null)
|
|
|
|
{
|
|
|
|
sb.append("\"\",\"\",\"\"");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pay.getItemStack() != null)
|
|
|
|
{
|
|
|
|
sb.append(pay.getItemStack().getAmount()).append(",");
|
|
|
|
sb.append(pay.getItemStack().getType().toString()).append(",");
|
|
|
|
sb.append(pay.getItemStack().getDurability());
|
|
|
|
}
|
|
|
|
if (pay.getMoney() != null)
|
|
|
|
{
|
|
|
|
sb.append(pay.getMoney()).append(",");
|
|
|
|
sb.append("money").append(",");
|
|
|
|
sb.append(ess.getSettings().getCurrencySymbol());
|
|
|
|
}
|
2011-12-07 09:31:18 +00:00
|
|
|
if (pay.getExperience() != null)
|
|
|
|
{
|
|
|
|
sb.append(pay.getExperience()).append(",");
|
|
|
|
sb.append("exp").append(",");
|
|
|
|
sb.append("\"\"");
|
|
|
|
}
|
2011-07-06 00:58:59 +00:00
|
|
|
}
|
2011-07-18 02:49:38 +00:00
|
|
|
if (loc == null)
|
|
|
|
{
|
|
|
|
sb.append(",\"\",\"\",\"\",\"\"");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sb.append(",\"");
|
|
|
|
sb.append(loc.getWorld().getName()).append("\",");
|
|
|
|
sb.append(loc.getBlockX()).append(",");
|
|
|
|
sb.append(loc.getBlockY()).append(",");
|
|
|
|
sb.append(loc.getBlockZ()).append(",");
|
|
|
|
}
|
2011-07-06 00:58:59 +00:00
|
|
|
sb.append("\n");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
fw.write(sb.toString());
|
|
|
|
fw.flush();
|
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
|
|
|
Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
}
|
2011-10-25 21:18:28 +00:00
|
|
|
|
2011-07-06 00:58:59 +00:00
|
|
|
public static void closeLog()
|
|
|
|
{
|
2011-10-09 20:25:15 +00:00
|
|
|
if (fw != null)
|
|
|
|
{
|
2011-07-06 00:58:59 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
fw.close();
|
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
|
|
|
Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
fw = null;
|
|
|
|
}
|
|
|
|
}
|
2011-05-22 18:53:23 +00:00
|
|
|
}
|