diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index b79d95622..90d9ce4ed 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -48,6 +48,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; +import net.ess3.api.IItemDb; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Server; @@ -748,7 +749,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials } @Override - public ItemDb getItemDb() + public IItemDb getItemDb() { return itemDb; } diff --git a/Essentials/src/com/earth2me/essentials/IEssentials.java b/Essentials/src/com/earth2me/essentials/IEssentials.java index f45f15b53..7e2ad65a4 100644 --- a/Essentials/src/com/earth2me/essentials/IEssentials.java +++ b/Essentials/src/com/earth2me/essentials/IEssentials.java @@ -4,6 +4,7 @@ import com.earth2me.essentials.metrics.Metrics; import com.earth2me.essentials.perm.PermissionsHandler; import com.earth2me.essentials.register.payment.Methods; import java.util.List; +import net.ess3.api.IItemDb; import net.ess3.api.IJails; import net.ess3.api.IWarps; import org.bukkit.World; @@ -68,7 +69,7 @@ public interface IEssentials extends Plugin void showError(final CommandSender sender, final Throwable exception, final String commandLabel); - ItemDb getItemDb(); + IItemDb getItemDb(); UserMap getUserMap(); diff --git a/Essentials/src/com/earth2me/essentials/ItemDb.java b/Essentials/src/com/earth2me/essentials/ItemDb.java index 3bf49d836..8c4ce1603 100644 --- a/Essentials/src/com/earth2me/essentials/ItemDb.java +++ b/Essentials/src/com/earth2me/essentials/ItemDb.java @@ -141,6 +141,42 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb retval.setDurability(metaData); return retval; } + + public List getMatching (User user, String[] args) throws Exception { + List is = new ArrayList(); + + if (args[0].equalsIgnoreCase("hand")) + { + is.add(user.getItemInHand()); + } + else if (args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("invent") || args[0].equalsIgnoreCase("all")) + { + for (ItemStack stack : user.getInventory().getContents()) + { + if (stack == null || stack.getType() == Material.AIR) + { + continue; + } + is.add(stack); + } + } + else if (args[0].equalsIgnoreCase("blocks")) + { + for (ItemStack stack : user.getInventory().getContents()) + { + if (stack == null || stack.getTypeId() > 255 || stack.getType() == Material.AIR) + { + continue; + } + is.add(stack); + } + } + else + { + is.add(get(args[0])); + } + return is; + } public String names(ItemStack item) { diff --git a/Essentials/src/com/earth2me/essentials/api/IItemDb.java b/Essentials/src/com/earth2me/essentials/api/IItemDb.java index 678b5c7b0..063502fad 100644 --- a/Essentials/src/com/earth2me/essentials/api/IItemDb.java +++ b/Essentials/src/com/earth2me/essentials/api/IItemDb.java @@ -1,6 +1,8 @@ package com.earth2me.essentials.api; +import com.earth2me.essentials.User; +import java.util.List; import org.bukkit.inventory.ItemStack; @@ -9,4 +11,8 @@ public interface IItemDb ItemStack get(final String name, final int quantity) throws Exception; ItemStack get(final String name) throws Exception; + + public String names(ItemStack item); + + List getMatching(User user, String[] args) throws Exception; } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandsell.java b/Essentials/src/com/earth2me/essentials/commands/Commandsell.java index a52c5d982..3a8f5b9fa 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandsell.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandsell.java @@ -5,6 +5,7 @@ import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import com.earth2me.essentials.utils.NumberUtil; import java.math.BigDecimal; +import java.util.List; import java.util.Locale; import java.util.logging.Level; import org.bukkit.Material; @@ -28,60 +29,31 @@ public class Commandsell extends EssentialsCommand { throw new NotEnoughArgumentsException(); } - ItemStack is = null; - if (args[0].equalsIgnoreCase("hand")) + List is = ess.getItemDb().getMatching(user, args); + int count = 0; + + for (ItemStack stack : is) { - is = user.getItemInHand(); + try + { + totalWorth = totalWorth.add(sellItem(user, stack, args, is.size() > 1)); + count++; + } + catch (Exception e) + { + } } - else if (args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("invent") || args[0].equalsIgnoreCase("all")) + if (count > 1 && totalWorth.signum() > 0) { - for (ItemStack stack : user.getInventory().getContents()) - { - if (stack == null || stack.getType() == Material.AIR) - { - continue; - } - try - { - totalWorth = totalWorth.add(sellItem(user, stack, args, true)); - } - catch (Exception e) - { - } - } - if (totalWorth.signum() > 0) - { - user.sendMessage(_("totalWorthAll", type, NumberUtil.displayCurrency(totalWorth, ess))); - } - return; - } - else if (args[0].equalsIgnoreCase("blocks")) - { - for (ItemStack stack : user.getInventory().getContents()) - { - if (stack == null || stack.getTypeId() > 255 || stack.getType() == Material.AIR) - { - continue; - } - try - { - totalWorth = totalWorth.add(sellItem(user, stack, args, true)); - } - catch (Exception e) - { - } - } - if (totalWorth.signum() > 0) + if (args[0].equalsIgnoreCase("blocks")) { user.sendMessage(_("totalWorthBlocks", type, NumberUtil.displayCurrency(totalWorth, ess))); } - return; + else + { + user.sendMessage(_("totalWorthAll", type, NumberUtil.displayCurrency(totalWorth, ess))); + } } - if (is == null) - { - is = ess.getItemDb().get(args[0]); - } - sellItem(user, is, args, false); } private BigDecimal sellItem(User user, ItemStack is, String[] args, boolean isBulkSell) throws Exception @@ -155,7 +127,8 @@ public class Commandsell extends EssentialsCommand //TODO: Prices for Enchantments final ItemStack ris = is.clone(); ris.setAmount(amount); - if (!user.getInventory().containsAtLeast(ris, amount)) { + if (!user.getInventory().containsAtLeast(ris, amount)) + { // This should never happen. throw new IllegalStateException("Trying to remove more items than are available."); }