Update for 1.19.1

This commit is contained in:
Johannes Frohnmeyer 2022-07-28 11:53:52 +02:00
parent 7e10364ccb
commit c9fb475f6c
Signed by: Johannes
GPG Key ID: E76429612C2929F4
11 changed files with 76 additions and 75 deletions

View File

@ -6,7 +6,7 @@ dependencies {
//modRuntimeOnly("io.gitlab.jfronny.libjf:libjf-devutil-v0:${project.jfapi_version}") //modRuntimeOnly("io.gitlab.jfronny.libjf:libjf-devutil-v0:${project.jfapi_version}")
modImplementation "com.terraformersmc:modmenu:4.0.0-beta.4" modImplementation "com.terraformersmc:modmenu:4.0.5"
// Compat fix // Compat fix
include modImplementation(fabricApi.module("fabric-command-api-v2", project.fabric_version)) include modImplementation(fabricApi.module("fabric-command-api-v2", project.fabric_version))

View File

@ -1,16 +1,16 @@
# https://fabricmc.net/develop/ # https://fabricmc.net/develop/
minecraft_version=1.19 minecraft_version=1.19.1
yarn_mappings=build.1 yarn_mappings=build.1
loader_version=0.14.6 loader_version=0.14.8
maven_group=io.gitlab.jfronny maven_group=io.gitlab.jfronny
archives_base_name=GoogleChat archives_base_name=GoogleChat
jfapi_version=2.9.0 jfapi_version=2.10.0
fabric_version=0.55.1+1.19 fabric_version=0.58.5+1.19.1
modrinth_id=Hd7ZLKRk modrinth_id=google-chat
modrinth_required_dependencies=WKwQAwke modrinth_required_dependencies=libjf
modrinth_optional_dependencies=mOgUt4GM modrinth_optional_dependencies=modmenu
curseforge_id=574331 curseforge_id=574331
curseforge_required_dependencies=libjf curseforge_required_dependencies=libjf
curseforge_optional_dependencies=modmenu curseforge_optional_dependencies=modmenu

View File

@ -0,0 +1,35 @@
package io.gitlab.jfronny.googlechat.mixin;
import io.gitlab.jfronny.googlechat.GoogleChat;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.client.gui.hud.MessageIndicator;
import net.minecraft.client.network.PlayerListEntry;
import net.minecraft.client.network.message.MessageHandler;
import net.minecraft.network.message.*;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.*;
import java.time.Instant;
@Mixin(MessageHandler.class)
public class MessageHandlerMixin {
@Redirect(method = "processChatMessageInternal(Lnet/minecraft/network/message/MessageType$Parameters;Lnet/minecraft/network/message/SignedMessage;Lnet/minecraft/text/Text;Lnet/minecraft/client/network/PlayerListEntry;ZLjava/time/Instant;)Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/hud/ChatHud;addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;Lnet/minecraft/client/gui/hud/MessageIndicator;)V"))
void googlechat$injectAddMessage(ChatHud instance, Text message, @Nullable MessageSignatureData signature, @Nullable MessageIndicator indicator, MessageType.Parameters args$params, SignedMessage args$message, Text args$decorated, @Nullable PlayerListEntry args$senderEntry, boolean args$onlyShowSecureChat, Instant args$receptionTimestamp) {
MinecraftClient mc = MinecraftClient.getInstance();
if (mc != null && mc.player != null && args$senderEntry != null && !args$senderEntry.getProfile().getId().equals(mc.player.getUuid()))
message = googlechat$translate(message);
instance.addMessage(message, signature, indicator);
}
@Redirect(method = "processProfilelessMessage(Lnet/minecraft/network/message/MessageType$Parameters;Lnet/minecraft/network/message/SignedMessage;Lnet/minecraft/text/Text;)Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/hud/ChatHud;addMessage(Lnet/minecraft/text/Text;)V"))
void googlechat$injectAddMessage(ChatHud instance, Text message) {
instance.addMessage(googlechat$translate(message));
}
private Text googlechat$translate(Text message) {
return GoogleChat.translateIfNeeded(message, GoogleChat.Direction.S2C, true);
}
}

View File

@ -6,7 +6,7 @@
"client": [ "client": [
"ChatScreenMixin", "ChatScreenMixin",
"ClientPlayerEntityMixin", "ClientPlayerEntityMixin",
"ClientPlayNetworkHandlerMixin" "MessageHandlerMixin"
], ],
"injectors": { "injectors": {
"defaultRequire": 1 "defaultRequire": 1

View File

@ -1,4 +0,0 @@
package io.gitlab.jfronny.googlechat;
public record CacheKey(String sourceText, String sourceLangId, String targetLangId) {
}

View File

@ -1,17 +0,0 @@
package io.gitlab.jfronny.googlechat;
import java.util.*;
public class FixedSizeCache<K, V> extends LinkedHashMap<K, V> {
private final int maxSize;
public FixedSizeCache(int size) {
super(size + 2, 1F);
this.maxSize = size;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > maxSize;
}
}

View File

@ -5,7 +5,6 @@ import io.gitlab.jfronny.libjf.translate.api.*;
import net.fabricmc.api.*; import net.fabricmc.api.*;
import net.fabricmc.fabric.api.message.v1.*; import net.fabricmc.fabric.api.message.v1.*;
import net.fabricmc.loader.api.*; import net.fabricmc.loader.api.*;
import net.minecraft.client.*;
import net.minecraft.text.*; import net.minecraft.text.*;
import java.util.*; import java.util.*;
@ -15,24 +14,26 @@ public class GoogleChat implements ModInitializer {
public static final String MOD_ID = "google-chat"; public static final String MOD_ID = "google-chat";
public static final Logger LOGGER = Logger.forName(MOD_ID); public static final Logger LOGGER = Logger.forName(MOD_ID);
public static final TranslateService<?> TRANSLATE_SERVICE = TranslateService.getConfigured(); public static final TranslateService<?> TRANSLATE_SERVICE = TranslateService.getConfigured();
private static final FixedSizeCache<CacheKey, String> TRANSLATION_CACHE = new FixedSizeCache<>(126);
@Override @Override
public void onInitialize() { public void onInitialize() {
ServerMessageDecoratorEvent.EVENT.register(ServerMessageDecoratorEvent.CONTENT_PHASE, (sender, message) -> { ServerMessageDecoratorEvent.EVENT.register(ServerMessageDecoratorEvent.CONTENT_PHASE, (sender, message) -> {
if (sender != null) // Client messages should first be translated to the server language Text original;
if (sender != null) { // Client messages should first be translated to the server language
if (hasTarget(Direction.C2S) && hasTarget(Direction.S2C)) // Do not translate back and forth
return CompletableFuture.completedFuture(message);
original = message;
message = translateIfNeeded(message, Direction.C2S, true); message = translateIfNeeded(message, Direction.C2S, true);
LOGGER.info("Applied C2S translation from " + original + " to " + message);
}
// All messages should be translated to the client language before sending // All messages should be translated to the client language before sending
original = message;
message = translateIfNeeded(message, Direction.S2C, true); message = translateIfNeeded(message, Direction.S2C, true);
LOGGER.info("Applied S2C translation from " + original + " to " + message);
return CompletableFuture.completedFuture(message); return CompletableFuture.completedFuture(message);
}); });
} }
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) { public static Text translateIfNeeded(Text source, Direction direction, boolean respectRegex) {
if (shouldSkipOutright(direction)) return source; if (shouldSkipOutright(direction)) return source;
String sourceString = toString(source); String sourceString = toString(source);
@ -49,7 +50,7 @@ public class GoogleChat implements ModInitializer {
if (source.getContent() instanceof TranslatableTextContent tx) { if (source.getContent() instanceof TranslatableTextContent tx) {
Object[] args = tx.getArgs(); Object[] args = tx.getArgs();
args = Arrays.copyOf(args, args.length); args = Arrays.copyOf(args, args.length);
// We're not translating TranslatableText but want potential keys // We're not translating TranslatableText, but are translating arguments
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
args[i] = args[i] instanceof Text tx1 ? translateIfNeeded(tx1, direction, false) args[i] = args[i] instanceof Text tx1 ? translateIfNeeded(tx1, direction, false)
: args[i] instanceof String tx1 ? translateIfNeeded(tx1, direction, false) : args[i] instanceof String tx1 ? translateIfNeeded(tx1, direction, false)
@ -81,19 +82,17 @@ public class GoogleChat implements ModInitializer {
return style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_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) { public static String translateIfNeeded(String source, Direction direction, boolean respectRegex) {
if (shouldSkipOutright(direction)) return source; if (shouldSkipOutright(direction)) return source;
if (respectRegex && failsRegex(source, direction)) if (respectRegex && failsRegex(source, direction))
return source; return source;
TranslateService<TLang> svc = (TranslateService<TLang>) GoogleChat.TRANSLATE_SERVICE; // Generics bypass // Ignore generics since this is apparently not something java supports
TLang sourceLang = svc.parseLang(direction == Direction.C2S ? GoogleChatConfig.clientLanguage : GoogleChatConfig.serverLanguage); @SuppressWarnings("rawtypes") TranslateService svc = GoogleChat.TRANSLATE_SERVICE;
TLang targetLang = svc.parseLang(direction == Direction.C2S ? GoogleChatConfig.serverLanguage : GoogleChatConfig.clientLanguage); Language sourceLang = svc.parseLang(direction == Direction.C2S ? GoogleChatConfig.clientLanguage : GoogleChatConfig.serverLanguage);
CacheKey key = new CacheKey(source, sourceLang.getIdentifier(), targetLang.getIdentifier()); Language targetLang = svc.parseLang(direction == Direction.C2S ? GoogleChatConfig.serverLanguage : GoogleChatConfig.clientLanguage);
if (TRANSLATION_CACHE.containsKey(key)) return TRANSLATION_CACHE.get(key);
try { try {
String translated = svc.translate(source, sourceLang, targetLang); //noinspection unchecked
TRANSLATION_CACHE.put(key, translated); return svc.translate(source, sourceLang, targetLang);
return translated;
} catch (TranslateException e) { } catch (TranslateException e) {
LOGGER.error("Could not translate text: " + source, e); LOGGER.error("Could not translate text: " + source, e);
return source; return source;
@ -107,12 +106,14 @@ public class GoogleChat implements ModInitializer {
} }
private static boolean shouldSkipOutright(Direction direction) { private static boolean shouldSkipOutright(Direction direction) {
if (!GoogleChatConfig.enabled) return true; return !GoogleChatConfig.enabled || !hasTarget(direction);
Language clientLang = TRANSLATE_SERVICE.parseLang(GoogleChatConfig.clientLanguage); }
Language serverLang = TRANSLATE_SERVICE.parseLang(GoogleChatConfig.serverLanguage);
if (direction == Direction.S2C && clientLang.getIdentifier().equals("auto")) return true; private static boolean hasTarget(Direction direction) {
if (direction == Direction.C2S && serverLang.getIdentifier().equals("auto")) return true; return TRANSLATE_SERVICE.parseLang(switch (direction) {
return false; case C2S -> GoogleChatConfig.serverLanguage;
case S2C -> GoogleChatConfig.clientLanguage;
}).getIdentifier().equals("auto");
} }
public enum Direction { public enum Direction {

View File

@ -1,19 +0,0 @@
package io.gitlab.jfronny.googlechat.mixin;
import io.gitlab.jfronny.googlechat.*;
import net.minecraft.client.gui.hud.*;
import net.minecraft.client.network.*;
import net.minecraft.network.message.*;
import net.minecraft.text.*;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;
@Mixin(ClientPlayNetworkHandler.class)
public class ClientPlayNetworkHandlerMixin {
@Redirect(method = "handleMessage(Lnet/minecraft/network/message/MessageType;Lnet/minecraft/network/message/SignedMessage;Lnet/minecraft/network/message/MessageSender;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/hud/InGameHud;onChatMessage(Lnet/minecraft/network/message/MessageType;Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSender;)V"))
private void googlechat$interceptChatMessage(InGameHud hud, MessageType type, Text message, MessageSender sender) {
if (!GoogleChat.isSelf(sender.uuid()))
message = GoogleChat.translateIfNeeded(message, GoogleChat.Direction.S2C, true);
hud.onChatMessage(type, message, sender);
}
}

View File

@ -14,11 +14,16 @@
"main": ["io.gitlab.jfronny.googlechat.GoogleChat"] "main": ["io.gitlab.jfronny.googlechat.GoogleChat"]
}, },
"mixins": [ "mixins": [
"GoogleChat.mixins.json" {
"config": "google-chat.client.mixins.json",
"environment": "client"
}
], ],
"depends": { "depends": {
"fabricloader": ">=0.12.12", "fabricloader": ">=0.12.12",
"minecraft": "*" "minecraft": "*",
"libjf-config-v0": "*",
"libjf-translate-v1": "*"
}, },
"custom": { "custom": {
"libjf:config": { "libjf:config": {