From 6f2c5b8572eff26a9f54619085cde5969985cb41 Mon Sep 17 00:00:00 2001 From: JFronny Date: Wed, 15 Feb 2023 20:32:19 +0100 Subject: [PATCH] Tweak translation logic to support carpet (also needs update in LibJF) --- .../googlechat/mixin/ChatScreenMixin.java | 11 +-- .../gitlab/jfronny/googlechat/GoogleChat.java | 78 ++++++++++++------- 2 files changed, 55 insertions(+), 34 deletions(-) diff --git a/src/client/java/io/gitlab/jfronny/googlechat/mixin/ChatScreenMixin.java b/src/client/java/io/gitlab/jfronny/googlechat/mixin/ChatScreenMixin.java index 0d31a9c..1e66c41 100644 --- a/src/client/java/io/gitlab/jfronny/googlechat/mixin/ChatScreenMixin.java +++ b/src/client/java/io/gitlab/jfronny/googlechat/mixin/ChatScreenMixin.java @@ -1,15 +1,16 @@ package io.gitlab.jfronny.googlechat.mixin; -import io.gitlab.jfronny.googlechat.*; -import io.gitlab.jfronny.libjf.LibJf; -import net.minecraft.client.gui.screen.*; -import org.spongepowered.asm.mixin.*; -import org.spongepowered.asm.mixin.injection.*; +import io.gitlab.jfronny.googlechat.GoogleChat; +import net.minecraft.client.gui.screen.ChatScreen; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyVariable; @Mixin(ChatScreen.class) public class ChatScreenMixin { @ModifyVariable(method = "sendMessage(Ljava/lang/String;Z)Z", at = @At(value = "HEAD"), argsOnly = true, ordinal = 0) String googlechat$translateChatText(String chatText) { + if (chatText.startsWith("/")) return chatText; // Bypass for client-side commands (Carpet, ...) return GoogleChat.translateIfNeeded(chatText, GoogleChat.Direction.C2S, true); } } diff --git a/src/main/java/io/gitlab/jfronny/googlechat/GoogleChat.java b/src/main/java/io/gitlab/jfronny/googlechat/GoogleChat.java index f0b0e11..a2e5c95 100644 --- a/src/main/java/io/gitlab/jfronny/googlechat/GoogleChat.java +++ b/src/main/java/io/gitlab/jfronny/googlechat/GoogleChat.java @@ -37,37 +37,25 @@ public class GoogleChat implements ModInitializer { public static Text translateIfNeeded(Text source, Direction direction, boolean respectRegex) { if (shouldSkipOutright(direction)) return source; String sourceString = toString(source); - if (respectRegex && failsRegex(sourceString, direction)) - return source; - if (GoogleChatConfig.desugar) { - MutableText translatedText = Text.literal(translateIfNeeded(sourceString, direction, true)); - if (GoogleChatConfig.debugLogs) LOGGER.info("Translated " + sourceString + " to " + toString(translatedText)); - if (GoogleChatConfig.translationTooltip) - return source.copy().setStyle(addHover(Style.EMPTY, Text.literal("Translated: ").append(translatedText))); - else - return translatedText.setStyle(addHover(Style.EMPTY, Text.literal("Original: ").append(source))); - } + if (respectRegex && failsRegex(sourceString, direction)) return source; MutableText translated; - if (source.getContent() instanceof TranslatableTextContent tx) { - Object[] args = tx.getArgs(); - args = Arrays.copyOf(args, args.length); - // We're not translating TranslatableText, but are translating arguments - for (int i = 0; i < args.length; i++) { - args[i] = args[i] instanceof Text tx1 ? translateIfNeeded(tx1, direction, false) - : args[i] instanceof String tx1 ? translateIfNeeded(tx1, direction, false) - : args[i]; - } - translated = Text.translatable(tx.getKey(), args); - } else if (source.getContent() instanceof LiteralTextContent tx) { - translated = Text.literal(translateIfNeeded(tx.string(), direction, false)).setStyle(source.getStyle()); + if (GoogleChatConfig.desugar) { + translated = Text.literal(translateIfNeeded(sourceString, direction, true)); } else { - //LOGGER.info("Unhandled text type: " + source.getClass() + " (" + source + ")"); - translated = source.copy(); + translated = MutableText.of(translateIfNeeded(source.getContent(), direction, false)) + .setStyle(source.getStyle()); + for (Text sibling : source.getSiblings()) { + translated.append(translateIfNeeded(sibling, direction, false)); + } } - if (GoogleChatConfig.translationTooltip) - return source.copy().styled(style -> addHover(style, translated)); - else + if (GoogleChatConfig.debugLogs) LOGGER.info("Translated " + sourceString + " to " + toString(translated)); + if (GoogleChatConfig.translationTooltip) { + return source.copy().styled(style -> addHover(style, Text.literal("Translated: ").append(translated))); + } else if (translated.getStyle().getHoverEvent() == null) { + return translated.styled(style -> addHover(style, Text.literal("Original: ").append(source))); + } else { return translated; + } } private static String toString(Text text) { @@ -79,14 +67,46 @@ public class GoogleChat implements ModInitializer { return sb.toString(); } + public static TextContent translateIfNeeded(TextContent source, Direction direction, boolean respectRegex) { + if (shouldSkipOutright(direction)) return source; + String sourceString = toString(source); + if (respectRegex && failsRegex(sourceString, direction)) return source; + //TODO This method (and the check for translatable args) should be converted to a switch pattern when available + if (source instanceof TranslatableTextContent tx) { + Object[] args = tx.getArgs(); + args = Arrays.copyOf(args, args.length); + // We're not translating TranslatableText, but are translating arguments + for (int i = 0; i < args.length; i++) { + if (args[i] instanceof Text tx1) args[i] = translateIfNeeded(tx1, direction, false); + else if (args[i] instanceof TextContent tx1) args[i] = translateIfNeeded(tx1, direction, false); + else if (args[i] instanceof String tx1) args[i] = translateIfNeeded(tx1, direction, false); + else args[i] = args[i]; + } + return new TranslatableTextContent(tx.getKey(), args); + } else if (source instanceof LiteralTextContent tx) { + return new LiteralTextContent(translateIfNeeded(tx.string(), direction, false)); + } else { +// LOGGER.info("Unhandled text type: " + source.getClass() + " (" + source + ")"); + return source; + } + } + + private static String toString(TextContent text) { + StringBuilder sb = new StringBuilder(); + text.visit(asString -> { + sb.append(asString); + return Optional.empty(); + }); + return sb.toString(); + } + private static Style addHover(Style style, Text hoverText) { return style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText)); } public static String translateIfNeeded(String source, Direction direction, boolean respectRegex) { if (shouldSkipOutright(direction)) return source; - if (respectRegex && failsRegex(source, direction)) - return source; + if (respectRegex && failsRegex(source, direction)) return source; // Ignore generics since this is apparently not something java supports @SuppressWarnings("rawtypes") TranslateService svc = GoogleChat.TRANSLATE_SERVICE; Language sourceLang = svc.parseLang(direction == Direction.C2S ? GoogleChatConfig.clientLanguage : GoogleChatConfig.serverLanguage);