Fix Enchant sign, the user has to hold the item in hand now. A * or "any" in the second line allows to enchant all possible items.

This commit is contained in:
snowleo 2011-12-07 14:23:56 +01:00
parent 766f0a9f13
commit 435219bab6
5 changed files with 49 additions and 32 deletions

View File

@ -64,7 +64,7 @@ public class Trade
}
if (getItemStack() != null
&& !InventoryWorkaround.containsItem(user.getInventory(), true, itemStack))
&& !InventoryWorkaround.containsItem(user.getInventory(), true, true, itemStack))
{
throw new ChargeException(_("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " ")));
}
@ -133,11 +133,11 @@ public class Trade
}
if (getItemStack() != null)
{
if (!InventoryWorkaround.containsItem(user.getInventory(), true, itemStack))
if (!InventoryWorkaround.containsItem(user.getInventory(), true, true, itemStack))
{
throw new ChargeException(_("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " ")));
}
InventoryWorkaround.removeItem(user.getInventory(), true, getItemStack());
InventoryWorkaround.removeItem(user.getInventory(), true, true, getItemStack());
user.updateInventory();
}
if (command != null && !command.isEmpty()

View File

@ -156,7 +156,7 @@ public class Commandsell extends EssentialsCommand
//TODO: Prices for Enchantments
final ItemStack ris = is.clone();
ris.setAmount(amount);
InventoryWorkaround.removeItem(user.getInventory(), true, ris);
InventoryWorkaround.removeItem(user.getInventory(), true, true, ris);
user.updateInventory();
Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(worth * amount, ess), user.getLocation(), ess);
user.giveMoney(worth * amount);

View File

@ -103,7 +103,7 @@ public class Commandunlimited extends EssentialsCommand
{
message = "enableUnlimited";
enableUnlimited = true;
if (!InventoryWorkaround.containsItem(target.getInventory(), true, stack))
if (!InventoryWorkaround.containsItem(target.getInventory(), true, true, stack))
{
target.getInventory().addItem(stack);
}

View File

@ -20,12 +20,12 @@ public final class InventoryWorkaround
{
}
public static int first(final Inventory inventory, final ItemStack item, final boolean forceDurability, final boolean forceAmount)
public static int first(final Inventory inventory, final ItemStack item, final boolean forceDurability, final boolean forceAmount, final boolean forceEnchantments)
{
return next(inventory, item, 0, forceDurability, forceAmount);
return next(inventory, item, 0, forceDurability, forceAmount, forceEnchantments);
}
public static int next(final Inventory cinventory, final ItemStack item, final int start, final boolean forceDurability, final boolean forceAmount)
public static int next(final Inventory cinventory, final ItemStack item, final int start, final boolean forceDurability, final boolean forceAmount, final boolean forceEnchantments)
{
final ItemStack[] inventory = cinventory.getContents();
for (int i = start; i < inventory.length; i++)
@ -35,7 +35,7 @@ public final class InventoryWorkaround
{
continue;
}
if (item.getTypeId() == cItem.getTypeId() && (!forceAmount || item.getAmount() == cItem.getAmount()) && (!forceDurability || cItem.getDurability() == item.getDurability()) && cItem.getEnchantments().equals(item.getEnchantments()))
if (item.getTypeId() == cItem.getTypeId() && (!forceAmount || item.getAmount() == cItem.getAmount()) && (!forceDurability || cItem.getDurability() == item.getDurability()) && (!forceEnchantments || cItem.getEnchantments().equals(item.getEnchantments())))
{
return i;
}
@ -192,7 +192,7 @@ public final class InventoryWorkaround
return leftover;
}
public static Map<Integer, ItemStack> removeItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items)
public static Map<Integer, ItemStack> removeItem(final Inventory cinventory, final boolean forceDurability, final boolean forceEnchantments, final ItemStack... items)
{
final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
@ -217,7 +217,7 @@ public final class InventoryWorkaround
}
// get first Item, ignore the amount
final int first = first(cinventory, item, forceDurability, false);
final int first = first(cinventory, item, forceDurability, false, forceEnchantments);
// Drat! we don't have this type in the inventory
if (first == -1)
@ -250,7 +250,7 @@ public final class InventoryWorkaround
return leftover;
}
public static boolean containsItem(final Inventory cinventory, final boolean forceDurability, final ItemStack... items)
public static boolean containsItem(final Inventory cinventory, final boolean forceDurability, final boolean forceEnchantments, final ItemStack... items)
{
final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
@ -272,7 +272,7 @@ public final class InventoryWorkaround
combined[j] = items[i].clone();
break;
}
if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability()) && combined[j].getEnchantments().equals(items[i].getEnchantments()))
if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability()) && (!forceEnchantments || combined[j].getEnchantments().equals(items[i].getEnchantments())))
{
combined[j].setAmount(combined[j].getAmount() + items[i].getAmount());
break;
@ -298,7 +298,7 @@ public final class InventoryWorkaround
break;
}
final int slot = next(cinventory, item, position, forceDurability, false);
final int slot = next(cinventory, item, position, forceDurability, false, forceEnchantments);
// Drat! we don't have this type in the inventory
if (slot == -1)

View File

@ -7,6 +7,7 @@ import com.earth2me.essentials.User;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
import java.util.Locale;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
@ -22,11 +23,11 @@ public class SignEnchant extends EssentialsSign
@Override
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
{
final ItemStack stack = getItemStack(sign.getLine(1), 1, ess);
final ItemStack stack = sign.getLine(1).equals("*") || sign.getLine(1).equalsIgnoreCase("any") ? null : getItemStack(sign.getLine(1), 1, ess);
final String[] enchantLevel = sign.getLine(2).split(":");
if (enchantLevel.length != 2)
{
throw new SignException(_("invalidSignLine", 2));
throw new SignException(_("invalidSignLine", 3));
}
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
if (enchantment == null)
@ -49,7 +50,10 @@ public class SignEnchant extends EssentialsSign
}
try
{
stack.addEnchantment(enchantment, level);
if (stack != null)
{
stack.addEnchantment(enchantment, level);
}
}
catch (Throwable ex)
{
@ -62,42 +66,55 @@ public class SignEnchant extends EssentialsSign
@Override
protected boolean onSignInteract(ISign sign, User player, String username, IEssentials ess) throws SignException, ChargeException
{
final ItemStack search = getItemStack(sign.getLine(1), 1, ess);
final ItemStack search = sign.getLine(1).equals("*") || sign.getLine(1).equalsIgnoreCase("any") ? null : getItemStack(sign.getLine(1), 1, ess);
int slot = -1;
final Trade charge = getTrade(sign, 3, ess);
charge.isAffordableFor(player);
if (InventoryWorkaround.containsItem(player.getInventory(), false, search))
{
slot = InventoryWorkaround.first(player.getInventory(), search, false, true);
}
if (slot == -1)
{
throw new SignException(_("missingItems", 1, search.toString()));
}
final String[] enchantLevel = sign.getLine(2).split(":");
if (enchantLevel.length != 2)
{
throw new SignException(_("invalidSignLine", 2));
throw new SignException(_("invalidSignLine", 3));
}
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
if (enchantment == null)
{
throw new SignException(_("enchantmentNotFound"));
}
final ItemStack toEnchant = player.getInventory().getItem(slot);
int level;
try
{
toEnchant.addEnchantment(enchantment, Integer.parseInt(enchantLevel[1]));
level = Integer.parseInt(enchantLevel[1]);
}
catch (NumberFormatException ex)
{
toEnchant.addEnchantment(enchantment, enchantment.getMaxLevel());
level = enchantment.getMaxLevel();
}
final ItemStack playerHand = player.getItemInHand();
if (playerHand == null
|| playerHand.getAmount() != 1
|| (playerHand.containsEnchantment(enchantment)
&& playerHand.getEnchantmentLevel(enchantment) == level))
{
throw new SignException(_("missingItems", 1, sign.getLine(1)));
}
if (search != null && playerHand.getType() != search.getType())
{
throw new SignException(_("missingItems", 1, search.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ')));
}
final ItemStack toEnchant = playerHand;
try
{
toEnchant.addEnchantment(enchantment, level);
}
catch (Exception ex)
{
throw new SignException(ex.getMessage(), ex);
}
charge.charge(player);
Trade.log("Sign", "Enchant", "Interact", username, charge, username, charge, sign.getBlock().getLocation(), ess);
player.getInventory().setItem(slot, toEnchant);
player.updateInventory();
return true;
}