Wrap ItemDb accessors and move Item string matching to itemdb class.

This commit is contained in:
KHobbits 2013-07-13 18:52:53 +01:00
parent fc63f63de6
commit 5eb3d9fa42
5 changed files with 67 additions and 50 deletions

View file

@ -48,6 +48,7 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import net.ess3.api.IItemDb;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Server; import org.bukkit.Server;
@ -748,7 +749,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials
} }
@Override @Override
public ItemDb getItemDb() public IItemDb getItemDb()
{ {
return itemDb; return itemDb;
} }

View file

@ -4,6 +4,7 @@ import com.earth2me.essentials.metrics.Metrics;
import com.earth2me.essentials.perm.PermissionsHandler; import com.earth2me.essentials.perm.PermissionsHandler;
import com.earth2me.essentials.register.payment.Methods; import com.earth2me.essentials.register.payment.Methods;
import java.util.List; import java.util.List;
import net.ess3.api.IItemDb;
import net.ess3.api.IJails; import net.ess3.api.IJails;
import net.ess3.api.IWarps; import net.ess3.api.IWarps;
import org.bukkit.World; import org.bukkit.World;
@ -68,7 +69,7 @@ public interface IEssentials extends Plugin
void showError(final CommandSender sender, final Throwable exception, final String commandLabel); void showError(final CommandSender sender, final Throwable exception, final String commandLabel);
ItemDb getItemDb(); IItemDb getItemDb();
UserMap getUserMap(); UserMap getUserMap();

View file

@ -141,6 +141,42 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb
retval.setDurability(metaData); retval.setDurability(metaData);
return retval; 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) public String names(ItemStack item)
{ {

View file

@ -1,6 +1,8 @@
package com.earth2me.essentials.api; package com.earth2me.essentials.api;
import com.earth2me.essentials.User;
import java.util.List;
import org.bukkit.inventory.ItemStack; 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, final int quantity) throws Exception;
ItemStack get(final String name) throws Exception; ItemStack get(final String name) throws Exception;
public String names(ItemStack item);
List<ItemStack> getMatching(User user, String[] args) throws Exception;
} }

View file

@ -5,6 +5,7 @@ import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.NumberUtil;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.Material; import org.bukkit.Material;
@ -28,60 +29,31 @@ public class Commandsell extends EssentialsCommand
{ {
throw new NotEnoughArgumentsException(); throw new NotEnoughArgumentsException();
} }
ItemStack is = null; List<ItemStack> is = ess.getItemDb().getMatching(user, args);
if (args[0].equalsIgnoreCase("hand")) 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 (args[0].equalsIgnoreCase("blocks"))
{
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)
{ {
user.sendMessage(_("totalWorthBlocks", type, NumberUtil.displayCurrency(totalWorth, ess))); 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 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 //TODO: Prices for Enchantments
final ItemStack ris = is.clone(); final ItemStack ris = is.clone();
ris.setAmount(amount); ris.setAmount(amount);
if (!user.getInventory().containsAtLeast(ris, amount)) { if (!user.getInventory().containsAtLeast(ris, amount))
{
// This should never happen. // This should never happen.
throw new IllegalStateException("Trying to remove more items than are available."); throw new IllegalStateException("Trying to remove more items than are available.");
} }