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.nukkitx.protocol.bedrock.packet.TextPacket;
import java.util.List;
@Translator(packet = ServerChatPacket.class)
public class JavaChatTranslator extends PacketTranslator<ServerChatPacket> {
@ -61,10 +63,21 @@ public class JavaChatTranslator extends PacketTranslator<ServerChatPacket> {
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<String> 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()));
}

View file

@ -41,14 +41,16 @@ import java.util.*;
public class MessageUtils {
public static List<String> getTranslationParams(Message[] messages) {
public static List<String> getTranslationParams(Message[] messages, String locale) {
List<String> 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<String> 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<String> 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<String> 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) {