Tweak translation logic to support carpet (also needs update in LibJF)
All checks were successful
ci/woodpecker/push/jfmod Pipeline was successful
All checks were successful
ci/woodpecker/push/jfmod Pipeline was successful
This commit is contained in:
parent
1e21ba6cc6
commit
6f2c5b8572
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user