Adding Enchantments from config to ItemStack

Discussion in 'Spigot Plugin Development' started by iAmGuus, Jun 7, 2015.

  1. Hi guys,

    I got a little problem with parsing an itemstack from config.
    Everything works fine except for parsing the enchantments.

    This is what an itemstack looks like:
    Code (Text):
      0:
        id: 302
        data: -1
        amount: 1
        name: 'Special Chainmail Helmet'
        lore: null
        enchants:
          OXYGEN:3
          PROTECTION_ENVIRONMENTAL:3
          PROTECTION_PROJECTILE:3
          PROTECTION_EXPLOSIONS:3
          DURABILITY:3
    Now all the data above the section 'enchants' loads fine. When I try to load the enchants section,
    It just seems like it is null.

    Here is my code for parsing an itemstack:
    PHP:
        public static ItemStack parseItem(ConfigurationSection c) {
            int id = c.getInt("id");
            short data = (short) c.getInt("data");
            int amount = c.getInt("amount");
           
            ItemStack item = new ItemStack(id, amount, data);
            ItemMeta im = item.getItemMeta();
           
            String name = c.getString("name");
           
            if (c.getConfigurationSection("enchants") != null) {
                item.addUnsafeEnchantments(loadHashMap(c.getConfigurationSection("enchants")));
            }
           
            List<String> lore = new ArrayList<String>();
            for (String s : c.getStringList("lore")) {
                s = "§7" + s;
                lore.add(s);
            }
           
            im.setDisplayName("§r" + name);
            im.setLore(lore);
            item.setItemMeta(im);
            return item;
        }
    It is about the part:
    Code (Text):
            if (c.getConfigurationSection("enchants") != null) {
                item.addUnsafeEnchantments(loadHashMap(c.getConfigurationSection("enchants")));
            }
    Here is the method loadHashMap:
    PHP:
        private static Map<Enchantment, Integer> loadHashMap(ConfigurationSection c) {
            HashMap<Enchantment, Integer> map = new HashMap<Enchantment, Integer>();
           
            System.out.println("check0");
            if (c.getKeys(false) != null) {
                System.out.println("check1");
                for (String s : c.getKeys(false)) {
                    if (Enchantment.getByName(s) != null) {
                        System.out.println("check2");
                        map.put(Enchantment.getByName(s), c.getInt(s));
                        System.out.println("added enchantment " + Enchantment.getByName(s).toString() + " with level " + c.getInt(s));
                    }
                }
            }
           
            return map;
        }
    As you see, I have also added debug messages,
    It doesn't even print out "check0", because in the parseItem method, I also check for null.

    Any ideas how I could load and add the enchantments to the ItemStack?

    Thanks,
    iAmGuus
     
  2. The only thing I could think of is that there is no space between the colon and the value. It shouldn't matter..

     
  3. @gustav9797 Me and my friend already tried adding a space, with no luck
     
  4. Code (Text):

    item.addUnsafeEnchantments(loadHashMap(c.getConfigurationSection("enchants")));
     
    This is the line that doesn't run, correct? Are you completely sure?
     
  5. @gustav9797 , yup, that line doesn't run because of all the null checks.
     
  6. This is directly from the reference. So when it runs, it can't find it. The problem is somewhere else.