Java Code Examples for org.bukkit.inventory.meta.ItemMeta#getLore()

The following examples show how to use org.bukkit.inventory.meta.ItemMeta#getLore() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnitMaterial.java    From civcraft with GNU General Public License v2.0 6 votes vote down vote up
public static void setOwningTown(Town town, ItemStack stack) {
	if (town == null) {
		return;
	}
	
	ItemMeta meta = stack.getItemMeta();
	if (meta != null && meta.hasLore()) {
		List<String> lore = meta.getLore();
		
		lore = stripTownLore(lore);
		
		if (lore != null) {
			lore.add("Town:"+town.getName()+" "+CivColor.Black+town.getId());
		}
		
		meta.setLore(lore);
		stack.setItemMeta(meta);
	}	
}
 
Example 2
Source File: ItemFactory.java    From TradePlus with GNU General Public License v3.0 6 votes vote down vote up
public ItemFactory(ItemStack stack) {
  damage = stack.getDurability();
  material = stack.getType();
  amount = stack.getAmount();
  data = stack.getData().getData();
  if (stack.hasItemMeta()) {
    ItemMeta meta = stack.getItemMeta();
    if (meta.hasDisplayName()) {
      display = meta.getDisplayName();
    }
    if (meta.hasLore()) {
      lore = meta.getLore();
    }
    if (Sounds.version != 17) flags = new ArrayList<>(meta.getItemFlags());
  }
  if (Sounds.version > 113) {
    customModelData = ItemUtils1_14.getCustomModelData(stack);
  }
}
 
Example 3
Source File: FuelManager.java    From NyaaUtils with MIT License 6 votes vote down vote up
public void updateItem(ItemStack item, int fuelID, int durability) {
    FuelItem fuel = plugin.cfg.fuelConfig.fuel.get(fuelID);
    if (fuel == null) {
        return;
    }
    String hex = toHexString(fuelID) + toHexString(durability) + toHexString(new Random().nextInt(65535));
    String str = "";
    for (int i = 0; i < hex.length(); i++) {
        str += ChatColor.COLOR_CHAR + hex.substring(i, i + 1);
    }
    str += ChatColor.COLOR_CHAR + "r";
    ItemMeta meta = fuel.getItem().getItemMeta();
    List<String> lore;
    if (meta.hasLore()) {
        lore = meta.getLore();
        lore.set(0, lore_prefix + str + lore.get(0));
        lore.add(lore_prefix + I18n.format("user.elytra_enhance.fuel_durability", durability, fuel.getMaxDurability()));
    } else {
        lore = new ArrayList<>();
        lore.add(lore_prefix + str + I18n.format("user.elytra_enhance.fuel_durability", durability, fuel.getMaxDurability()));
    }
    item.setType(fuel.getItem().getType());
    item.setData(fuel.getItem().getData());
    meta.setLore(lore);
    item.setItemMeta(meta);
}
 
Example 4
Source File: ItemStackBuilder.java    From AstralEdit with Apache License 2.0 6 votes vote down vote up
/**
 * Removes the lore matching the lines
 *
 * @param lore lore
 * @return builder
 */
public ItemStackBuilder removeLore(String... lore) {
    final ItemMeta itemMeta = this.getItemMeta();
    final List<String> data = new ArrayList<>();
    for (final String s : itemMeta.getLore()) {
        boolean add = true;
        for (final String k : lore) {
            if (s.equals(k)) {
                add = false;
            }
        }
        if (add) {
            data.add(s);
        }
    }
    itemMeta.setLore(data);
    this.setItemMeta(itemMeta);
    return this;
}
 
Example 5
Source File: ChallengeLogic.java    From uSkyBlock with GNU General Public License v3.0 6 votes vote down vote up
public ItemStack getItemStack(PlayerInfo playerInfo, String challengeName) {
    Challenge challenge = getChallenge(challengeName);
    ChallengeCompletion completion = playerInfo.getChallenge(challengeName);
    ItemStack currentChallengeItem = challenge.getDisplayItem(completion, defaults.enableEconomyPlugin);
    ItemMeta meta = currentChallengeItem.getItemMeta();
    List<String> lores = meta.getLore();
    if (challenge.isRepeatable() || completion.getTimesCompleted() == 0) {
        lores.add(tr("\u00a7e\u00a7lClick to complete this challenge."));
    } else {
        lores.add(tr("\u00a74\u00a7lYou can't repeat this challenge."));
    }
    if (completion.getTimesCompleted() > 0) {
        meta.addEnchant(Enchantment.LOYALTY, 0, true);
    }
    meta.setLore(lores);
    currentChallengeItem.setItemMeta(meta);
    return currentChallengeItem;
}
 
Example 6
Source File: ExpCapsuleCommands.java    From NyaaUtils with MIT License 6 votes vote down vote up
public static Integer getStoredExp(ItemStack item) {
    if (!item.hasItemMeta() || !item.getItemMeta().hasLore()) return null;
    ItemMeta meta = item.getItemMeta();
    List<String> lores = meta.getLore();
    for (String str : lores) {
        if (str.contains(EXP_CAPSULE_MAGIC)) {
            int offset = str.lastIndexOf(EXP_CAPSULE_MAGIC) + EXP_CAPSULE_MAGIC.length();
            String rem = str.substring(offset);
            Integer exp = null;
            try {
                exp = Integer.parseInt(rem);
            } catch (NumberFormatException ex) {
                ex.printStackTrace();
            }
            return exp;
        }
    }
    return null;
}
 
Example 7
Source File: RechargeableHelper.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
static void setCharge(ItemMeta meta, float charge, float capacity) {
    BigDecimal decimal = BigDecimal.valueOf(charge).setScale(2, RoundingMode.HALF_UP);
    float value = decimal.floatValue();

    if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_14)) {
        meta.getPersistentDataContainer().set(CHARGE_KEY, PersistentDataType.FLOAT, value);
    }

    List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
    for (int i = 0; i < lore.size(); i++) {
        String line = lore.get(i);

        if (line.startsWith(LORE_PREFIX)) {
            lore.set(i, LORE_PREFIX + value + " / " + capacity + " J");
            meta.setLore(lore);
            return;
        }
    }

    lore.add(LORE_PREFIX + value + " / " + capacity + " J");
    meta.setLore(lore);
}
 
Example 8
Source File: APIUtils.java    From BedWars with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @param stack
 * @param hash
 */
public static void hashIntoInvisibleString(ItemStack stack, String hash) {
	ItemMeta meta = stack.getItemMeta();
	try {
		NamespacedKey key = new NamespacedKey((Plugin) BedwarsAPI.getInstance(), BEDWARS_NAMESPACED_KEY);
		PersistentDataContainer container = meta.getPersistentDataContainer();
		List<String> propertyLines = new ArrayList<>();
		if (container.has(key, PersistentDataType.STRING)) {
			String oldString = container.get(key, PersistentDataType.STRING);
			propertyLines.addAll((List<String>) new Gson().fromJson(oldString, List.class));
		}
		propertyLines.add(hash);
		container.set(key, PersistentDataType.STRING, new Gson().toJson(propertyLines));
	} catch (Throwable ignored) {
		// Use the Lore API instead
		List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();

		lore.add(convertToInvisibleString(hash));
		meta.setLore(lore);
	}
	stack.setItemMeta(meta);
}
 
Example 9
Source File: MetadatableItemStack.java    From HeavySpleef with GNU General Public License v3.0 6 votes vote down vote up
public void setMetadata(String key, String value) {
	ItemMeta meta = getItemMeta();
	List<String> lore;
	
	if (meta.hasLore()) {
		lore = meta.getLore();
	} else {
		lore = Lists.newArrayList();
	}
	
	String loreString = HIDDEN_PREFIX + key + (value != null ? KEY_VALUE_DELIMITER + value : "");
	loreString = hideString(loreString);
	lore.add(loreString);
	
	meta.setLore(lore);
	setItemMeta(meta);
}
 
Example 10
Source File: EnchantManager.java    From ce with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static ItemStack addEnchant(ItemStack item, CEnchantment ce, int level) {
    ItemMeta im = item.getItemMeta();
    List<String> lore = new ArrayList<String>();
    if (im.hasLore()) {
        lore = im.getLore();
        if (maxEnchants < enchantments.size()) {
            int counter = maxEnchants;
            for (String s : lore)
                if (containsEnchantment(s)) {
                    counter--;
                    if (counter <= 0) {
                        return item;
                    }
                }
        }
    }
    if (level > ce.getEnchantmentMaxLevel())
        level = ce.getEnchantmentMaxLevel();
    lore.add(ce.getDisplayName() + " " + intToLevel(level));
    im.setLore(lore);
    item.setItemMeta(im);
    item.addUnsafeEnchantment(glowEnchantment, 0);
    return item;
}
 
Example 11
Source File: RechargeableHelper.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
static float getCharge(ItemMeta meta) {
    if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_14)) {
        Float value = meta.getPersistentDataContainer().get(CHARGE_KEY, PersistentDataType.FLOAT);

        // If persistent data is available, we just return this value
        if (value != null) {
            return value;
        }
    }

    // If no persistent data exists, we will just fall back to the lore
    if (meta.hasLore()) {
        for (String line : meta.getLore()) {
            if (line.startsWith(LORE_PREFIX) && line.contains(" / ") && line.endsWith(" J")) {
                return Float.parseFloat(PatternUtils.SLASH_SEPARATOR.split(line)[0].replace(LORE_PREFIX, ""));
            }
        }
    }

    return 0;
}
 
Example 12
Source File: Gun.java    From QualityArmory with GNU General Public License v3.0 5 votes vote down vote up
public static void updateAmmo(Gun g, ItemMeta current, int amount) {
	if (((current == null || !current.hasLore())))
		return;
	List<String> lore = current.getLore();
	updateAmmo(g, lore, amount);
	current.setLore(lore);
	return;
}
 
Example 13
Source File: Items.java    From StaffPlus with GNU General Public License v3.0 5 votes vote down vote up
public ItemStackBuilder addLore(List<String> lore)
{
	ItemMeta itemMeta = itemStack.getItemMeta();
	List<String> original = itemMeta.getLore();
	if(original == null) original = new ArrayList<String>();
	original.addAll(Strings.format(lore));
	itemMeta.setLore(original);
	itemStack.setItemMeta(itemMeta);
	return this;
}
 
Example 14
Source File: UnitMaterial.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
public static Town getOwningTown(ItemStack stack) {
	ItemMeta meta = stack.getItemMeta();
	if (meta == null || !meta.hasLore()) {
		return null;
	}
	
	String loreLine = null;
	List<String> lore = meta.getLore();
	for (String str : lore) {
		if (str.startsWith("Town:")) {
			loreLine = str;
			break;
		}
	}
	
	if (loreLine == null) {
		return null;
	}
	
	try {
		String[] split = loreLine.split(CivColor.Black);
		int townId = Integer.valueOf(split[1]);
		
		return CivGlobal.getTownFromId(townId);
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
	
}
 
Example 15
Source File: EnchantManager.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void removeEnchant(ItemStack item, CEnchantment ce) {
    if (!item.hasItemMeta() || !item.getItemMeta().hasLore())
        return;
    ItemMeta im = item.getItemMeta();
    List<String> lore = im.getLore();
    for (String s : lore)
        if (s.startsWith(ce.getDisplayName()) || s.startsWith(ce.getOriginalName())) {
            lore.remove(s);
            im.setLore(lore);
            item.setItemMeta(im);
            if (item.getEnchantments().containsKey(glowEnchantment))
                item.removeEnchantment(glowEnchantment);
            return;
        }
}
 
Example 16
Source File: JsonItem.java    From TrMenu with MIT License 5 votes vote down vote up
public static String toJson(ItemStack item) {
    JsonObject json = new JsonObject();
    String type = item.getType().name();
    byte data = item.getData().getData();
    int amount = item.getAmount();

    json.addProperty("type", item.getType().name());
    if (data > 0) {
        json.addProperty("data", data);
    }
    if (amount > 1) {
        json.addProperty("amount", amount);
    }
    if (item.hasItemMeta()) {
        // Uncolor
        ItemMeta meta = item.getItemMeta();
        if (meta.hasDisplayName()) {
            meta.setDisplayName(meta.getDisplayName().replace('§', '&'));
        }
        if (meta.hasLore()) {
            List<String> lore = meta.getLore();
            lore.replaceAll(s -> s.replace('§', '&'));
            meta.setLore(lore);
        }
        item.setItemMeta(meta);
        json.add("meta", new JsonParser().parse(NMS.handle().loadNBT(item).toJson()));
    }
    return json.toString();
}
 
Example 17
Source File: ItemBuilder.java    From EnchantmentsEnhance with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Add a lore line.
 *
 * @param line The lore line to add.
 * @param pos  The index of where to put it.
 */
public ItemBuilder addLoreLine(String line, int pos) {
    ItemMeta im = is.getItemMeta();
    List<String> lore = new ArrayList<>(im.getLore());
    lore.set(pos, Util.toColor(line));
    im.setLore(lore);
    is.setItemMeta(im);
    return this;
}
 
Example 18
Source File: ItemLine.java    From Holograms with MIT License 4 votes vote down vote up
private static ItemStack parseItem(String text) {
    Matcher matcher = linePattern.matcher(text);
    if (matcher.find()) {
        text = matcher.group(3);
    }
    String[] split = text.split(" ");
    short durability = -1;
    String data = split[0];

    if (data.contains(":")) {
        String[] datasplit = data.split(":");
        data = datasplit[0];
        durability = Short.parseShort(datasplit[1]);
    }

    Material material = data.matches("[0-9]+")
            ? Material.getMaterial(Integer.parseInt(data))
            : Material.getMaterial(data.toUpperCase());

    // Throw exception if the material provided was wrong
    if (material == null) {
        throw new IllegalArgumentException("Invalid material " + data);
    }

    int amount;
    try {
        amount = split.length == 1 ? 1 : Integer.parseInt(split[1]);
    } catch (NumberFormatException ex) {
        throw new IllegalArgumentException("Invalid amount \"" + split[1] + "\"", ex);
    }
    ItemStack item = new ItemStack(material, amount, (short) Math.max(0, durability));
    ItemMeta meta = item.getItemMeta();

    // No meta data was provided, we can return here
    if (split.length < 3) {
        return item;
    }

    // Go through all the item meta specified
    for (int i = 2 ; i < split.length ; i++) {
        String[] information = split[i].split(":");

        // Data, name, or lore has been specified
        switch (information[0].toLowerCase()) {
            case "name":
                // Replace '_' with spaces
                String name = information[1].replace(' ', ' ');
                meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
                break;
            case "lore":
                // If lore was specified 2x for some reason, don't overwrite
                List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
                String[] loreLines = information[1].split("\\|");

                // Format all the lines and add them as lore
                for (String line : loreLines) {
                    line = line.replace('_', ' '); // Replace '_' with space
                    lore.add(ChatColor.translateAlternateColorCodes('&', line));
                }

                meta.setLore(lore);
                break;
            case "data":
                short dataValue = Short.parseShort(information[1]);
                item.setDurability(dataValue);
            default:
                // Try parsing enchantment if it was nothing else
                Enchantment ench = Enchantment.getByName(information[0].toUpperCase());
                int level = Integer.parseInt(information[1]);

                if (ench != null) {
                    meta.addEnchant(ench, level, true);
                } else {
                    throw new IllegalArgumentException("Invalid enchantment " + information[0]);
                }
                break;
        }
    }

    item.setItemMeta(meta);
    return item;
}
 
Example 19
Source File: ItemData.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Compares {@link ItemMeta}s for {@link #matchAlias(ItemData)}.
 * Note that this does NOT compare everything; only the most
 * important bits.
 * @param first Meta of this item.
 * @param second Meta of given item.
 * @return Match quality of metas.
 * Lowest is {@link MatchQuality#SAME_MATERIAL}.
 */
private static MatchQuality compareItemMetas(ItemMeta first, ItemMeta second) {
	MatchQuality quality = MatchQuality.EXACT; // Lowered as we go on
	
	// Display name
	String ourName = first.hasDisplayName() ? first.getDisplayName() : null;
	String theirName = second.hasDisplayName() ? second.getDisplayName() : null;
	if (!Objects.equals(ourName, theirName)) {
		quality = ourName != null ? MatchQuality.SAME_MATERIAL : quality;
	}
	
	// Lore
	List<String> ourLore = first.hasLore() ? first.getLore() : null;
	List<String> theirLore = second.hasLore() ? second.getLore() : null;
	if (!Objects.equals(ourLore, theirLore)) {
		quality = ourLore != null ? MatchQuality.SAME_MATERIAL : quality;
	}
	
	// Enchantments
	Map<Enchantment, Integer> ourEnchants = first.getEnchants();
	Map<Enchantment, Integer> theirEnchants = second.getEnchants();
	if (!Objects.equals(ourEnchants, theirEnchants)) {
		quality = !ourEnchants.isEmpty() ? MatchQuality.SAME_MATERIAL : quality;
	}
	
	// Item flags
	Set<ItemFlag> ourFlags = first.getItemFlags();
	Set<ItemFlag> theirFlags = second.getItemFlags();
	if (!Objects.equals(ourFlags, theirFlags)) {
		quality = !ourFlags.isEmpty() ? MatchQuality.SAME_MATERIAL : quality;
	}
	
	// Potion data
	if (second instanceof PotionMeta) {
		if (!(first instanceof PotionMeta)) {
			return MatchQuality.DIFFERENT; // Second is a potion, first is clearly not
		}
		// Compare potion type, including extended and level 2 attributes
		PotionData ourPotion = ((PotionMeta) first).getBasePotionData();
		PotionData theirPotion = ((PotionMeta) second).getBasePotionData();
		return !Objects.equals(ourPotion, theirPotion) ? MatchQuality.SAME_MATERIAL : quality;
	}
	
	return quality;
}
 
Example 20
Source File: DynamicLore.java    From EliteMobs with GNU General Public License v3.0 3 votes vote down vote up
private static void setLoreToWorth(ItemStack itemStack) {

        double itemWorth = ItemWorthCalculator.determineItemWorth(itemStack);
        double itemResale = ItemWorthCalculator.determineResaleWorth(itemStack);

        ItemMeta itemMeta = itemStack.getItemMeta();

        List<String> newLore = new ArrayList<>();

        for (String loreLine : itemMeta.getLore()) {

            if (isEconomyLoreLine(loreLine, itemWorth, itemResale)) {

                String lorePrefix = lorePrefix(loreLine, itemWorth, itemResale);

                newLore.add(lorePrefix + targetValueLine(itemWorth));

            } else {

                newLore.add(loreLine);

            }

        }

        itemMeta.setLore(newLore);
        itemStack.setItemMeta(itemMeta);

    }