From dfbf4e661125266a168e173fdc657b90775df864 Mon Sep 17 00:00:00 2001 From: sciwhiz12 Date: Fri, 29 Jan 2021 01:59:22 +0800 Subject: [PATCH] Fix eager translations --- .../sciwhiz12/concord/util/MessageUtil.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main/java/tk/sciwhiz12/concord/util/MessageUtil.java b/src/main/java/tk/sciwhiz12/concord/util/MessageUtil.java index c18dc13c..4bd9fb71 100644 --- a/src/main/java/tk/sciwhiz12/concord/util/MessageUtil.java +++ b/src/main/java/tk/sciwhiz12/concord/util/MessageUtil.java @@ -1,10 +1,14 @@ package tk.sciwhiz12.concord.util; import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.util.text.IFormattableTextComponent; +import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.LanguageMap; import net.minecraft.util.text.StringTextComponent; +import net.minecraft.util.text.Style; import net.minecraft.util.text.TextComponent; import net.minecraft.util.text.TranslationTextComponent; +import net.minecraft.util.text.event.HoverEvent; import tk.sciwhiz12.concord.ConcordConfig; import tk.sciwhiz12.concord.ModPresenceTracker; @@ -50,6 +54,8 @@ public static TextComponent eagerTranslate(final TranslationTextComponent compon Object obj = oldArgs[i]; if (obj instanceof TranslationTextComponent) { newArgs[i] = eagerTranslate((TranslationTextComponent) obj); + } else if (obj instanceof IFormattableTextComponent) { + newArgs[i] = eagerCheckStyle((IFormattableTextComponent) obj); } else { newArgs[i] = oldArgs[i]; } @@ -57,7 +63,33 @@ public static TextComponent eagerTranslate(final TranslationTextComponent compon TranslationTextComponent result = new TranslationTextComponent(LanguageMap.getInstance().func_230503_a_(component.getKey()), newArgs); - result.mergeStyle(component.getStyle()); - return result; + result.setStyle(component.getStyle()); + + for (ITextComponent sibling : component.getSiblings()) { + if (sibling instanceof TranslationTextComponent) { + result.append(eagerTranslate((TranslationTextComponent) sibling)); + } else if (sibling instanceof IFormattableTextComponent) { + result.append(eagerCheckStyle((IFormattableTextComponent) sibling)); + } else { + result.append(sibling); + } + } + + return eagerCheckStyle(result); + } + + public static Text eagerCheckStyle(Text component) { + Style style = component.getStyle(); + HoverEvent hover = style.getHoverEvent(); + if (hover != null && hover.getAction() == HoverEvent.Action.SHOW_TEXT) { + ITextComponent hoverText = hover.getParameter(HoverEvent.Action.SHOW_TEXT); + if (hoverText instanceof TranslationTextComponent) { + style = style.setHoverEvent( + new HoverEvent(HoverEvent.Action.SHOW_TEXT, eagerTranslate((TranslationTextComponent) hoverText)) + ); + } + } + component.setStyle(style); + return component; } }