summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKHobbits <rob@khobbits.co.uk>2013-07-13 18:52:53 +0100
committerKHobbits <rob@khobbits.co.uk>2013-07-13 18:52:53 +0100
commit5eb3d9fa42838f006a329f311a522aeb8fdd1d63 (patch)
treee5b40c0a66aca0ba51e9b1e0d7f1a789205904a5
parentfc63f63de6ada7d44ab0fcd77b982ff203f19c40 (diff)
downloadEssentials-5eb3d9fa42838f006a329f311a522aeb8fdd1d63.tar
Essentials-5eb3d9fa42838f006a329f311a522aeb8fdd1d63.tar.gz
Essentials-5eb3d9fa42838f006a329f311a522aeb8fdd1d63.tar.lz
Essentials-5eb3d9fa42838f006a329f311a522aeb8fdd1d63.tar.xz
Essentials-5eb3d9fa42838f006a329f311a522aeb8fdd1d63.zip
Wrap ItemDb accessors and move Item string matching to itemdb class.
-rw-r--r--Essentials/src/com/earth2me/essentials/Essentials.java3
-rw-r--r--Essentials/src/com/earth2me/essentials/IEssentials.java3
-rw-r--r--Essentials/src/com/earth2me/essentials/ItemDb.java36
-rw-r--r--Essentials/src/com/earth2me/essentials/api/IItemDb.java6
-rw-r--r--Essentials/src/com/earth2me/essentials/commands/Commandsell.java59
5 files changed, 62 insertions, 45 deletions
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<ItemStack> getMatching (User user, String[] args) throws Exception {
+ List<ItemStack> is = new ArrayList<ItemStack>();
+
+ 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<ItemStack> 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"))
- {
- is = user.getItemInHand();
- }
- else if (args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("invent") || args[0].equalsIgnoreCase("all"))
+ List<ItemStack> is = ess.getItemDb().getMatching(user, args);
+ int count = 0;
+
+ for (ItemStack stack : is)
{
- for (ItemStack stack : user.getInventory().getContents())
+ try
{
- if (stack == null || stack.getType() == Material.AIR)
- {
- continue;
- }
- try
- {
- totalWorth = totalWorth.add(sellItem(user, stack, args, true));
- }
- catch (Exception e)
- {
- }
+ totalWorth = totalWorth.add(sellItem(user, stack, args, is.size() > 1));
+ count++;
}
- if (totalWorth.signum() > 0)
+ catch (Exception e)
{
- user.sendMessage(_("totalWorthAll", type, NumberUtil.displayCurrency(totalWorth, ess)));
}
- return;
}
- else if (args[0].equalsIgnoreCase("blocks"))
+ if (count > 1 && totalWorth.signum() > 0)
{
- for (ItemStack stack : user.getInventory().getContents())
+ if (args[0].equalsIgnoreCase("blocks"))
{
- if (stack == null || stack.getTypeId() > 255 || stack.getType() == Material.AIR)
- {
- continue;
- }
- try
- {
- totalWorth = totalWorth.add(sellItem(user, stack, args, true));
- }
- catch (Exception e)
- {
- }
+ user.sendMessage(_("totalWorthBlocks", type, NumberUtil.displayCurrency(totalWorth, ess)));
}
- if (totalWorth.signum() > 0)
+ else
{
- user.sendMessage(_("totalWorthBlocks", type, NumberUtil.displayCurrency(totalWorth, ess)));
+ user.sendMessage(_("totalWorthAll", type, NumberUtil.displayCurrency(totalWorth, ess)));
}
- return;
}
- 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.");
}