Armor Checking

Discussion in 'Plugin Development' started by MordorKing78, Nov 18, 2014.

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

    MordorKing78

    I Want to do if a player is wearing armor. And he runs the command /acheck
    a message will pop up like:
    You a wearing Elf Armor (<< Leather Armor)

    But I can't get it done.
    I Tried things like this:
    Code:java
    1. boolean lclass = p.getInventory().getBoots().equals(Material.LEATHER_BOOTS) && p.getInventory().getLeggings().equals(Material.LEATHER_LEGGINGS) && p.getInventory().getChestplate().equals(Material.LEATHER_CHESTPLATE) && p.getInventory().getHelmet().equals(Material.LEATHER_HELMET);
    2. if(lclass == true){
    3. String slclass = "§e[§aArcher§e]";
    4. }
    5. p.sendMessage("");


    But that should not work because then I have to do like p.sendMessage(lclass + fclass + class + "");

    Edit: I'm Stuck please help :+|
     
  2. Offline

    mrCookieSlime

    MordorKing78
    an ItemStack will never be equal to a Material, in fact, it wont even be equal to another ItemStack.
    first do a null check and then check if the items type is Leather Armor.
     
  3. Offline

    Fuzzybear04

    mrCookieSlime

    if(p.getInventory().getHelmet().equals(new ItemStack(Material.LEATHER_BOOTS));

    Yei or Nei?
     
  4. Offline

    mrCookieSlime

    Fuzzybear04
    Not going to work. As I said ItemStacks are never equal to other ItemStacks, you have to use isSimiliar()
     
  5. Offline

    Fuzzybear04

    mrCookieSlime

    Code:java
    1. if(p.getInventory().getHelmet().getType() .equals(Material.LEATHER_HELMET)){
    2.  


    Doesn't come up with any errors :s
     
  6. Offline

    Rocoty

    How about learning about object oriented design (yes, a shot in the dark, I'm assuming you haven't learned it yet) and construct an extensible polymorphic architecture?

    PS: Sorry for the fancy words, I don't know what struck me!

    mrCookieSlime .equals() and == are not the same. overriding the equals method is common practice to allow for comparison between different objects that may qualify as "equal". It is overridden as such in ItemStack. In fact, the equals method in ItemStack uses isSimilar internally in addition to some other operations.

    Source: http://jd.bukkit.org/dev/doxygen/db/d8c/ItemStack_8java_source.html#l00300

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  7. Offline

    mrCookieSlime

    When did I say that equals() and == are the same?
    But I know what you meant and why you assumed that.
    Mistakes like that happen if it is late ^^.
    However I didnt know that Bukkit overrides the .equals() method until now. You always learn something new...

    And please dont double post.
     
  8. Offline

    ColonelHedgehog


    Indeed.


    When comparing enums, it's best to use:

    someEnumRelatedObject == Enum.WHATEVER

    In this case, you need to compare getHelmet().getType() with Material.LEATHER_HELMET using the aforementioned "==" equality operators.
     
  9. Offline

    Rocoty

    And it's not really far-fetched either, is it? I mean, this is the Bukkit forums after all.
     
  10. Offline

    ColonelHedgehog

    How do you mean?
     
  11. Offline

    MordorKing78

    Lol, So many reactions..
    Don't bother the code.. I just added it because you can see how much I am in "trouble". I Just want something like this:
    - Player is wearing Leather armor? (Sending message "You are wearing some "Cow Killed armor" ."
    - When a player is wearing Iron armor (Sending message "Hardly mined "iron armor"")
    - When a player is wearing Diamond Armor (Sending message "You are wearing "Shining Armor"!")

    Tags: ColonelHedgehog Rocoty mrCookieSlime Fuzzybear04
     
  12. Offline

    Fuzzybear04


    I know, just pointing out/probing other possibilities that MordorKing78 can use
     
  13. Offline

    MordorKing78

  14. Offline

    Fuzzybear04

Thread Status:
Not open for further replies.

Share This Page