Class MessageTranslator
java.lang.Object
org.geysermc.geyser.translator.text.MessageTranslator
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic net.kyori.adventure.text.ComponentcomponentFromNbtTag(Object nbtTag) static StringconvertFromNullableNbtTag(Optional<GeyserSession> session, @Nullable Object nbtTag) Should only be used byJavaRegistryReaders, as these do not always have aGeyserSessionavailable.static StringconvertIncomingToPlainText(String message) Convert legacy format message to plain textstatic StringconvertLegacyMessage(String message, String locale) Converts a legacy Java string (using Java §-prefixed formatting codes) to a bedrock message.static StringconvertLegacyMessage(GeyserSession session, String message) Shorthand forconvertLegacyMessage(String, String).static StringconvertLenientNbtMessage(GeyserSession session, @Nullable Object tag) Shorthand forconvertLenientNbtMessage(GeyserSession, Object, String), uses"Report to Geyser"as fallback.static StringconvertLenientNbtMessage(GeyserSession session, @Nullable Object tag, String fallback) UsesconvertNbtMessage(GeyserSession, Object)to convert a Java text component, serialised to NBT, to a bedrock string.static StringconvertMessage(net.kyori.adventure.text.Component message) DO NOT USE THIS METHOD unless where you're calling from does not have a (reliable) way of getting the context's locale.static StringconvertMessage(net.kyori.adventure.text.Component message, String locale) Convert a Java message to the legacy format ready for bedrock.static StringconvertMessage(GeyserSession session, net.kyori.adventure.text.Component message) Convenience method for locale getting.static StringconvertMessageForTooltip(net.kyori.adventure.text.Component message, String locale) Convert a Java message to the legacy format ready for bedrock, for use in item tooltips (a gray color is applied).static StringconvertMessageRaw(net.kyori.adventure.text.Component message, String locale) Convert a Java message to the legacy format ready for bedrock.static StringconvertNbtMessage(Object tag, String locale) Parses the given NBT tag as aComponentusingcomponentFromNbtTag(Object), then converts it to a bedrock message usingconvertMessage(Component, String).static StringconvertNbtMessage(GeyserSession session, Object tag) Shorthand method forconvertNbtMessage(Object, String).static StringconvertToPlainText(net.kyori.adventure.text.Component message, String locale) Convert a Java message to plain textstatic StringconvertToPlainTextLenient(String message, String locale) Convert JSON and legacy format message to plain textstatic net.kyori.adventure.text.format.StylegetStyleFromNbtMap(org.cloudburstmc.nbt.NbtMap map) static voidhandleChatPacket(GeyserSession session, net.kyori.adventure.text.Component message, org.geysermc.mcprotocollib.protocol.data.game.Holder<org.geysermc.mcprotocollib.protocol.data.game.chat.ChatType> chatTypeHolder, net.kyori.adventure.text.Component targetName, net.kyori.adventure.text.Component sender, @Nullable UUID senderUuid) static voidinit()static booleanisTooLong(String message, GeyserSession session) Checks if the given message is over 256 characters (Java edition server chat limit) and sends a message to the user if it isstatic StringnormalizeSpace(String string) Normalizes whitespaces - a thing a vanilla client apparently does with commands and chat messages.signTextFromNbtTag(GeyserSession session, List<?> nbtTag)
-
Constructor Details
-
MessageTranslator
public MessageTranslator()
-
-
Method Details
-
convertMessageRaw
Convert a Java message to the legacy format ready for bedrock. UnlikeconvertMessage(Component, String)this version does not add a leading color reset. In Bedrock some places have build-in colors.- Parameters:
message- Java messagelocale- Locale to use for translation strings- Returns:
- Parsed and formatted message for bedrock
-
convertMessage
Convert a Java message to the legacy format ready for bedrock. UnlikeconvertMessageRaw(Component, String)this adds a leading color reset. In Bedrock some places have build-in colors.- Parameters:
message- Java messagelocale- Locale to use for translation strings- Returns:
- Parsed and formatted message for bedrock
-
convertMessage
public static String convertMessage(GeyserSession session, net.kyori.adventure.text.Component message) Convenience method for locale getting. -
convertMessage
DO NOT USE THIS METHOD unless where you're calling from does not have a (reliable) way of getting the context's locale. -
convertLegacyMessage
Converts a legacy Java string (using Java §-prefixed formatting codes) to a bedrock message.This method parses the string into a component using
LegacyComponentSerializer, then converts that component usingconvertMessage(Component, String).- Parameters:
message- the legacy Java messagelocale- the locale- Returns:
- the converted message to be sent to bedrock
-
convertLegacyMessage
Shorthand forconvertLegacyMessage(String, String). -
convertLenientNbtMessage
public static String convertLenientNbtMessage(GeyserSession session, @Nullable Object tag, String fallback) UsesconvertNbtMessage(GeyserSession, Object)to convert a Java text component, serialised to NBT, to a bedrock string.This method has a few special cases:
- For
nullmessages, an empty string is returned. - For
Stringtags, of whichString.isBlank()returns true, the original, not-converted string is returned. - For empty
NbtLists, an empty string is returned. - When
convertNbtMessage(GeyserSession, Object)throws aRuntimeException,fallbackis returned.
Generally, prefer this method over
convertNbtMessage(GeyserSession, Object), as it is safer and handles edge-cases.- Parameters:
session- theGeyserSession(used for determining the locale to use)tag- the NBT tag to convertfallback- the string to return when an error occurs during conversion- Returns:
- the converted message to be sent to bedrock
- For
-
convertLenientNbtMessage
Shorthand forconvertLenientNbtMessage(GeyserSession, Object, String), uses"Report to Geyser"as fallback. -
convertNbtMessage
Shorthand method forconvertNbtMessage(Object, String).Generally, prefer
convertLenientNbtMessage(GeyserSession, Object, String)over this method, as it is safer and handles edge-cases. -
convertNbtMessage
Parses the given NBT tag as aComponentusingcomponentFromNbtTag(Object), then converts it to a bedrock message usingconvertMessage(Component, String).- Parameters:
tag- the NBT tag to convertlocale- the locale to use- Returns:
- the converted message to be sent to bedrock
-
convertFromNullableNbtTag
public static String convertFromNullableNbtTag(Optional<GeyserSession> session, @Nullable Object nbtTag) Should only be used byJavaRegistryReaders, as these do not always have aGeyserSessionavailable. -
convertMessageForTooltip
public static String convertMessageForTooltip(net.kyori.adventure.text.Component message, String locale) Convert a Java message to the legacy format ready for bedrock, for use in item tooltips (a gray color is applied).- Parameters:
message- Java messagelocale- Locale to use for translation strings- Returns:
- Parsed and formatted message for bedrock, in gray color
-
convertToPlainText
Convert a Java message to plain text- Parameters:
message- Message to convertlocale- Locale to use for translation strings- Returns:
- The plain text of the message
-
convertIncomingToPlainText
Convert legacy format message to plain text- Parameters:
message- Message to convert- Returns:
- The plain text of the message
-
convertToPlainTextLenient
Convert JSON and legacy format message to plain text- Parameters:
message- Message to convertlocale- Locale to use for translation strings- Returns:
- The plain text of the message
-
componentFromNbtTag
-
signTextFromNbtTag
-
getStyleFromNbtMap
public static net.kyori.adventure.text.format.Style getStyleFromNbtMap(org.cloudburstmc.nbt.NbtMap map) -
handleChatPacket
public static void handleChatPacket(GeyserSession session, net.kyori.adventure.text.Component message, org.geysermc.mcprotocollib.protocol.data.game.Holder<org.geysermc.mcprotocollib.protocol.data.game.chat.ChatType> chatTypeHolder, net.kyori.adventure.text.Component targetName, net.kyori.adventure.text.Component sender, @Nullable UUID senderUuid) -
isTooLong
Checks if the given message is over 256 characters (Java edition server chat limit) and sends a message to the user if it is- Parameters:
message- Message to checksession-GeyserSessionfor the user- Returns:
- True if the message is too long, false if not
-
normalizeSpace
Normalizes whitespaces - a thing a vanilla client apparently does with commands and chat messages. -
init
public static void init()
-