TF-EssentialsX/Essentials/src/com/earth2me/essentials/commands/Commandsell.java

144 lines
3.7 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
2013-06-08 21:31:19 +00:00
import com.earth2me.essentials.utils.NumberUtil;
2013-05-05 03:13:17 +00:00
import java.math.BigDecimal;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import org.bukkit.Material;
2011-11-18 17:42:26 +00:00
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
public class Commandsell extends EssentialsCommand
{
public Commandsell()
{
super("sell");
}
@Override
2011-12-01 13:42:39 +00:00
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
2013-05-05 03:13:17 +00:00
BigDecimal totalWorth = BigDecimal.ZERO;
String type = "";
if (args.length < 1)
{
throw new NotEnoughArgumentsException();
}
List<ItemStack> is = ess.getItemDb().getMatching(user, args);
int count = 0;
for (ItemStack stack : is)
{
try
{
totalWorth = totalWorth.add(sellItem(user, stack, args, is.size() > 1));
count++;
}
catch (Exception e)
{
}
}
if (count > 1 && totalWorth.signum() > 0)
{
if (args[0].equalsIgnoreCase("blocks"))
{
user.sendMessage(_("totalWorthBlocks", type, NumberUtil.displayCurrency(totalWorth, ess)));
}
else
{
user.sendMessage(_("totalWorthAll", type, NumberUtil.displayCurrency(totalWorth, ess)));
}
}
}
2013-05-05 03:13:17 +00:00
private BigDecimal sellItem(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)
{
amount = Integer.parseInt(args[1].replaceAll("[^0-9]", ""));
if (args[1].startsWith("-"))
{
amount = -amount;
}
}
2013-05-05 03:13:17 +00:00
BigDecimal worth = ess.getWorth().getPrice(is);
boolean stack = args.length > 1 && args[1].endsWith("s");
boolean requireStack = ess.getSettings().isTradeInStacks(id);
2013-05-05 03:13:17 +00:00
if (worth == null)
{
throw new Exception(_("itemCannotBeSold"));
}
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(_("itemNotEnough1"));
user.sendMessage(_("itemNotEnough2"));
throw new Exception(_("itemNotEnough3"));
}
else
{
return worth.multiply(BigDecimal.valueOf(amount));
}
}
BigDecimal result = worth.multiply(BigDecimal.valueOf(amount));
//TODO: Prices for Enchantments
final ItemStack ris = is.clone();
ris.setAmount(amount);
if (!user.getInventory().containsAtLeast(ris, amount))
{
// This should never happen.
throw new IllegalStateException("Trying to remove more items than are available.");
}
user.getInventory().removeItem(ris);
user.updateInventory();
2013-05-05 03:13:17 +00:00
Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(result, ess), user.getLocation(), ess);
user.giveMoney(result);
2013-06-08 21:31:19 +00:00
user.sendMessage(_("itemSold", NumberUtil.displayCurrency(result, ess), amount, is.getType().toString().toLowerCase(Locale.ENGLISH), NumberUtil.displayCurrency(worth, ess)));
logger.log(Level.INFO, _("itemSoldConsole", user.getDisplayName(), is.getType().toString().toLowerCase(Locale.ENGLISH), NumberUtil.displayCurrency(result, ess), amount, NumberUtil.displayCurrency(worth, ess)));
2013-05-05 03:13:17 +00:00
return result;
}
}