Geyser/connector/src/main/java/org/geysermc/connector/utils/MessageUtils.java

300 lines
10 KiB
Java
Raw Normal View History

/*
* Copyright (c) 2019-2020 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
2019-07-21 01:10:30 +00:00
package org.geysermc.connector.utils;
2020-04-04 18:39:14 +00:00
import com.fasterxml.jackson.databind.JsonNode;
import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamColor;
2019-07-21 01:10:30 +00:00
import com.github.steveice10.mc.protocol.data.message.ChatColor;
import com.github.steveice10.mc.protocol.data.message.ChatFormat;
import com.github.steveice10.mc.protocol.data.message.Message;
import com.github.steveice10.mc.protocol.data.message.TranslationMessage;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
2020-04-05 01:37:39 +00:00
import org.geysermc.connector.GeyserConnector;
2019-07-21 01:10:30 +00:00
2020-04-04 18:39:14 +00:00
import java.io.InputStream;
import java.util.*;
2019-07-21 01:10:30 +00:00
public class MessageUtils {
2020-04-05 01:37:39 +00:00
private static final HashMap<String, HashMap<String, String>> LOCALE_MAPPINGS = new HashMap<>();
2020-04-04 18:39:14 +00:00
static {
/* Load the language mappings */
2020-04-05 01:37:39 +00:00
InputStream languagesStream = Toolbox.getResource("mappings/locales.json");
JsonNode locales;
2020-04-04 18:39:14 +00:00
try {
2020-04-05 01:37:39 +00:00
locales = Toolbox.JSON_MAPPER.readTree(languagesStream);
2020-04-04 18:39:14 +00:00
} catch (Exception e) {
2020-04-05 01:37:39 +00:00
throw new AssertionError("Unable to load Java locale list", e);
2020-04-04 18:39:14 +00:00
}
2020-04-05 01:37:39 +00:00
for (JsonNode localeNode : locales.get("locales")) {
String currentLocale = localeNode.asText();
InputStream stream = Toolbox.getResource("mappings/lang/" + currentLocale + ".json");
JsonNode lang;
try {
lang = Toolbox.JSON_MAPPER.readTree(stream);
} catch (Exception e) {
throw new AssertionError("Unable to load Java lang map for " + currentLocale, e);
}
Iterator<Map.Entry<String, JsonNode>> langIterator = lang.fields();
HashMap<String, String> langMap = new HashMap<>();
while (langIterator.hasNext()) {
Map.Entry<String, JsonNode> entry = langIterator.next();
langMap.put(entry.getKey(), entry.getValue().asText());
}
LOCALE_MAPPINGS.put(currentLocale.toLowerCase(), langMap);
2020-04-04 18:39:14 +00:00
}
}
2019-07-21 01:10:30 +00:00
public static List<String> getTranslationParams(Message[] messages) {
List<String> strings = new ArrayList<>();
for (Message message : messages) {
if (message instanceof TranslationMessage) {
TranslationMessage translation = (TranslationMessage) message;
2019-07-21 01:10:30 +00:00
String builder = "%" + translation.getTranslationKey();
strings.add(builder);
2019-07-21 01:10:30 +00:00
if (translation.getTranslationKey().equals("commands.gamemode.success.other")) {
strings.add("");
}
2019-07-21 19:48:36 +00:00
if (translation.getTranslationKey().equals("command.context.here")) {
strings.add(" - no permission or invalid command!");
}
strings.addAll(getTranslationParams(translation.getTranslationParams()));
2019-07-21 01:10:30 +00:00
} else {
String builder = getFormat(message.getStyle().getFormats()) +
getColor(message.getStyle().getColor()) +
getBedrockMessage(message);
strings.add(builder);
2019-07-21 01:10:30 +00:00
}
}
return strings;
}
public static String getTranslationText(TranslationMessage message) {
return getFormat(message.getStyle().getFormats()) + getColor(message.getStyle().getColor())
+ "%" + message.getTranslationKey();
2019-07-21 01:10:30 +00:00
}
2020-04-05 01:37:39 +00:00
public static String getBedrockMessageWithTranslate(Message message, String locale) {
2019-07-21 01:10:30 +00:00
JsonParser parser = new JsonParser();
if (isMessage(message.getText())) {
JsonObject object = parser.parse(message.getText()).getAsJsonObject();
message = Message.fromJson(formatJson(object));
}
2020-04-04 16:25:38 +00:00
String messageText = message.getText();
2020-04-05 01:37:39 +00:00
if (locale != null) {
messageText = getLocaleString(messageText, locale);
2020-04-04 16:25:38 +00:00
}
StringBuilder builder = new StringBuilder(messageText);
2019-07-21 01:10:30 +00:00
for (Message msg : message.getExtra()) {
builder.append(getFormat(msg.getStyle().getFormats()));
builder.append(getColor(msg.getStyle().getColor()));
if (!(msg.getText() == null)) {
builder.append(getBedrockMessage(msg));
}
}
return builder.toString();
}
2020-04-04 16:25:38 +00:00
2020-04-05 01:37:39 +00:00
public static String getBedrockMessageWithTranslate(Message message) {
return getBedrockMessageWithTranslate(message, null);
}
private static String getLocaleString(String messageText, String locale) {
HashMap<String, String> localeStrings = LOCALE_MAPPINGS.get(locale.toLowerCase());
if (localeStrings == null)
localeStrings = LOCALE_MAPPINGS.get("en_us");
String newLocaleString = localeStrings.getOrDefault(messageText, messageText);
GeyserConnector.getInstance().getLogger().info("Converting '" + messageText + "' -> '" + newLocaleString + "'");
return newLocaleString;
2020-04-04 16:25:38 +00:00
}
public static String getBedrockMessage(Message message) {
2020-04-05 01:37:39 +00:00
return getBedrockMessageWithTranslate(message);
2020-04-04 16:25:38 +00:00
}
2019-07-21 01:10:30 +00:00
private static String getColor(ChatColor color) {
String base = "\u00a7";
switch (color) {
case BLACK:
base += "0";
break;
case DARK_BLUE:
base += "1";
break;
case DARK_GREEN:
base += "2";
break;
case DARK_AQUA:
base += "3";
break;
case DARK_RED:
base += "4";
break;
case DARK_PURPLE:
base += "5";
break;
case GOLD:
base += "6";
break;
case GRAY:
base += "7";
break;
case DARK_GRAY:
base += "8";
break;
case BLUE:
base += "9";
break;
case GREEN:
base += "a";
break;
case AQUA:
base += "b";
break;
case RED:
base += "c";
break;
case LIGHT_PURPLE:
base += "d";
break;
case YELLOW:
base += "e";
break;
case WHITE:
base += "f";
break;
case RESET:
case NONE:
2019-07-21 01:10:30 +00:00
base += "r";
break;
default:
2020-03-06 23:29:11 +00:00
return "";
2019-07-21 01:10:30 +00:00
}
return base;
}
private static String getFormat(List<ChatFormat> formats) {
2020-03-06 21:44:49 +00:00
StringBuilder str = new StringBuilder();
2019-07-21 01:10:30 +00:00
for (ChatFormat cf : formats) {
String base = "\u00a7";
switch (cf) {
case OBFUSCATED:
base += "k";
break;
case BOLD:
base += "l";
break;
case STRIKETHROUGH:
base += "m";
break;
case UNDERLINED:
base += "n";
break;
case ITALIC:
base += "o";
break;
default:
2020-03-06 21:44:49 +00:00
return "";
2019-07-21 01:10:30 +00:00
}
2020-03-06 21:44:49 +00:00
str.append(base);
2019-07-21 01:10:30 +00:00
}
2020-03-06 21:44:49 +00:00
return str.toString();
2019-07-21 01:10:30 +00:00
}
public static boolean isMessage(String text) {
JsonParser parser = new JsonParser();
try {
JsonObject object = parser.parse(text).getAsJsonObject();
try {
Message.fromJson(formatJson(object));
} catch (Exception ex) {
return false;
}
} catch (Exception ex) {
return false;
}
return true;
}
public static JsonObject formatJson(JsonObject object) {
if (object.has("hoverEvent")) {
JsonObject sub = (JsonObject) object.get("hoverEvent");
JsonElement element = sub.get("value");
if (element instanceof JsonArray) {
JsonObject newobj = new JsonObject();
newobj.add("extra", element);
newobj.addProperty("text", "");
sub.remove("value");
sub.add("value", newobj);
}
}
if (object.has("extra")) {
JsonArray a = object.getAsJsonArray("extra");
for (int i = 0; i < a.size(); i++) {
if (!(a.get(i) instanceof JsonPrimitive))
formatJson((JsonObject) a.get(i));
}
}
return object;
}
public static String toChatColor(TeamColor teamColor) {
for (ChatColor color : ChatColor.values()) {
if (color.name().equals(teamColor.name())) {
return getColor(color);
}
}
for (ChatFormat format : ChatFormat.values()) {
if (format.name().equals(teamColor.name())) {
return getFormat(Collections.singletonList(format));
}
}
return "";
}
2019-07-21 01:10:30 +00:00
}