1.15.2 Check for a material

Discussion in 'Spigot Plugin Development' started by viuw, Apr 3, 2020.

  1. Is the a way to check for a material "stuff" without checking for any type of the material tools or armor?

    Materials: diamond, gold, iron, wood

    Note: Don't check for a name
    #1 viuw, Apr 3, 2020
    Last edited: Apr 3, 2020
  2. Sorry, confused on what you mean specifically. Are you asking how to check what the actual Material enum is or how to check what material an item is made out of. I'm assuming the latter, in which case you can check what the string starts with

    Code (Java):
    if (Material.toString().startsWith("DIAMOND")) {
                //code will execute if it is anything diamond related, tools, armour and even the blocks
    #2 Wick__, Apr 3, 2020
    Last edited: Apr 3, 2020
  3. I just did that too lol, thanks
  4. It's kinda hard to figure out your goal.
    Well if it is to shorten your code you can do exactly what the guy above me suggested, works like a charm but in some cases it accepts unwanted items such as:
    Code (Text):
    so @viuw the correct filter string is:
    Code (Text):
  5. I think he is asking how he can do it without filtering them by their name.

    Create a collection:
    Code (Java):
    static final Set<Material> MATERIALS = EnumSet.of(Material.DIAMOND_PICKAXE, Material.DIAMOND_SHOVEL /* and so on... */);
    Then check whether it contains the Material you want to check.

    In my opinion, that is the cleanest and most future proof solution.

    Do not forget to specify the api-version attribute in your plugin.yml file.
    • Agree Agree x 1
  6. Filter by name theoretically requires much less work & provides forwards compability.
  7. I do not think that creating a Set with some Materials is much work so it does not matter whether you do something that is less work.
    Not really. What if Mojang added a new material like a diamond nugget? XMaterials exists as well. And what if you wanted to add a command that shows the materials you filter? That is no problem with a Set but gets quite messy with your approach.
  8. I think that checking for the name is much easier then creating a map with every item. If you have many items its much more time consuming if you create a map for everything.
  9. It is a Set, not a Map. And I think I can create such a Set with all food types existing in Minecraft in four minutes. The question is not what is easier but rather what is better.
  10. I agree with you but if he needs this for example only once for 3 items he will get the same result in less time if he just asks for the name. Of course it is only a matter of seconds, I would do it with a set myself for example.
    • Like Like x 1
  11. drives_a_ford


    For code maintainability creating a set is far superior.
    It's clear which materials fall under your certain condition.
    Whereas when using name comparisons, you'd have to manually go and check.
    • Agree Agree x 1
  12. That is true, of course, as I said I would have done the same. Of course it depends on how experienced the developer is, some do it this way and others that way.