Itemstack is not invincible

Discussion in 'Plugin Development' started by JaySN, Oct 20, 2017.

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

    JaySN

    Well this is extremely confusing because nothing in this code is working nor any error is showing up.
    Code:
    @EventHandler
        public void repairItems(EntityDamageByEntityEvent event) {
            if (event.getDamager() instanceof Player) {
    
                //Try and catch method because different spigot versions
                try {
    
                    ((Player) event.getDamager()).getInventory().getItemInMainHand().setDurability((short)0); //The sword(weapon) which the player uses to pvp will not be broken
    
                } catch(NoSuchMethodError e){
    
                    ((Player) event.getDamager()).getInventory().getItemInHand().setDurability((short)0); //The sword(weapon) which the player uses to pvp will not be broken
                }
    
            } else if (event.getEntity() instanceof Player) {
    
                ItemStack[] armour = ((Player) event.getEntity()).getInventory().getArmorContents(); //Armour which the player is wearing
                for (ItemStack i : armour) {
                    i.setDurability((short) 0);
                }
            }
        }
     
  2. Offline

    Zombie_Striker

    @JaySN
    1. Have you registered the class as a listener?
    2. Does any other events in the class work?
    3. Are you sure the damager is a player? Are you sure the entity is a player?
     
  3. Offline

    JaySN

    1)
    upload_2017-10-20_22-5-13.png
    yes
    2) yes
    3)It is supposed to.. isnt it? and how can it not be?
     
  4. Offline

    MightyOne

    Uuuuh some versions ago (not 1.12) i had the problem that items did not update. That took me quiet a while to figure out. Idk in my case i used Inventory#update() but isnt there already something like ItemStack#update() ?
     
  5. Offline

    Zombie_Striker

    @MightyOne
    The reason update works is because that updates refreshes all itemstacks in the inventory. It should be deprecated, because you should use setItem if you only need to update one slot.

    @JaySN
    Just noticed you are only setting the durability of the item. You need to reset the slot in order for the changes to take into effect. Use PlayerInventory#setItem(slot, i); to update the durability.
     
  6. Offline

    MightyOne

  7. Offline

    Caderape2

    @JaySN
    an entity can be a player, or a mob.
    What will happen if the player hit someone with an empty hand or with a block ?
     
  8. Offline

    JaySN

    hmm, okay let me see if I have any changes :)
     
Thread Status:
Not open for further replies.

Share This Page