2011-04-16 00:42:43 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2013-07-14 00:03:08 +00:00
|
|
|
import static com.earth2me.essentials.I18n._;
|
2013-07-14 00:07:59 +00:00
|
|
|
import com.earth2me.essentials.commands.NotEnoughArgumentsException;
|
2011-04-16 00:42:43 +00:00
|
|
|
import java.io.File;
|
2013-05-05 03:13:17 +00:00
|
|
|
import java.math.BigDecimal;
|
2011-11-21 01:55:26 +00:00
|
|
|
import java.util.Locale;
|
2011-04-16 00:42:43 +00:00
|
|
|
import java.util.logging.Logger;
|
2013-07-14 00:03:08 +00:00
|
|
|
import org.bukkit.Material;
|
2011-04-16 06:28:56 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2011-04-16 00:42:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
public class Worth implements IConf
|
|
|
|
{
|
|
|
|
private static final Logger logger = Logger.getLogger("Minecraft");
|
2011-06-01 10:40:12 +00:00
|
|
|
private final EssentialsConf config;
|
2011-04-16 00:42:43 +00:00
|
|
|
|
|
|
|
public Worth(File dataFolder)
|
|
|
|
{
|
|
|
|
config = new EssentialsConf(new File(dataFolder, "worth.yml"));
|
|
|
|
config.setTemplateName("/worth.yml");
|
|
|
|
config.load();
|
|
|
|
}
|
|
|
|
|
2013-05-05 03:13:17 +00:00
|
|
|
public BigDecimal getPrice(ItemStack itemStack)
|
2011-04-16 00:42:43 +00:00
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
String itemname = itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "");
|
2013-05-05 09:41:19 +00:00
|
|
|
BigDecimal result;
|
|
|
|
result = config.getBigDecimal("worth." + itemname + "." + itemStack.getDurability(), BigDecimal.ONE.negate());
|
2013-05-05 18:59:35 +00:00
|
|
|
if (result.signum() < 0)
|
2011-10-25 21:18:28 +00:00
|
|
|
{
|
2013-05-05 09:41:19 +00:00
|
|
|
result = config.getBigDecimal("worth." + itemname + ".0", BigDecimal.ONE.negate());
|
2011-04-16 14:39:31 +00:00
|
|
|
}
|
2013-05-05 18:59:35 +00:00
|
|
|
if (result.signum() < 0)
|
2011-10-25 21:18:28 +00:00
|
|
|
{
|
2013-05-05 09:41:19 +00:00
|
|
|
result = config.getBigDecimal("worth." + itemname, BigDecimal.ONE.negate());
|
2011-04-16 06:28:56 +00:00
|
|
|
}
|
2013-05-05 18:59:35 +00:00
|
|
|
if (result.signum() < 0)
|
2013-08-11 22:43:45 +00:00
|
|
|
{
|
|
|
|
result = config.getBigDecimal("worth." + itemStack.getTypeId() + "." + itemStack.getDurability(), BigDecimal.ONE.negate());
|
|
|
|
}
|
|
|
|
if (result.signum() < 0)
|
|
|
|
{
|
|
|
|
result = config.getBigDecimal("worth." + itemStack.getTypeId() + ".0", BigDecimal.ONE.negate());
|
|
|
|
}
|
|
|
|
if (result.signum() < 0)
|
|
|
|
{
|
|
|
|
result = config.getBigDecimal("worth." + itemStack.getTypeId(), BigDecimal.ONE.negate());
|
|
|
|
}
|
|
|
|
if (result.signum() < 0)
|
2011-10-25 21:18:28 +00:00
|
|
|
{
|
2013-05-05 09:41:19 +00:00
|
|
|
result = config.getBigDecimal("worth-" + itemStack.getTypeId(), BigDecimal.ONE.negate());
|
2011-04-16 06:28:56 +00:00
|
|
|
}
|
2013-05-05 18:59:35 +00:00
|
|
|
if (result.signum() < 0)
|
2013-05-05 03:13:17 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2013-05-05 09:41:19 +00:00
|
|
|
return result;
|
2011-04-16 00:42:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-14 00:03:08 +00:00
|
|
|
public int getAmount(IEssentials ess, User user, ItemStack is, String[] args, boolean isBulkSell) throws Exception
|
|
|
|
{
|
|
|
|
if (is == null || is.getType() == Material.AIR)
|
|
|
|
{
|
|
|
|
throw new Exception(_("itemSellAir"));
|
|
|
|
}
|
|
|
|
int id = is.getTypeId();
|
|
|
|
int amount = 0;
|
|
|
|
|
|
|
|
if (args.length > 1)
|
|
|
|
{
|
2013-07-14 00:07:59 +00:00
|
|
|
try {
|
|
|
|
amount = Integer.parseInt(args[1].replaceAll("[^0-9]", ""));
|
|
|
|
}
|
|
|
|
catch (NumberFormatException ex) {
|
|
|
|
throw new NotEnoughArgumentsException(ex);
|
|
|
|
}
|
2013-07-14 00:03:08 +00:00
|
|
|
if (args[1].startsWith("-"))
|
|
|
|
{
|
|
|
|
amount = -amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean stack = args.length > 1 && args[1].endsWith("s");
|
|
|
|
boolean requireStack = ess.getSettings().isTradeInStacks(id);
|
|
|
|
|
|
|
|
if (requireStack && !stack)
|
|
|
|
{
|
|
|
|
throw new Exception(_("itemMustBeStacked"));
|
|
|
|
}
|
|
|
|
|
|
|
|
int max = 0;
|
|
|
|
for (ItemStack s : user.getInventory().getContents())
|
|
|
|
{
|
|
|
|
if (s == null || !s.isSimilar(is))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
max += s.getAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack)
|
|
|
|
{
|
|
|
|
amount *= is.getType().getMaxStackSize();
|
|
|
|
}
|
|
|
|
if (amount < 1)
|
|
|
|
{
|
|
|
|
amount += max;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requireStack)
|
|
|
|
{
|
|
|
|
amount -= amount % is.getType().getMaxStackSize();
|
|
|
|
}
|
|
|
|
if (amount > max || amount < 1)
|
|
|
|
{
|
|
|
|
if (!isBulkSell)
|
|
|
|
{
|
|
|
|
user.sendMessage(_("itemNotEnough2"));
|
|
|
|
user.sendMessage(_("itemNotEnough3"));
|
|
|
|
throw new Exception(_("itemNotEnough1"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2011-04-16 06:28:56 +00:00
|
|
|
public void setPrice(ItemStack itemStack, double price)
|
2011-04-16 00:42:43 +00:00
|
|
|
{
|
2011-10-25 21:18:28 +00:00
|
|
|
if (itemStack.getType().getData() == null)
|
|
|
|
{
|
2011-11-21 01:55:26 +00:00
|
|
|
config.setProperty("worth." + itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""), price);
|
2011-10-25 21:18:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-16 14:39:31 +00:00
|
|
|
// Bukkit-bug: getDurability still contains the correct value, while getData().getData() is 0.
|
2011-11-21 01:55:26 +00:00
|
|
|
config.setProperty("worth." + itemStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", "") + "." + itemStack.getDurability(), price);
|
2011-04-16 06:28:56 +00:00
|
|
|
}
|
2011-10-25 21:18:28 +00:00
|
|
|
config.removeProperty("worth-" + itemStack.getTypeId());
|
2011-04-16 00:42:43 +00:00
|
|
|
config.save();
|
|
|
|
}
|
|
|
|
|
2011-11-18 18:07:28 +00:00
|
|
|
@Override
|
2011-04-16 00:42:43 +00:00
|
|
|
public void reloadConfig()
|
|
|
|
{
|
|
|
|
config.load();
|
|
|
|
}
|
|
|
|
}
|