Skip to content

Commit 4e65a8b

Browse files
committed
Escape MiniMessage tags in item display names to prevent Nexo colorization
1 parent 7fce4b4 commit 4e65a8b

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

common/src/main/java/com/loohp/interactivechat/modules/ItemDisplay.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.loohp.interactivechat.utils.ComponentCompacting;
3636
import com.loohp.interactivechat.utils.ComponentFlattening;
3737
import com.loohp.interactivechat.utils.ComponentReplacing;
38+
import com.loohp.interactivechat.utils.ComponentStyling;
3839
import com.loohp.interactivechat.utils.ComponentUtils;
3940
import com.loohp.interactivechat.utils.FilledMapUtils;
4041
import com.loohp.interactivechat.utils.HashUtils;
@@ -222,13 +223,18 @@ public static Component createItemDisplay(OfflineICPlayer player, ItemStack item
222223
for (Component child : flattened) {
223224
if (child instanceof TextComponent) {
224225
TextComponent text = (TextComponent) child;
225-
cleaned.add(text.content(ChatColorUtils.stripColor(text.content())));
226+
cleaned.add(text.content(ChatColorUtils.escapeMiniMessageTags(ChatColorUtils.stripColor(text.content()))));
226227
} else {
227228
cleaned.add(child);
228229
}
229230
}
230231
itemDisplayNameComponent = ComponentCompacting.optimize(Component.empty().children(cleaned));
231232

233+
ItemStack hoverItem = (trimmedItem == null ? item : trimmedItem).clone();
234+
if (itemMeta != null && itemMeta.hasDisplayName()) {
235+
NMS.getInstance().setItemStackDisplayName(hoverItem, ComponentStyling.stripColor(itemDisplayNameComponent));
236+
}
237+
232238
amountString = String.valueOf(itemAmount);
233239
Key key = ItemNBTUtils.getNMSItemStackNamespacedKey(item);
234240

@@ -237,12 +243,12 @@ public static Component createItemDisplay(OfflineICPlayer player, ItemStack item
237243
if (item.getType().equals(Material.AIR)) {
238244
showHover = false;
239245
}
240-
Map<Key, DataComponentValue> dataComponents = ItemNBTUtils.getNMSItemStackDataComponents(trimmedItem == null ? item : trimmedItem);
246+
Map<Key, DataComponentValue> dataComponents = ItemNBTUtils.getNMSItemStackDataComponents(hoverItem);
241247
dataComponents.remove(Key.key("minecraft", "container"));
242248
dataComponents.remove(Key.key("minecraft", "bundle_contents"));
243249
showItem = dataComponents.isEmpty() ? ShowItem.showItem(key, itemAmount) : ShowItem.showItem(key, itemAmount, dataComponents);
244250
} else {
245-
String tag = ItemNBTUtils.getNMSItemStackTag(trimmedItem == null ? item : trimmedItem);
251+
String tag = ItemNBTUtils.getNMSItemStackTag(hoverItem);
246252
showItem = tag == null ? ShowItem.showItem(key, itemAmount) : ShowItem.showItem(key, itemAmount, BinaryTagHolder.binaryTagHolder(tag));
247253
}
248254

common/src/main/java/com/loohp/interactivechat/utils/ChatColorUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package com.loohp.interactivechat.utils;
2222

2323
import com.loohp.interactivechat.InteractiveChat;
24+
import net.kyori.adventure.text.minimessage.MiniMessage;
2425
import net.md_5.bungee.api.ChatColor;
2526

2627
import java.util.Arrays;
@@ -46,6 +47,10 @@ public static String stripColor(String string) {
4647
return string.replaceAll("\u00a7[0-9A-Fa-fk-orx]", "");
4748
}
4849

50+
public static String escapeMiniMessageTags(String string) {
51+
return MiniMessage.miniMessage().escapeTags(string);
52+
}
53+
4954
public static String filterIllegalColorCodes(String string) {
5055
return filterIllegalColorCodes(string, com.loohp.interactivechat.InteractiveChat.version.isLegacyRGB());
5156
}

0 commit comments

Comments
 (0)