0

I'm creating a Minecraft Item Placing System. Basically it's like looping through 100 string like "Hi, %employee, welcome to %company." and I would replace those %employee with their name and %company with the company name.

But in my case, I tried replacing them but, it didn't workout very well after the second object.

What I mean is that after the first Item, the second item won't replace the string.

I've tried debugging, using async, everything I could possibly imagined. But no luck.

public ItemStack getMailItem(Mail m) {
    ItemStack item = null;
    if (m.isRead()) {
        item = items.get(ItemData.READ_MAIL);
    }
    else {
        item = items.get(ItemData.MAIL);
        item.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
        item.addItemFlags(ItemFlag.HIDE_ENCHANTS);
    }
    ItemMeta meta = item.getItemMeta();
    String name = meta.getDisplayName();
    name = name.replaceAll("%a", m.getSender());
    name = name.replaceAll("%b", m.getDate());
    Utility.broadcast("Date: " + m.getDate());
    meta.setDisplayName(Utility.TransColor(name));
    ArrayList<String> msgs = new ArrayList<String>();
    for (String str : m.getMessages()) {
        msgs.add("&f" + str);
    }
    meta.setLore(Utility.TransColor(msgs));
    item.setItemMeta(meta);
    NBTItem nbt = new NBTItem(item);
    nbt.setString("c2e", m.getUniqueID().toString());
    return nbt.getItem();
}

Basically It works the first time but the second time it doesn't work. It will use the name of the first sender and the date from the first mail.

I expect that for example it has a data like this: Johnny, 5 AM Mark, 5 PM Albert, 1 PM

But when It's done:

Johnny, 5 AM Johnny, 5 AM Johnny, 5 AM

It's replaced but it doesn't use the right string.

I've debugged and it shows the right string but it doesn't replace the right string.

  • Where do you call `getMailItem` and how do you generate `Mail m`? – Adder Jul 02 '19 at 15:37
  • After I debugged, the date in Mail m is already correct. It just doesn't work when I replaceall them. – Jycko Sianjaya Jul 02 '19 at 15:39
  • 2
    What is `items`? What is `ItemStack`? What is `ItemMeta`? Do all the `ItemStack` object refer to the same `ItemMeta` object? Or are `ItemMeta` fields static? – Andreas Jul 02 '19 at 15:39
  • 1
    Unrelated but if you are are not using any regex syntax while replacement don't use `replaceAll` but simple `replace` (it also replaces *all* occurrences of specified string, despite lack of that suffix). – Pshemo Jul 02 '19 at 15:39
  • Possible duplicate of [Why does my ArrayList contain N copies of the last item added to the list?](https://stackoverflow.com/q/19843506/5221149) – Andreas Jul 02 '19 at 15:40
  • 4
    `String name = meta.getDisplayName();` presumably gets the `"%a, %b"` string, you then update `name` to be `"Johnny, 5 AM"` and call `meta.setDisplayName(Utility.TransColor(name));`. Since the **`meta` object is always the same**, next time you do `String name = meta.getDisplayName();`, you will get the updated value (`"Johnny, 5 AM"`), so there is nothing to replace. Why is that confusing you? – Andreas Jul 02 '19 at 15:45
  • 2
    I'm with Andreas here: I suspect that you're working on the same object every time without realizing it. It would be best to post a [mcve] since without that we'd have to guess. My guess would be that all items share the same `ItemMeta` instance and thus `meta.setDisplayName(Utility.TransColor(name));` replaces the "original" name `"%a, %b"` with `"Johnny, 5 AM"` so that in the next iteration there's no more `"%a"` or `"%b"` in that string that can be replaced. – Thomas Jul 02 '19 at 15:45
  • Oh yes I forgot to clone the object xD Thanks a ton guys didnt thought of that – Jycko Sianjaya Jul 03 '19 at 03:26

0 Answers0