Setting Item Durability

Discussion in 'Plugin Development' started by Signatured, Mar 27, 2015.

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

    Signatured

    I'm trying to repair my pickaxe every time it breaks a block. I have researched many ways to do this, but none have worked so far. Here is my current EventHandler:

    Code:java
    1.  
    2. @EventHandler
    3. public void onBlockBreak(BlockBreakEvent e) {
    4. Player player = e.getPlayer();
    5. if (player.getGameMode() == GameMode.CREATIVE)
    6. return;
    7.  
    8. ItemStack tool = player.getItemInHand();
    9. ItemMeta toolmeta = tool.getItemMeta();
    10.  
    11. if (toolmeta.getDisplayName() != null && tool != null) {
    12. System.out.println("worked1");
    13. if (toolmeta.getDisplayName() == pm.pick1UnlockedName || toolmeta.getDisplayName() == pm.pick1UnlockedName ||
    14. toolmeta.getDisplayName() == pm.pick1UnlockedName || toolmeta.getDisplayName() == pm.pick1UnlockedName ||
    15. toolmeta.getDisplayName() == pm.pick1UnlockedName || toolmeta.getDisplayName() == pm.pick1UnlockedName ||
    16. toolmeta.getDisplayName() == pm.pick1UnlockedName || toolmeta.getDisplayName() == pm.pick1UnlockedName ||
    17. toolmeta.getDisplayName() == pm.pick1UnlockedName) {
    18. tool.setDurability((short)0);
    19. }
    20. }
    21. }
    I was told setting the durability to 0 repairs the pickaxe, however this does not seem to be working, and if it is, it's not updating. I've also tried adding 1 durability every time a block is broken, this doesn't work either. Am I not updating something correctly? Thanks for any help.
     
  2. Offline

    MexMaster

    1. Don't compare Strings with '==', use string1.equals(string2)
    2. I think p.getItemInHand maybe returns a copy of the itemstack in hand.
    Try doing
    ItemStack item = p.getItemInHand();
    //set durability
    p.setItemInHand(item);
    to set it back
    2.5 If that doesn't work try p.updateInventory(); after setting the item in hand
    3. You're doing 9 times exactly the same in your if statement.
    4. I don't think the durability should be zero, I think it should be the max durability of this item
    5. Is 'worked1' printed?
     
    Last edited: Mar 28, 2015
  3. Offline

    TheDiamond06

    @MexMaster You can cancel the event and then do what happens when you break a block manually.
    Code:java
    1.  
    2. e.setCancelled(true);
    3. e.getBlock().setType(Material.AIR);
    4. World world = b.getWorld();
    5. world.dropItem(b.getLocation(), b.getType());
    6.  

    You can test if it is stone and change it to cobblestone if you want.

    Another way you can do this:
    Code:java
    1.  
    2. if(p.getInventory().getItemInHand() != null || p.getInventory().getItemInHand() != Material.AIR)
    3. {
    4. p.getInventory().getItemInHand().setDurability((short) (p.getInventory().getItemInHand().getDurability() - 1));
    5. }
    6.  

    You can subtract more than this if you want. Subtracting durability is adding it.
     
  4. Offline

    MexMaster

    @TheDiamond06
    Your method works like a silk touch pickaxe, if you mine diamond ore you will get diamond ore and not diamonds.
    Also doing so can break plugins like worldguard. Because you are cancelling the event, so worldguard doesn't have to care if he's allowed to build here because its canceled anyway. So everybody could destroy everything.
    (Not sure if wg is working that way, its just an example)
     
  5. Offline

    TheDiamond06

    @MexMaster That is why I mentioned the second method. The first one does conflict with worldguard, but you can work around that with some work. You can also fix the silk touch problem which would take a while. I recommend the second method because it is easier.

    @Signatured You are not updating the inventory
    Code:java
    1. p.updateInventory();
     
    Last edited by a moderator: Mar 28, 2015
Thread Status:
Not open for further replies.

Share This Page