Solved Removing items with Enchantment data.

Discussion in 'Plugin Development' started by elementalgodz11, Jul 6, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    elementalgodz11

    What I'm trying to do is remove an item based on a string. Which I have got it working, but if the item has an enchantment it will still not remove the itemstack

    Here is how I am getting the ItemStack, this works fine.
    Code:
    ItemStack stack = plugin.getMaterialUtil().parseStack(args[1]);
    I use the following method to check if they have sufficient amount by looping through all the contents in the inventory, this does not happen as it ignores the Enchantment data.
    Code:
    if (getAmountOfItemInInventory(player.getInventory(), stack) < 1) {
    player.sendMessage(ChatColor.RED + "You don't have any " + name + "!");
    return true;
    }
    Finally I remove the item using:
    Code:
    player.getInventory().removeItem(stack);
    this is the part that's not working as the stack doesn't have the Enchantment data.

    What I have thought about doing is cloning the inventory, removing all enchantments in that cloned inventory, apply what I need to do, and then revert back to the old inventory.

    Any idea's?

    ended up writing my own method as a workaround for this, adding the Enchantments durability and data to the input from the inventory contents so the item is found:

    Code:java
    1. public void removeWorkaround(ItemStack stack, PlayerInventory inventory) {
    2.  
    3. int amount = stack.getAmount();
    4.  
    5. for (int i = 0; i < amount; i++) {
    6.  
    7. for (ItemStack contents : inventory.getContents()) {
    8.  
    9. if (contents != null && contents.getAmount() > 0 && contents.getType() == stack.getType() && contents.getDurability() == stack.getDurability()) {
    10.  
    11. ItemStack newStack = contents.clone();
    12. newStack.setAmount(1);
    13. inventory.removeItem(newStack);
    14. break;
    15.  
    16. }
    17.  
    18. }
    19.  
    20. }
    21.  
    22. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page