use of net.citizensnpcs.api.event.CitizensSerialiseMetaEvent in project CitizensAPI by CitizensDev.
the class ItemStorage method serialiseMeta.
private static void serialiseMeta(DataKey key, ItemMeta meta) {
key.removeKey("flags");
try {
key.setBoolean("unbreakable", meta.isUnbreakable());
} catch (Throwable t) {
// probably backwards-compat issue, don't log
}
int j = 0;
for (ItemFlag flag : ItemFlag.values()) {
if (meta.hasItemFlag(flag)) {
key.setString("flags." + j++, flag.name());
}
}
if (meta instanceof Repairable) {
Repairable rep = (Repairable) meta;
key.setInt("repaircost", rep.getRepairCost());
} else {
key.removeKey("repaircost");
}
if (meta.hasLore()) {
List<String> lore = meta.getLore();
DataKey root = key.getRelative("lore");
for (int i = 0; i < lore.size(); i++) {
root.setString(Integer.toString(i), lore.get(i));
}
} else {
key.removeKey("lore");
}
if (meta.hasDisplayName()) {
key.setString("displayname", meta.getDisplayName());
} else {
key.removeKey("displayname");
}
if (meta instanceof BookMeta) {
BookMeta book = (BookMeta) meta;
DataKey pages = key.getRelative("book.pages");
for (int i = 1; i <= book.getPageCount(); i++) {
pages.setString(Integer.toString(i), book.getPage(i));
}
key.setString("book.title", book.getTitle());
key.setString("book.author", book.getAuthor());
serialiseEnchantments(key.getRelative("book.enchantments"), book.getEnchants());
} else {
key.removeKey("book");
}
if (meta instanceof SkullMeta) {
SkullMeta skull = (SkullMeta) meta;
key.setString("skull.owner", skull.getOwner());
} else {
key.removeKey("skull");
}
if (meta instanceof FireworkMeta) {
FireworkMeta firework = (FireworkMeta) meta;
int i = 0;
for (FireworkEffect effect : firework.getEffects()) {
serialiseFireworkEffect(key.getRelative("firework.effects." + i), effect);
i++;
}
key.setInt("firework.power", firework.getPower());
} else {
key.removeKey("firework");
}
if (meta instanceof MapMeta) {
MapMeta map = (MapMeta) meta;
key.setBoolean("map.scaling", map.isScaling());
} else {
key.removeKey("map");
}
if (meta instanceof LeatherArmorMeta) {
LeatherArmorMeta armor = (LeatherArmorMeta) meta;
Color color = armor.getColor();
key.setInt("armor.color", color.asRGB());
} else {
key.removeKey("armor");
}
if (meta instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta ench = (EnchantmentStorageMeta) meta;
for (Map.Entry<Enchantment, Integer> e : ench.getStoredEnchants().entrySet()) {
key.getRelative("enchantmentstorage").setInt(e.getKey().getName(), e.getValue());
}
} else {
key.removeKey("enchantmentstorage");
}
if (meta instanceof PotionMeta) {
PotionMeta potion = (PotionMeta) meta;
PotionData data = potion.getBasePotionData();
List<PotionEffect> effects = potion.getCustomEffects();
key.setBoolean("potion.data.extended", data.isExtended());
key.setBoolean("potion.data.upgraded", data.isUpgraded());
key.setString("potion.data.type", data.getType().name());
key.removeKey("potion.effects");
DataKey effectKey = key.getRelative("potion.effects");
for (int i = 0; i < effects.size(); i++) {
PotionEffect effect = effects.get(i);
DataKey sub = effectKey.getRelative(Integer.toString(i));
sub.setBoolean("ambient", effect.isAmbient());
sub.setInt("amplifier", effect.getAmplifier());
sub.setInt("duration", effect.getDuration());
sub.setString("type", effect.getType().getName());
}
} else {
key.removeKey("potion");
}
key.removeKey("blockstate");
if (meta instanceof BlockStateMeta) {
BlockStateMeta state = (BlockStateMeta) meta;
if (state.hasBlockState()) {
DataKey root = key.getRelative("blockstate");
BlockState blockstate = state.getBlockState();
if (blockstate instanceof Banner) {
Banner banner = (Banner) blockstate;
serialiseBanner(root.getRelative("banner"), banner);
} else {
root.removeKey("banner");
}
}
}
if (meta instanceof BannerMeta) {
BannerMeta banner = (BannerMeta) meta;
DataKey root = key.getRelative("banner");
if (banner.getBaseColor() != null) {
root.setString("basecolor", banner.getBaseColor().name());
} else {
root.removeKey("basecolor");
}
List<org.bukkit.block.banner.Pattern> patterns = banner.getPatterns();
root.removeKey("patterns");
for (int i = 0; i < patterns.size(); i++) {
org.bukkit.block.banner.Pattern pattern = patterns.get(i);
DataKey sub = root.getRelative("patterns." + i);
sub.setString("color", pattern.getColor().name());
sub.setString("type", pattern.getPattern().getIdentifier());
}
} else {
key.removeKey("banner");
}
Bukkit.getPluginManager().callEvent(new CitizensSerialiseMetaEvent(key, meta));
}
Aggregations