How to get a Material from a config

Discussion in 'Plugin Development' started by justin_393, Oct 10, 2014.

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

    justin_393

    So, I have never got a custom material from a config, so I was wondering if this is the correct way to do it.

    Code:java
    1. new ItemStack(Material.getMaterial(core
    2. .getConfig().getString("PATH").toUpperCase()));
     
  2. Offline

    coasterman10

    That will work if the user types in exactly the material name as it appears in the Material enum including underscores, except it is case insensitive here. The best way to do this would probably be to use item IDs instead of the item names because it's quite impractical to expect the user to look up the enum constant name for the material they want. If they don't type it correctly here, it will return null, and throw an exception trying to create a new ItemStack.

    I would suggest getting an integer value for the ID, and then adding a null check in case the ID provided is invalid.
     
    Code0 and justin_393 like this.
  3. Offline

    will181

    I'd personally be happy to do it like that, although item IDs are also a good suggestion.

    If you do want to go with using the getString() method, perhaps enclose it within a try/catch just to be sure.
     
  4. Offline

    Code0

    If you really want to do it the way OP did it, I'd setup a template in the config file so the user knows what to put. will181 justin_393
     
    Europia79 likes this.
  5. Offline

    Tss1410

    Material.valueOf(string); ?
     
  6. Offline

    Code0

    As coasterman10 just said. Tss1410
     
  7. Offline

    CMPSCdev

    Tss1410

    Furthermore, I would suggest using ' Material.valueOf(String).toUppercase' as all materials are defined by uppercase lettering; "DIAMOND_SWORD"
     
    Code0 likes this.
Thread Status:
Not open for further replies.

Share This Page