Solved Material#getMaterial() Always returning null

Discussion in 'Spigot Plugin Development' started by AtomicZombies, Oct 10, 2019 at 12:44 AM.

  1. The solution to this is going to be super obvious, sorry. I'm probably coding too late.

    Code (Java):
                String[] values = string.split("~");
                System.out.println("So, the value of the Material is " + values[0]);
                for (int i = 0; i < Integer.parseInt(values[1]); i++) {
                    returnValue.add(new ItemStack(Material.getMaterial(values[0])));
                }
    So my goal is to add this ItemStack to a List many times. For whatever reason, Material#getMaterial(String) is always returning null. To debug, I added that print statement, and it shows this in the console:
    Code (Text):
    [19:30:19] [Server thread/INFO]: So, the value of the Material is RED_DYE
    Material#valueOf() does a similar thing and throws an error for whatever reason. What am I missing?
     
  2. Gadse

    Gadse Previously GummiBoat

    Material#getMaterial should work just fine. Check if it exists with Material.RED_DYE in your code to be sure.
    Are you using the right version in your plugin and on your server? Updating to the newest 1.14.4 is probably recommended.
    Also, don't forget to note api-version: 1.14 in your plugin.yml
     
  3. Huh, it was the `api-version: 1.14` that did it, thanks.
    Why is that required though?
     
  4. Gadse

    Gadse Previously GummiBoat

    It tells the server to definitely use api of or above that version. If you don't specify it, it will try to be as backwards-compatible as possible. I'm pretty sure RED_DYE only started to be a thing in 1.14, thus it couldn't be found in a "backwards compatible" version, returning null.
     
    • Winner Winner x 1
  5. It's well documented here.
     
    • Winner Winner x 1