Skip to content

Commit dfbf4e6

Browse files
committed
Fix eager translations
1 parent 8c9f09f commit dfbf4e6

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/main/java/tk/sciwhiz12/concord/util/MessageUtil.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package tk.sciwhiz12.concord.util;
22

33
import net.minecraft.entity.player.ServerPlayerEntity;
4+
import net.minecraft.util.text.IFormattableTextComponent;
5+
import net.minecraft.util.text.ITextComponent;
46
import net.minecraft.util.text.LanguageMap;
57
import net.minecraft.util.text.StringTextComponent;
8+
import net.minecraft.util.text.Style;
69
import net.minecraft.util.text.TextComponent;
710
import net.minecraft.util.text.TranslationTextComponent;
11+
import net.minecraft.util.text.event.HoverEvent;
812
import tk.sciwhiz12.concord.ConcordConfig;
913
import tk.sciwhiz12.concord.ModPresenceTracker;
1014

@@ -50,14 +54,42 @@ public static TextComponent eagerTranslate(final TranslationTextComponent compon
5054
Object obj = oldArgs[i];
5155
if (obj instanceof TranslationTextComponent) {
5256
newArgs[i] = eagerTranslate((TranslationTextComponent) obj);
57+
} else if (obj instanceof IFormattableTextComponent) {
58+
newArgs[i] = eagerCheckStyle((IFormattableTextComponent) obj);
5359
} else {
5460
newArgs[i] = oldArgs[i];
5561
}
5662
}
5763

5864
TranslationTextComponent result =
5965
new TranslationTextComponent(LanguageMap.getInstance().func_230503_a_(component.getKey()), newArgs);
60-
result.mergeStyle(component.getStyle());
61-
return result;
66+
result.setStyle(component.getStyle());
67+
68+
for (ITextComponent sibling : component.getSiblings()) {
69+
if (sibling instanceof TranslationTextComponent) {
70+
result.append(eagerTranslate((TranslationTextComponent) sibling));
71+
} else if (sibling instanceof IFormattableTextComponent) {
72+
result.append(eagerCheckStyle((IFormattableTextComponent) sibling));
73+
} else {
74+
result.append(sibling);
75+
}
76+
}
77+
78+
return eagerCheckStyle(result);
79+
}
80+
81+
public static <Text extends IFormattableTextComponent> Text eagerCheckStyle(Text component) {
82+
Style style = component.getStyle();
83+
HoverEvent hover = style.getHoverEvent();
84+
if (hover != null && hover.getAction() == HoverEvent.Action.SHOW_TEXT) {
85+
ITextComponent hoverText = hover.getParameter(HoverEvent.Action.SHOW_TEXT);
86+
if (hoverText instanceof TranslationTextComponent) {
87+
style = style.setHoverEvent(
88+
new HoverEvent(HoverEvent.Action.SHOW_TEXT, eagerTranslate((TranslationTextComponent) hoverText))
89+
);
90+
}
91+
}
92+
component.setStyle(style);
93+
return component;
6294
}
6395
}

0 commit comments

Comments
 (0)