2023-03-12 09:37:39 +01:00
|
|
|
package io.gitlab.jfronny.googlechat.server;
|
|
|
|
|
2023-03-12 10:19:20 +01:00
|
|
|
import io.gitlab.jfronny.googlechat.*;
|
2023-03-12 09:37:39 +01:00
|
|
|
import net.fabricmc.api.DedicatedServerModInitializer;
|
2023-03-22 19:12:14 +01:00
|
|
|
import net.fabricmc.fabric.api.event.Event;
|
2023-03-12 09:37:39 +01:00
|
|
|
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.libjf.LibJf.LOGGER;
|
|
|
|
|
|
|
|
public class GoogleChatServer implements DedicatedServerModInitializer {
|
|
|
|
@Override
|
|
|
|
public void onInitializeServer() {
|
2023-03-22 19:12:14 +01:00
|
|
|
// 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
|
2023-07-18 21:33:41 +02:00
|
|
|
ServerMessageDecoratorEvent.EVENT.register(Event.DEFAULT_PHASE, (sender, originalMessage) -> {
|
|
|
|
CompletableFuture<Text> futureMessage = CompletableFuture.completedFuture(originalMessage);
|
2023-03-12 09:37:39 +01:00
|
|
|
if (sender != null) { // Client messages should first be translated to the server language
|
2023-07-18 21:33:41 +02:00
|
|
|
if (hasTarget(GoogleChat.Direction.C2S) && hasTarget(GoogleChat.Direction.S2C)) {
|
|
|
|
// Do not translate back and forth
|
|
|
|
return futureMessage;
|
|
|
|
}
|
|
|
|
futureMessage = futureMessage.thenApplyAsync(msg -> {
|
|
|
|
var translated = GoogleChatCache.c2s(msg);
|
|
|
|
if (GoogleChatConfig.Advanced.debugLogs) LOGGER.info("Applied C2S translation from " + msg + " to " + translated);
|
|
|
|
return translated;
|
|
|
|
});
|
2023-03-12 09:37:39 +01:00
|
|
|
}
|
|
|
|
// All messages should be translated to the client language before sending
|
2023-07-18 21:33:41 +02:00
|
|
|
futureMessage = futureMessage.thenApplyAsync(msg -> {
|
|
|
|
var translated = GoogleChatCache.s2c(msg);
|
|
|
|
if (GoogleChatConfig.Advanced.debugLogs) LOGGER.info("Applied S2C translation from " + msg + " to " + translated);
|
|
|
|
return translated;
|
|
|
|
});
|
|
|
|
return futureMessage;
|
2023-03-12 09:37:39 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|