|
| 1 | +package me.byteful.plugin.leveltools.util; |
| 2 | + |
| 3 | +import net.kyori.adventure.text.Component; |
| 4 | +import net.kyori.adventure.text.TextComponent; |
| 5 | +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; |
| 6 | +import org.bukkit.inventory.ItemStack; |
| 7 | +import org.bukkit.inventory.meta.ItemMeta; |
| 8 | + |
| 9 | +final class AdventureHelper { |
| 10 | + private AdventureHelper() { |
| 11 | + } |
| 12 | + |
| 13 | + static void setDisplayNameWithTranslatable(ItemMeta meta, String text, ItemStack stack) { |
| 14 | + Component translatable = Component.translatable(stack.getType().translationKey()); |
| 15 | + TextComponent parsed = LegacyComponentSerializer.legacySection().deserialize(text); |
| 16 | + |
| 17 | + TextComponent.Builder result = Component.text(); |
| 18 | + processComponent(parsed, translatable, result); |
| 19 | + |
| 20 | + meta.displayName(result.build()); |
| 21 | + } |
| 22 | + |
| 23 | + private static void processComponent(TextComponent component, Component translatable, TextComponent.Builder result) { |
| 24 | + String content = component.content(); |
| 25 | + if (content.contains("{item}")) { |
| 26 | + String[] parts = content.split("\\{item}", -1); |
| 27 | + for (int i = 0; i < parts.length; i++) { |
| 28 | + if (!parts[i].isEmpty()) { |
| 29 | + result.append(Component.text(parts[i]).style(component.style())); |
| 30 | + } |
| 31 | + if (i < parts.length - 1) { |
| 32 | + result.append(translatable); |
| 33 | + } |
| 34 | + } |
| 35 | + } else if (!content.isEmpty()) { |
| 36 | + result.append(Component.text(content).style(component.style())); |
| 37 | + } |
| 38 | + |
| 39 | + for (Component child : component.children()) { |
| 40 | + if (child instanceof TextComponent) { |
| 41 | + processComponent((TextComponent) child, translatable, result); |
| 42 | + } else { |
| 43 | + result.append(child); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
0 commit comments