1.12.2 Items with many placeholders

Discussion in 'Spigot Plugin Development' started by GabrielVitza, Jul 27, 2021.

  1. Hi all!

    I have a system of items that contain several placeholders that need to be replaced.

    My question is, what is the best and most efficient way to replace those placeholders?
    I would like an example code!

    Thank you!
  2. Bruh how many placeholders do you want to replace that you‘re even thinking about the „most effective method“?
    • Like Like x 1
    • Agree Agree x 1
  3. There are 5+ placeholders, but these must be replaced each time a player opens the inventory or updated when he interacts with that inventory.
  4. Do you know how many calculations the game does per tick? You can replace thousand placeholders on each inventoryevent without getting into trouble
    • Like Like x 1
  5. This is my code:
    Code (Text):
    public static ItemStack replace(ItemStack item, Object... placeholders) {
            ItemMeta meta = item.getItemMeta();
            List<String> lore1 = meta.getLore();
            String lore = serializeLore(lore1);
            String name = meta.getDisplayName();
            for(int i = 0; i < placeholders.length; i+=2) {
                lore = lore.replace((String)placeholders[i], (String)placeholders[i + 1]);
                name = name.replace((String)placeholders[i], (String)placeholders[i + 1]);
            return item;
        private static String serializeLore(List<String> lore) {
            String result = "";
            for(int i = 0; i < lore.size(); i++) {
                result += lore.get(i) + (i + 1 < lore.size() ? "\n" : "");
            return result;
    the second parameter of the 'replace' function is an array, on even positions are the variables, and on odd positions are the values with which those placeholders are replaced.
    Code (Text):
    replace (item, "%player%", "mfnalex", "%coins%", "3000", etc ...);
    Do you think this method is not efficient?
    would you change anything about her?
    If yes, what?

  6. Yes and no,

    First, you must implement safety checks such as
    Code (Java):
    Or checking if lore is present to prevent unnecessary method calling.
    Second, before replacing, check if the lore contains the character '%'.
    If so, then invoke the replace method.
    • Like Like x 1
  7. in any case it will contain at least one placeholder, because the whole lore is serialized in a single string, because it does not go through the lore line by line
  8. Stop trolling please and READ what people already told you
    • Like Like x 1
  9. Sorry! Are you talking about me?
    If so, to know that the purpose of this thread is not to troll!
    • Funny Funny x 1