Solved Lore not updating

Discussion in 'Plugin Development' started by bwfcwalshy, Mar 17, 2015.

Thread Status:
Not open for further replies.
  1. Hello everyone, I am working on a big wand plugin. The last thing for me to do is finish of the API and get durabilities done. I have done a damage method which gets the durability and -1's and then tries to reset the lore. The issue is that the code seems to work fine but when I look at the wand ingame my durability is 20 still.. I have done debugs and when I update the inventory it seems to reset to 20.

    Code
    Code:
    public void setDurability(Player player, Wand wand, int durability) {
            if(wand.getItemStack().hasItemMeta() && wand.getItemStack().getItemMeta().hasLore()){
                ItemStack is = wand.getItemStack();
                ItemMeta im = is.getItemMeta();
                List<String> lore = im.getLore();
                Messages.log("Lore: " + lore);
                lore.remove(0);
                Messages.log("Lore: " + lore);
                if(lore.size() >= 1){
                    lore.set(0, ChatColor.GRAY + "" + ChatColor.BOLD + "Durability: " + durability);
                    Messages.log("Lore: " + lore);
                }else{
                    lore.add(ChatColor.GRAY + "" + ChatColor.BOLD + "Durability: " + durability);
                    Messages.log("Lore: " + lore);
                }
                im.setLore(lore);
                is.setItemMeta(im);
                Messages.log("ItemMeta: " + is.getItemMeta().getLore());
               
                player.updateInventory();
                Messages.log("Updated inventory");
                Messages.log("Lore: " + player.getItemInHand().getItemMeta().getLore());
            }
        }
    Debug results
    Code:
    [19:12:37 INFO]: Lore: [º7ºlDurability: 20]
    [19:12:37 INFO]: Lore: []
    [19:12:37 INFO]: Lore: [º7ºlDurability: 19]
    [19:12:37 INFO]: ItemMeta: [º7ºlDurability: 19]
    [19:12:37 INFO]: Updated inventory
    [19:12:37 INFO]: Lore: [º7ºlDurability: 20]
     
  2. Offline

    Funergy

    @bwfcwalshy You are sure you are editing the itemstack from the player's inventory?
     
  3. Offline

    Funergy

    @bwfcwalshy It seems that you are only editing the ItemStack from wand.getItemStack();
    Can you show me the line of getItemStack() in the Wand class?
     
  4. Offline

    Totom3

    @bwfcwalshy I'm not sure about that, but I noticed CraftBukkit's method have a tendency to return cloned instances of objects related to ItemStacks, instead of returning the original ones. Perhaps you need to set the item with player.setItemInHand()? Or wand.setItemStack() ?
     
  5. Offline

    xTigerRebornx

  6. Offline

    Konato_K

    @bwfcwalshy You're not setting back the wand to the player
     
  7. @bwfcwalshy
    Well, the lore isn`t updating because you are not setting back the item, as @Konato_K said. Just get the HeldItemSlot and set it`s item to the new one.
    Another bug too is that you are removing the line 0 and then setting the line 0 to the new durability. That will work if you have 1 lore, but imagine you have 3 lines. It will result on this:
    Without updates:
    Durability
    This is a Lore
    This is another lore

    Update 1:
    Durability
    This is another lore

    Update 2+:
    Durability

    To fix this just remove the 'lore.remove(0);'
     
  8. Offline

    Funergy

    @bwfcwalshy Did you fix it? Since you are not replying on any of this messages. Mind setting the thread to solved if its fixed :p
     
  9. @Funergy I was busy then asleep :p

    I am redoing it which should fix the issue. I will set this as solved unless it still throws weird NPE's

    EDIT: Yeah fixed, I feel like such a noob right now.

    Locked.
     
    Last edited: Mar 18, 2015
    Funergy likes this.
Thread Status:
Not open for further replies.

Share This Page