get material from a string

Discussion in 'Spigot Plugin Development' started by _TMT_, Jun 9, 2016.

  1. Quick question that I couldn't find the answer to on google. Is there a way to get a material from a string such as "DIAMOND". For example if I was creating an ItemStack that I wanted to control the material of it with player input.
  2. Code (Text):

  3. Choco


    No... use Material#getMaterial(String)
    You won't get an IllegalArgumentException this way. If the material is not found, it will instead return null rather than throwing an error
  4. As said above. You'll also want to either catch the Exceptions that can throw or make sure the item is valid. Snippet from a project of mine:
    Code (Text):
    if (Material.matchMaterial(args[1]) == null) {
        player.sendMessage(ChatColor.RED + "Invalid Material!");
        return true;
    Material material = Material.matchMaterial(args[1]);