Solved Issue checking if a player has a item in hand

Discussion in 'Plugin Development' started by 99storm2, Jun 11, 2014.

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

    99storm2

    I need to check if a player is holding any piece of armour in his hand. Ex. Diamond: Helmet, Chest plate, pants, and boots. Then repeat for all the other types of armour.

    What would be the most efficient way of doing this be.

    Right now i only know how to check each piece of armour.

    Code:
    if(inhand == Material.DIAMOND_HELMET || inhand == Material.DIAMOND_CHESTPLATE{Repeat for the rest}) {
    }

    I was thinking it would be easier to do like
    Material diamond = then all the diamond armor
    but that doesnt work so im not sure what to try.
    if it did work then i could just do


    Code:
    if(inhand == diamond || inhand == gold{Repeat}) {
    }
    Is there a way to put all the diamond armour into an array list or something and then checking the array list?

    Sorry about this if there is a simple way to do this im still learning.
    Thanks in advance!
     
  2. Offline

    Tzeentchful

    The best way would be to make a static list in the class then you can just use the .contains() method to see if the material is in the list.

    This will go at the top of your class when you declare you global variables
    Code:java
    1. public static final List<Material> ARMOR = new ArrayList<Material>();
    2.  
    3. static {
    4. ARMOR.add(Material.DIAMOND_HELMET);
    5. ARMOR.add(Material.DIAMOND_CHESTPLATE);
    6. //... reset of materials here
    7. }


    Then wherever you want to check do this.
    Code:java
    1. if(ARMOR.contains(player.getItemInHand().getType())) {
    2. //Do something
    3. }
     
    99storm2 likes this.
  3. Offline

    99storm2

    Tzeentchful Thanks so much! This will help me complete my plugin!
     
  4. Offline

    fireblast709

  5. Offline

    99storm2

    Thanks for the suggestion! I have already used the list in my code but i think i'm going to go back when i'm done and make it cleaner and nicer. I'm also going to need to add more features to it so i will definitely keep this in mind.
     
  6. Offline

    AdamQpzm

    Tzeentchful Yes, a public static list is clearly 'the best way'
    99storm2 There's an edit post option :)
     
  7. Offline

    99storm2

    AdamQpzm Sorry about that i was at school and i couldnt figure out how to tag someone in a edit because i forgot to tag him. I decided to delete the first comment and then re-comment but for some reason it deleted on my school computer but never actually deleted.
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page