[Solved] setting armor durability

Discussion in 'Plugin Development' started by Jogy34, Jun 23, 2012.

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

    Jogy34

    I am trying to set the durability on a piece of armor that is on a player. I am simply doing this:
    PlayerInventory inven = player.getInventory();
    ItemStack[] armor = inven.getArmorContents();
    //check to make sure they have armor
    //subtract the amount the armor resists from total damage
    //damage player
    armor[3].setDurability((short) (armor[3].getDurability() - 10));
    armor[2].setDurability((short) (armor[3].getDurability() - 10));
    armor[1].setDurability((short) (armor[3].getDurability() - 10));
    armor[0].setDurability((short) (armor[3].getDurability() - 10));
    The armor is staying at full durability every time. I've also tried putting a player.updateInventory() at the bottom but still nothing.

    I fixed all of the above issue however when the armor is supposed to be destroyed it is staying on the player until they actually take damage. Anyone have any suggestions to fix that

    Never mind
     
  2. Offline

    CXdur

    Hmm, this should work..
    Code:
        p.getInventory().getHelmet().setDurability(durability);
        p.getInventory().getChestplate().setDurability(durability);
        p.getInventory().getLeggings().setDurability(durability);
        p.getInventory().getBoots().setDurability(durability);
    
     
  3. Offline

    Jogy34

    Still nothing
     
  4. Offline

    mcgamer99

    Try this:

    Code:
    p.getInventory().getChestplate().setDurability((short) 0);
    p.getInventory().getHelmet().setDurability((short) 0);
    p.getInventory().getLeggings().setDurability((short) 0);
    p.getInventory().getBoots().setDurability((short) 0);
     
     
    
     
  5. Offline

    Jogy34

    CXdur said to do the same thing and I already said that it didn't work
     
  6. Offline

    Taco

    The error in this bit is that you're always setting the durability to whatever's in the 4'th slot of that array. Not sure what you're trying to accomplish, so I can't really suggest any code for you.
     
  7. Offline

    CorrieKay

    the problem is that durability starts at zero, and counts up. If you subtract durability, you heal the item.
     
  8. Offline

    Jogy34

    I just typed that wrong

    Thank you is works now
     
  9. Offline

    Njol

    getArmorContents() returns a copy of the player's armour, i.e. changes to this copy are not reflected on the player. You have to save your changes with setArmorContents(armor) and then call player.updateInventory() to send it to the player.
     
  10. You should also check against null... so here's an example with what everybody suggested:
    Code:
    ItemStack[] armor = inventory.getArmorContents();
    
    for(int i = 0; i < armor.length; i++)
    {
        if(armor[i] != null)
            armor[i].setDurability((short)(armor[i].getDurability() + 10));
    }
    
    inventory.setArmorContents(armor);
    player.updateInventory(); // You should ignore deprecated state of this method since there is no alternative.
    
    However, I am unsure how to make the armor break when it gets to its final damage value...
     
  11. Offline

    Njol

    Add this to the if (armor[j] != null) statement:
    Code:
    if (armor[j].getDurability() > armor[j].getType().getMaxDurability())
        armor[j] = null;
    i.e.
    Code:
    for(int j = 0; j < armor.length; j++) {
        if(armor[j] != null) {
            armor[j].setDurability((short)(armor[j].getDurability() + 10));
            if (armor[j].getDurability() > armor[j].getType().getMaxDurability())
                armor[j] = null;
        }
    }
    edit: fuck this shitty editor >_>, had to change i to j
     
  12. Offline

    Jogy34

    I was already doing that



    I am already doing that I was just posted the relevant code.
     
Thread Status:
Not open for further replies.

Share This Page