Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author SkyVox
- */
- public class ItemAPI {
- private List<StringBuilder> items = new ArrayList<>();
- private Logger logger;
- /**
- * @param plugin
- */
- public ItemAPI(Plugin plugin) {
- this.logger = plugin.getLogger();
- }
- /**
- * This will convert your file item to StringBuilder.
- *
- * @param file
- * @param location
- * @return StringBuilder
- */
- public StringBuilder convert(FileConfiguration file, String location) {
- String type = null; // Item Type cannot be null.
- short data = (file.contains(location + ".data") ? Short.parseShort(file.getString(location + ".data")) : Byte.valueOf((byte) 0));
- int amount = (file.contains(location + ".amount") ? Integer.parseInt(file.getString(location + ".amount")) : Integer.valueOf(0));
- String name = (file.contains(location + ".name") ? (name = file.getString(location + ".name").replace("§", "&")) : null);
- try {
- type = Integer.toString(file.getInt(location + ".type"));
- } catch (NumberFormatException ex) {
- if (file.getString(location + ".type").contains(":")) {
- String[] typeSplit = file.getString(location + ".type").split(":");
- type = typeSplit[0];
- try {
- data = Byte.valueOf((byte) Integer.parseInt(typeSplit[1]));
- } catch (NumberFormatException ex2) {
- data = Byte.valueOf((byte) 0);
- }
- } else {
- type = file.getString(location + ".type").toUpperCase();
- }
- }
- // Create Final Item as StringBuilder.
- StringBuilder strFinalItem = new StringBuilder();
- strFinalItem.append("<type>" + type + "</type>");
- strFinalItem.append("<data>" + Short.toString(data) + "</data>");
- strFinalItem.append("<amount>" + Integer.toString(amount) + "</amount>");
- strFinalItem.append("<name>" + name + "</name>");
- // Add lore to Final Item if contains lore on File.
- if (file.contains(location + ".lore")) {
- int i = 0;
- int size = file.getStringList(location + ".lore").size();
- strFinalItem.append("<lore>");
- for (String lore : file.getStringList(location + ".lore")) {
- strFinalItem.append(lore);
- i+=1;
- if (i <= size) {
- strFinalItem.append("<break%lore>");
- }
- }
- strFinalItem.append("</lore>");
- }
- // Add enchant to Final Item if contains enchants on File.
- if (file.contains(location + ".enchants")) {
- int i = 0;
- int size = file.getStringList(location + ".enchants").size();
- String enchantmentName = null;
- Enchantment enchantment = null;
- strFinalItem.append("<enchant>");
- for (String enchants : file.getStringList(location + ".enchants")) {
- try {
- if (enchants.contains(",")) {
- enchantmentName = enchants.split(",")[0].toUpperCase();
- enchantment = Enchantment.getByName(enchantmentName);
- strFinalItem.append(enchantment.getName() + "," + Integer.parseInt(enchants.split(",")[1]));
- } else {
- enchantmentName = enchants.toUpperCase();
- enchantment = Enchantment.getByName(enchantmentName);
- strFinalItem.append(enchantment.getName() + "," + Integer.toString(1));
- }
- i+=1;
- if (i <= size) {
- strFinalItem.append("<break%enchant>");
- }
- } catch (ArrayIndexOutOfBoundsException ex) {
- logger.log(Level.WARNING, "Could not found enchantment name or enchantment level!");
- } catch (NumberFormatException ex) {
- logger.log(Level.WARNING, "Could not parse to integer '" + enchants.split(",")[1] + "'!");
- } catch (NullPointerException ex) {
- logger.log(Level.WARNING, "Could not found any Enchantment named by '" + enchantmentName + "'!");
- }
- }
- enchantmentName = null;
- enchantment = null;
- strFinalItem.append("</enchant>");
- }
- /*
- * Here some extra things that can be added.
- */
- // Item Price.
- if (file.contains(location + ".price")) {
- strFinalItem.append("<price>" + file.getDouble(location + ".price") + "</price>");
- }
- // Item Slot.
- if (file.contains(location + ".slot")) {
- strFinalItem.append("<slot>" + file.getInt(location + ".slot") + "</slot>");
- }
- // Item Action. TODO: Improve this.
- if (file.contains(location + ".action")) {
- strFinalItem.append("<action>" + file.getString(location + ".action").toUpperCase() + "</action>");
- }
- // Add glow.
- if (file.contains(location + ".glow")) {
- strFinalItem.append("<glow>" + Boolean.toString(file.getString(location + ".glow").equalsIgnoreCase("true")) + "</glow>");
- }
- // Convert File Item to StringBuilder Item!
- // Return FinalItem.
- return strFinalItem;
- }
- /**
- * Convert your ItemStack to StringBuilder.
- * All values will return as HTML syntax.
- *
- * @param item
- * @return StringBuilder
- */
- public StringBuilder serializer(ItemStack item) throws NullPointerException {
- if (!item.hasItemMeta()) {
- logger.log(Level.SEVERE, "Could not convert ItemStack to StringBuilder, This Item doesn't has ItemMeta!");
- return null;
- }
- String type = item.getType().toString().toUpperCase();
- short data = item.getDurability();
- int amount = item.getAmount();
- String name = (item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName().replace("§", "&") : ChatColor.GRAY + "");
- // Final Item, as StringBuilder.
- StringBuilder strFinalItem = new StringBuilder();
- strFinalItem.append("<type>" + type + "</type>");
- strFinalItem.append("<data>" + Short.toString(data) + "</data>");
- strFinalItem.append("<amount>" + Integer.toString(amount) + "</amount>");
- strFinalItem.append("<name>" + name + "</name>");
- // Add lore to Final Item.
- if (item.getItemMeta().hasLore()) {
- int i = 0;
- int size = item.getItemMeta().getLore().size();
- strFinalItem.append("<lore>");
- for (String lore : item.getItemMeta().getLore()) {
- strFinalItem.append(lore);
- i+=1;
- if (i <= size) {
- strFinalItem.append("<break%lore>");
- }
- }
- strFinalItem.append("</lore>");
- }
- // Add enchantment to Final Item.
- if (item.getItemMeta().hasEnchants()) {
- int i = 0;
- int size = item.getItemMeta().getEnchants().keySet().size();
- strFinalItem.append("<enchant>");
- for (Enchantment enchant : item.getItemMeta().getEnchants().keySet()) {
- try {
- strFinalItem.append(enchant.getName().toUpperCase() + "," + item.getEnchantmentLevel(enchant));
- i+=1;
- if (i <= size) {
- strFinalItem.append("<break%enchant>");
- }
- } catch (Exception ignore) {}
- }
- strFinalItem.append("</enchant>");
- }
- // Convert ItemStack to StringBuilder Item!
- // Return FinalItem.
- return strFinalItem;
- }
- /**
- * Returns Final Item added an extra syntax.
- *
- * @param strFinalItem
- * @param price
- * @return Final Item plus extra Price
- */
- public StringBuilder extraPrice(StringBuilder strFinalItem, double price) {
- return strFinalItem.append("<price>" + Double.toString(price) + "</price>");
- }
- /**
- * Returns Final Item added an extra syntax.
- *
- * @param strFinalItem
- * @param slot
- * @return Final Item plus extra Inventory Slot
- */
- public StringBuilder extraInventorySlot(StringBuilder strFinalItem, int slot) {
- return strFinalItem.append("<slot>" + Integer.toString(slot) + "</slot>");
- }
- /**
- * Returns Final Item added an extra syntax.
- *
- * @param strFinalItem
- * @param action
- * @return Final Item plus extra Action
- */
- public StringBuilder extraAction(StringBuilder strFinalItem, String action) {
- return strFinalItem.append("<action>" + action + "</action>");
- }
- /**
- * Returns Final Item added an extra syntax.
- *
- * @param strFinalItem
- * @param glow
- * @return Final Item plus extra Glow
- */
- public StringBuilder extraGlow(StringBuilder strFinalItem, boolean glow) {
- return strFinalItem.append("<glow>" + Boolean.toString(glow) + "</glow>");
- }
- /**
- * Convert your Final Item to ItemStack.
- *
- * @param finalItem
- * @return FinalItem as ItemStack
- */
- public ItemStack deserializer(StringBuilder strFinalItem) {
- Object type = getType(strFinalItem);
- short data = getData(strFinalItem);
- int amount = getAmount(strFinalItem);
- String name = getName(strFinalItem);
- List<String> lore = getLore(strFinalItem);
- HashMap<Enchantment, Integer> enchantment = getEnchantments(strFinalItem);
- // New ItemStack.
- ItemStack item = null;
- // Check if 'TYPE' are Integer or String.
- switch (type.getClass().getSimpleName().toUpperCase()) {
- case("STRING"):
- item = new ItemStack(Material.getMaterial((String) type), amount, data);
- break;
- case("INT"):
- case("INTEGER"):
- item = new ItemStack(Material.getMaterial((int) type), amount, data);
- break;
- }
- ItemMeta meta = item.getItemMeta();
- // Set Name.
- if (name != null) {
- meta.setDisplayName(name);
- } else {
- String[] itemName = item.getType().toString().split("_");
- for (int i = 0; i < itemName.length; i++) {
- if (name != null) {
- name = (name + Character.toUpperCase(itemName[0].charAt(0)) + itemName[0].substring(1));
- } else {
- name = Character.toUpperCase(itemName[0].charAt(0)) + itemName[0].substring(1);
- }
- }
- meta.setDisplayName(name);
- }
- // Add lore.
- if (lore != null) {
- meta.setLore(lore);
- }
- // Add enchantment
- if (enchantment != null) {
- for (Entry<Enchantment, Integer> entries : enchantment.entrySet()) {
- meta.addEnchant(entries.getKey(), entries.getValue(), true);
- }
- }
- // Set Item glow.
- if (isGlow(strFinalItem)) {
- meta.addEnchant(Enchantment.OXYGEN, 1, true);
- meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
- }
- // Set ItemMeta.
- item.setItemMeta(meta);
- // Return ItemStack.
- return item;
- }
- /**
- * This method will return the Inventory Item slot.
- * Will return -1 if slot does not exist on Final Item.
- *
- * @param strFinalItem
- * @return int slot
- */
- public int getSlot(StringBuilder strFinalItem) {
- try {
- String slot = strFinalItem.toString().split("<slot>")[1];
- return Integer.parseInt(slot.substring(0, slot.indexOf("</slot>")));
- } catch (ArrayIndexOutOfBoundsException ignore) {}
- // Return -1, this Final Item does not have Slot param.
- return -1;
- }
- /**
- * This method will return the Item Type as a Object,
- * And this Object can be INTEGER or STRING.
- *
- * Integer - Item ID.
- * String - Item Name.
- *
- * @throws NullPointerException
- * @param strFinalItem
- * @return Object type
- */
- public Object getType(StringBuilder strFinalItem) throws NullPointerException {
- try {
- String type = strFinalItem.toString().split("<type>")[1];
- return type.substring(0, type.indexOf("</type>"));
- } catch (ArrayIndexOutOfBoundsException ignore) {}
- // Will return null if Final Item does not have type.
- return null;
- }
- /**
- * This method will return the Item Data.
- * If Final Item does not have Item Data will return 0 (by default).
- *
- * @param strFinalItem
- * @return short
- */
- public short getData(StringBuilder strFinalItem) {
- try {
- String data = strFinalItem.toString().split("<data>")[1];
- return Short.parseShort(data.substring(0, data.indexOf("</data>")));
- } catch (ArrayIndexOutOfBoundsException ignore) {}
- // If Final Item does not have Item Data will return 0 (by default).
- // Return 0.
- return (short) 0;
- }
- /**
- * This method will return the Item amount.
- * If Final Item does not have Item amount will return 1 (by default).
- *
- * @param strFinalItem
- * @return int
- */
- public int getAmount(StringBuilder strFinalItem) {
- try {
- String amount = strFinalItem.toString().split("<amount>")[1];
- return Integer.parseInt(amount.substring(0, amount.indexOf("</amount>")));
- } catch (ArrayIndexOutOfBoundsException ignore) {}
- // If Final Item does not have Item amount will return 1 (by default).
- return 1;
- }
- /**
- * This method will return the Item name.
- * Will return null if name does not exist on Final Item.
- *
- * @param strFinalItem
- * @return String
- */
- public String getName(StringBuilder strFinalItem) throws NullPointerException {
- try {
- String name = strFinalItem.toString().split("<name>")[1];
- return ChatColor.translateAlternateColorCodes('&', name.substring(0, name.indexOf("</name>")));
- } catch (ArrayIndexOutOfBoundsException ignore) {}
- // Return null if name does not exist on Final Item.
- return null;
- }
- /**
- * This method will check if has Glow on Final Item.
- *
- * @param strFinalItem
- * @return
- */
- public boolean isGlow(StringBuilder strFinalItem) {
- try {
- String glow = strFinalItem.toString().split("<glow>")[1];
- return glow.substring(0, glow.indexOf("</amount>")).equalsIgnoreCase("true");
- } catch (ArrayIndexOutOfBoundsException ignore) {}
- // Return false if glow does not exist on Final Item.
- return false;
- }
- /**
- * This method will return the Item lore.
- *
- * @param strFinalItem
- * @return List string
- */
- public List<String> getLore(StringBuilder strFinalItem) {
- List<String> itemLore = new ArrayList<>();
- String lore = null;
- try {
- lore = strFinalItem.toString().split("<lore>")[1];
- lore = lore.substring(0, lore.indexOf("</lore>"));
- } catch (ArrayIndexOutOfBoundsException ex) {
- return null;
- }
- for (String str : lore.split("<break%lore>")) {
- itemLore.add(ChatColor.translateAlternateColorCodes('&', str));
- }
- // Return itemLore.
- return itemLore;
- }
- /**
- * This method will return Item enchantment.
- *
- * @param finalItem
- * @return
- */
- public HashMap<Enchantment, Integer> getEnchantments(StringBuilder finalItem) {
- HashMap<Enchantment, Integer> itemEnchantments = new HashMap<>();
- String enchantment = null;
- try {
- enchantment = finalItem.toString().split("<enchant>")[1];
- enchantment = enchantment.substring(0, enchantment.indexOf("</enchant>"));
- } catch (ArrayIndexOutOfBoundsException ex) {
- return null;
- }
- for (String enchantments : enchantment.split("<break%enchant>")) {
- Enchantment enchant = Enchantment.getByName(enchantments.split(",")[0].toUpperCase());
- int level = Integer.parseInt(enchantments.split(",")[1]);
- itemEnchantments.put(enchant, level);
- }
- // Return itemEnchantments.
- return itemEnchantments;
- }
- /**
- * Save your Item in a List.
- *
- * @param item
- */
- public void save(StringBuilder item) {
- items.add(item);
- }
- /**
- * Save your Item in a List.
- *
- * @param item
- */
- public void save(ItemStack item) {
- items.add(this.serializer(item));
- }
- /**
- * Get a specific item using his name.
- *
- * @param name
- * @return a specific saved item as StringBuilder
- */
- public StringBuilder getItem(String name) {
- return getItems().stream().filter(entry -> getName(entry).equalsIgnoreCase(name)).findFirst().orElse(null);
- }
- /**
- * Get a specific item using his name.
- *
- * @param name
- * @return a specific saved item as ItemStack
- */
- public ItemStack getItemAsItemStack(String name) {
- try {
- return getItemsAsItemStack().stream().filter(entry -> entry.getItemMeta().getDisplayName().equalsIgnoreCase(name)).findFirst().orElse(null);
- } catch (NullPointerException ignore) {}
- // Will return null if item doesn't exists or Item doesn't has itemMeta/DisplayName.
- return null;
- }
- /**
- * @return list with all saved Items as StringBuilder
- */
- public List<StringBuilder> getItems() {
- return items;
- }
- /**
- * @return list with all saved Items as StringBuilder
- */
- public List<ItemStack> getItemsAsItemStack() {
- List<ItemStack> i = new ArrayList<>();
- items.forEach(entry -> i.add(this.deserializer(entry)));
- return i;
- }
- /*
- * -
- * Below here, some useful methods.
- * Every method here is about ItemStack.
- */
- /**
- * Check if player has a specific Item.
- * Set amount = '0' to don't check amount.
- * Set name = 'null' to don't check name.
- *
- * @param player
- * @param material
- * @param amount
- * @param name
- * @return if a player contains {@link material} on his inventory.
- */
- public static boolean contains(Player player, Material material, int amount, String name) {
- int count = 0;
- for (ItemStack items : player.getInventory().all(material).values()) {
- /*
- * Not necessary.
- *
- * if ((items == null) || (items.getType() == Material.AIR)) {
- * continue;
- * }
- *
- */
- if (name != null) {
- if (items.hasItemMeta()) {
- if (items.getItemMeta().hasDisplayName()) {
- if (items.getItemMeta().getDisplayName().equals(name)) {
- if (amount <= 0) {
- return true;
- } else {
- count+=items.getAmount();
- if (count >= amount) {
- return true;
- }
- }
- }
- continue;
- }
- }
- }
- if (amount <= 0) {
- return true;
- } else {
- count+=items.getAmount();
- if (count >= amount) {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * Check if player has a specific Item.
- * Set amount = '0' to don't check amount.
- * Set name = 'null' to don't check name.
- *
- * @param player
- * @param item
- * @param amount
- * @return if a player contains {@link item} on his inventory.
- */
- public static boolean contains(Player player, ItemStack item, int amount) {
- return contains(player, item.getType(), amount, (item.hasItemMeta() ? item.getItemMeta().getDisplayName() : null));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment