Solved Java OR operator and Materials?

Discussion in 'Plugin Development' started by JayHerlth, Feb 5, 2014.

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

    JayHerlth

    So, I thought I could be sly and see if this bit of code would work but get this error.
    Code:java
    1. if (event.getBrokenItem().getType().equals(Material.STONE_SWORD || Material.STONE_AXE || Material.STONE_PICKAXE || Material.STONE_HOE || Material.STONE_SPADE))
    Any workaround? Or must I build a large if/else if statement with other if/else if statements nested in a whole mess that's no fun to work around..? I plan to be assigning statements to every tool type (wood, stone, iron, gold, diamond, flint/steel, fishing rod, bow, etc.) I'd rather be able to combine all of one type into one if statement.
     
  2. Offline

    NinjaWAffles

    I'm not sure if it is the most efficient way to do it, but when dealing with this kind of "data," I'd use a switch-case statement, such as.

    Code:Java
    1.  
    2. switch(e.getBrokenItem().getType())
    3. {
    4. case Material.DIAMOND_SWORD:
    5. doDiamondSword();
    6. break;
    7. case Material.DIAMOND_PICKAXE:
    8. doDiamondPickaxe();
    9. break;
    10. }
    11.  


    I have not tested what it above. It was written in the forum's text editor. I don't have my IDE open. Don't copy and paste. :p
     
    JayHerlth likes this.
  3. Offline

    JayHerlth

    I've been sleep deprived lately and running off of about 4 hours of sleep these past nights. I think I remember hearing somewhere that switch case with bukkit won't work or is bad, or maybe I thought switch case didn't work in Java in the first place and wrongly remembered that... Honestly, I'm a wreck while trying to cram Java so deep into my mind right now. I'll look into using a switch case, it would make this project so much more easier for me later down the road for the future features of this plugin. Kind of ignored it.
    The purpose of this will prompt the user that they broke the tool and they're dealt damage based on that tool type. If I can get this switch case to work I'd be able to scratch off my future feature of custom messages based on tool type && material. Thanks NinjaWAffles

    NinjaWAffles I found out where I was thinking it wouldn't work...aha, I wasn't too deprived to forget completely.
    The source of my thought of it not working is from here apparently it needs to be an int or String value and I don't quite understand the whole Material.WOOD_SWORD yet, I'm assuming they're enums?

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

    xTrollxDudex

    JayHerlth
    Try using a bit OR instead of a logical OR. Just one | .

    If that doesn't work, then use switches. There's nothig wrong with it other than you may/may not need Java7 and if your target uses Java6, well screw Java6. Its been 6 years since it came out and Java7 contains lots of performance improvements so your audience should hassle themselves to get Java7.
     
    JayHerlth likes this.
  5. Offline

    JayHerlth

    Thank you xTrollxDudex
    Didn't realize that the material types were enums, made life pretty darn nice, especially for the future! :D
     
  6. Offline

    Maurdekye

    JayHerlth Well, if you ever need to do this for any other datatype, my goto method is just to use Arrays.asList().contains(), as so;
    Code:java
    1. if (Arrays.asList(Material.STONE_SWORD, Material.STONE_PICK, Material.STONE_AXE, Material.STONE_HOE, Material.STONE_SPADE).contains(event.getBrokenItem().getType())) {
    2. // Do something or other
    3. }
     
    JayHerlth likes this.
  7. Offline

    JayHerlth

    Maurdekye I Love You.

    I'm new to java, didn't know there was a .contains
    Life just got great for future projects, maybe even this... Actually, yes, this, very much.
     
  8. Offline

    Maurdekye

    JayHerlth Man, when I learned about that method I loved it too.
     
    JayHerlth likes this.
  9. Offline

    JayHerlth

    For this, switch is the best considering the functionality I wish to add later on. It works though :) So glad I found out the switch works for enum values.
    NinjaWAffles Here we are, your code was kinda off but got it to work nonetheless, thank you.

    [​IMG]
     
  10. Offline

    1Rogue

    I would say switch is the best, not only for lower time complexity but much more flexibility:

    Code:java
    1. switch (yourMaterial) {
    2. case DIAMOND_SWORD:
    3. case GOLD_SWORD:
    4. case IRON_SWORD:
    5. doSomethingForMetalSwords();
    6. break;
    7. case STONE_SWORD:
    8. case WOODEN_SWORD:
    9. doSomethingElse();
    10. break;
    11. default:
    12. sendSomeMessage();
    13. break;
    14. }


    I don't have java working on this computer but iirc you can also opt to only break in the "default" case and then have that code would run every time.
     
    JayHerlth likes this.
  11. Offline

    JayHerlth

    It is possible I could do something like in your switch. I'm thinking up ideas right now with what I can do with my plugin. I have a few routes I could take.
     
  12. Offline

    hexaan


    Even though you have found an answer to your problem I would still like to add that you cannot use || when you are passing data to a method. In this case the equals method.

    If you still wanted to use the or operator you would have to form your code like this.

    Code:java
    1. if (event.getBrokenItem().getType().equals(Material.STONE_SWORD) || event.getBrokenItem().getType().equals(Material.STONE_AXE)){
    2. }


    (Sorry for so many edits first post)
     
Thread Status:
Not open for further replies.

Share This Page