111 lines
5.1 KiB
Java
111 lines
5.1 KiB
Java
package io.gitlab.jfronny.googlechat;
|
|
|
|
import io.gitlab.jfronny.libjf.translate.api.Language;
|
|
import io.gitlab.jfronny.libjf.translate.api.TranslateException;
|
|
import io.gitlab.jfronny.libjf.translate.api.TranslateService;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.loader.api.FabricLoader;
|
|
import net.minecraft.client.MinecraftClient;
|
|
import net.minecraft.text.*;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.UUID;
|
|
|
|
public class GoogleChat {
|
|
public static final String MOD_ID = "google-chat";
|
|
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
|
public static final TranslateService<?> TRANSLATE_SERVICE = TranslateService.getConfigured();
|
|
|
|
public static boolean isSelf(UUID sender) {
|
|
MinecraftClient mc = MinecraftClient.getInstance();
|
|
return mc == null || mc.player == null || sender.equals(mc.player.getUuid());
|
|
}
|
|
|
|
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) {
|
|
LiteralText translatedText = new LiteralText(translateIfNeeded(sourceString, direction, true));
|
|
if (GoogleChatConfig.translationTooltip)
|
|
return source.copy().setStyle(addHover(Style.EMPTY, new LiteralText("Translated: ").append(translatedText)));
|
|
else
|
|
return translatedText.setStyle(addHover(Style.EMPTY, new LiteralText("Original: ").append(source)));
|
|
}
|
|
MutableText translated;
|
|
if (source instanceof TranslatableText tx) {
|
|
Object[] args = tx.getArgs();
|
|
args = Arrays.copyOf(args, args.length);
|
|
// We're not translating TranslatableText but want potential keys
|
|
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 = new TranslatableText(tx.getKey(), args);
|
|
} else if (source instanceof LiteralText tx) {
|
|
translated = new LiteralText(translateIfNeeded(tx.getRawString(), direction, false)).setStyle(tx.getStyle());
|
|
} else {
|
|
//LOGGER.info("Unhandled text type: " + source.getClass() + " (" + source + ")");
|
|
translated = source.copy();
|
|
}
|
|
if (GoogleChatConfig.translationTooltip)
|
|
return source.copy().styled(style -> addHover(style, translated));
|
|
else
|
|
return translated;
|
|
}
|
|
|
|
private static String toString(Text text) {
|
|
StringBuilder sb = new StringBuilder();
|
|
text.asOrderedText().accept((index, style, codePoint) -> {
|
|
sb.append((char)codePoint);
|
|
return true;
|
|
});
|
|
return sb.toString();
|
|
}
|
|
|
|
private static Style addHover(Style style, Text hoverText) {
|
|
return style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText));
|
|
}
|
|
|
|
public static <TLang extends Language> String translateIfNeeded(String source, Direction direction, boolean respectRegex) {
|
|
if (shouldSkipOutright(direction)) return source;
|
|
if (respectRegex && failsRegex(source, direction))
|
|
return source;
|
|
TranslateService<TLang> svc = (TranslateService<TLang>) GoogleChat.TRANSLATE_SERVICE; // Generics bypass
|
|
TLang clientLang = svc.parseLang(GoogleChatConfig.clientLanguage);
|
|
TLang serverLang = svc.parseLang(GoogleChatConfig.serverLanguage);
|
|
try {
|
|
return svc.translate(source,
|
|
direction == Direction.C2S ? clientLang : serverLang,
|
|
direction == Direction.C2S ? serverLang : clientLang);
|
|
} catch (TranslateException e) {
|
|
LOGGER.error("Could not translate text: " + source, e);
|
|
return source;
|
|
}
|
|
}
|
|
|
|
private static boolean failsRegex(String text, Direction direction) {
|
|
boolean isSender = (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) == (direction == Direction.C2S);
|
|
if (isSender) return text.matches(GoogleChatConfig.sendingRegex) == GoogleChatConfig.sendingRegexIsBlacklist;
|
|
else return text.matches(GoogleChatConfig.receivingRegex) == GoogleChatConfig.receivingRegexIsBlacklist;
|
|
}
|
|
|
|
private static boolean shouldSkipOutright(Direction direction) {
|
|
if (!GoogleChatConfig.enabled) return true;
|
|
Language clientLang = TRANSLATE_SERVICE.parseLang(GoogleChatConfig.clientLanguage);
|
|
Language serverLang = TRANSLATE_SERVICE.parseLang(GoogleChatConfig.serverLanguage);
|
|
if (direction == Direction.S2C && clientLang.getIdentifier().equals("auto")) return true;
|
|
if (direction == Direction.C2S && serverLang.getIdentifier().equals("auto")) return true;
|
|
return false;
|
|
}
|
|
|
|
public enum Direction {
|
|
C2S,
|
|
S2C
|
|
}
|
|
}
|