Solved Durability

Discussion in 'Plugin Development' started by Wantsome, Jul 11, 2019.

  1. Offline

    Wantsome

    Hello, I made a custom gun called assault rifle. I simply made it so it would lose durability whenever I would left click. upon depleting of the durability of the gun when hitting 0 it should say "no ammo" which work on 1.12 Minecraft update. updating to 1.14 its does work but after one shot it does not, I've put debug messages and they display when I shoot the first bullet but does not after I try to shoot the second. I am confused. here is my code:
    Code:
    public class assaultrifle implements Listener {
    
        @EventHandler
        @SuppressWarnings("deprecation")
        public void playerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if (player != null && player.getItemInHand() != null && player.getItemInHand().getItemMeta() != null && player.getItemInHand().getItemMeta().getDisplayName() != null) {
                if (player.getItemInHand().getItemMeta().equals(guns.getManager().assaultrifle.getItemMeta())) {
                    player.sendMessage("debug0");
                    if (event.getAction().equals(Action.LEFT_CLICK_AIR)) {
                        player.sendMessage("debug1");
                        ItemStack i = player.getInventory().getItemInHand();
                        if (i.getDurability() < 1562) {
                            player.sendMessage("debug2");
                            i.setDurability((short) (i.getDurability() + 8));
    
        
                    } else {
                            player.sendMessage("[Debug] no ammo");}
                    }
                }
            }
        }
    }
     
    Last edited: Jul 11, 2019
  2. Offline

    Kars

    ItemMeta is an object. Your line:
    PHP:
    player.getItemInHand().getItemMeta().equals(guns.getManager().assaultrifle.getItemMeta())
    Compares two objects. The fact that it results in true even the first time is luck in my opinion.

    You should compare attributes of the ItemMeta, for instance compare all the lore strings in getItemInHand.getLore(). If they all match, you will know the item is the one you are looking for.

    If your assault rifle has any other defining attributes that can be deduced from the ItemMeta, you can compare them as well. Just don't compare two ItemMeta objects.
     
  3. Offline

    Wantsome

    Alright, I was overthinking it lol. thanks for the input though! :)
     

Share This Page