Add support for /worth all and /worth hand

This commit is contained in:
KHobbits 2013-07-14 01:03:08 +01:00
parent d044ba2fb9
commit a14104c529
21 changed files with 208 additions and 123 deletions

View File

@ -141,10 +141,12 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb
retval.setDurability(metaData);
return retval;
}
public List<ItemStack> getMatching (User user, String[] args) throws Exception {
@Override
public List<ItemStack> getMatching(User user, String[] args) throws Exception
{
List<ItemStack> is = new ArrayList<ItemStack>();
if (args[0].equalsIgnoreCase("hand"))
{
is.add(user.getItemInHand());
@ -158,7 +160,7 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb
continue;
}
is.add(stack);
}
}
}
else if (args[0].equalsIgnoreCase("blocks"))
{
@ -171,13 +173,18 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb
is.add(stack);
}
}
else
else if (args.length > 0)
{
is.add(get(args[0]));
}
else {
is.add(user.getItemInHand());
}
return is;
}
@Override
public String names(ItemStack item)
{
ItemData itemData = new ItemData(item.getTypeId(), item.getDurability());

View File

@ -1,9 +1,11 @@
package com.earth2me.essentials;
import static com.earth2me.essentials.I18n._;
import java.io.File;
import java.math.BigDecimal;
import java.util.Locale;
import java.util.logging.Logger;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
@ -43,6 +45,72 @@ public class Worth implements IConf
return result;
}
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)
{
amount = Integer.parseInt(args[1].replaceAll("[^0-9]", ""));
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;
}
public void setPrice(ItemStack itemStack, double price)
{
if (itemStack.getType().getData() == null)

View File

@ -8,7 +8,6 @@ import java.math.BigDecimal;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
@ -32,15 +31,31 @@ public class Commandsell extends EssentialsCommand
List<ItemStack> is = ess.getItemDb().getMatching(user, args);
int count = 0;
boolean isBulk = is.size() > 1;
for (ItemStack stack : is)
{
try
{
totalWorth = totalWorth.add(sellItem(user, stack, args, is.size() > 1));
count++;
if (stack.getAmount() > 0)
{
totalWorth = totalWorth.add(sellItem(user, stack, args, isBulk));
count++;
for (ItemStack zeroStack : is)
{
if (!zeroStack.equals(stack) && zeroStack.isSimilar(stack))
{
zeroStack.setAmount(0);
}
}
}
}
catch (Exception e)
{
if (!isBulk)
{
throw e;
}
}
}
if (count > 1 && totalWorth.signum() > 0)
@ -58,72 +73,21 @@ public class Commandsell extends EssentialsCommand
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;
}
}
int amount = ess.getWorth().getAmount(ess, user, is, args, isBulkSell);
BigDecimal worth = ess.getWorth().getPrice(is);
boolean stack = args.length > 1 && args[1].endsWith("s");
boolean requireStack = ess.getSettings().isTradeInStacks(id);
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));
if (amount == 0)
{
return result;
}
//TODO: Prices for Enchantments
final ItemStack ris = is.clone();
ris.setAmount(amount);

View File

@ -4,6 +4,7 @@ import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.NumberUtil;
import java.math.BigDecimal;
import java.util.List;
import java.util.Locale;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
@ -16,97 +17,108 @@ public class Commandworth extends EssentialsCommand
{
super("worth");
}
//TODO: Remove duplication
@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{
ItemStack iStack = user.getInventory().getItemInHand();
int amount = iStack.getAmount();
if (args.length > 0)
BigDecimal totalWorth = BigDecimal.ZERO;
String type = "";
List<ItemStack> is = ess.getItemDb().getMatching(user, args);
int count = 0;
boolean isBulk = is.size() > 1;
for (ItemStack stack : is)
{
iStack = ess.getItemDb().get(args[0]);
}
try
{
if (args.length > 1)
try
{
amount = Integer.parseInt(args[1]);
if (stack.getAmount() > 0)
{
totalWorth = totalWorth.add(itemWorth(user.getBase(), user, stack, args));
count++;
for (ItemStack zeroStack : is)
{
if (!zeroStack.equals(stack) && zeroStack.isSimilar(stack))
{
zeroStack.setAmount(0);
}
}
}
}
catch (Exception e)
{
if (!isBulk)
{
throw e;
}
}
}
catch (NumberFormatException ex)
if (count > 1 && totalWorth.signum() > 0)
{
amount = iStack.getType().getMaxStackSize();
if (args[0].equalsIgnoreCase("blocks"))
{
user.sendMessage(_("totalSellableBlocks", type, NumberUtil.displayCurrency(totalWorth, ess)));
}
else
{
user.sendMessage(_("totalSellableAll", type, NumberUtil.displayCurrency(totalWorth, ess)));
}
}
iStack.setAmount(amount);
final BigDecimal worth = ess.getWorth().getPrice(iStack);
if (worth == null)
{
throw new Exception(_("itemCannotBeSold"));
}
final BigDecimal result = worth.multiply(BigDecimal.valueOf(amount));
user.sendMessage(iStack.getDurability() != 0
? _("worthMeta",
iStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""),
iStack.getDurability(),
NumberUtil.displayCurrency(result, ess),
amount,
NumberUtil.displayCurrency(worth, ess))
: _("worth",
iStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""),
NumberUtil.displayCurrency(result, ess),
amount,
NumberUtil.displayCurrency(worth, ess)));
}
@Override
protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
public void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
{
String type = "";
if (args.length < 1)
{
throw new NotEnoughArgumentsException();
}
ItemStack iStack = ess.getItemDb().get(args[0]);
int amount = iStack.getAmount();
try
ItemStack stack = ess.getItemDb().get(args[0]);
itemWorth(sender, null, stack, args);
}
private BigDecimal itemWorth(CommandSender sender, User user, ItemStack is, String[] args) throws Exception
{
int amount = 1;
if (user == null)
{
if (args.length > 1)
{
amount = Integer.parseInt(args[1]);
amount = Integer.parseInt(args[1].replaceAll("[^0-9]", ""));
}
}
catch (NumberFormatException ex)
else
{
amount = iStack.getType().getMaxStackSize();
amount = ess.getWorth().getAmount(ess, user, is, args, true);
}
iStack.setAmount(amount);
final BigDecimal worth = ess.getWorth().getPrice(iStack);
BigDecimal worth = ess.getWorth().getPrice(is);
if (worth == null)
{
throw new Exception(_("itemCannotBeSold"));
}
final BigDecimal result = worth.multiply(BigDecimal.valueOf(amount));
sender.sendMessage(iStack.getDurability() != 0
BigDecimal result = worth.multiply(BigDecimal.valueOf(amount));
sender.sendMessage(is.getDurability() != 0
? _("worthMeta",
iStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""),
iStack.getDurability(),
is.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""),
is.getDurability(),
NumberUtil.displayCurrency(result, ess),
amount,
NumberUtil.displayCurrency(worth, ess))
: _("worth",
iStack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""),
is.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""),
NumberUtil.displayCurrency(result, ess),
amount,
NumberUtil.displayCurrency(worth, ess)));
return result;
}
}

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -526,3 +526,5 @@ inventoryClearFail=\u00a74Hrac {0} \u00a74nema\u00a7c {1} \u00a74z\u00a7c {2}\u0
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Alle\u00a7c {0} \u00a76von {1} \u00a76entfernt.
inventoryClearingStack=\u00a7c {0} {1} \u00a76von {2} \u00a76entfernt.
inventoryClearFail=\u00a74Spieler {0} \u00a74hat keine\u00a7c {1} {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Tous les\u00a7c {0} \u00a76de l''inventaire de
inventoryClearingStack=\u00a7c{0} \u00a7c {1} \u00a76ont \u00e9t\u00e9 supprim\u00e9s de l''inventaire de {2}\u00a76.
inventoryClearFail=\u00a74Le joueur {0} \u00a74n''a pas\u00a7c {1}\u00a7c {2}\u00a74 sur lui.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Alle\u00a7c {0} \u00a76van {1}\u00a76 is verwij
inventoryClearingStack=\u00a76\u00a7c {0} \u00a76stuks\u00a7c {1} \u00a76zijn verwijderd uit de inventaris van {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74heeft geen\u00a7c {1} \u00a74stuks\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a77Wyczyszczono wszystko\u00a7c {0} \u00a77od {1}\
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Rensade alla\u00a7c {0} \u00a76fr\u00e5n {1}\u0
inventoryClearingStack=\u00a76Tog bort \u00a7c {0} \u00a76av\u00a7c {1} \u00a76fr\u00e5n {2}\u00a76.
inventoryClearFail=\u00a74Spelaren {0} \u00a74har inte \u00a7c {1} \u00a74av\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.

View File

@ -519,3 +519,5 @@ inventoryClearingAllStack=\u00a76Cleared all\u00a7c {0} \u00a76from {1}\u00a76.
inventoryClearingStack=\u00a76Removed\u00a7c {0} \u00a76of\u00a7c {1} \u00a76from {2}\u00a76.
inventoryClearFail=\u00a74Player {0} \u00a74does not have\u00a7c {1} \u00a74of\u00a7c {2}\u00a74.
localNoOne=
totalSellableAll=\u00a7aThe total worth of all sellable items and blocks is \u00a7c{1}\u00a7a.
totalSellableBlocks=\u00a7aThe total worth of all sellable blocks is \u00a7c{1}\u00a7a.