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-06-19 10:57:34 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.message.Message;
|
|
|
|
import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
|
2020-06-19 18:29:01 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.message.TextMessage;
|
2020-06-19 10:57:34 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.message.TranslationMessage;
|
|
|
|
import com.github.steveice10.mc.protocol.data.message.style.ChatColor;
|
|
|
|
import com.github.steveice10.mc.protocol.data.message.style.ChatFormat;
|
2020-06-29 12:50:16 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
|
2020-06-28 13:57:41 +00:00
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonParser;
|
2020-07-06 22:36:04 +00:00
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
|
|
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
2020-04-13 16:49:29 +00:00
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2019-07-21 01:10:30 +00:00
|
|
|
|
2020-06-28 13:57:41 +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-06-26 22:33:38 +00:00
|
|
|
private static final Map<String, Integer> COLORS = new HashMap<>();
|
2020-06-28 13:57:41 +00:00
|
|
|
private static final Map<TeamColor, String> TEAM_COLORS = new HashMap<>();
|
2020-06-26 22:33:38 +00:00
|
|
|
|
|
|
|
static {
|
|
|
|
COLORS.put(ChatColor.BLACK, 0x000000);
|
|
|
|
COLORS.put(ChatColor.DARK_BLUE, 0x0000aa);
|
|
|
|
COLORS.put(ChatColor.DARK_GREEN, 0x00aa00);
|
|
|
|
COLORS.put(ChatColor.DARK_AQUA, 0x00aaaa);
|
|
|
|
COLORS.put(ChatColor.DARK_RED, 0xaa0000);
|
|
|
|
COLORS.put(ChatColor.DARK_PURPLE, 0xaa00aa);
|
|
|
|
COLORS.put(ChatColor.GOLD, 0xffaa00);
|
|
|
|
COLORS.put(ChatColor.GRAY, 0xaaaaaa);
|
|
|
|
COLORS.put(ChatColor.DARK_GRAY, 0x555555);
|
|
|
|
COLORS.put(ChatColor.BLUE, 0x5555ff);
|
|
|
|
COLORS.put(ChatColor.GREEN, 0x55ff55);
|
|
|
|
COLORS.put(ChatColor.AQUA, 0x55ffff);
|
|
|
|
COLORS.put(ChatColor.RED, 0xff5555);
|
|
|
|
COLORS.put(ChatColor.LIGHT_PURPLE, 0xff55ff);
|
|
|
|
COLORS.put(ChatColor.YELLOW, 0xffff55);
|
|
|
|
COLORS.put(ChatColor.WHITE, 0xffffff);
|
|
|
|
|
2020-06-28 13:57:41 +00:00
|
|
|
TEAM_COLORS.put(TeamColor.BLACK, getColor(ChatColor.BLACK));
|
|
|
|
TEAM_COLORS.put(TeamColor.DARK_BLUE, getColor(ChatColor.DARK_BLUE));
|
|
|
|
TEAM_COLORS.put(TeamColor.DARK_GREEN, getColor(ChatColor.DARK_GREEN));
|
|
|
|
TEAM_COLORS.put(TeamColor.DARK_AQUA, getColor(ChatColor.DARK_AQUA));
|
|
|
|
TEAM_COLORS.put(TeamColor.DARK_RED, getColor(ChatColor.DARK_RED));
|
|
|
|
TEAM_COLORS.put(TeamColor.DARK_PURPLE, getColor(ChatColor.DARK_PURPLE));
|
|
|
|
TEAM_COLORS.put(TeamColor.GOLD, getColor(ChatColor.GOLD));
|
|
|
|
TEAM_COLORS.put(TeamColor.GRAY, getColor(ChatColor.GRAY));
|
|
|
|
TEAM_COLORS.put(TeamColor.DARK_GRAY, getColor(ChatColor.DARK_GRAY));
|
|
|
|
TEAM_COLORS.put(TeamColor.BLUE, getColor(ChatColor.BLUE));
|
|
|
|
TEAM_COLORS.put(TeamColor.GREEN, getColor(ChatColor.GREEN));
|
|
|
|
TEAM_COLORS.put(TeamColor.AQUA, getColor(ChatColor.AQUA));
|
|
|
|
TEAM_COLORS.put(TeamColor.RED, getColor(ChatColor.RED));
|
|
|
|
TEAM_COLORS.put(TeamColor.LIGHT_PURPLE, getColor(ChatColor.LIGHT_PURPLE));
|
|
|
|
TEAM_COLORS.put(TeamColor.YELLOW, getColor(ChatColor.YELLOW));
|
|
|
|
TEAM_COLORS.put(TeamColor.WHITE, getColor(ChatColor.WHITE));
|
|
|
|
TEAM_COLORS.put(TeamColor.OBFUSCATED, getFormat(Collections.singletonList(ChatFormat.OBFUSCATED)));
|
|
|
|
TEAM_COLORS.put(TeamColor.BOLD, getFormat(Collections.singletonList(ChatFormat.BOLD)));
|
|
|
|
TEAM_COLORS.put(TeamColor.STRIKETHROUGH, getFormat(Collections.singletonList(ChatFormat.STRIKETHROUGH)));
|
|
|
|
TEAM_COLORS.put(TeamColor.ITALIC, getFormat(Collections.singletonList(ChatFormat.ITALIC)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively parse each message from a list for usage in a {@link TranslationMessage}
|
|
|
|
*
|
|
|
|
* @param messages A {@link List} of {@link Message} to parse
|
|
|
|
* @param locale A locale loaded to get the message for
|
|
|
|
* @param parent A {@link Message} to use as the parent (can be null)
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static List<String> getTranslationParams(List<Message> messages, String locale, Message parent) {
|
2020-04-04 06:27:34 +00:00
|
|
|
List<String> strings = new ArrayList<>();
|
|
|
|
for (Message message : messages) {
|
2020-06-28 13:57:41 +00:00
|
|
|
message = fixMessageStyle(message, parent);
|
|
|
|
|
2020-04-04 06:27:34 +00:00
|
|
|
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) {
|
2020-06-19 10:57:34 +00:00
|
|
|
String builder = "%" + translation.getKey();
|
2020-04-05 11:19:20 +00:00
|
|
|
strings.add(builder);
|
|
|
|
}
|
2019-07-21 01:10:30 +00:00
|
|
|
|
2020-06-19 10:57:34 +00:00
|
|
|
if (translation.getKey().equals("commands.gamemode.success.other")) {
|
2019-07-21 01:10:30 +00:00
|
|
|
strings.add("");
|
|
|
|
}
|
|
|
|
|
2020-06-19 10:57:34 +00:00
|
|
|
if (translation.getKey().equals("command.context.here")) {
|
2019-07-21 19:48:36 +00:00
|
|
|
strings.add(" - no permission or invalid command!");
|
|
|
|
}
|
|
|
|
|
2020-06-28 13:57:41 +00:00
|
|
|
// Collect all params and add format corrections to the end of them
|
|
|
|
List<String> furtherParams = new ArrayList<>();
|
|
|
|
for (String param : getTranslationParams(translation.getWith(), locale, message)) {
|
|
|
|
String newParam = param;
|
|
|
|
if (parent.getStyle().getFormats().size() != 0) {
|
|
|
|
newParam += getFormat(parent.getStyle().getFormats());
|
|
|
|
}
|
|
|
|
if (parent.getStyle().getColor() != ChatColor.NONE) {
|
|
|
|
newParam += getColor(parent.getStyle().getColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
furtherParams.add(newParam);
|
|
|
|
}
|
|
|
|
|
2020-04-05 11:19:20 +00:00
|
|
|
if (locale != null) {
|
2020-06-19 10:57:34 +00:00
|
|
|
strings.add(insertParams(LocaleUtils.getLocaleString(translation.getKey(), locale), furtherParams));
|
2020-04-23 06:26:54 +00:00
|
|
|
} else {
|
2020-04-05 11:19:20 +00:00
|
|
|
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-06-28 13:57:41 +00:00
|
|
|
getColor(message.getStyle().getColor());
|
|
|
|
builder += getTranslatedBedrockMessage(message, locale, false, parent);
|
2020-04-04 06:27:34 +00:00
|
|
|
strings.add(builder);
|
2019-07-21 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings;
|
|
|
|
}
|
|
|
|
|
2020-06-28 13:57:41 +00:00
|
|
|
public static String getTranslatedBedrockMessage(Message message, String locale) {
|
|
|
|
return getTranslatedBedrockMessage(message, locale, true);
|
2020-04-05 11:19:20 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 13:57:41 +00:00
|
|
|
public static String getTranslatedBedrockMessage(Message message, String locale, boolean shouldTranslate) {
|
|
|
|
return getTranslatedBedrockMessage(message, locale, shouldTranslate, null);
|
2019-07-21 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 13:57:41 +00:00
|
|
|
/**
|
|
|
|
* Translate a given {@link TranslationMessage} to the given locale
|
|
|
|
*
|
|
|
|
* @param message The {@link Message} to send
|
|
|
|
* @param locale
|
|
|
|
* @param shouldTranslate
|
|
|
|
* @param parent
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String getTranslatedBedrockMessage(Message message, String locale, boolean shouldTranslate, Message parent) {
|
2019-07-21 01:10:30 +00:00
|
|
|
JsonParser parser = new JsonParser();
|
2020-06-19 10:57:34 +00:00
|
|
|
if (isMessage(message.toString())) {
|
|
|
|
JsonObject object = parser.parse(message.toString()).getAsJsonObject();
|
2020-06-28 13:57:41 +00:00
|
|
|
message = MessageSerializer.fromJson(object);
|
2019-07-21 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 13:57:41 +00:00
|
|
|
message = fixMessageStyle(message, parent);
|
|
|
|
|
2020-06-19 18:29:01 +00:00
|
|
|
String messageText = (message instanceof TranslationMessage) ? ((TranslationMessage) message).getKey() : ((TextMessage) 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()));
|
2020-06-28 13:57:41 +00:00
|
|
|
builder.append(getColor(message.getStyle().getColor()));
|
2020-04-13 21:29:22 +00:00
|
|
|
builder.append(messageText);
|
|
|
|
|
2019-07-21 01:10:30 +00:00
|
|
|
for (Message msg : message.getExtra()) {
|
|
|
|
builder.append(getFormat(msg.getStyle().getFormats()));
|
2020-06-28 13:57:41 +00:00
|
|
|
builder.append(getColor(msg.getStyle().getColor()));
|
2020-06-19 10:57:34 +00:00
|
|
|
if (!(msg.toString() == null)) {
|
2020-04-09 00:30:27 +00:00
|
|
|
boolean isTranslationMessage = (msg instanceof TranslationMessage);
|
2020-05-05 02:32:57 +00:00
|
|
|
String extraText = "";
|
|
|
|
|
|
|
|
if (isTranslationMessage) {
|
2020-06-28 13:57:41 +00:00
|
|
|
List<String> paramsTranslated = getTranslationParams(((TranslationMessage) msg).getWith(), locale, message);
|
|
|
|
extraText = insertParams(getTranslatedBedrockMessage(msg, locale, isTranslationMessage, message), paramsTranslated);
|
2020-05-05 02:32:57 +00:00
|
|
|
} else {
|
2020-06-28 13:57:41 +00:00
|
|
|
extraText = getTranslatedBedrockMessage(msg, locale, isTranslationMessage, message);
|
2020-05-05 02:32:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
builder.append(extraText);
|
|
|
|
builder.append("\u00a7r");
|
2019-07-21 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-05 02:32:57 +00:00
|
|
|
|
2019-07-21 01:10:30 +00:00
|
|
|
return builder.toString();
|
|
|
|
}
|
2020-04-04 16:25:38 +00:00
|
|
|
|
2020-06-28 13:57:41 +00:00
|
|
|
/**
|
|
|
|
* If the passed {@link Message} color or format are empty then copy from parent
|
|
|
|
*
|
|
|
|
* @param message {@link Message} to update
|
|
|
|
* @param parent Parent {@link Message} for style
|
|
|
|
* @return The updated {@link Message}
|
|
|
|
*/
|
|
|
|
private static Message fixMessageStyle(Message message, Message parent) {
|
|
|
|
if (parent == null) {
|
|
|
|
return message;
|
|
|
|
}
|
2020-06-29 12:50:16 +00:00
|
|
|
MessageStyle.Builder styleBuilder = message.getStyle().toBuilder();
|
2020-06-28 13:57:41 +00:00
|
|
|
|
|
|
|
// Copy color from parent
|
2020-06-29 12:50:16 +00:00
|
|
|
if (message.getStyle().getColor() == ChatColor.NONE) {
|
|
|
|
styleBuilder.color(parent.getStyle().getColor());
|
2020-06-28 13:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy formatting from parent
|
2020-06-29 12:50:16 +00:00
|
|
|
if (message.getStyle().getFormats().size() == 0) {
|
|
|
|
styleBuilder.formats(parent.getStyle().getFormats());
|
2020-06-28 13:57:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 12:50:16 +00:00
|
|
|
return message.toBuilder().style(styleBuilder.build()).build();
|
2020-04-11 18:43:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 16:25:38 +00:00
|
|
|
public static String getBedrockMessage(Message message) {
|
2020-06-19 18:29:01 +00:00
|
|
|
if (isMessage(((TextMessage) message).getText())) {
|
|
|
|
return getBedrockMessage(((TextMessage) message).getText());
|
2020-04-23 06:26:54 +00:00
|
|
|
} else {
|
2020-06-19 10:57:34 +00:00
|
|
|
return getBedrockMessage(MessageSerializer.toJsonString(message));
|
2020-04-23 06:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 01:19:37 +00:00
|
|
|
/**
|
|
|
|
* Verifies the message is valid JSON in case it's plaintext. Works around GsonComponentSeraializer not using lenient mode.
|
|
|
|
* See https://wiki.vg/Chat for messages sent in lenient mode, and for a description on leniency.
|
|
|
|
*
|
|
|
|
* @param message Potentially lenient JSON message
|
|
|
|
* @return Bedrock formatted message
|
|
|
|
*/
|
|
|
|
public static String getBedrockMessageLenient(String message) {
|
|
|
|
if (isMessage(message)) {
|
|
|
|
return getBedrockMessage(message);
|
|
|
|
} else {
|
|
|
|
final JsonObject obj = new JsonObject();
|
|
|
|
obj.addProperty("text", message);
|
|
|
|
return getBedrockMessage(obj.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 06:26:54 +00:00
|
|
|
public static String getBedrockMessage(String message) {
|
2020-04-29 15:58:29 +00:00
|
|
|
Component component = phraseJavaMessage(message);
|
2020-04-23 06:26:54 +00:00
|
|
|
return LegacyComponentSerializer.legacy().serialize(component);
|
|
|
|
}
|
|
|
|
|
2020-04-29 15:58:29 +00:00
|
|
|
public static Component phraseJavaMessage(String message) {
|
2020-07-06 22:36:04 +00:00
|
|
|
return GsonComponentSerializer.gson().deserialize(message);
|
2020-04-29 15:58:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 06:26:54 +00:00
|
|
|
public static String getJavaMessage(String message) {
|
|
|
|
Component component = LegacyComponentSerializer.legacy().deserialize(message);
|
2020-07-06 22:36:04 +00:00
|
|
|
return GsonComponentSerializer.gson().serialize(component);
|
2020-04-04 16:25:38 +00:00
|
|
|
}
|
2020-04-05 11:19:20 +00:00
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
/**
|
|
|
|
* Inserts the given parameters into the given message both in sequence and as requested
|
|
|
|
*
|
|
|
|
* @param message Message containing possible parameter replacement strings
|
2020-04-23 06:26:54 +00:00
|
|
|
* @param params A list of parameter strings
|
2020-04-21 05:28:44 +00:00
|
|
|
* @return Parsed message with all params inserted as needed
|
|
|
|
*/
|
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 {
|
2020-04-23 06:26:54 +00:00
|
|
|
newMessage = newMessage.replaceFirst("%" + m.group(1) + "\\$s", params.get(Integer.parseInt(m.group(1)) - 1));
|
2020-04-05 22:37:41 +00:00
|
|
|
} catch (Exception e) {
|
2020-04-21 05:28:44 +00:00
|
|
|
// Couldn't find the param to replace
|
2020-04-05 22:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 11:19:20 +00:00
|
|
|
for (String text : params) {
|
2020-05-05 02:32:57 +00:00
|
|
|
newMessage = newMessage.replaceFirst("%s", text.replaceAll("%s", "%r"));
|
2020-04-05 11:19:20 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 02:32:57 +00:00
|
|
|
newMessage = newMessage.replaceAll("%r", "MISSING!");
|
|
|
|
|
2020-04-05 11:19:20 +00:00
|
|
|
return newMessage;
|
|
|
|
}
|
2019-07-21 01:10:30 +00:00
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
/**
|
|
|
|
* Convert a ChatColor into a string for inserting into messages
|
|
|
|
*
|
|
|
|
* @param color ChatColor to convert
|
|
|
|
* @return The converted color string
|
|
|
|
*/
|
2020-06-21 02:24:45 +00:00
|
|
|
private static String getColor(String color) {
|
2019-07-21 01:10:30 +00:00
|
|
|
String base = "\u00a7";
|
|
|
|
switch (color) {
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.BLACK:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "0";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.DARK_BLUE:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "1";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.DARK_GREEN:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "2";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.DARK_AQUA:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "3";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.DARK_RED:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "4";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.DARK_PURPLE:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "5";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.GOLD:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "6";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.GRAY:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "7";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.DARK_GRAY:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "8";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.BLUE:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "9";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.GREEN:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "a";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.AQUA:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "b";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.RED:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "c";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.LIGHT_PURPLE:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "d";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.YELLOW:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "e";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.WHITE:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "f";
|
|
|
|
break;
|
2020-06-21 02:24:45 +00:00
|
|
|
case ChatColor.RESET:
|
2020-06-19 18:29:01 +00:00
|
|
|
//case NONE:
|
2019-07-21 01:10:30 +00:00
|
|
|
base += "r";
|
|
|
|
break;
|
2020-06-26 22:33:38 +00:00
|
|
|
case "": // To stop recursion
|
2020-03-06 23:29:11 +00:00
|
|
|
return "";
|
2020-06-26 22:33:38 +00:00
|
|
|
default:
|
|
|
|
return getClosestColor(color);
|
2019-07-21 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2020-06-26 22:33:38 +00:00
|
|
|
/**
|
|
|
|
* Based on https://github.com/ViaVersion/ViaBackwards/blob/master/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/chat/TranslatableRewriter1_16.java
|
|
|
|
*
|
|
|
|
* @param color A color string
|
|
|
|
* @return The closest color to that string
|
|
|
|
*/
|
|
|
|
private static String getClosestColor(String color) {
|
|
|
|
if (!color.startsWith("#")) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
int rgb = Integer.parseInt(color.substring(1), 16);
|
|
|
|
int r = (rgb >> 16) & 0xFF;
|
|
|
|
int g = (rgb >> 8) & 0xFF;
|
|
|
|
int b = rgb & 0xFF;
|
|
|
|
|
|
|
|
String closest = null;
|
|
|
|
int smallestDiff = 0;
|
|
|
|
|
|
|
|
for (Map.Entry<String, Integer> testColor : COLORS.entrySet()) {
|
|
|
|
if (testColor.getValue() == rgb) {
|
|
|
|
return testColor.getKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
int testR = (testColor.getValue() >> 16) & 0xFF;
|
|
|
|
int testG = (testColor.getValue() >> 8) & 0xFF;
|
|
|
|
int testB = testColor.getValue() & 0xFF;
|
|
|
|
|
|
|
|
// Check by the greatest diff of the 3 values
|
|
|
|
int rDiff = Math.abs(testR - r);
|
|
|
|
int gDiff = Math.abs(testG - g);
|
|
|
|
int bDiff = Math.abs(testB - b);
|
|
|
|
int maxDiff = Math.max(Math.max(rDiff, gDiff), bDiff);
|
|
|
|
if (closest == null || maxDiff < smallestDiff) {
|
|
|
|
closest = testColor.getKey();
|
|
|
|
smallestDiff = maxDiff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return getColor(closest);
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
/**
|
|
|
|
* Convert a list of ChatFormats into a string for inserting into messages
|
|
|
|
*
|
|
|
|
* @param formats ChatFormats to convert
|
|
|
|
* @return The converted chat formatting string
|
|
|
|
*/
|
2019-07-21 01:10:30 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
/**
|
|
|
|
* Checks if the given text string is a json message
|
|
|
|
*
|
|
|
|
* @param text String to test
|
|
|
|
* @return True if its a valid message json string, false if not
|
|
|
|
*/
|
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 {
|
2020-06-28 13:57:41 +00:00
|
|
|
MessageSerializer.fromJson(object);
|
2019-07-21 01:10:30 +00:00
|
|
|
} catch (Exception ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-04 06:27:34 +00:00
|
|
|
public static String toChatColor(TeamColor teamColor) {
|
2020-06-28 13:57:41 +00:00
|
|
|
return TEAM_COLORS.getOrDefault(teamColor, "");
|
2020-04-04 06:27:34 +00:00
|
|
|
}
|
2020-04-13 16:49:29 +00:00
|
|
|
|
2020-04-21 05:28:44 +00:00
|
|
|
/**
|
|
|
|
* Checks if the given message is over 256 characters (Java edition server chat limit) and sends a message to the user if it is
|
|
|
|
*
|
|
|
|
* @param message Message to check
|
|
|
|
* @param session GeyserSession for the user
|
|
|
|
* @return True if the message is too long, false if not
|
|
|
|
*/
|
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.");
|
2020-04-16 03:50:02 +00:00
|
|
|
return true;
|
2020-04-13 16:49:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 03:50:02 +00:00
|
|
|
return false;
|
2020-04-13 16:49:29 +00:00
|
|
|
}
|
2019-07-21 01:10:30 +00:00
|
|
|
}
|