How can I check for an Armor Item?

Discussion in 'Plugin Development' started by deleted_91027365, Feb 19, 2015.

Thread Status:
Not open for further replies.
  1. hey,

    Is it possible to check for an Armor Item?
    I mean, can I check, if a Player wears a Leather Chestplate?
    I want to do that:

    Code:
    if(PLAYER ARMOR CHECK){
    //my code
    }
    
    else {
    //some other Code
    }
    Is it possible
     
  2. Not sure if this is exactly correct, but: player#getInventory()#getChestplate();
     
  3. @CodePlaysMinecraft Thats what I thought, too. But I don´t know how I can sepcify the Item
     
  4. Offline

    Bancey

    @dunklesToast When are you checking what armor the player has on? Assuming you can already get the player then this would most probably work:
    Code:
     if(player.getInventory().getChestplate() == Material.DIAMOND_CHESTPLATE) {
    //do stuff
    }
    This is assuming "player" is an instance of a player.
    "DIAMOND_CHESTPLATE" can be swapped out for any armor piece.
    -Bancey
     
  5. @Bancey I only testing stuff... I check this on Player Join Event
     
  6. Offline

    Bancey

    @dunklesToast In that case then put that if statement in your event listener and it will fire when a player logs in.
     
  7. Now Eclipse says:

    Incompatible operand types ItemStack and Material

    @Bancey Like this?

    Code:
    @EventHandler
        public void onJoin(org.bukkit.event.player.PlayerJoinEvent e){
            Player p = e.getPlayer();
           
           
            if(p.getInventory().getChestplate() == Material.DIAMOND_CHESTPLATE) {
           
            p.getInventory().getChestplate().setDurability((short) 239);
            p.sendMessage("TEXT");
            }
    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Feb 19, 2015
  8. Offline

    Zombie_Striker

    Code:
    @EventHandler
        public void onJoin(org.bukkit.event.player.PlayerJoinEvent e){
            Player p = e.getPlayer();
     
            if(p.getInventory().getChestplate().getType().equals(Material.DIAMOND_CHESTPLATE) ){
          
            p.getInventory().getChestplate().setDurability((short) 239);
            p.sendMessage("TEXT");
            }
    You forgot that you have to add .getType(), and also its better to just do .equals()
     
    deleted_91027365 likes this.
  9. @Zombie_Striker
    Not sure, but I think getChestplate() returns null if there is no chestplate equipped, and not an ItemStack of air, therefore @dunklesToast should also check if "p.getInventory().getChestplate() != null" before the ".equals" statement.

    I.e.
    Code:
    if(p.getInventory().getChestplate() != null && p.getInventory().getChestplate().getType().equals(Material.DIAMOND_CHESTPLATE)) {
     
  10. Offline

    SuperOriginal

    Please mark this thread as solved (thread tools)
     
Thread Status:
Not open for further replies.

Share This Page