Properly separate client and server parts

This commit is contained in:
Johannes Frohnmeyer 2023-03-12 09:37:39 +01:00
parent 3c916a560e
commit 60e3c5c125
Signed by: Johannes
GPG Key ID: E76429612C2929F4
7 changed files with 46 additions and 32 deletions

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.googlechat.mixin;
package io.gitlab.jfronny.googlechat.client.mixin;
import io.gitlab.jfronny.googlechat.GoogleChat;
import net.minecraft.client.gui.screen.ChatScreen;

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.googlechat.mixin;
package io.gitlab.jfronny.googlechat.client.mixin;
import io.gitlab.jfronny.googlechat.GoogleChat;
import net.minecraft.client.network.ClientPlayerEntity;

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.googlechat.mixin;
package io.gitlab.jfronny.googlechat.client.mixin;
import com.mojang.authlib.GameProfile;
import io.gitlab.jfronny.googlechat.GoogleChat;

View File

@ -1,7 +1,7 @@
{
"required": true,
"minVersion": "0.8",
"package": "io.gitlab.jfronny.googlechat.mixin",
"package": "io.gitlab.jfronny.googlechat.client.mixin",
"compatibilityLevel": "JAVA_17",
"client": [
"ChatScreenMixin",

View File

@ -1,39 +1,19 @@
package io.gitlab.jfronny.googlechat;
import io.gitlab.jfronny.commons.log.*;
import io.gitlab.jfronny.commons.log.Logger;
import io.gitlab.jfronny.libjf.translate.api.*;
import net.fabricmc.api.*;
import net.fabricmc.fabric.api.message.v1.*;
import net.fabricmc.loader.api.*;
import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.Arrays;
import java.util.Optional;
public class GoogleChat implements ModInitializer {
public class GoogleChat {
public static final String MOD_ID = "google-chat";
public static final Logger LOGGER = Logger.forName(MOD_ID);
public static final TranslateService<?> TRANSLATE_SERVICE = TranslateService.getConfigured();
@Override
public void onInitialize() {
ServerMessageDecoratorEvent.EVENT.register(ServerMessageDecoratorEvent.CONTENT_PHASE, (sender, message) -> {
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);
if (GoogleChatConfig.debugLogs) LOGGER.info("Applied C2S translation from " + original + " to " + message);
}
// All messages should be translated to the client language before sending
original = message;
message = translateIfNeeded(message, Direction.S2C, true);
if (GoogleChatConfig.debugLogs) LOGGER.info("Applied S2C translation from " + original + " to " + message);
return CompletableFuture.completedFuture(message);
});
}
public static Text translateIfNeeded(Text source, Direction direction, boolean respectRegex) {
if (shouldSkipOutright(direction)) return source;
String sourceString = toString(source);
@ -130,7 +110,7 @@ public class GoogleChat implements ModInitializer {
return !GoogleChatConfig.enabled || !hasTarget(direction);
}
private static boolean hasTarget(Direction direction) {
public static boolean hasTarget(Direction direction) {
return !TRANSLATE_SERVICE.parseLang(switch (direction) {
case C2S -> GoogleChatConfig.serverLanguage;
case S2C -> GoogleChatConfig.clientLanguage;

View File

@ -0,0 +1,34 @@
package io.gitlab.jfronny.googlechat.server;
import io.gitlab.jfronny.googlechat.GoogleChat;
import io.gitlab.jfronny.googlechat.GoogleChatConfig;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.fabric.api.message.v1.ServerMessageDecoratorEvent;
import net.minecraft.text.Text;
import java.util.concurrent.CompletableFuture;
import static io.gitlab.jfronny.googlechat.GoogleChat.hasTarget;
import static io.gitlab.jfronny.googlechat.GoogleChat.translateIfNeeded;
import static io.gitlab.jfronny.libjf.LibJf.LOGGER;
public class GoogleChatServer implements DedicatedServerModInitializer {
@Override
public void onInitializeServer() {
ServerMessageDecoratorEvent.EVENT.register(ServerMessageDecoratorEvent.CONTENT_PHASE, (sender, message) -> {
Text original;
if (sender != null) { // Client messages should first be translated to the server language
if (hasTarget(GoogleChat.Direction.C2S) && hasTarget(GoogleChat.Direction.S2C)) // Do not translate back and forth
return CompletableFuture.completedFuture(message);
original = message;
message = translateIfNeeded(message, GoogleChat.Direction.C2S, true);
if (GoogleChatConfig.debugLogs) LOGGER.info("Applied C2S translation from " + original + " to " + message);
}
// All messages should be translated to the client language before sending
original = message;
message = translateIfNeeded(message, GoogleChat.Direction.S2C, true);
if (GoogleChatConfig.debugLogs) LOGGER.info("Applied S2C translation from " + original + " to " + message);
return CompletableFuture.completedFuture(message);
});
}
}

View File

@ -16,7 +16,7 @@
"environment": "*",
"entrypoints": {
"libjf:config": ["io.gitlab.jfronny.googlechat.JFC_GoogleChatConfig"],
"main": ["io.gitlab.jfronny.googlechat.GoogleChat"]
"server": ["io.gitlab.jfronny.googlechat.server.GoogleChatServer"]
},
"mixins": [
{