Solved Trouble with removing item?

Discussion in 'Plugin Development' started by khave, Sep 12, 2015.

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

    khave

    Hello Bukkit.

    I'm having trouble removing an item. When I try to set the amount to 1 less than before it removes the entire inventory and I have no idea why:
    Code:
    public void updateItemDurability(Player player, ItemStack itemStack, int minusDurability) {
            if(itemStack != null && itemStack.hasItemMeta()) {
                ItemMeta itemMeta = itemStack.getItemMeta();
                List<String> lore = itemMeta.getLore();
                ItemManager itemManager = new ItemManager(MoreItemsItem.getIdentifierFromItem(itemStack));
                String[] dur = itemStack.getItemMeta().getLore().get(itemStack.getItemMeta().getLore().size() - 2).split("/");
                int durability = Integer.valueOf(dur[0].replace(ChatColor.DARK_GRAY + "Durability: " + ChatColor.RED, "").replace(ChatColor.DARK_GRAY + "", ""));
                int updatedDurability = durability - minusDurability;
                lore.set(itemStack.getItemMeta().getLore().size() - 2, ChatColor.DARK_GRAY + "Durability: " + ChatColor.RED + updatedDurability + ChatColor.DARK_GRAY + "/" + ChatColor.RED + itemManager.getDurability());
                //Set item durability if it's lower
                if (updatedDurability < itemStack.getType().getMaxDurability()) {
                    itemStack.setDurability((short) (itemStack.getType().getMaxDurability() - updatedDurability));
                }
    
                itemMeta.setLore(lore);
                itemStack.setItemMeta(itemMeta);
                if (updatedDurability <= 0) {
                    player.sendMessage(itemStack.getItemMeta().getDisplayName());
                    itemStack.setAmount(itemStack.getAmount() - 1);
                    player.playSound(player.getLocation(), Sound.ITEM_BREAK, 1.0f, 1.0f);
                }
            }
        }
    Furthermore when I use this code it simply doesn't do anything, the item break sound still works, so I am at a loss for what to do:

    Code:
      public void updateItemDurability(Player player, ItemStack itemStack, int minusDurability) {
            if(itemStack != null && itemStack.hasItemMeta()) {
                ItemMeta itemMeta = itemStack.getItemMeta();
                List<String> lore = itemMeta.getLore();
                ItemManager itemManager = new ItemManager(MoreItemsItem.getIdentifierFromItem(itemStack));
                String[] dur = itemStack.getItemMeta().getLore().get(itemStack.getItemMeta().getLore().size() - 2).split("/");
                int durability = Integer.valueOf(dur[0].replace(ChatColor.DARK_GRAY + "Durability: " + ChatColor.RED, "").replace(ChatColor.DARK_GRAY + "", ""));
                int updatedDurability = durability - minusDurability;
                if (updatedDurability <= 0) {
                    player.sendMessage(itemStack.getItemMeta().getDisplayName());
                    itemStack.setAmount(itemStack.getAmount() - 1);
    
                    player.playSound(player.getLocation(), Sound.ITEM_BREAK, 1.0f, 1.0f);
                }
                lore.set(itemStack.getItemMeta().getLore().size() - 2, ChatColor.DARK_GRAY + "Durability: " + ChatColor.RED + updatedDurability + ChatColor.DARK_GRAY + "/" + ChatColor.RED + itemManager.getDurability());
    
                if (updatedDurability < itemStack.getType().getMaxDurability()) {
                    itemStack.setDurability((short) (itemStack.getType().getMaxDurability() - updatedDurability));
                }
    
                itemMeta.setLore(lore);
                itemStack.setItemMeta(itemMeta);
            }
        }
     
  2. Offline

    Ruptur

    @khave
    You didnt put the item in the player's inventory. If the item is from the player's inventory then you have to do player.updateInventory() to show the new changes.
     
  3. Offline

    khave

    No no, the item is already in the inventory. I added that way before. Updating the inventory wouldn't work as all items get cleared. Removing the item doesn't work, even if I try to update the inventory.
     
  4. Offline

    Ruptur

    @khave
    From the code you've shown, the inventory should not get cleared.
    Perhaps its from something else.
    Can you show the bit where the method gets called
     
  5. Offline

    khave

    Yea here it is:
    Code:
    @EventHandler
        public void onEntityDamageByEntity(EntityDamageByEntityEvent e) {
            if (!(e.getDamager() instanceof Player)) return;
    
            Player player = (Player) e.getDamager();
    
    
            ItemStack itemStack = player.getItemInHand();
            ItemMeta itemMeta = itemStack.getItemMeta();
    
    
            //If CustomItem
            if (itemMeta != null && itemMeta.hasLore()) {
                //Get hidden ItemName
                String itemName = itemMeta.getLore().get(itemMeta.getLore().size() - 1).replaceAll("ยง", "");
                MoreItemsItem moreItemsItem = new MoreItemsItem(itemName);
                moreItemsItem.update(player, itemStack, 1);
            }
        }
    EDIT:
    Noticed I was calling the wrong method lol.
     
    Last edited: Sep 12, 2015
Thread Status:
Not open for further replies.

Share This Page