.setDurability((short) 0); Issues

Discussion in 'Spigot Plugin Development' started by AaronL98, May 9, 2015.

  1. I made a custom repairing plugin a while ago, and the main core of it (repairing the actual tool) is rather glitchy.
    It will set the item durability to max, but what I've noticed is that there is a very unpredictable, rare "glitch". Sometimes, whenever a player mines a block with their newly repaired tool, it breaks instantly. As if it had 1 block hit left on it.
    This has resulted in many of my players moaning at me and im fed up of it lol.

    I'm currently doing the following to "repair" the tool:
    Code (Text):
                p.getItemInHand().setDurability((short) 0);
    Any help is appreciated.
  2. Setting the durability to 0 will break the item. Also, I don't see why you are using a short.

    Anyways, post your code so that we can see where the bug is happening.
  3. Here's my repairing method.

    Setting it to 0 seemed to work on all cases of testing it.
  4. Correct me if I'm wrong, but I believe it actually does it in the reverse direction, where 0 is fully repaired and, for example, 1562 would be broken for a diamond pickaxe; however, I haven't worked with ItemStacks in a while.
    • Like Like x 1
    • Agree Agree x 1
  5. My mistake, also haven't worked with itemstacks in a while. 0 does repair the item.
  6. From the information you've given, the code is working properly.

    Here's my guess as to what's happening: The player using the item to its literally 0 limit. So, even after repairing it with your plugin .. the durability doesn't update until the event fires at which point it may be too late. It's just a guess because I don't actually see your code.

    As to remedying your problem, why not just give the player a literal brand new item instead of taking the existing item and changing it?
    1. Get the item in the hand.
    2. Get the lore, enchants, and other meta, save them to objects.
    3. Delete the item in the hand
    4. Create a new item with that lore, enchants, etc
    5. Put the item in their hand.
    • Like Like x 1
  7. Hey Aaron!

    First, try removing your updateInventory();

    If that is not working try a different method. It should work btw.

    1. Create a method were you create a itemstack + gives it to you.

    2. Get the displayname, lore etc.

    3. Put that in your method and it gives a new Item with the same things as the item which is 'repaired'.
    • Like Like x 2
  8. when you repair, you need to use the damage value as the repairing value... it's very counter intuitive but that's what you need to do... I have to do the same thing in my TokenEnchant's repair function.
    • Like Like x 1
  9. No, you are wrong. 0 is the max durability of the item. Using player#.getItemInHand().setDurability((short)0); will work fine.
  11. fyte me :p
    • Friendly Friendly x 1
  12. Apologies for the large bump.
    I put @Svenna 's solution in place, but this still doesn't want to fix this bug. I'm having a lot of players complaining about disappearing pickaxes when mining just after repairing the pickaxe.

    Here's my method of giving a brand new, fresh item with the same metadata of the previous pickaxe which was repaired.
  13. Just a FYI, I coded the same plugin. Remove #updateInventory(). Also, I'm assuming you're trying the repair method without checking if the item in hand is a block or if it has no damage at all; add a check to both. You can fix the item without having to create a new one.
  14. Hi Aaron! I don't love spoonfeeding but yeah, here is what I mean:

    Code (Text):
        public void repair(Player p){
            ItemStack is = p.getItemInHand();
            ItemStack newis = new ItemStack(is.getType(), is.getAmount());
            ItemMeta meta = is.getItemMeta();
            if (is.hasItemMeta()){ // if statements to get no null pointers
                if (is.getItemMeta().hasDisplayName()) meta.setDisplayName(is.getItemMeta().getDisplayName());
                if (is.getItemMeta().hasLore())  meta.setLore(is.getItemMeta().getLore());
    • Informative Informative x 1
  15. Alright. And yeah, I am checking that the item being repaired is one of the following: Diamond pick, helmet, chestplate, leggings or boots. And also checking if it has been damaged.

    Thanks for this. I don't also really support it, but I'll give it a go, thanks.
  16. Large bump incoming again.
    Having this code running for ~20 days or so, and I'm still getting player complaints that their pickaxe keeps disappearing after repairing.

    Completely stumped and have no idea what is even causing this.
    Current code for giving new pickaxe:
    #16 AaronL98, Aug 10, 2015
    Last edited: Aug 10, 2015
  17. @AaronL98
    I ran into the same issue with setting durability however this seemed to work for me:

    Code (Text):
    ItemStack is = p.getItemInHand();
    is.setDurability((short) 0);
  18. using setDurability((short) 0) is what I initially had issues with ;/
  19. In your original post? Yeah but I tried doing that as well, using p.getItemInHand().setDurability(...); But once I had started doing what i said in my previous post it worked. Might be just me, But hey doesn't hurt to give it a shot.
  20. Ah I spot the difference now.
    But it's weird, because as it stands right now i'm literally giving a brand new item, where the durability should be full.