Skip to content

Commit

Permalink
Fix eager translations
Browse files Browse the repository at this point in the history
  • Loading branch information
sciwhiz12 committed Jan 28, 2021
1 parent 8c9f09f commit dfbf4e6
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/main/java/tk/sciwhiz12/concord/util/MessageUtil.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -50,14 +54,42 @@ 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];
}
}

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 extends IFormattableTextComponent> 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;
}
}

0 comments on commit dfbf4e6

Please sign in to comment.