Solved How to get the material

Discussion in 'Spigot Plugin Development' started by AntonioC94, Apr 1, 2020.

  1. Hello there!
    So I just want to know, how can I get the material from the main hand and then save in the config.
    I have this code to get the material

    Code (Text):

    String name= args[1];
    ItemStack item = player.getInventory().getItemInMainHand();
    main.getConfig().set("Location." + name + ".Item", item.getType().getKey().toString());
     
    But all the time when I want to get the material, I have null , so maybe I need to change the way
     
  2. drives_a_ford

    Moderator

    Use Material#name.
    So item.getType().name().
     
  3. thanks <3
     
  4. Choco

    Moderator

    No, you're better to use the Material's key as the OP has done. To get the material, use Material#matchMaterial(). It will also fetch by keys.
     
  5. drives_a_ford

    Moderator

    Since matchMaterial matches all kinds of magic, I've usually chosen to ignore it and simply use the enum's names. But if you say it's preferred, then so be it.