Item durabilities not changing correctly

Discussion in 'Plugin Development' started by MrTwiggy, Mar 11, 2013.

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

    MrTwiggy

    Hey there! I need to be able to set up a scheduler that changes the durability of an item over time, like this:

    Code:
     Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(PLUGIN, new Runnable() 
    {
    @Override  
       public void run() 
       {
    if (item.getDurability() > 0)
    {
    short currentDurability = item.getDurability();
    gunItem.setDurability((short) currentDurability - 1);
    }
       }
    }, 0, 1);
    
    Where the item is an object from the players inventory or hotbar and is based in as a parameter. It IS getting called, and it IS changing the durability of the item reference it has, however, the player's item they hold is not being changed, even though I passed in the appropriate reference.

    Here's where things get weird. It works perfectly fine and normal with Bows, but not with other items that have durability, such as pickaxes, axes, spades, etc.

    Has anyone seen this before?
     
  2. Offline

    Technius

    When the item is retrieved with getItem(), the item returned is a cloned copy. When an item is set with setItem(), it item set is a cloned copy. In other words, you will have to find the ItemStack in the inventory and set the slot of the ItemStack to your modified ItemStack every time.
     
  3. Offline

    Digi

    MrTwiggy
    This is a very complicated thing you chose, because players can move items in chests, can throw them, can give them to other players and plugins can also take them from players...

    You'll need to mark your item with a special name or lore or something then every time the task runs you store the player that last had it and search for the item again and do your thing.
    When the item is not found you can still continue to drain durability in an imaginary way or you can just cancel the task.

    I recommend you use new BukkitRunnable() instead of getScheduler() thing because you can use cancel() method inside it and also use run...() from within it or at the back of it.
    See: http://wiki.bukkit.org/Scheduler_Programming
     
    chasechocolate likes this.
  4. Offline

    MrTwiggy

    I thought this could be it at first, however, this doesn't explain why it works for bows and not other items?

    Bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  5. Offline

    Technius

    Those items change within Minecraft's code. CraftBukkit creates a copy and "mirrors" it so that it's a Minecraft object. CraftBukkit directly references Minecraft's code.
     
Thread Status:
Not open for further replies.

Share This Page