ItemStack from Config

Discussion in 'Plugin Development' started by elementalgodz11, May 24, 2014.

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

    elementalgodz11

    How can I set the material of an itemstack using the config:

    I've tried using this but I'm getting a NPE:
    Code:java
    1. Set<String> kits = plugin.getConfig().getConfigurationSection("Kits").getKeys(false);
    2.  
    3. for (String kitNames : kits) {
    4.  
    5. ItemStack items = new ItemStack(Material.DIRT);
    6. items.setType(Material.getMaterial(plugin.getConfig().getString("Kits." + kits + ".ItemImage")));
    7. items.setAmount(1);
    8. ItemMeta itemsMeta = items.getItemMeta();
    9. itemsMeta.setDisplayName(ChatColor.GOLD + kitNames);
    10. items.setItemMeta(itemsMeta);
    11. inventory.addItem(items);
    12.  
    13. }


    However the actual config has this:
    Code:java
    1. Kits:
    2. Example:
    3. ItemImage: WOOD


    So I'm unsure to why it's giving the error at line 48: which is:
    items.setType(Material.getMaterial(plugin.getConfig().getString("Kits." + kits + ".ItemImage")));
     
  2. elementalgodz11
    to store material: keep the material as string (Material.toString();)
    to get it : Material.fromString(string);
     
  3. Offline

    elementalgodz11

    Someone_Like_You
    There is no fromString method in the Material class.
     
  4. Offline

    Schaakmatth

    Code:java
    1. Set<String> kits = plugin.getConfig().getConfigurationSection("Kits").getKeys(false);
    2.  
    3. for (String kitNames : kits) {
    4.  
    5. ItemStack items = new ItemStack(Material.getMaterial(plugin.getConfig().getString("Kits." + kits + ".ItemImage", 1)));
    6. ItemMeta itemsMeta = items.getItemMeta();
    7. itemsMeta.setDisplayName(ChatColor.GOLD + kitNames);
    8. items.setItemMeta(itemsMeta);
    9. inventory.addItem(items);
    10.  
    11. }




    elementalgodz11

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  5. elementalgodz11 well, my point as more like, store the material as string, and get it from a string..
     
Thread Status:
Not open for further replies.

Share This Page