Exemplo n.º 1
0
 public MCItemMeta itemMeta(Construct c, int i, Target t) {
   MCItemMeta meta =
       Static.getServer().getItemFactory().getItemMeta(StaticLayer.GetConvertor().getMaterial(i));
   if (c instanceof CNull) {
     return meta;
   }
   CArray ma = null;
   if (c instanceof CArray) {
     ma = (CArray) c;
     try {
       if (ma.containsKey("display")) {
         Construct dni = ma.get("display");
         if (!(dni instanceof CNull)) {
           meta.setDisplayName(dni.val());
         }
       }
       if (ma.containsKey("lore")) {
         Construct li = ma.get("lore");
         if (li instanceof CNull) {
           // do nothing
         } else if (li instanceof CArray) {
           CArray la = (CArray) li;
           List<String> ll = new ArrayList<String>();
           for (int j = 0; j < la.size(); j++) {
             ll.add(la.get(j).val());
           }
           meta.setLore(ll);
         } else {
           throw new Exceptions.FormatException("Lore was expected to be an array.", t);
         }
       }
       if (meta instanceof MCLeatherArmorMeta) {
         if (ma.containsKey("color")) {
           Construct ci = ma.get("color");
           if (ci instanceof CNull) {
             // nothing
           } else if (ci instanceof CArray) {
             ((MCLeatherArmorMeta) meta).setColor(color((CArray) ci, t));
           } else {
             throw new Exceptions.FormatException("Color was expected to be an array.", t);
           }
         }
       }
       if (meta instanceof MCBookMeta) {
         if (ma.containsKey("title")) {
           Construct title = ma.get("title");
           if (!(title instanceof CNull)) {
             ((MCBookMeta) meta).setTitle(title.val());
           }
         }
         if (ma.containsKey("author")) {
           Construct author = ma.get("author");
           if (!(author instanceof CNull)) {
             ((MCBookMeta) meta).setAuthor(author.val());
           }
         }
         if (ma.containsKey("pages")) {
           Construct pages = ma.get("pages");
           if (pages instanceof CNull) {
             // nothing
           } else if (pages instanceof CArray) {
             CArray pa = (CArray) pages;
             List<String> pl = new ArrayList<String>();
             for (int j = 0; j < pa.size(); j++) {
               pl.add(pa.get(j).val());
             }
             ((MCBookMeta) meta).setPages(pl);
           } else {
             throw new Exceptions.FormatException("Pages field was expected to be an array.", t);
           }
         }
       }
       if (meta instanceof MCSkullMeta) {
         if (ma.containsKey("owner")) {
           Construct owner = ma.get("owner");
           if (!(owner instanceof CNull)) {
             ((MCSkullMeta) meta).setOwner(owner.val());
           }
         }
       }
       if (meta instanceof MCEnchantmentStorageMeta) {
         if (ma.containsKey("stored")) {
           Construct stored = ma.get("stored");
           if (stored instanceof CNull) {
             // Still doing nothing
           } else if (stored instanceof CArray) {
             for (String index : ((CArray) stored).keySet()) {
               try {
                 CArray earray = (CArray) ((CArray) stored).get(index);
                 MCEnchantment etype =
                     StaticLayer.GetConvertor().GetEnchantmentByName(earray.get("etype").val());
                 int elevel = Static.getInt32(earray.get("elevel"), t);
                 ((MCEnchantmentStorageMeta) meta).addStoredEnchant(etype, elevel, true);
               } catch (Exception bade) {
                 throw new Exceptions.FormatException(
                     "Could not get enchantment data from index " + index, t);
               }
             }
           } else {
             throw new Exceptions.FormatException(
                 "Stored field was expected to be an array of Enchantment arrays", t);
           }
         }
       }
     } catch (Exception ex) {
       throw new Exceptions.FormatException(
           "Could not get ItemMeta from the given information.", t);
     }
   } else {
     throw new Exceptions.FormatException(
         "An array was expected but recieved " + c + " instead.", t);
   }
   return meta;
 }
Exemplo n.º 2
0
 public Construct itemMeta(MCItemStack is, Target t) {
   Construct ret, display, lore, color, title, author, pages, owner, stored;
   if (!is.hasItemMeta()) {
     ret = new CNull(t);
   } else {
     ret = CArray.GetAssociativeArray(t);
     MCItemMeta meta = is.getItemMeta();
     if (meta.hasDisplayName()) {
       display = new CString(meta.getDisplayName(), t);
     } else {
       display = new CNull(t);
     }
     if (meta.hasLore()) {
       lore = new CArray(t);
       for (String l : meta.getLore()) {
         ((CArray) lore).push(new CString(l, t));
       }
     } else {
       lore = new CNull(t);
     }
     ((CArray) ret).set("display", display, t);
     ((CArray) ret).set("lore", lore, t);
     if (meta instanceof MCLeatherArmorMeta) {
       color = color(((MCLeatherArmorMeta) meta).getColor(), t);
       ((CArray) ret).set("color", color, t);
     }
     if (meta instanceof MCBookMeta) {
       if (((MCBookMeta) meta).hasTitle()) {
         title = new CString(((MCBookMeta) meta).getTitle(), t);
       } else {
         title = new CNull(t);
       }
       if (((MCBookMeta) meta).hasAuthor()) {
         author = new CString(((MCBookMeta) meta).getAuthor(), t);
       } else {
         author = new CNull(t);
       }
       if (((MCBookMeta) meta).hasPages()) {
         pages = new CArray(t);
         for (String p : ((MCBookMeta) meta).getPages()) {
           ((CArray) pages).push(new CString(p, t));
         }
       } else {
         pages = new CNull(t);
       }
       ((CArray) ret).set("title", title, t);
       ((CArray) ret).set("author", author, t);
       ((CArray) ret).set("pages", pages, t);
     }
     if (meta instanceof MCSkullMeta) {
       if (((MCSkullMeta) meta).hasOwner()) {
         owner = new CString(((MCSkullMeta) meta).getOwner(), t);
       } else {
         owner = new CNull(t);
       }
       ((CArray) ret).set("owner", owner, t);
     }
     if (meta instanceof MCEnchantmentStorageMeta) {
       if (((MCEnchantmentStorageMeta) meta).hasStoredEnchants()) {
         stored = new CArray(t);
         for (Map.Entry<MCEnchantment, Integer> entry :
             ((MCEnchantmentStorageMeta) meta).getStoredEnchants().entrySet()) {
           CArray eObj = CArray.GetAssociativeArray(t);
           eObj.set("etype", new CString(entry.getKey().getName(), t), t);
           eObj.set("elevel", new CInt(entry.getValue(), t), t);
           ((CArray) stored).push(eObj);
         }
       } else {
         stored = new CNull(t);
       }
       ((CArray) ret).set("stored", stored, t);
     }
   }
   return ret;
 }