feat: use async on server

This commit is contained in:
Johannes Frohnmeyer 2023-07-18 21:33:41 +02:00
parent 08d9c0633b
commit 21c0cd1000
Signed by: Johannes
GPG Key ID: E76429612C2929F4
1 changed files with 17 additions and 15 deletions

View File

@ -17,24 +17,26 @@ public class GoogleChatServer implements DedicatedServerModInitializer {
// 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, message) -> {
Text original;
if (sender != null && hasTarget(GoogleChat.Direction.C2S)) {
if (hasTarget(GoogleChat.Direction.S2C)) return CompletableFuture.completedFuture(message); // Do not translate back and forth
}
ServerMessageDecoratorEvent.EVENT.register(Event.DEFAULT_PHASE, (sender, originalMessage) -> {
CompletableFuture<Text> futureMessage = CompletableFuture.completedFuture(originalMessage);
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 = GoogleChatCache.c2s(message);
if (GoogleChatConfig.Advanced.debugLogs) LOGGER.info("Applied C2S translation from " + original + " to " + message);
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;
});
}
// All messages should be translated to the client language before sending
original = message;
message = GoogleChatCache.s2c(message);
if (GoogleChatConfig.Advanced.debugLogs) LOGGER.info("Applied S2C translation from " + original + " to " + message);
return CompletableFuture.completedFuture(message);
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;
});
}
}