Solved Item Durability

Discussion in 'Plugin Development' started by false_chicken, Sep 25, 2019.

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

    false_chicken

    Hey everyone. I have not done anything Bukkit related since about 2014. Just now getting back into a little development but I am having some issues related to durability. I cannot seem to get the metadata to set properly. I am sure this is just some misunderstanding on my part as to how this works.

    I am trying to allow the shovel to create grass paths with the dirt block. I can make the block change but the durability part doesn't work. Also as a side issue I notice that the shovel does not swing as it does with the grass blocks. Any help with these two issues would be greatly appreciated! Thanks!

    Code:
    public class EventListener implements Listener {
       
       @EventHandler
       public void onPlayerInteract (PlayerInteractEvent event) {
         Player player = event.getPlayer();
         ItemStack shovel = player.getInventory().getItemInMainHand();
         
         if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
           Bukkit.getLogger().info("Player interacted with: " + event.getClickedBlock().getType().toString());
           
           if (event.getClickedBlock().getType() == Material.DIRT) {
              if (event.getItem().getType() == Material.WOODEN_SHOVEL ||
                  event.getItem().getType() == Material.STONE_SHOVEL ||
                  event.getItem().getType() == Material.IRON_SHOVEL ||
                  event.getItem().getType() == Material.GOLDEN_SHOVEL ||
                  event.getItem().getType() == Material.DIAMOND_SHOVEL) {
               
                event.getClickedBlock().setType(Material.GRASS_PATH);
               
               
                Damageable meta = (Damageable) shovel.getItemMeta();
                meta.setDamage(meta.getDamage()-50);
                shovel.setItemMeta((ItemMeta) meta);
               
                player.getInventory().setItemInMainHand(shovel);
                player.updateInventory();
              }
           }
         }
       }
    }
    
     
    Last edited: Sep 25, 2019
  2. Offline

    timtower Administrator Administrator Moderator

    @false_chicken player.updateInventory() might help already.
    And I believe that that swing is client side.
     
  3. Offline

    false_chicken

    Hmm. I added updateInventory() after setItemInMainHand() but it didn't seem to change anything. The result is the same. The dirt becomes a grass path but the nothing changes about the shovel.

    So.... a bit embarrassing but it turns out I was REPAIRING the shovel... I should have been ADDING "damage" not subtracting xD. Thanks for the help!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 25, 2019
    timtower likes this.
Thread Status:
Not open for further replies.

Share This Page