mirror of
https://github.com/EssentialsX/Essentials.git
synced 2025-01-10 18:38:18 +01:00
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:
parent
766f0a9f13
commit
435219bab6
@ -64,7 +64,7 @@ public class Trade
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (getItemStack() != null
|
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("_", " ")));
|
throw new ChargeException(_("missingItems", getItemStack().getAmount(), getItemStack().getType().toString().toLowerCase(Locale.ENGLISH).replace("_", " ")));
|
||||||
}
|
}
|
||||||
@ -133,11 +133,11 @@ public class Trade
|
|||||||
}
|
}
|
||||||
if (getItemStack() != null)
|
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("_", " ")));
|
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();
|
user.updateInventory();
|
||||||
}
|
}
|
||||||
if (command != null && !command.isEmpty()
|
if (command != null && !command.isEmpty()
|
||||||
|
@ -156,7 +156,7 @@ 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);
|
||||||
InventoryWorkaround.removeItem(user.getInventory(), true, ris);
|
InventoryWorkaround.removeItem(user.getInventory(), true, true, ris);
|
||||||
user.updateInventory();
|
user.updateInventory();
|
||||||
Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(worth * amount, ess), user.getLocation(), ess);
|
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);
|
user.giveMoney(worth * amount);
|
||||||
|
@ -103,7 +103,7 @@ public class Commandunlimited extends EssentialsCommand
|
|||||||
{
|
{
|
||||||
message = "enableUnlimited";
|
message = "enableUnlimited";
|
||||||
enableUnlimited = true;
|
enableUnlimited = true;
|
||||||
if (!InventoryWorkaround.containsItem(target.getInventory(), true, stack))
|
if (!InventoryWorkaround.containsItem(target.getInventory(), true, true, stack))
|
||||||
{
|
{
|
||||||
target.getInventory().addItem(stack);
|
target.getInventory().addItem(stack);
|
||||||
}
|
}
|
||||||
|
@ -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();
|
final ItemStack[] inventory = cinventory.getContents();
|
||||||
for (int i = start; i < inventory.length; i++)
|
for (int i = start; i < inventory.length; i++)
|
||||||
@ -35,7 +35,7 @@ public final class InventoryWorkaround
|
|||||||
{
|
{
|
||||||
continue;
|
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;
|
return i;
|
||||||
}
|
}
|
||||||
@ -192,7 +192,7 @@ public final class InventoryWorkaround
|
|||||||
return leftover;
|
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>();
|
final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
||||||
|
|
||||||
@ -217,7 +217,7 @@ public final class InventoryWorkaround
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get first Item, ignore the amount
|
// 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
|
// Drat! we don't have this type in the inventory
|
||||||
if (first == -1)
|
if (first == -1)
|
||||||
@ -250,7 +250,7 @@ public final class InventoryWorkaround
|
|||||||
return leftover;
|
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>();
|
final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
||||||
|
|
||||||
@ -272,7 +272,7 @@ public final class InventoryWorkaround
|
|||||||
combined[j] = items[i].clone();
|
combined[j] = items[i].clone();
|
||||||
break;
|
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());
|
combined[j].setAmount(combined[j].getAmount() + items[i].getAmount());
|
||||||
break;
|
break;
|
||||||
@ -298,7 +298,7 @@ public final class InventoryWorkaround
|
|||||||
break;
|
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
|
// Drat! we don't have this type in the inventory
|
||||||
if (slot == -1)
|
if (slot == -1)
|
||||||
|
@ -7,6 +7,7 @@ import com.earth2me.essentials.User;
|
|||||||
import static com.earth2me.essentials.I18n._;
|
import static com.earth2me.essentials.I18n._;
|
||||||
import com.earth2me.essentials.Trade;
|
import com.earth2me.essentials.Trade;
|
||||||
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
|
||||||
|
import java.util.Locale;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
@ -22,11 +23,11 @@ public class SignEnchant extends EssentialsSign
|
|||||||
@Override
|
@Override
|
||||||
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
|
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(":");
|
final String[] enchantLevel = sign.getLine(2).split(":");
|
||||||
if (enchantLevel.length != 2)
|
if (enchantLevel.length != 2)
|
||||||
{
|
{
|
||||||
throw new SignException(_("invalidSignLine", 2));
|
throw new SignException(_("invalidSignLine", 3));
|
||||||
}
|
}
|
||||||
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
|
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
|
||||||
if (enchantment == null)
|
if (enchantment == null)
|
||||||
@ -49,7 +50,10 @@ public class SignEnchant extends EssentialsSign
|
|||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
stack.addEnchantment(enchantment, level);
|
if (stack != null)
|
||||||
|
{
|
||||||
|
stack.addEnchantment(enchantment, level);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Throwable ex)
|
catch (Throwable ex)
|
||||||
{
|
{
|
||||||
@ -62,42 +66,55 @@ public class SignEnchant extends EssentialsSign
|
|||||||
@Override
|
@Override
|
||||||
protected boolean onSignInteract(ISign sign, User player, String username, IEssentials ess) throws SignException, ChargeException
|
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;
|
int slot = -1;
|
||||||
final Trade charge = getTrade(sign, 3, ess);
|
final Trade charge = getTrade(sign, 3, ess);
|
||||||
charge.isAffordableFor(player);
|
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(":");
|
final String[] enchantLevel = sign.getLine(2).split(":");
|
||||||
if (enchantLevel.length != 2)
|
if (enchantLevel.length != 2)
|
||||||
{
|
{
|
||||||
throw new SignException(_("invalidSignLine", 2));
|
throw new SignException(_("invalidSignLine", 3));
|
||||||
}
|
}
|
||||||
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
|
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
|
||||||
if (enchantment == null)
|
if (enchantment == null)
|
||||||
{
|
{
|
||||||
throw new SignException(_("enchantmentNotFound"));
|
throw new SignException(_("enchantmentNotFound"));
|
||||||
}
|
}
|
||||||
|
int level;
|
||||||
final ItemStack toEnchant = player.getInventory().getItem(slot);
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
toEnchant.addEnchantment(enchantment, Integer.parseInt(enchantLevel[1]));
|
level = Integer.parseInt(enchantLevel[1]);
|
||||||
}
|
}
|
||||||
catch (NumberFormatException ex)
|
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);
|
charge.charge(player);
|
||||||
Trade.log("Sign", "Enchant", "Interact", username, charge, username, charge, sign.getBlock().getLocation(), ess);
|
Trade.log("Sign", "Enchant", "Interact", username, charge, username, charge, sign.getBlock().getLocation(), ess);
|
||||||
player.getInventory().setItem(slot, toEnchant);
|
|
||||||
player.updateInventory();
|
player.updateInventory();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user