Text Component
Text components provide the foundation for customizable text in Minecraft. The text component is responsible for the context and style of the text. For example a text component can display a localized message with a specific color and font.
Content Types
Minecraft includes several built-in component types that can be used and combined in different ways.
Plain Text
A plain text component displays the text exactly as it is written.
Translatable
Translatable text components are localized to the readers language. This requires an entry in the language file which can be added using a resource pack.
Simple Example
Fallback
You can choose to define fallback text that will be shown if the reader does not have a translation key for your text.
Arguments
Translatable text components can have arguments inserted into their text. These arguments are slotted in using %s
in
the translation text or %n%s
where n is the index you want to reference.
Scoreboard Text
Displays the value of a scoreboard entry. The score holder is targeted using the name
property. This can be a selector
like @p
, a name, or a *
to refer to the readers score. The objective
property sets the objective to get the value
of.
Entity Name
Displays the name of one or more entities. The entities are selected using a selector
. If multiple entities are
defined they will be separated using a ,
or the separator
value.
Key Binding
Displays the name of the key that a key binding is mapped to. This can be used to display input instructions using the
players configured key mappings. This should always be done where possible to improve the accessibility of your message.
The key binding is specified by setting keybind
to the ID of the key binding. A full list of vanilla key binding IDs
can be found here.
NBT Values
Displays a value from the games NBT data. For example you can display the health of the nearest entity or the contents of a chest by reading the NBT data. There are three different types of NBT text components but they share a few common properties.
nbt
- The path of the NBT value.interpret
- An optional boolean, when set to true NBT values will be interpreted as text components.separator
- An optional text component used to separate multiple values if more than one value is returned.
Entity NBT
To display an NBT value from an entity you need to define an entity selector using the entity
property. This example
will display the health of some pig in the world.
Block NBT
To display an NBT value from a block you need to define the position of the block using the block
property. This
example displays the ID of the item in the first slot of a chest at the coordinates 1, 64, 8
. The coordinates can also
be relative.
Storage NBT
Minecraft allows arbitrary NBT data to be stored in the world using commands and other means. This data is bound to a
resource location. You can specify the ID of the data to read using
the storage
property.
This command will set up a storage entry called example:test
with the value MyValue
Modded Content Types
Mods can add new content types to the game and can even expand upon the vanilla types. For example, the Bookshelf mod makes several values like the name of the current player available through its Extended Text feature. This feature can be used as an alternative to the Entity Name component that works when there is no world context available.