2020-01-09 03:05:42 +00:00
|
|
|
/*
|
|
|
|
* 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 06:27:34 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamColor;
|
2020-04-09 19:24:32 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.message.*;
|
2019-07-21 01:10:30 +00:00
|
|
|
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-13 16:49:29 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2019-07-21 01:10:30 +00:00
|
|
|
|
2020-04-04 18:39:14 +00:00
|
|
|
import java.util.*;
|
2020-04-05 22:37:41 +00:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2019-07-21 01:10:30 +00:00
|
|
|
|
|
|
|
public class MessageUtils {
|
|
|
|
|
2020-04-05 11:19:20 +00:00
|
|
|
public static List<String> getTranslationParams(Message[] messages, String locale) {
|
2020-04-04 06:27:34 +00:00
|
|
|
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
|
|
|
|
2020-04-05 11:19:20 +00:00
|
|
|
if (locale == null) {
|
|
|
|
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!");
|
|
|
|
}
|
|
|
|
|
2020-04-13 21:29:22 +00:00
|
|
|
List<String> furtherParams = getTranslationParams(translation.getTranslationParams(), locale);
|
2020-04-05 11:19:20 +00:00
|
|
|
if (locale != null) {
|
2020-04-08 20:11:56 +00:00
|
|
|
strings.add(insertParams(LocaleUtils.getLocaleString(translation.getTranslationKey(), locale), furtherParams));
|
2020-04-05 11:19:20 +00:00
|
|
|
}else{
|
|
|
|
strings.addAll(furtherParams);
|
|
|
|
}
|
2019-07-21 01:10:30 +00:00
|
|
|
} else {
|
2020-04-04 06:27:34 +00:00
|
|
|
String builder = getFormat(message.getStyle().getFormats()) +
|
2020-04-09 19:24:32 +00:00
|
|
|
getColorOrParent(message.getStyle());
|
2020-04-09 00:30:27 +00:00
|
|
|
builder += getTranslatedBedrockMessage(message, locale, false);
|
2020-04-04 06:27:34 +00:00
|
|
|
strings.add(builder);
|
2019-07-21 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings;
|
|
|
|
}
|
|
|
|
|
2020-04-05 11:19:20 +00:00
|
|
|
public static List<String> getTranslationParams(Message[] messages) {
|
|
|
|
return getTranslationParams(messages, null);
|
|
|
|
}
|
|
|
|
|
2019-07-21 01:10:30 +00:00
|
|
|
public static String getTranslationText(TranslationMessage message) {
|
2020-04-09 19:24:32 +00:00
|
|
|
return getFormat(message.getStyle().getFormats()) + getColorOrParent(message.getStyle())
|
2020-04-04 06:27:34 +00:00
|
|
|
+ "%" + message.getTranslationKey();
|
2019-07-21 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 00:30:27 +00:00
|
|
|
public static String getTranslatedBedrockMessage(Message message, String locale, boolean shouldTranslate) {
|
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-09 00:30:27 +00:00
|
|
|
if (locale != null && shouldTranslate) {
|
2020-04-08 20:11:56 +00:00
|
|
|
messageText = LocaleUtils.getLocaleString(messageText, locale);
|
2020-04-04 16:25:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 21:29:22 +00:00
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
builder.append(getFormat(message.getStyle().getFormats()));
|
|
|
|
builder.append(getColorOrParent(message.getStyle()));
|
|
|
|
builder.append(messageText);
|
|
|
|
|
2019-07-21 01:10:30 +00:00
|
|
|
for (Message msg : message.getExtra()) {
|
|
|
|
builder.append(getFormat(msg.getStyle().getFormats()));
|
2020-04-09 19:24:32 +00:00
|
|
|
builder.append(getColorOrParent(msg.getStyle()));
|
2019-07-21 01:10:30 +00:00
|
|
|
if (!(msg.getText() == null)) {
|
2020-04-09 00:30:27 +00:00
|
|
|
boolean isTranslationMessage = (msg instanceof TranslationMessage);
|
|
|
|
builder.append(getTranslatedBedrockMessage(msg, locale, isTranslationMessage));
|
2019-07-21 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
2020-04-04 16:25:38 +00:00
|
|
|
|
2020-04-11 18:43:29 +00:00
|
|
|
public static String getTranslatedBedrockMessage(Message message, String locale) {
|
|
|
|
return getTranslatedBedrockMessage(message, locale, true);
|
|
|
|
}
|
|
|
|
|
2020-04-04 16:25:38 +00:00
|
|
|
public static String getBedrockMessage(Message message) {
|
2020-04-09 00:30:27 +00:00
|
|
|
return getTranslatedBedrockMessage(message, null, false);
|
2020-04-04 16:25:38 +00:00
|
|
|
}
|
2020-04-05 11:19:20 +00:00
|
|
|
|
|
|
|
public static String insertParams(String message, List<String> params) {
|
|
|
|
String newMessage = message;
|
|
|
|
|
2020-04-05 22:37:41 +00:00
|
|
|
Pattern p = Pattern.compile("%([1-9])\\$s");
|
|
|
|
Matcher m = p.matcher(message);
|
|
|
|
while (m.find()) {
|
|
|
|
try {
|
|
|
|
newMessage = newMessage.replaceFirst("%" + m.group(1) + "\\$s" , params.get(Integer.parseInt(m.group(1)) - 1));
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Couldnt find the param to replace
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 11:19:20 +00:00
|
|
|
for (String text : params) {
|
|
|
|
newMessage = newMessage.replaceFirst("%s", text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newMessage;
|
|
|
|
}
|
2019-07-21 01:10:30 +00:00
|
|
|
|
2020-04-09 19:24:32 +00:00
|
|
|
private static String getColorOrParent(MessageStyle style) {
|
|
|
|
ChatColor chatColor = style.getColor();
|
|
|
|
|
2020-04-13 21:29:22 +00:00
|
|
|
if (chatColor == ChatColor.NONE && style.getParent() != null) {
|
|
|
|
return getColorOrParent(style.getParent());
|
2020-04-09 19:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return getColor(chatColor);
|
|
|
|
}
|
|
|
|
|
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:
|
2020-04-09 19:24:32 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-04-04 06:27:34 +00:00
|
|
|
|
|
|
|
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 "";
|
|
|
|
}
|
2020-04-13 16:49:29 +00:00
|
|
|
|
|
|
|
public static boolean isTooLong(String message, GeyserSession session) {
|
|
|
|
if (message.length() > 256) {
|
|
|
|
// TODO: Add Geyser localization and translate this based on language
|
|
|
|
session.sendMessage("Your message is bigger than 256 characters (" + message.length() + ") so it has not been sent.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-21 01:10:30 +00:00
|
|
|
}
|