Parsing ItemMeta

Discussion in 'Plugin Development' started by microgeek, May 22, 2013.

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

    microgeek

    For my Xml configuration library, I'm trying to parse serialized ItemStacks. I can parse the ItemStack without any issues by loading the map and using the deserialize method in ItemStack:
    Code:
        @Override
        public Object parsed(String path, XMLConfig config) {
            return ItemStack.deserialize(config.getKeysAndValues(path, true));
        }
    XML:
    Code:
        <item type="itemstack" key="itemstack_1">
            <item key="type" value="STONE_SWORD" />
            <item key="meta">
                <item key="==" value="ItemMeta" />
                <item key="meta-type" value="UNSPECIFIC" />
                <item key="display-name" value="Sword" />
            </item>
        </item>
    Map structure:
    Code:
    {meta.meta-type=UNSPECIFIC, type=STONE_SWORD, meta.display-name=Sword, meta.===ItemMeta}
    But because the "meta" key is a String Bukkit won't parse it, as seen in Bukkit's source:
    Code:
      public static ItemStack deserialize(Map<String, Object> args)
      {
        Material type = Material.getMaterial((String)args.get("type"));
        short damage = 0;
        int amount = 1;
     
        if (args.containsKey("damage")) {
          damage = ((Number)args.get("damage")).shortValue();
        }
     
        if (args.containsKey("amount")) {
          amount = ((Integer)args.get("amount")).intValue();
        }
     
        ItemStack result = new ItemStack(type, amount, damage);
     
        if (args.containsKey("enchantments")) {
          Object raw = args.get("enchantments");
     
          if ((raw instanceof Map)) {
            Map map = (Map)raw;
     
            for (Map.Entry entry : map.entrySet()) {
              Enchantment enchantment = Enchantment.getByName(entry.getKey().toString());
     
              if ((enchantment != null) && ((entry.getValue() instanceof Integer)))
                result.addUnsafeEnchantment(enchantment, ((Integer)entry.getValue()).intValue());
            }
          }
        }
        else if (args.containsKey("meta")) {
          Object raw = args.get("meta");
          if ((raw instanceof ItemMeta)) {
            result.setItemMeta((ItemMeta)raw);
          }
        }
    
    I need some way of parsing the "meta" key in the Map into an actual ItemMeta Object, and that's where I'm having some issues. Does anyone have any idea how YamlSnake does it? Sorry if my grammar is bad, I'm quite tired atm.
     
  2. snake yaml converts it to a map
     
Thread Status:
Not open for further replies.

Share This Page