// EnchantLevelMap public static JsonObject convertEnchantLevelMap(Map<Enchantment, Integer> enchantLevelMap) { JsonObject ret = new JsonObject(); for (Entry<Enchantment, Integer> entry : enchantLevelMap.entrySet()) { ret.addProperty(String.valueOf(entry.getKey().getId()), entry.getValue()); } return ret; }
// This is used in parallel with the isDefault. // Parallel usage is useful since we can then override isDeafult just like before. public static boolean isCustomDataDefault(Object entity) { if (!(entity instanceof Entity)) return true; JsonObject customData = ((Entity<?>) entity).getCustomData(); if (customData == null) return true; if (customData.entrySet().size() == 0) return true; return false; }
public static void transferAuthor(BookMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { if (!meta.hasTitle()) return; json.addProperty(BOOK_AUTHOR, meta.getAuthor()); } else { JsonElement element = json.get(BOOK_AUTHOR); if (element == null) return; meta.setAuthor(element.getAsString()); } }
public static void transferSkullMeta(SkullMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { if (!meta.hasOwner()) return; json.addProperty(SKULL_OWNER, meta.getOwner()); } else { JsonElement element = json.get(SKULL_OWNER); if (element == null) return; meta.setOwner(element.getAsString()); } }
public static void transferFireworkMetaPower( FireworkMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { json.addProperty(FIREWORK_FLIGHT, meta.getPower()); } else { JsonElement element = json.get(FIREWORK_FLIGHT); if (element == null) return; meta.setPower(element.getAsInt()); } }
public static void transferLore(ItemMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { if (!meta.hasLore()) return; json.add(LORE, convertStringList(meta.getLore())); } else { JsonElement element = json.get(LORE); if (element == null) return; meta.setLore(convertStringList(element)); } }
public static void transferName(ItemMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { if (!meta.hasDisplayName()) return; json.addProperty(NAME, meta.getDisplayName()); } else { JsonElement element = json.get(NAME); if (element == null) return; meta.setDisplayName(element.getAsString()); } }
public static void transferPages(BookMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { if (!meta.hasTitle()) return; json.add(BOOK_PAGES, convertStringList(meta.getPages())); } else { JsonElement element = json.get(BOOK_PAGES); if (element == null) return; meta.setPages(convertStringList(element)); } }
public static void transferMapMeta(MapMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { if (!meta.isScaling()) return; json.addProperty(MAP_SCALING, true); } else { JsonElement element = json.get(MAP_SCALING); if (element == null) return; meta.setScaling(element.getAsBoolean()); } }
public static Map<Enchantment, Integer> convertEnchantLevelMap(JsonElement jsonElement) { JsonObject json = jsonElement.getAsJsonObject(); Map<Enchantment, Integer> ret = new HashMap<Enchantment, Integer>(); for (Entry<String, JsonElement> entry : json.entrySet()) { int id = Integer.valueOf(entry.getKey()); Enchantment ench = Enchantment.getById(id); int lvl = entry.getValue().getAsInt(); ret.put(ench, lvl); } return ret; }
public static void transferId(ItemStack stack, JsonObject json, boolean stack2json) { if (stack2json) { int id = stack.getTypeId(); if (id == DEFAULT_ID) return; json.addProperty(ID, id); } else { JsonElement element = json.get(ID); if (element == null) return; stack.setTypeId(element.getAsInt()); } }
public static void transferFireworkEffectMeta( FireworkEffectMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { if (!meta.hasEffect()) return; json.add(FIREWORK_EFFECT, FireworkEffectAdapter.toJson(meta.getEffect())); } else { JsonElement element = json.get(FIREWORK_EFFECT); if (element == null) return; meta.setEffect(FireworkEffectAdapter.fromJson(element)); } }
public static void transferCount(ItemStack stack, JsonObject json, boolean stack2json) { if (stack2json) { int count = stack.getAmount(); if (count == DEFAULT_COUNT) return; json.addProperty(COUNT, count); } else { JsonElement element = json.get(COUNT); if (element == null) return; stack.setAmount(element.getAsInt()); } }
public static void transferDamage(ItemStack stack, JsonObject json, boolean stack2json) { // Durability is a weird name since it is the amount of damage. if (stack2json) { int damage = stack.getDurability(); if (damage == DEFAULT_DAMAGE) return; json.addProperty(DAMAGE, damage); } else { JsonElement element = json.get(DAMAGE); if (element == null) return; stack.setDurability(element.getAsShort()); } }
public static void transferEnchants(ItemMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { if (!meta.hasEnchants()) return; json.add(ENCHANTS, convertEnchantLevelMap(meta.getEnchants())); } else { JsonElement element = json.get(ENCHANTS); if (element == null) return; for (Entry<Enchantment, Integer> entry : convertEnchantLevelMap(element).entrySet()) { meta.addEnchant(entry.getKey(), entry.getValue(), true); } } }
public static void transferFireworkMetaEffects( FireworkMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { if (!meta.hasEffects()) return; json.add(FIREWORK_EFFECTS, convertFireworkEffectList(meta.getEffects())); } else { JsonElement element = json.get(FIREWORK_EFFECTS); if (element == null) return; meta.clearEffects(); meta.addEffects(convertFireworkEffectList(element)); } }
public static void transferRepaircost(ItemMeta meta, JsonObject json, boolean meta2json) { if (!(meta instanceof Repairable)) return; Repairable repairable = (Repairable) meta; if (meta2json) { if (!repairable.hasRepairCost()) return; json.addProperty(REPAIRCOST, repairable.getRepairCost()); } else { JsonElement element = json.get(REPAIRCOST); if (element == null) return; repairable.setRepairCost(element.getAsInt()); } }
public static void transferLeatherArmorMeta( LeatherArmorMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { Color color = meta.getColor(); if (Bukkit.getItemFactory().getDefaultLeatherColor().equals(color)) return; json.addProperty(LEATHER_ARMOR_COLOR, color.asRGB()); } else { JsonElement element = json.get(LEATHER_ARMOR_COLOR); if (element == null) return; meta.setColor(Color.fromRGB(element.getAsInt())); } }
public static void transferEnchantmentStorageMeta( EnchantmentStorageMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { if (!meta.hasStoredEnchants()) return; json.add(STORED_ENCHANTS, convertEnchantLevelMap(meta.getStoredEnchants())); } else { JsonElement element = json.get(STORED_ENCHANTS); if (element == null) return; // TODO: Add a pull request to get rid of this entry set loop! // TODO: A set, clear, remove all system is missing for (Entry<Enchantment, Integer> entry : convertEnchantLevelMap(element).entrySet()) { meta.addStoredEnchant(entry.getKey(), entry.getValue(), true); } } }
public static void transferPotionMeta(PotionMeta meta, JsonObject json, boolean meta2json) { if (meta2json) { if (!meta.hasCustomEffects()) return; json.add(POTION_EFFECTS, convertPotionEffectList(meta.getCustomEffects())); } else { JsonElement element = json.get(POTION_EFFECTS); if (element == null) element = json.get(POTION_EFFECTS_OLD); if (element == null) return; meta.clearCustomEffects(); for (PotionEffect pe : convertPotionEffectList(element)) { meta.addCustomEffect(pe, false); } } }