From 18311e3c1cf6b682f9c08e0d04d7aa03ff52dfeb Mon Sep 17 00:00:00 2001 From: rtm516 Date: Sun, 5 Apr 2020 12:19:20 +0100 Subject: [PATCH] Added argument parsing to serverside language processing --- .../translators/java/JavaChatTranslator.java | 17 ++++++++-- .../connector/utils/MessageUtils.java | 31 ++++++++++++++++--- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaChatTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaChatTranslator.java index f58c61c9a..017e0178e 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaChatTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaChatTranslator.java @@ -34,6 +34,8 @@ import com.github.steveice10.mc.protocol.data.message.TranslationMessage; import com.github.steveice10.mc.protocol.packet.ingame.server.ServerChatPacket; import com.nukkitx.protocol.bedrock.packet.TextPacket; +import java.util.List; + @Translator(packet = ServerChatPacket.class) public class JavaChatTranslator extends PacketTranslator { @@ -61,10 +63,21 @@ public class JavaChatTranslator extends PacketTranslator { if (packet.getMessage() instanceof TranslationMessage) { textPacket.setType(TextPacket.Type.TRANSLATION); textPacket.setNeedsTranslation(true); - textPacket.setParameters(MessageUtils.getTranslationParams(((TranslationMessage) packet.getMessage()).getTranslationParams())); - textPacket.setMessage(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), session.getClientData().getLanguageCode())); + + String locale = session.getClientData().getLanguageCode(); + + List paramsTranslated = MessageUtils.getTranslationParams(((TranslationMessage) packet.getMessage()).getTranslationParams(), locale); + textPacket.setParameters(paramsTranslated); + + textPacket.setMessage(MessageUtils.insertParams(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), locale), paramsTranslated)); } else { textPacket.setNeedsTranslation(false); + + // This make every message get translated which fixes alot of formatting issues + // but also causes players to be able to send translation strings as messages + // if thats all they send + // textPacket.setMessage(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), session.getClientData().getLanguageCode())); + textPacket.setMessage(MessageUtils.getBedrockMessage(packet.getMessage())); } diff --git a/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java b/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java index 8b32f0000..48ac8ab7b 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java @@ -41,14 +41,16 @@ import java.util.*; public class MessageUtils { - public static List getTranslationParams(Message[] messages) { + public static List getTranslationParams(Message[] messages, String locale) { List strings = new ArrayList<>(); for (Message message : messages) { if (message instanceof TranslationMessage) { TranslationMessage translation = (TranslationMessage) message; - String builder = "%" + translation.getTranslationKey(); - strings.add(builder); + if (locale == null) { + String builder = "%" + translation.getTranslationKey(); + strings.add(builder); + } if (translation.getTranslationKey().equals("commands.gamemode.success.other")) { strings.add(""); @@ -58,7 +60,12 @@ public class MessageUtils { strings.add(" - no permission or invalid command!"); } - strings.addAll(getTranslationParams(translation.getTranslationParams())); + List furtherParams = getTranslationParams(translation.getTranslationParams()); + if (locale != null) { + strings.add(insertParams(getLocaleString(translation.getTranslationKey(), locale), furtherParams)); + }else{ + strings.addAll(furtherParams); + } } else { String builder = getFormat(message.getStyle().getFormats()) + getColor(message.getStyle().getColor()) + @@ -70,6 +77,10 @@ public class MessageUtils { return strings; } + public static List getTranslationParams(Message[] messages) { + return getTranslationParams(messages, null); + } + public static String getTranslationText(TranslationMessage message) { return getFormat(message.getStyle().getFormats()) + getColor(message.getStyle().getColor()) + "%" + message.getTranslationKey(); @@ -92,7 +103,7 @@ public class MessageUtils { builder.append(getFormat(msg.getStyle().getFormats())); builder.append(getColor(msg.getStyle().getColor())); if (!(msg.getText() == null)) { - builder.append(getBedrockMessage(msg)); + builder.append(getTranslatedBedrockMessage(msg, locale)); } } return builder.toString(); @@ -117,6 +128,16 @@ public class MessageUtils { return getTranslatedBedrockMessage(message); } + public static String insertParams(String message, List params) { + String newMessage = message; + + for (String text : params) { + newMessage = newMessage.replaceFirst("%s", text); + } + + return newMessage; + } + private static String getColor(ChatColor color) { String base = "\u00a7"; switch (color) {