Java Edition:Text Component Injection

Missing Texture.png
Gear.gif
This article is a work in progress. 
Please help in the creation of this article by expanding or improving it.
Command Block.png
Warning: Command Method 
This method gives permanent access to commands that are normally locked behind the command toggle, which may be dangerous. Perform at your own risk.
Downgrade2.png
Warning: Downgrading 
This feature requires downgrading which can result in crashes, data loss, or corruption, and requires specific versions. Follow the page instructions carefully.

25w02a (a 1.21.5 snapshot) made changes to the way that text components were stored to disk. By abusing downgrading over these changes, nearly any text component may be created. Text components allow for many types of formatting and additional information to be encoded along with plain text, such as color, styling, hover events, click events, translations, and more. For specific information on the text component format, see the minecraft wiki. Note that the 1.21.4- format must be used.

Obtaining

All methods of obtaining use a downgrade. 25w02a to 25w42a (present) is the pre-downgrade version range, and 1.21.4 to 24w09a (a 1.20.5 snapshot) is the post-downgrade version range.

Not all content types are parsed in all situations. The score, selector and nbt types only work when the components can be resolved, which is a one time event that will replace these more complex content types with a static form of their results. Item names are not resolved. Written books are resolved the first time they are opened after signing, so resolution can occur after downgrading if the book is never opened in the pre-downgrade version range. Signs are resolved when the sign text is set, so the contents will not be resolved upon downgrading.

Item Names

To place text components onto any item, simply rename the item in an anvil to the SNBT representation of the desired text components at any point in the pre-downgrade version range. Downgrade to the post-downgrade version range and the text components will be applied. Note that this method is limited to SNBT strings that are 50 characters or less.

An example of the SNBT representation to give an item a colored name:

{"text":"Example Name", "color":"red"}

Books

To place text components onto a written book, simply write the SNBT representation of the desired text on each page at any point in the pre-downgrade version range. Sign the book. Downgrade to the post-downgrade version range and the text components will be applied. Each SNBT string must be 1023 characters or less. The usage of a 0 width font resource pack may be necessary to fit all 1023 characters onto the page due to character width limitations.

An example of the SNBT representation to create a page that when clicked prompts the user to open a URL:

{"text":"Click me to watch my latest music video!","clickEvent":{"action":"open_url","value":"https://www.youtube.com/watch?v=dQw4w9WgXcQ"}}

Signs

Signs are of particular interest due to how they handle the run_command click event. In most cases, the run command click event runs with the permissions of the executing player. However, signs will always run their click event commands with an operator level of 2, the same as command blocks. This feature is intended for map makers and server administrators, as it is normally impossible to create signs with these click events as a non-privileged player.

In singleplayer, the limit of the SNBT string length per line is not a static character count, but rather a limit on the amount of bytes after encoding to MUTF-8. 65,535 bytes is the maximum that may be written. In multiplayer, each SNBT string can be up to 384 characters.

An example of the SNBT representation to run /give @s bedrock:

{"text":"Free bedrock!", "clickEvent":{"action":"run_command","value":"give @s bedrock"}}

An important note to allow downgrading signs correctly is that if any line on a face of the sign fails to parse, the entire face will be set as blank, so empty lines must instead have "" to parse properly.

Uses

References