Added argument parsing to serverside language processing

This commit is contained in:
rtm516 2020-04-05 12:19:20 +01:00
parent c809ddb618
commit 18311e3c1c
2 changed files with 41 additions and 7 deletions

View file

@ -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.github.steveice10.mc.protocol.packet.ingame.server.ServerChatPacket;
import com.nukkitx.protocol.bedrock.packet.TextPacket; import com.nukkitx.protocol.bedrock.packet.TextPacket;
import java.util.List;
@Translator(packet = ServerChatPacket.class) @Translator(packet = ServerChatPacket.class)
public class JavaChatTranslator extends PacketTranslator<ServerChatPacket> { public class JavaChatTranslator extends PacketTranslator<ServerChatPacket> {
@ -61,10 +63,21 @@ public class JavaChatTranslator extends PacketTranslator<ServerChatPacket> {
if (packet.getMessage() instanceof TranslationMessage) { if (packet.getMessage() instanceof TranslationMessage) {
textPacket.setType(TextPacket.Type.TRANSLATION); textPacket.setType(TextPacket.Type.TRANSLATION);
textPacket.setNeedsTranslation(true); 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<String> paramsTranslated = MessageUtils.getTranslationParams(((TranslationMessage) packet.getMessage()).getTranslationParams(), locale);
textPacket.setParameters(paramsTranslated);
textPacket.setMessage(MessageUtils.insertParams(MessageUtils.getTranslatedBedrockMessage(packet.getMessage(), locale), paramsTranslated));
} else { } else {
textPacket.setNeedsTranslation(false); 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())); textPacket.setMessage(MessageUtils.getBedrockMessage(packet.getMessage()));
} }

View file

@ -41,14 +41,16 @@ import java.util.*;
public class MessageUtils { public class MessageUtils {
public static List<String> getTranslationParams(Message[] messages) { public static List<String> getTranslationParams(Message[] messages, String locale) {
List<String> strings = new ArrayList<>(); List<String> strings = new ArrayList<>();
for (Message message : messages) { for (Message message : messages) {
if (message instanceof TranslationMessage) { if (message instanceof TranslationMessage) {
TranslationMessage translation = (TranslationMessage) message; TranslationMessage translation = (TranslationMessage) message;
String builder = "%" + translation.getTranslationKey(); if (locale == null) {
strings.add(builder); String builder = "%" + translation.getTranslationKey();
strings.add(builder);
}
if (translation.getTranslationKey().equals("commands.gamemode.success.other")) { if (translation.getTranslationKey().equals("commands.gamemode.success.other")) {
strings.add(""); strings.add("");
@ -58,7 +60,12 @@ public class MessageUtils {
strings.add(" - no permission or invalid command!"); strings.add(" - no permission or invalid command!");
} }
strings.addAll(getTranslationParams(translation.getTranslationParams())); List<String> furtherParams = getTranslationParams(translation.getTranslationParams());
if (locale != null) {
strings.add(insertParams(getLocaleString(translation.getTranslationKey(), locale), furtherParams));
}else{
strings.addAll(furtherParams);
}
} else { } else {
String builder = getFormat(message.getStyle().getFormats()) + String builder = getFormat(message.getStyle().getFormats()) +
getColor(message.getStyle().getColor()) + getColor(message.getStyle().getColor()) +
@ -70,6 +77,10 @@ public class MessageUtils {
return strings; return strings;
} }
public static List<String> getTranslationParams(Message[] messages) {
return getTranslationParams(messages, null);
}
public static String getTranslationText(TranslationMessage message) { public static String getTranslationText(TranslationMessage message) {
return getFormat(message.getStyle().getFormats()) + getColor(message.getStyle().getColor()) return getFormat(message.getStyle().getFormats()) + getColor(message.getStyle().getColor())
+ "%" + message.getTranslationKey(); + "%" + message.getTranslationKey();
@ -92,7 +103,7 @@ public class MessageUtils {
builder.append(getFormat(msg.getStyle().getFormats())); builder.append(getFormat(msg.getStyle().getFormats()));
builder.append(getColor(msg.getStyle().getColor())); builder.append(getColor(msg.getStyle().getColor()));
if (!(msg.getText() == null)) { if (!(msg.getText() == null)) {
builder.append(getBedrockMessage(msg)); builder.append(getTranslatedBedrockMessage(msg, locale));
} }
} }
return builder.toString(); return builder.toString();
@ -117,6 +128,16 @@ public class MessageUtils {
return getTranslatedBedrockMessage(message); return getTranslatedBedrockMessage(message);
} }
public static String insertParams(String message, List<String> params) {
String newMessage = message;
for (String text : params) {
newMessage = newMessage.replaceFirst("%s", text);
}
return newMessage;
}
private static String getColor(ChatColor color) { private static String getColor(ChatColor color) {
String base = "\u00a7"; String base = "\u00a7";
switch (color) { switch (color) {