2020-11-16 23:57:57 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2020-11-16 23:57:57 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.translator.text;
|
2020-11-16 23:57:57 +00:00
|
|
|
|
2020-12-15 16:24:02 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer;
|
2020-11-16 23:57:57 +00:00
|
|
|
import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamColor;
|
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.renderer.TranslatableComponentRenderer;
|
|
|
|
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
|
|
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
2021-11-15 01:15:25 +00:00
|
|
|
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
2021-11-20 21:34:30 +00:00
|
|
|
import org.geysermc.geyser.GeyserImpl;
|
2021-11-21 18:36:42 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSessionImpl;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.text.MinecraftTranslationRegistry;
|
|
|
|
import org.geysermc.geyser.text.GsonComponentSerializerWrapper;
|
|
|
|
import org.geysermc.geyser.text.GeyserLocale;
|
2020-11-16 23:57:57 +00:00
|
|
|
|
2021-10-05 21:06:15 +00:00
|
|
|
import java.util.EnumMap;
|
2020-12-15 16:24:02 +00:00
|
|
|
import java.util.Map;
|
2021-10-05 21:06:15 +00:00
|
|
|
import java.util.regex.Pattern;
|
2020-11-16 23:57:57 +00:00
|
|
|
|
|
|
|
public class MessageTranslator {
|
|
|
|
// These are used for handling the translations of the messages
|
2021-10-05 22:55:32 +00:00
|
|
|
// Custom instead of TranslatableComponentRenderer#usingTranslationSource so we don't need to worry about finding a Locale class
|
|
|
|
private static final TranslatableComponentRenderer<String> RENDERER = new MinecraftTranslationRegistry();
|
2020-11-16 23:57:57 +00:00
|
|
|
|
2021-10-07 15:00:43 +00:00
|
|
|
// Possible TODO: replace the legacy hover event serializer with an empty one since we have no use for hover events
|
2021-10-20 22:42:02 +00:00
|
|
|
private static final GsonComponentSerializer GSON_SERIALIZER;
|
2020-11-27 23:28:08 +00:00
|
|
|
|
2020-11-16 23:57:57 +00:00
|
|
|
// Store team colors for player names
|
2021-10-05 21:06:15 +00:00
|
|
|
private static final Map<TeamColor, String> TEAM_COLORS = new EnumMap<>(TeamColor.class);
|
2020-11-27 23:28:08 +00:00
|
|
|
|
|
|
|
// Legacy formatting character
|
|
|
|
private static final String BASE = "\u00a7";
|
|
|
|
|
|
|
|
// Reset character
|
|
|
|
private static final String RESET = BASE + "r";
|
2020-11-16 23:57:57 +00:00
|
|
|
|
2021-10-05 21:06:15 +00:00
|
|
|
/* Various regexes to fix formatting for Bedrock's specifications */
|
|
|
|
private static final Pattern STRIKETHROUGH_UNDERLINE = Pattern.compile("\u00a7[mn]");
|
|
|
|
private static final Pattern COLOR_CHARACTERS = Pattern.compile("\u00a7([0-9a-f])");
|
|
|
|
private static final Pattern DOUBLE_RESET = Pattern.compile("\u00a7r\u00a7r");
|
|
|
|
|
2020-11-16 23:57:57 +00:00
|
|
|
static {
|
2021-10-05 21:06:15 +00:00
|
|
|
TEAM_COLORS.put(TeamColor.NONE, "");
|
|
|
|
|
|
|
|
TEAM_COLORS.put(TeamColor.BLACK, BASE + "0");
|
|
|
|
TEAM_COLORS.put(TeamColor.DARK_BLUE, BASE + "1");
|
|
|
|
TEAM_COLORS.put(TeamColor.DARK_GREEN, BASE + "2");
|
|
|
|
TEAM_COLORS.put(TeamColor.DARK_AQUA, BASE + "3");
|
|
|
|
TEAM_COLORS.put(TeamColor.DARK_RED, BASE + "4");
|
|
|
|
TEAM_COLORS.put(TeamColor.DARK_PURPLE, BASE + "5");
|
|
|
|
TEAM_COLORS.put(TeamColor.GOLD, BASE + "6");
|
|
|
|
TEAM_COLORS.put(TeamColor.GRAY, BASE + "7");
|
|
|
|
TEAM_COLORS.put(TeamColor.DARK_GRAY, BASE + "8");
|
|
|
|
TEAM_COLORS.put(TeamColor.BLUE, BASE + "9");
|
|
|
|
TEAM_COLORS.put(TeamColor.GREEN, BASE + "a");
|
|
|
|
TEAM_COLORS.put(TeamColor.AQUA, BASE + "b");
|
|
|
|
TEAM_COLORS.put(TeamColor.RED, BASE + "c");
|
|
|
|
TEAM_COLORS.put(TeamColor.LIGHT_PURPLE, BASE + "d");
|
|
|
|
TEAM_COLORS.put(TeamColor.YELLOW, BASE + "e");
|
|
|
|
TEAM_COLORS.put(TeamColor.WHITE, BASE + "f");
|
|
|
|
|
|
|
|
// Formats, not colors
|
|
|
|
TEAM_COLORS.put(TeamColor.OBFUSCATED, BASE + "k");
|
|
|
|
TEAM_COLORS.put(TeamColor.BOLD, BASE + "l");
|
|
|
|
TEAM_COLORS.put(TeamColor.STRIKETHROUGH, BASE + "m");
|
|
|
|
TEAM_COLORS.put(TeamColor.ITALIC, BASE + "o");
|
2021-10-20 22:42:02 +00:00
|
|
|
|
|
|
|
// Temporary fix for https://github.com/KyoriPowered/adventure/issues/447
|
|
|
|
GsonComponentSerializer source = DefaultComponentSerializer.get();
|
|
|
|
GSON_SERIALIZER = new GsonComponentSerializerWrapper(source);
|
|
|
|
// Tell MCProtocolLib to use this serializer, too.
|
|
|
|
DefaultComponentSerializer.set(GSON_SERIALIZER);
|
2020-11-16 23:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a Java message to the legacy format ready for bedrock
|
|
|
|
*
|
|
|
|
* @param message Java message
|
|
|
|
* @param locale Locale to use for translation strings
|
|
|
|
* @return Parsed and formatted message for bedrock
|
|
|
|
*/
|
2020-11-27 23:28:08 +00:00
|
|
|
public static String convertMessage(Component message, String locale) {
|
|
|
|
try {
|
2021-10-05 22:55:32 +00:00
|
|
|
// Translate any components that require it
|
|
|
|
message = RENDERER.render(message, locale);
|
2020-11-16 23:57:57 +00:00
|
|
|
|
2020-11-27 23:28:08 +00:00
|
|
|
String legacy = LegacyComponentSerializer.legacySection().serialize(message);
|
2020-11-16 23:57:57 +00:00
|
|
|
|
2020-11-27 23:28:08 +00:00
|
|
|
// Strip strikethrough and underline as they are not supported on bedrock
|
2021-10-05 21:06:15 +00:00
|
|
|
legacy = STRIKETHROUGH_UNDERLINE.matcher(legacy).replaceAll("");
|
2020-11-18 23:18:36 +00:00
|
|
|
|
2020-11-27 23:28:08 +00:00
|
|
|
// Make color codes reset formatting like Java
|
|
|
|
// See https://minecraft.gamepedia.com/Formatting_codes#Usage
|
2021-10-05 21:06:15 +00:00
|
|
|
legacy = COLOR_CHARACTERS.matcher(legacy).replaceAll("\u00a7r\u00a7$1");
|
|
|
|
legacy = DOUBLE_RESET.matcher(legacy).replaceAll("\u00a7r");
|
2020-11-18 23:18:36 +00:00
|
|
|
|
2020-11-27 23:28:08 +00:00
|
|
|
return legacy;
|
|
|
|
} catch (Exception e) {
|
2021-11-20 21:34:30 +00:00
|
|
|
GeyserImpl.getInstance().getLogger().debug(GSON_SERIALIZER.serialize(message));
|
|
|
|
GeyserImpl.getInstance().getLogger().error("Failed to parse message", e);
|
2020-11-18 23:18:36 +00:00
|
|
|
|
2020-11-27 23:28:08 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String convertMessage(String message, String locale) {
|
|
|
|
return convertMessage(GSON_SERIALIZER.deserialize(message), locale);
|
2020-11-16 23:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String convertMessage(String message) {
|
2021-11-20 23:29:46 +00:00
|
|
|
return convertMessage(message, GeyserLocale.getDefaultLocale());
|
2020-11-16 23:57:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 23:28:08 +00:00
|
|
|
public static String convertMessage(Component message) {
|
2021-11-20 23:29:46 +00:00
|
|
|
return convertMessage(message, GeyserLocale.getDefaultLocale());
|
2020-11-27 23:28:08 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 23:57:57 +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
|
|
|
|
* @param locale Locale to use for translation strings
|
|
|
|
* @return Bedrock formatted message
|
|
|
|
*/
|
|
|
|
public static String convertMessageLenient(String message, String locale) {
|
2021-09-10 18:10:56 +00:00
|
|
|
if (message.isBlank()) {
|
2020-11-27 23:28:08 +00:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-11-16 23:57:57 +00:00
|
|
|
return convertMessage(message, locale);
|
2020-11-27 23:28:08 +00:00
|
|
|
} catch (Exception ignored) {
|
2020-11-16 23:57:57 +00:00
|
|
|
String convertedMessage = convertMessage(convertToJavaMessage(message), locale);
|
|
|
|
|
|
|
|
// We have to do this since Adventure strips the starting reset character
|
2020-11-27 23:28:08 +00:00
|
|
|
if (message.startsWith(RESET) && !convertedMessage.startsWith(RESET)) {
|
|
|
|
convertedMessage = RESET + convertedMessage;
|
2020-11-16 23:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return convertedMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String convertMessageLenient(String message) {
|
2021-11-20 23:29:46 +00:00
|
|
|
return convertMessageLenient(message, GeyserLocale.getDefaultLocale());
|
2020-11-16 23:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a Bedrock message string back to a format Java can understand
|
|
|
|
*
|
|
|
|
* @param message Message to convert
|
|
|
|
* @return The formatted JSON string
|
|
|
|
*/
|
|
|
|
public static String convertToJavaMessage(String message) {
|
|
|
|
Component component = LegacyComponentSerializer.legacySection().deserialize(message);
|
2020-11-27 23:28:08 +00:00
|
|
|
return GSON_SERIALIZER.serialize(component);
|
2020-11-16 23:57:57 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 01:15:25 +00:00
|
|
|
/**
|
|
|
|
* Convert JSON and legacy format message to plain text
|
|
|
|
*
|
|
|
|
* @param message Message to convert
|
|
|
|
* @param locale Locale to use for translation strings
|
|
|
|
* @return The plain text of the message
|
|
|
|
*/
|
|
|
|
public static String convertToPlainText(String message, String locale) {
|
|
|
|
if (message == null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
Component messageComponent = null;
|
|
|
|
if (message.startsWith("{") && message.endsWith("}")) {
|
|
|
|
// Message is a JSON object
|
|
|
|
try {
|
|
|
|
messageComponent = GSON_SERIALIZER.deserialize(message);
|
|
|
|
// Translate any components that require it
|
|
|
|
messageComponent = RENDERER.render(messageComponent, locale);
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (messageComponent == null) {
|
|
|
|
messageComponent = LegacyComponentSerializer.legacySection().deserialize(message);
|
|
|
|
}
|
|
|
|
return PlainTextComponentSerializer.plainText().serialize(messageComponent);
|
|
|
|
}
|
|
|
|
|
2020-11-16 23:57:57 +00:00
|
|
|
/**
|
|
|
|
* Convert a team color to a chat color
|
|
|
|
*
|
2020-11-27 23:28:08 +00:00
|
|
|
* @param teamColor Color or format to convert
|
2020-11-16 23:57:57 +00:00
|
|
|
* @return The chat color character
|
|
|
|
*/
|
|
|
|
public static String toChatColor(TeamColor teamColor) {
|
2021-10-05 21:06:15 +00:00
|
|
|
return TEAM_COLORS.getOrDefault(teamColor, "");
|
2020-11-16 23:57:57 +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
|
2021-11-21 18:36:42 +00:00
|
|
|
* @param session {@link GeyserSessionImpl} for the user
|
2020-11-16 23:57:57 +00:00
|
|
|
* @return True if the message is too long, false if not
|
|
|
|
*/
|
2021-11-21 18:36:42 +00:00
|
|
|
public static boolean isTooLong(String message, GeyserSessionImpl session) {
|
2020-11-16 23:57:57 +00:00
|
|
|
if (message.length() > 256) {
|
2021-11-20 23:29:46 +00:00
|
|
|
session.sendMessage(GeyserLocale.getPlayerLocaleString("geyser.chat.too_long", session.getLocale(), message.length()));
|
2020-11-16 23:57:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-07 15:56:23 +00:00
|
|
|
|
|
|
|
public static void init() {
|
|
|
|
// no-op
|
|
|
|
}
|
2020-11-16 23:57:57 +00:00
|
|
|
}
|