Chat

From wiki.vg
Revision as of 02:02, 6 January 2024 by LassiPulkkinen (talk | contribs) (→‎Shared between all components: Simplify descriptions, referring to sections under Styling. Legacy formatting codes in color no longer appear to work; ditto for "reset".)
Jump to navigation Jump to search

Minecraft supports two-way chat communication via the Player Chat Message packet, which support a variety of formatting options. Note that this system isn't exclusive to the Chat Message packet - it's used in several other places (including written books, death messages, window titles, and the like).

Heads up!

Currently out of date. 1.20.3+ encodes the JSON Chat into NBT in most cases when sent over the network (indicated via the Chat and Json Chat data types). How its converted in both directions needs to be documented.

Current system (JSON Chat)

Chat data is sent in JSON. For most situations, the JSON is strictly parsed, but lenient JSON parsing is used for the Disconnect packet and for written book text.

Inheritance

Each component can have multiple siblings. Each sibling component inherits properties from the style of its parent, using those styles if they are not defined in the sibling's own style.

For example, in this component:

{
    "text": "foo",
    "bold": true,
    "extra": [
        {
            "text": "bar"
        },
        {
            "text": "baz",
            "bold": false
        },
        {
            "text": "qux",
            "bold": true
        }
    ]
}

The text "foo", "bar", and "qux" are all rendered as bold while "baz" is not.

Schema

Each situation where a component can appear may be either a JSON object (the most common choice), an array, or a JSON primitive (which is converted to a String).

JSON primitives can be used for components, in which case they are implicitly converted into a String component. Note that it is invalid to have a JSON primitive as the root object, so it is not valid to merely send the text in quotes. This shorthand is useful for sibling lists, though.

It is also legal to put a JSON array where a component would go, so long as the array is not empty. The first component in the array is the parent of all of the following components. This can produce unusual behavior with style inheritance, but it is still useful shorthand.

The most common type of component is a JSON object; the format is as follows:

Shared between all components

All component types have certain fields that represent the style. All of these fields may be skipped, in which case the parent's style will be inherited.

bold, italic, underlined, strikethrough, obfuscated
Boolean - Enables or disables the given style. See #Styles below.
font
String - Specifies the font. See #Fonts below. This option is only valid on 1.16+; otherwise the property is ignored.
color
String - Specifies the text color, which may be a color name or a #-prefixed hexadecimal RGB specification. See #Colors below.
insertion
String - Specifies text to be inserted into chat at the cursor when shift-clicked. Only used for messages in chat; has no effect in other locations at this time.
clickEvent
JSON object - Defines an event that occurs when this component is clicked. Contains the fields action and value, both strings.
action may be one of the following:
open_url
Opens the given URL in the default web browser. Ignored if the player has opted to disable links in chat; may open a GUI prompting the user if the setting for that is enabled. The link's protocol must be set and must be http or https, for security reasons.
open_file
Cannot be used within JSON chat. Opens a link to any protocol, but cannot be used in JSON chat for security reasons. Only exists to internally implement links for screenshots.
run_command
Runs the given command. Not required to be a command - clicking this only causes the client to send the given content as a chat message, so if not prefixed with /, they will say the given text instead. If used in a book GUI, the GUI is closed after clicking.
twitch_user_info
No longer supported; cannot be used within JSON chat. Only usable in 1.8 and below; twitch support was removed in 1.9. Additionally, this is only used internally by the client. On click, opens a twitch user info GUI screen. Value should be the twitch user name.
suggest_command
Only usable for messages in chat. Replaces the content of the chat box with the given text - usually a command, but it is not required to be a command (commands should be prefixed with /).
change_page
Only usable within written books. Changes the page of the book to the given page, starting at 1. For instance, "value":"1" switches the book to the first page. If the page is less than one or beyond the number of pages in the book, the event is ignored.
copy_to_clipboard
Copies the given text to the client's clipboard when clicked.
hoverEvent
JSON object - Defines an event that occurs when this component hovered over. Contains the fields action (a string) and contents (dependent on action).

Warning.png The content field was introduced in Minecraft 1.16, replacing the old value field, which is still supported by new versions for compatibility. value functioned similarly to content, but was always interpreted as a chat component, with show_item and show_entity data represented as sNBT (e.g. "value":"{id:35,Damage:5,Count:2,tag:{display:{Name:Testing}}}").

action may be one of the following:
show_text
Displays a tooltip with arbitrary text. contents can either be a string directly ("contents":"la") or a full component ("contents":{"text":"la","color":"red"}).
show_item
Displays a tooltip describing an item. contents contains the following fields:
id
The textual identifier of the item's type. If unrecognized, defaults to minecraft:air.
count
The number of items in the item stack. If omitted, defaults to 1.
tag
The item's NBT information as an sNBT string (as would be used in /give) (if present).
show_entity
Displays a tooltip describing an entity. Shown only if Advanced tooltips (F3+H) is enabled. contents contains the following fields:
type
The textual identifier of the entity's type. If unrecognized, defaults to minecraft:pig.
id
The entity's UUID (with dashes). Does not need to correspond to an existing entity; only for display.
name
The entity's custom name (if present).
show_achievement
No longer supported. Since 1.12, this no longer exists; advancements instead simply use show_text. The ID of an achievement or statistic to display. Example: "value":"achievement.openInventory".
extra
An array of sibling components. If present, cannot be empty.

Beyond that, there are additional properties. Attempt to parse as each of the following in the order they are described:

String component

A String component contains only text. If the JSON contains a text key, then the component is a String component. The only content of this component is its text, with no additional processing.

Translation component

Huh.png The following information needs to be added to this page:
Is using this with an invalid key intended or deprecated? Also, what exactly happens on invalid formats?

Translates text into the current language. If the JSON contains a translate key, then the component is a translation component.

Translation supports %s, %n$s (where n is an index) and %% format tokens. %% is just an escaped percent symbol. %s marks text to replace using content from the optional with tag. with is an array of components. %n$s acts like %s but instead will use the text component in with that is at the index of n.

It should be noted that when the key is not known by a language file, it will result in the translation key being the value of the text. For Example, soundCategory.hostile exists and will result in "Hostile Creatures" whereas soundCategory.angry (Which does not exist) will result in "soundCategory.angry".

Warning.png Prior to 1.19.1 the narrator was hardcoded to replace the translation key chat.type.text with chat.type.text.narrate. This mechanism has been superseded by chat types, and is no longer present in new versions.

Keybind component

Displays the client's current keybind for the specified key. If the component contains a keybind key, then it is a keybind component. The value is named after the keys in options.txt (for instance, for key_key.forward in options.txt, key.forward would be used in the component and W would be displayed). For keys that are not known, the value provided should be displayed instead (for instance key.invalid would remain as key.invalid).

Score component

Displays a score. If the JSON contains a score key, then the component is a score component.

The score JSON object contains data about the objective.

When being sent to the client, it must contain name, objective, and value keys. name is a player username or entity UUID (if it is a player, it is a username); objective is the name of the objective; value is the resolved value of that objective.

When being sent to the server, value is not used. name can be an entity selector (that selects one entity), or alternatively * which matches the sending player.

Selector component

Displays the results of an entity selector. If the component contains a selector key, then it is a selector component.

Warning.png This component is meant for client-to-server interactions (such as commands), and should not be sent directly to the client.

If received by the client, it won't be resolved, and will always be displayed as the selection pattern.

The server resolves the list selector into a list of entities, each with an appropriate hover event (and click event if it happens to be a player).

Entries in the list are separated by a comma by default, but can be overriden with any component if a separator is specified.

NBT component

Displays the results of a NBT query. If the component contains a nbt key, then it is a NBT component.

Warning.png This component is meant for client-to-server interactions (such as commands), and should not be sent directly to the client.

If received by the client, it won't be resolved, and will always be displayed as empty text.

The server constructs a list using the data found at the nbt path, for each entry of a given source. This source varies, and is defined by the presence of one of the three following fields:

  • block, where the data comes from the block entity at the given position;
  • entity, where the data comes from the entities resulted from the given entity selector;
  • storage, where the data comes from the internal storage defined by the given Identifier.

Entries in the list are separated by a comma by default, but can be overriden with any component if a separator is specified.

Furthermore, if the boolean interpret is set to true, the server will attempt to resolve each of the data as its own component, before appending them with the separator.

Examples

Here are some examples from the Notchian server for how chat formatting can be used.

TODO - additional examples

Standard chat message

The normal chat message uses a translation component along with some styling.

Warning.png Since 1.19.1 the Notchian client uses the chat type mechanism, meaning chat messages are no longer sent directly like shown in this example.

{"translate":"chat.type.text","with":[{"text":"Herobrine","clickEvent":{"action":"suggest_command","value":"/msg Herobrine "},"hoverEvent":{"action":"show_entity","value":"{id:f84c6a79-0a4e-45e0-879b-cd49ebd4c4e2,name:Herobrine}"},"insertion":"Herobrine"},{"text":"I don't exist"}]}

The chat.type.text translation key becomes <%s> %s, which is then filled in with the player's name and the actual message. Note that the player's name has a click event (which inserts text to message the player), a hover event (which shows the entity ID; also note that type is missing), and an insertion (for the player's name).

Old system

Control Sequences

The client treats certain two-character sequences specially. The first character must be:

  • § (U+00A7) for Minecraft
  • & for Minecraft Classic

The following character indicates a certain color or formatting to apply to the proceeding text.

e.g.: This is white, but §4this is dark red. §lThis is still red, but also bold, while §rthis is neither.

In Minecraft Classic, the Notchian client expects that an escape code in a chat message will be followed by at least one character, and will otherwise crash with a StringIndexOutOfBoundsException. The server must sanitize outbound messages by removing any control characters at the end.

Styling

Colors

These correspond very roughly to the colors available in ANSI terminals.

Hex digit to color mapping
  1. REDIRECT Text formatting/ColorSample
Sample Common Name Legacy Code JSON Name Foreground Color Background Color
R G B Hex R G B Hex
Black 0 black 0 0 0 #000000 0 0 0 #000000
Dark blue 1 dark_blue 0 0 170 #0000aa 0 0 42 #00002a
Dark green 2 dark_green 0 170 0 #00aa00 0 42 0 #002a00
Dark cyan 3 dark_aqua 0 170 170 #00aaaa 0 42 42 #002a2a
Dark red 4 dark_red 170 0 0 #aa0000 42 0 0 #2a0000
Purple 5 dark_purple 170 0 170 #aa00aa 42 0 42 #2a002a
Gold 6 gold 255 170 0 #ffaa00 42 42 0 #2a2a00
Gray 7 gray 170 170 170 #aaaaaa 42 42 42 #2a2a2a
Dark gray 8 dark_gray 85 85 85 #555555 21 21 21 #151515
Blue 9 blue 85 85 255 #5555ff 21 21 63 #15153f
Bright green a green 85 255 85 #55ff55 21 63 21 #153f15
Cyan b aqua 85 255 255 #55ffff 21 63 63 #153f3f
Red c red 255 85 85 #ff5555 63 21 21 #3f1515
Pink d light_purple 255 85 255 #ff55ff 63 21 63 #3f153f
Yellow e yellow 255 255 85 #ffff55 63 63 21 #3f3f15
White f white 255 255 255 #ffffff 63 63 63 #3f3f3f

Styles

The "Random" style is used for the "§kFUNKY LOL" message in /title/splashes.txt inside minecraft.jar.

Sample Style Legacy Code JSON Name
Random chat.gif Random k obfuscated
Bold chat.png Bold l bold
Strikethrough chat.png Strikethrough m strikethrough
Underlined chat.png Underlined n underline
Italic chat.png Italic o italic
Plain chat.png Plain White r reset

Fonts

Since Minecraft 1.16 it has been possible to add custom fonts via resource packs, and use them in text components through the font field.

By default, the following fonts are available:

Identifier Default Look Notes
minecraft:default Standard Minecraft font (with Unifont fallback) Default font. Hardcoded to alias minecraft:uniform when Force Unicode Font is enabled.
minecraft:uniform GNU Unifont Default font when Force Unicode Font is enabled.
minecraft:alt Standard Galactic Alphabet Enchanting table font.
minecraft:illageralt Illageralt from Minecraft Dungeons Unused.

Processing chat

Client chat mode

The client may use the Chat Mode field of the Client Information packet to indicate that it only wants to receive some types of chat messages.

It is the server's responsibility to not send packets if the client has the given type disabled. However, it is the client's responsibility to not send incorrect chat packets.

Here's a matrix comparing what packets the server should send to a clients based on their chat settings:

Clientbound packet Client setting Usage
Full Commands only Hidden
Player Chat Message Player-initiated chat messages, including the commands /say, /me, /msg, /tell, /w and /teammsg.
Disguised Chat Message Messages sent by non-players using the commands /say, /me, /msg, /tell, /w and /teammsg.
System Chat Message Feedback from running a command, such as "Your game mode has been updated to creative."
System Chat Message (overlay) Game state information that is displayed above the hot bar, such as "You may not rest now, the bed is too far away".

Here's a matrix comparing what the client may send based on its chat setting:

Serverbound packet Client setting
Full Commands only Hidden
Chat Message [note 1]
Chat Command

If the client attempts to send a chat message and the server rejects it, the Notchian server will send that client a System Chat Message (non-overlay) with a red chat.disabled.options translation component (which becomes "Chat disabled in client options.").

Social Interactions (blocking)

1.16 added a social interactions screen that lets players block chat from other players. Blocking takes place clientside by detecting whether a message is from a blocked player.

Player Chat Message packets are blocked based on the included sender UUID.

Disguised Chat Message packets are never blocked.

System Chat Message packets (regardless of the Overlay field!) are blocked based on the first occurrence of <playername> anywhere in the message, including split across multiple chat components, where playername may be any text, including the empty string or whitespace. If playername is the name of a blocked player (matched case-sensitively), the message is blocked. This only occurs if Hide Matched Names is enabled in Chat Settings (the default).

Notes

  1. This behavior varies. The Notchian server previously rejected chat messages, but now allows them to go through (sending them to all players, but they're invisible on the sender's side). CraftBukkit and derivatives continue to reject this. See MC-116824 for more information.

Cite error: <ref> tag with name "zero" defined in <references> is not used in prior text.
Cite error: <ref> tag with name "space" defined in <references> is not used in prior text.