From 2605e050a06892aea628bebb2b0202baafc084e4 Mon Sep 17 00:00:00 2001 From: md678685 Date: Mon, 19 Nov 2018 19:41:01 +0000 Subject: [PATCH] Deduplicate ItemDb code --- .../essentials/items/AbstractItemDb.java | 243 ++++++++++++++++++ .../earth2me/essentials/items/FlatItemDb.java | 238 +---------------- .../essentials/items/LegacyItemDb.java | 230 +---------------- 3 files changed, 249 insertions(+), 462 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/items/AbstractItemDb.java b/Essentials/src/com/earth2me/essentials/items/AbstractItemDb.java index 531c60d33..6783d6ae8 100644 --- a/Essentials/src/com/earth2me/essentials/items/AbstractItemDb.java +++ b/Essentials/src/com/earth2me/essentials/items/AbstractItemDb.java @@ -1,6 +1,249 @@ package com.earth2me.essentials.items; import com.earth2me.essentials.IConf; +import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.StringUtil; +import org.bukkit.Color; +import org.bukkit.FireworkEffect; +import org.bukkit.Material; +import org.bukkit.block.Banner; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.*; +import org.bukkit.potion.Potion; +import org.bukkit.potion.PotionEffect; + +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; +import java.util.Set; + +import static com.earth2me.essentials.I18n.tl; public abstract class AbstractItemDb implements IConf, net.ess3.api.IItemDb { + + @Override + public ItemStack get(final String id, final int quantity) throws Exception { + final ItemStack retval = get(id.toLowerCase(Locale.ENGLISH)); + retval.setAmount(quantity); + return retval; + } + + @Override + public List getMatching(User user, String[] args) throws Exception { + List is = new ArrayList<>(); + + if (args.length < 1) { + is.add(user.getItemInHand().clone()); + } else if (args[0].equalsIgnoreCase("hand")) { + is.add(user.getItemInHand().clone()); + } else if (args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("invent") || args[0].equalsIgnoreCase("all")) { + for (ItemStack stack : user.getBase().getInventory().getContents()) { + if (stack == null || stack.getType() == Material.AIR) { + continue; + } + is.add(stack.clone()); + } + } else if (args[0].equalsIgnoreCase("blocks")) { + for (ItemStack stack : user.getBase().getInventory().getContents()) { + if (stack == null || stack.getType() == Material.AIR) { + continue; + } + is.add(stack.clone()); + } + } else { + is.add(get(args[0])); + } + + if (is.isEmpty() || is.get(0).getType() == Material.AIR) { + throw new Exception(tl("itemSellAir")); + } + + return is; + } + + @Override + public String names(ItemStack item) { + List nameList = nameList(item); + + if (nameList.size() > 15) { + nameList = nameList.subList(0, 14); + } + return StringUtil.joinList(", ", nameList); + } + + @Override + public String serialize(ItemStack is) { + String mat = is.getType().name(); + if (is.getData().getData() != 0) { + mat = mat + ":" + is.getData().getData(); + } + int quantity = is.getAmount(); + StringBuilder sb = new StringBuilder(); // Add space AFTER you add something. We can trim at end. + sb.append(mat).append(" ").append(quantity).append(" "); + + // ItemMeta applies to anything. + if (is.hasItemMeta()) { + ItemMeta meta = is.getItemMeta(); + if (meta.hasDisplayName()) { + sb.append("name:").append(meta.getDisplayName().replaceAll(" ", "_")).append(" "); + } + + if (meta.hasLore()) { + sb.append("lore:"); + boolean first = true; + for (String s : meta.getLore()) { + // Add | before the line if it's not the first one. Easy but weird way + // to do this since we need each line separated by | + if (!first) { + sb.append("|"); + } + first = false; + sb.append(s.replaceAll(" ", "_")); + } + sb.append(" "); + } + + if (meta.hasEnchants()) { + for (Enchantment e : meta.getEnchants().keySet()) { + sb.append(e.getName().toLowerCase()).append(":").append(meta.getEnchantLevel(e)).append(" "); + } + } + + Set flags = meta.getItemFlags(); + if (flags != null && !flags.isEmpty()) { + sb.append("itemflags:"); + boolean first = true; + for (ItemFlag flag : flags) { + if (!first) { + sb.append(","); + } + sb.append(flag.name()); + first = false; + } + } + } + + switch (is.getType()) { + case WRITTEN_BOOK: + // Everything from http://wiki.ess3.net/wiki/Item_Meta#Books in that order. + // Interesting as I didn't see a way to do pages or chapters. + BookMeta bookMeta = (BookMeta) is.getItemMeta(); + if (bookMeta.hasTitle()) { + sb.append("title:").append(bookMeta.getTitle()).append(" "); + } + if (bookMeta.hasAuthor()) { + sb.append("author:").append(bookMeta.getAuthor()).append(" "); + } + // Only other thing it could have is lore but that's done up there ^^^ + break; + case ENCHANTED_BOOK: + EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) is.getItemMeta(); + for (Enchantment e : enchantmentStorageMeta.getStoredEnchants().keySet()) { + sb.append(e.getName().toLowerCase()).append(":").append(enchantmentStorageMeta.getStoredEnchantLevel(e)).append(" "); + } + break; + case FIREWORK_ROCKET: + case FIREWORK_STAR: + // Everything from http://wiki.ess3.net/wiki/Item_Meta#Fireworks in that order. + FireworkMeta fireworkMeta = (FireworkMeta) is.getItemMeta(); + if (fireworkMeta.hasEffects()) { + for (FireworkEffect effect : fireworkMeta.getEffects()) { + if (effect.getColors() != null && !effect.getColors().isEmpty()) { + sb.append("color:"); + boolean first = true; + for (Color c : effect.getColors()) { + if (!first) { + sb.append(","); // same thing as above. + } + sb.append(c.toString()); + first = false; + } + sb.append(" "); + } + + sb.append("shape: ").append(effect.getType().name()).append(" "); + if (effect.getFadeColors() != null && !effect.getFadeColors().isEmpty()) { + sb.append("fade:"); + boolean first = true; + for (Color c : effect.getFadeColors()) { + if (!first) { + sb.append(","); // same thing as above. + } + sb.append(c.toString()); + first = false; + } + sb.append(" "); + } + } + sb.append("power: ").append(fireworkMeta.getPower()).append(" "); + } + break; + case POTION: + Potion potion = Potion.fromItemStack(is); + for (PotionEffect e : potion.getEffects()) { + // long but needs to be effect:speed power:2 duration:120 in that order. + sb.append("splash:").append(potion.isSplash()).append(" ").append("effect:").append(e.getType().getName().toLowerCase()).append(" ").append("power:").append(e.getAmplifier()).append(" ").append("duration:").append(e.getDuration() / 20).append(" "); + } + break; + case SKELETON_SKULL: + case WITHER_SKELETON_SKULL: + // item stack with meta + SkullMeta skullMeta = (SkullMeta) is.getItemMeta(); + if (skullMeta != null && skullMeta.hasOwner()) { + sb.append("player:").append(skullMeta.getOwner()).append(" "); + } + break; + case LEATHER_HELMET: + case LEATHER_CHESTPLATE: + case LEATHER_LEGGINGS: + case LEATHER_BOOTS: + LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) is.getItemMeta(); + int rgb = leatherArmorMeta.getColor().asRGB(); + sb.append("color:").append(rgb).append(" "); + break; + case BLACK_BANNER: + case BLUE_BANNER: + case BROWN_BANNER: + case CYAN_BANNER: + case GRAY_BANNER: + case GREEN_BANNER: + case LIGHT_BLUE_BANNER: + case LIGHT_GRAY_BANNER: + case LIME_BANNER: + case MAGENTA_BANNER: + case ORANGE_BANNER: + case PINK_BANNER: + case PURPLE_BANNER: + case RED_BANNER: + case WHITE_BANNER: + case YELLOW_BANNER: + BannerMeta bannerMeta = (BannerMeta) is.getItemMeta(); + if (bannerMeta != null) { + int basecolor = bannerMeta.getBaseColor().getColor().asRGB(); + sb.append("basecolor:").append(basecolor).append(" "); + for (org.bukkit.block.banner.Pattern p : bannerMeta.getPatterns()) { + String type = p.getPattern().getIdentifier(); + int color = p.getColor().getColor().asRGB(); + sb.append(type).append(",").append(color).append(" "); + } + } + break; + case SHIELD: + // Hacky fix for accessing Shield meta - https://github.com/drtshock/Essentials/pull/745#issuecomment-234843795 + BlockStateMeta shieldMeta = (BlockStateMeta) is.getItemMeta(); + Banner shieldBannerMeta = (Banner) shieldMeta.getBlockState(); + int basecolor = shieldBannerMeta.getBaseColor().getColor().asRGB(); + sb.append("basecolor:").append(basecolor).append(" "); + for (org.bukkit.block.banner.Pattern p : shieldBannerMeta.getPatterns()) { + String type = p.getPattern().getIdentifier(); + int color = p.getColor().getColor().asRGB(); + sb.append(type).append(",").append(color).append(" "); + } + break; + } + + return sb.toString().trim().replaceAll("§", "&"); + } } diff --git a/Essentials/src/com/earth2me/essentials/items/FlatItemDb.java b/Essentials/src/com/earth2me/essentials/items/FlatItemDb.java index c2af18e7b..346f03c1e 100644 --- a/Essentials/src/com/earth2me/essentials/items/FlatItemDb.java +++ b/Essentials/src/com/earth2me/essentials/items/FlatItemDb.java @@ -1,25 +1,15 @@ package com.earth2me.essentials.items; -import com.earth2me.essentials.IConf; import com.earth2me.essentials.ManagedFile; -import com.earth2me.essentials.User; -import com.earth2me.essentials.utils.StringUtil; import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import net.ess3.api.IEssentials; -import org.bukkit.Color; -import org.bukkit.FireworkEffect; import org.bukkit.Material; -import org.bukkit.block.Banner; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.*; -import org.bukkit.potion.Potion; +import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.potion.PotionData; -import org.bukkit.potion.PotionEffect; import java.util.*; import java.util.logging.Logger; @@ -56,7 +46,7 @@ public class FlatItemDb extends AbstractItemDb { } this.rebuild(); - LOGGER.info(String.format("Loaded %s items.", listNames().size())); + LOGGER.info(String.format("Loaded %s items from items.json.", listNames().size())); } private void rebuild() { @@ -103,13 +93,6 @@ public class FlatItemDb extends AbstractItemDb { } } - @Override - public ItemStack get(final String id, final int quantity) throws Exception { - ItemStack is = get(id); - is.setAmount(quantity); - return is; - } - @Override public ItemStack get(final String id) throws Exception { ItemData data = getByName(id); @@ -144,49 +127,6 @@ public class FlatItemDb extends AbstractItemDb { return null; } - @Override - public List getMatching(User user, String[] args) throws Exception { - List is = new ArrayList<>(); - - if (args.length < 1) { - is.add(user.getItemInHand().clone()); - } else if (args[0].equalsIgnoreCase("hand")) { - is.add(user.getItemInHand().clone()); - } else if (args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("invent") || args[0].equalsIgnoreCase("all")) { - for (ItemStack stack : user.getBase().getInventory().getContents()) { - if (stack == null || stack.getType() == Material.AIR) { - continue; - } - is.add(stack.clone()); - } - } else if (args[0].equalsIgnoreCase("blocks")) { - for (ItemStack stack : user.getBase().getInventory().getContents()) { - if (stack == null || stack.getType() == Material.AIR) { - continue; - } - is.add(stack.clone()); - } - } else { - is.add(get(args[0])); - } - - if (is.isEmpty() || is.get(0).getType() == Material.AIR) { - throw new Exception(tl("itemSellAir")); - } - - return is; - } - - @Override - public String names(ItemStack item) { - List nameList = nameList(item); - - if (nameList.size() > 15) { - nameList = nameList.subList(0, 14); - } - return StringUtil.joinList(", ", nameList); - } - @Override public List nameList(ItemStack item) { List names = new ArrayList<>(); @@ -222,180 +162,6 @@ public class FlatItemDb extends AbstractItemDb { return null; } - @Override - public String serialize(ItemStack is) { - String mat = is.getType().name(); - if (is.getData().getData() != 0) { - mat = mat + ":" + is.getData().getData(); - } - int quantity = is.getAmount(); - StringBuilder sb = new StringBuilder(); // Add space AFTER you add something. We can trim at end. - sb.append(mat).append(" ").append(quantity).append(" "); - - // ItemMeta applies to anything. - if (is.hasItemMeta()) { - ItemMeta meta = is.getItemMeta(); - if (meta.hasDisplayName()) { - sb.append("name:").append(meta.getDisplayName().replaceAll(" ", "_")).append(" "); - } - - if (meta.hasLore()) { - sb.append("lore:"); - boolean first = true; - for (String s : meta.getLore()) { - // Add | before the line if it's not the first one. Easy but weird way - // to do this since we need each line separated by | - if (!first) { - sb.append("|"); - } - first = false; - sb.append(s.replaceAll(" ", "_")); - } - sb.append(" "); - } - - if (meta.hasEnchants()) { - for (Enchantment e : meta.getEnchants().keySet()) { - sb.append(e.getName().toLowerCase()).append(":").append(meta.getEnchantLevel(e)).append(" "); - } - } - - Set flags = meta.getItemFlags(); - if (flags != null && !flags.isEmpty()) { - sb.append("itemflags:"); - boolean first = true; - for (ItemFlag flag : flags) { - if (!first) { - sb.append(","); - } - sb.append(flag.name()); - first = false; - } - } - } - - switch (is.getType()) { - case WRITTEN_BOOK: - // Everything from http://wiki.ess3.net/wiki/Item_Meta#Books in that order. - // Interesting as I didn't see a way to do pages or chapters. - BookMeta bookMeta = (BookMeta) is.getItemMeta(); - if (bookMeta.hasTitle()) { - sb.append("title:").append(bookMeta.getTitle()).append(" "); - } - if (bookMeta.hasAuthor()) { - sb.append("author:").append(bookMeta.getAuthor()).append(" "); - } - // Only other thing it could have is lore but that's done up there ^^^ - break; - case ENCHANTED_BOOK: - EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) is.getItemMeta(); - for (Enchantment e : enchantmentStorageMeta.getStoredEnchants().keySet()) { - sb.append(e.getName().toLowerCase()).append(":").append(enchantmentStorageMeta.getStoredEnchantLevel(e)).append(" "); - } - break; - case FIREWORK_ROCKET: - case FIREWORK_STAR: - // Everything from http://wiki.ess3.net/wiki/Item_Meta#Fireworks in that order. - FireworkMeta fireworkMeta = (FireworkMeta) is.getItemMeta(); - if (fireworkMeta.hasEffects()) { - for (FireworkEffect effect : fireworkMeta.getEffects()) { - if (effect.getColors() != null && !effect.getColors().isEmpty()) { - sb.append("color:"); - boolean first = true; - for (Color c : effect.getColors()) { - if (!first) { - sb.append(","); // same thing as above. - } - sb.append(c.toString()); - first = false; - } - sb.append(" "); - } - - sb.append("shape: ").append(effect.getType().name()).append(" "); - if (effect.getFadeColors() != null && !effect.getFadeColors().isEmpty()) { - sb.append("fade:"); - boolean first = true; - for (Color c : effect.getFadeColors()) { - if (!first) { - sb.append(","); // same thing as above. - } - sb.append(c.toString()); - first = false; - } - sb.append(" "); - } - } - sb.append("power: ").append(fireworkMeta.getPower()).append(" "); - } - break; - case POTION: - Potion potion = Potion.fromItemStack(is); - for (PotionEffect e : potion.getEffects()) { - // long but needs to be effect:speed power:2 duration:120 in that order. - sb.append("splash:").append(potion.isSplash()).append(" ").append("effect:").append(e.getType().getName().toLowerCase()).append(" ").append("power:").append(e.getAmplifier()).append(" ").append("duration:").append(e.getDuration() / 20).append(" "); - } - break; - case SKELETON_SKULL: - case WITHER_SKELETON_SKULL: - // item stack with meta - SkullMeta skullMeta = (SkullMeta) is.getItemMeta(); - if (skullMeta != null && skullMeta.hasOwner()) { - sb.append("player:").append(skullMeta.getOwner()).append(" "); - } - break; - case LEATHER_HELMET: - case LEATHER_CHESTPLATE: - case LEATHER_LEGGINGS: - case LEATHER_BOOTS: - LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) is.getItemMeta(); - int rgb = leatherArmorMeta.getColor().asRGB(); - sb.append("color:").append(rgb).append(" "); - break; - case BLACK_BANNER: - case BLUE_BANNER: - case BROWN_BANNER: - case CYAN_BANNER: - case GRAY_BANNER: - case GREEN_BANNER: - case LIGHT_BLUE_BANNER: - case LIGHT_GRAY_BANNER: - case LIME_BANNER: - case MAGENTA_BANNER: - case ORANGE_BANNER: - case PINK_BANNER: - case PURPLE_BANNER: - case RED_BANNER: - case WHITE_BANNER: - case YELLOW_BANNER: - BannerMeta bannerMeta = (BannerMeta) is.getItemMeta(); - if (bannerMeta != null) { - int basecolor = bannerMeta.getBaseColor().getColor().asRGB(); - sb.append("basecolor:").append(basecolor).append(" "); - for (org.bukkit.block.banner.Pattern p : bannerMeta.getPatterns()) { - String type = p.getPattern().getIdentifier(); - int color = p.getColor().getColor().asRGB(); - sb.append(type).append(",").append(color).append(" "); - } - } - break; - case SHIELD: - // Hacky fix for accessing Shield meta - https://github.com/drtshock/Essentials/pull/745#issuecomment-234843795 - BlockStateMeta shieldMeta = (BlockStateMeta) is.getItemMeta(); - Banner shieldBannerMeta = (Banner) shieldMeta.getBlockState(); - int basecolor = shieldBannerMeta.getBaseColor().getColor().asRGB(); - sb.append("basecolor:").append(basecolor).append(" "); - for (org.bukkit.block.banner.Pattern p : shieldBannerMeta.getPatterns()) { - String type = p.getPattern().getIdentifier(); - int color = p.getColor().getColor().asRGB(); - sb.append(type).append(",").append(color).append(" "); - } - break; - } - - return sb.toString().trim().replaceAll("§", "&"); - } - @Override @Deprecated public Material getFromLegacyId(int id) { diff --git a/Essentials/src/com/earth2me/essentials/items/LegacyItemDb.java b/Essentials/src/com/earth2me/essentials/items/LegacyItemDb.java index a270b1cc5..fc7ef3ef6 100644 --- a/Essentials/src/com/earth2me/essentials/items/LegacyItemDb.java +++ b/Essentials/src/com/earth2me/essentials/items/LegacyItemDb.java @@ -1,22 +1,14 @@ package com.earth2me.essentials.items; import com.earth2me.essentials.ManagedFile; -import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.NumberUtil; -import com.earth2me.essentials.utils.StringUtil; import net.ess3.api.IEssentials; import net.ess3.nms.refl.ReflUtil; import org.apache.commons.lang.StringUtils; -import org.bukkit.Color; -import org.bukkit.FireworkEffect; import org.bukkit.Material; -import org.bukkit.block.Banner; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.*; -import org.bukkit.potion.Potion; -import org.bukkit.potion.PotionEffect; import java.util.*; import java.util.logging.Logger; @@ -126,13 +118,8 @@ public class LegacyItemDb extends AbstractItemDb { for (List nameList : names.values()) { Collections.sort(nameList, LengthCompare.INSTANCE); } - } - @Override - public ItemStack get(final String id, final int quantity) throws Exception { - final ItemStack retval = get(id.toLowerCase(Locale.ENGLISH)); - retval.setAmount(quantity); - return retval; + LOGGER.info(String.format("Loaded %s items from items.csv.", listNames().size())); } @Override @@ -183,12 +170,7 @@ public class LegacyItemDb extends AbstractItemDb { } retval = ess.getServer().getUnsafe().modifyItemStack(retval, nbt); } - Material MOB_SPAWNER; - try { - MOB_SPAWNER = Material.SPAWNER; - } catch (Exception e) { - MOB_SPAWNER = Material.valueOf("MOB_SPAWNER"); - } + Material MOB_SPAWNER = EnumUtil.getMaterial("SPAWNER", "MOB_SPAWNER"); if (mat == MOB_SPAWNER) { if (metaData == 0) metaData = EntityType.PIG.getTypeId(); try { @@ -196,7 +178,7 @@ public class LegacyItemDb extends AbstractItemDb { } catch (IllegalArgumentException e) { throw new Exception("Can't spawn entity ID " + metaData + " from mob spawners."); } - } else if (mat == Material.LEGACY_MONSTER_EGG) { + } else if (mat.name().contains("MONSTER_EGG")) { EntityType type; try { type = EntityType.fromId(metaData); @@ -214,49 +196,6 @@ public class LegacyItemDb extends AbstractItemDb { return retval; } - @Override - public List getMatching(User user, String[] args) throws Exception { - List is = new ArrayList<>(); - - if (args.length < 1) { - is.add(user.getItemInHand().clone()); - } else if (args[0].equalsIgnoreCase("hand")) { - is.add(user.getItemInHand().clone()); - } else if (args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("invent") || args[0].equalsIgnoreCase("all")) { - for (ItemStack stack : user.getBase().getInventory().getContents()) { - if (stack == null || stack.getType() == Material.AIR) { - continue; - } - is.add(stack.clone()); - } - } else if (args[0].equalsIgnoreCase("blocks")) { - for (ItemStack stack : user.getBase().getInventory().getContents()) { - if (stack == null || stack.getType() == Material.AIR) { - continue; - } - is.add(stack.clone()); - } - } else { - is.add(get(args[0])); - } - - if (is.isEmpty() || is.get(0).getType() == Material.AIR) { - throw new Exception(tl("itemSellAir")); - } - - return is; - } - - @Override - public String names(ItemStack item) { - List nameList = nameList(item); - - if (nameList.size() > 15) { - nameList = nameList.subList(0, 14); - } - return StringUtil.joinList(", ", nameList); - } - @Override public List nameList(ItemStack item) { ItemData itemData = new ItemData(item.getType(), item.getDurability()); @@ -286,167 +225,6 @@ public class LegacyItemDb extends AbstractItemDb { return name; } - @Override - public String serialize(ItemStack is) { - String mat = is.getType().name(); - if (is.getData().getData() != 0) { - mat = mat + ":" + is.getData().getData(); - } - int quantity = is.getAmount(); - StringBuilder sb = new StringBuilder(); // Add space AFTER you add something. We can trim at end. - sb.append(mat).append(" ").append(quantity).append(" "); - - // ItemMeta applies to anything. - if (is.hasItemMeta()) { - ItemMeta meta = is.getItemMeta(); - if (meta.hasDisplayName()) { - sb.append("name:").append(meta.getDisplayName().replaceAll(" ", "_")).append(" "); - } - - if (meta.hasLore()) { - sb.append("lore:"); - boolean first = true; - for (String s : meta.getLore()) { - // Add | before the line if it's not the first one. Easy but weird way - // to do this since we need each line separated by | - if (!first) { - sb.append("|"); - } - first = false; - sb.append(s.replaceAll(" ", "_")); - } - sb.append(" "); - } - - if (meta.hasEnchants()) { - for (Enchantment e : meta.getEnchants().keySet()) { - sb.append(e.getName().toLowerCase()).append(":").append(meta.getEnchantLevel(e)).append(" "); - } - } - } - - switch (is.getType()) { - case WRITTEN_BOOK: - // Everything from http://wiki.ess3.net/wiki/Item_Meta#Books in that order. - // Interesting as I didn't see a way to do pages or chapters. - BookMeta bookMeta = (BookMeta) is.getItemMeta(); - if (bookMeta.hasTitle()) { - sb.append("title:").append(bookMeta.getTitle()).append(" "); - } - if (bookMeta.hasAuthor()) { - sb.append("author:").append(bookMeta.getAuthor()).append(" "); - } - // Only other thing it could have is lore but that's done up there ^^^ - break; - case ENCHANTED_BOOK: - EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) is.getItemMeta(); - for (Enchantment e : enchantmentStorageMeta.getStoredEnchants().keySet()) { - sb.append(e.getName().toLowerCase()).append(":").append(enchantmentStorageMeta.getStoredEnchantLevel(e)).append(" "); - } - break; - case FIREWORK_ROCKET: - case FIREWORK_STAR: - // Everything from http://wiki.ess3.net/wiki/Item_Meta#Fireworks in that order. - FireworkMeta fireworkMeta = (FireworkMeta) is.getItemMeta(); - if (fireworkMeta.hasEffects()) { - for (FireworkEffect effect : fireworkMeta.getEffects()) { - if (effect.getColors() != null && !effect.getColors().isEmpty()) { - sb.append("color:"); - boolean first = true; - for (Color c : effect.getColors()) { - if (!first) { - sb.append(","); // same thing as above. - } - sb.append(c.toString()); - first = false; - } - sb.append(" "); - } - - sb.append("shape: ").append(effect.getType().name()).append(" "); - if (effect.getFadeColors() != null && !effect.getFadeColors().isEmpty()) { - sb.append("fade:"); - boolean first = true; - for (Color c : effect.getFadeColors()) { - if (!first) { - sb.append(","); // same thing as above. - } - sb.append(c.toString()); - first = false; - } - sb.append(" "); - } - } - sb.append("power: ").append(fireworkMeta.getPower()).append(" "); - } - break; - case POTION: - Potion potion = Potion.fromItemStack(is); - for (PotionEffect e : potion.getEffects()) { - // long but needs to be effect:speed power:2 duration:120 in that order. - sb.append("splash:").append(potion.isSplash()).append(" ").append("effect:").append(e.getType().getName().toLowerCase()).append(" ").append("power:").append(e.getAmplifier()).append(" ").append("duration:").append(e.getDuration() / 20).append(" "); - } - break; - case SKELETON_SKULL: - case WITHER_SKELETON_SKULL: - // item stack with meta - SkullMeta skullMeta = (SkullMeta) is.getItemMeta(); - if (skullMeta != null && skullMeta.hasOwner()) { - sb.append("player:").append(skullMeta.getOwner()).append(" "); - } - break; - case LEATHER_HELMET: - case LEATHER_CHESTPLATE: - case LEATHER_LEGGINGS: - case LEATHER_BOOTS: - LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) is.getItemMeta(); - int rgb = leatherArmorMeta.getColor().asRGB(); - sb.append("color:").append(rgb).append(" "); - break; - case BLACK_BANNER: - case BLUE_BANNER: - case BROWN_BANNER: - case CYAN_BANNER: - case GRAY_BANNER: - case GREEN_BANNER: - case LIGHT_BLUE_BANNER: - case LIGHT_GRAY_BANNER: - case LIME_BANNER: - case MAGENTA_BANNER: - case ORANGE_BANNER: - case PINK_BANNER: - case PURPLE_BANNER: - case RED_BANNER: - case WHITE_BANNER: - case YELLOW_BANNER: - BannerMeta bannerMeta = (BannerMeta) is.getItemMeta(); - if (bannerMeta != null) { - int basecolor = bannerMeta.getBaseColor().getColor().asRGB(); - sb.append("basecolor:").append(basecolor).append(" "); - for (org.bukkit.block.banner.Pattern p : bannerMeta.getPatterns()) { - String type = p.getPattern().getIdentifier(); - int color = p.getColor().getColor().asRGB(); - sb.append(type).append(",").append(color).append(" "); - } - } - break; - case SHIELD: - // Hacky fix for accessing Shield meta - https://github.com/drtshock/Essentials/pull/745#issuecomment-234843795 - BlockStateMeta shieldMeta = (BlockStateMeta) is.getItemMeta(); - Banner shieldBannerMeta = (Banner) shieldMeta.getBlockState(); - int basecolor = shieldBannerMeta.getBaseColor().getColor().asRGB(); - sb.append("basecolor:").append(basecolor).append(" "); - for (org.bukkit.block.banner.Pattern p : shieldBannerMeta.getPatterns()) { - String type = p.getPattern().getIdentifier(); - int color = p.getColor().getColor().asRGB(); - sb.append(type).append(",").append(color).append(" "); - } - break; - } - - return sb.toString().trim().replaceAll("§", "&"); - } - @Override public Material getFromLegacyId(int id) { ItemData data = this.legacyIds.get(id);