GoogleChat/src/main/java/io/gitlab/jfronny/googlechat/server/GoogleChatServer.java

45 lines
2.3 KiB
Java

package io.gitlab.jfronny.googlechat.server;
import io.gitlab.jfronny.googlechat.*;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.message.v1.ServerMessageDecoratorEvent;
import net.minecraft.text.Text;
import java.util.concurrent.CompletableFuture;
import static io.gitlab.jfronny.libjf.LibJf.LOGGER;
public class GoogleChatServer implements DedicatedServerModInitializer {
@Override
public void onInitializeServer() {
// Default phase is executed between CONTENT and STYLING
// Perform translation there instead of during CONTENT to better support other mods (such as chat-transform)
// If this causes an incompatibility, I'll add my own phase
ServerMessageDecoratorEvent.EVENT.register(Event.DEFAULT_PHASE, (sender, originalMessage) -> {
CompletableFuture<Text> futureMessage = CompletableFuture.completedFuture(originalMessage);
if (!GoogleChatConfig.General.enabled) return futureMessage; // fast fallthrough
if (sender != null) { // Client messages should first be translated to the server language
if (TranslationDirection.C2S.hasTarget()) {
if (TranslationDirection.S2C.hasTarget()) {
// Do not translate back and forth
return futureMessage;
}
}
futureMessage = futureMessage.thenApplyAsync(msg -> {
var translated = GoogleChat.translateIfNeeded(msg, TranslationDirection.C2S, true);
if (GoogleChatConfig.Advanced.debugLogs) LOGGER.info("Applied C2S translation from " + msg + " to " + translated);
return translated;
});
}
// All messages should be translated to the client language before sending
futureMessage = futureMessage.thenApplyAsync(msg -> {
var translated = GoogleChat.translateIfNeeded(msg, TranslationDirection.S2C, true);
if (GoogleChatConfig.Advanced.debugLogs) LOGGER.info("Applied S2C translation from " + msg + " to " + translated);
return translated;
});
return futureMessage;
});
}
}