|
| 1 | +package me.f0reach.vshop.locale; |
| 2 | + |
| 3 | +import net.kyori.adventure.text.Component; |
| 4 | +import net.kyori.adventure.text.minimessage.MiniMessage; |
| 5 | + |
| 6 | +import java.util.Locale; |
| 7 | +import java.util.function.Function; |
| 8 | + |
| 9 | +/** |
| 10 | + * Resolves player-facing labels for the domain enums ({@code TradeSide}, |
| 11 | + * {@code LimitScope}, {@code CoOwnerRole}, {@code ShopType}) from the locale |
| 12 | + * files, so raw constant names never reach players. |
| 13 | + * |
| 14 | + * <p>Keys are derived as {@code enum.<kebab-class>.<kebab-value>} — e.g. |
| 15 | + * {@code enum.trade-side.sell}, {@code enum.limit-scope.per-player}, |
| 16 | + * {@code enum.co-owner-role.primary}. A missing key falls back to |
| 17 | + * {@link Enum#name()} rather than the message key, so an incomplete |
| 18 | + * translation degrades to the previous behavior instead of leaking |
| 19 | + * {@code enum.trade-side.sell} into a chest lore line.</p> |
| 20 | + * |
| 21 | + * <p>Labels are intentionally plain (no color tags): call sites insert them |
| 22 | + * with {@code Placeholder.component}, and the surrounding message supplies the |
| 23 | + * color — e.g. {@code "<yellow><side></yellow>"}.</p> |
| 24 | + * |
| 25 | + * <p>This is display only. Enum names remain the wire format for the database, |
| 26 | + * YAML import/export, command arguments and Dialog dropdown option IDs.</p> |
| 27 | + */ |
| 28 | +public final class EnumLabels { |
| 29 | + |
| 30 | + private final Function<String, String> resolver; |
| 31 | + private final MiniMessage miniMessage; |
| 32 | + |
| 33 | + public EnumLabels(MessageManager messages) { |
| 34 | + this(messages::getRaw, messages.miniMessage()); |
| 35 | + } |
| 36 | + |
| 37 | + /** For tests: pass a plain key-to-raw resolver instead of a live MessageManager. */ |
| 38 | + public EnumLabels(Function<String, String> resolver, MiniMessage miniMessage) { |
| 39 | + this.resolver = resolver; |
| 40 | + this.miniMessage = miniMessage; |
| 41 | + } |
| 42 | + |
| 43 | + /** Message key for {@code value} — e.g. {@code enum.limit-scope.per-player}. */ |
| 44 | + public static String keyFor(Enum<?> value) { |
| 45 | + return "enum." + kebab(value.getDeclaringClass().getSimpleName()) |
| 46 | + + "." + value.name().toLowerCase(Locale.ROOT).replace('_', '-'); |
| 47 | + } |
| 48 | + |
| 49 | + /** Localized label as raw MiniMessage, or the constant name if untranslated. */ |
| 50 | + public String raw(Enum<?> value) { |
| 51 | + if (value == null) return ""; |
| 52 | + String key = keyFor(value); |
| 53 | + String found = resolver.apply(key); |
| 54 | + return found == null || found.equals(key) ? value.name() : found; |
| 55 | + } |
| 56 | + |
| 57 | + /** Localized label, ready for {@code Placeholder.component}. */ |
| 58 | + public Component label(Enum<?> value) { |
| 59 | + return miniMessage.deserialize(raw(value)); |
| 60 | + } |
| 61 | + |
| 62 | + /** {@code CoOwnerRole} -> {@code co-owner-role}. */ |
| 63 | + private static String kebab(String simpleName) { |
| 64 | + StringBuilder out = new StringBuilder(simpleName.length() + 4); |
| 65 | + for (int i = 0; i < simpleName.length(); i++) { |
| 66 | + char c = simpleName.charAt(i); |
| 67 | + if (Character.isUpperCase(c)) { |
| 68 | + if (i > 0) out.append('-'); |
| 69 | + out.append(Character.toLowerCase(c)); |
| 70 | + } else { |
| 71 | + out.append(c); |
| 72 | + } |
| 73 | + } |
| 74 | + return out.toString(); |
| 75 | + } |
| 76 | +} |
0 commit comments