Text components - ShaneBeee/SkBee GitHub Wiki

Text components are a system in Minecraft, which allows you to manipulate different parts of texts, with things like links, command suggestions, etc.

CREATING:

Text Component:

Creating a new text component is fairly simple, lets take a look at the syntax.

[a] [new] text component[s] (from|of) %strings%

This creates a new text component.
Let's take a look at an example

set {_a} to a new text component from "This is my new component"

Pretty simple right? Well, we're just getting started.

Translate Component:

Well these are super cool. Most of the words/messages in Minecraft are sent to the client in a translatable context. This means a little string is sent to the client and the client translates it based on the language they're using.
All of the translation options can be found in the lang file of the Minecraft jar (you'll need to unzip the jar).

(minecraft data folder)/versions/1.16.2/assets/minecraft/lang/(lang).json)

Here is an example of what it looks like in the English lang file:

 "item.minecraft.diamond_sword": "Diamond Sword",

So if you're holding a diamond sword in your hand, the server sends "item.minecraft.diamond_sword" and the client will translate this to "Diamond Sword", or whatever it is for said language.
So we can create a component based on that.
Syntax:

[a] [new] translate component[s] (from|of) %itemtypes/strings%
[a] [new] translate component[s] (from|of) %string% (with|using) %objects%

In this first example, we can translate based on a string from the lang file, or from an item.
Here are some examples:

set {_a} to translate component from "item.minecraft.diamond_sword"
set {_b} to translate component from a diamond sword
set {_c} to translate component from player's tool

Now when sending these components to a player, it will match the language they have set.
Some of these translatable messages include different options, lets look at some examples from the English lang file:

"death.fell.accident.ladder": "%1$s fell off a ladder",
"death.fell.accident.vines": "%1$s fell off some vines",
"death.fell.accident.weeping_vines": "%1$s fell off some weeping vines",

Here, %1$s would be replaced by the player's name.
How do we use these? Super simple, let's look at the pattern again:

[a] [new] translate component[s] (from|of) %string% (with|using) %objects%

So we just put in our objects area, the objects that will fit this part, ex:

set {_a} to translate component from "death.fell.accident.ladder" using player

Now when this message is sent to the client, they will see:

ShaneBee fell off a ladder

Super easy and fun right?

MODIFYING:

We can modify these components with some formatting and also have them run some hover and click events. Let's take a look at what we can do. (Note: You can add both a hover and click event to one component, as well as formatting)

Formatting

There are several different format options for components, such as color, bold, italics, obfuscation, etc.
Let's look at the syntax:

[the] (color|bold|italic|(obfuscate[d]|magic)|strikethrough|underline[d]|insert[ion]) format of %basecomponents%

Color supports both color codes (ie: blue), as well as RGB colors (in Minecraft 1.16+)
Let's see some examples:

set {_a} to text component from "this is a nice line"
set color format of {_a} to blue
set color format of {_a} to rgb(100, 55, 100)

Pretty simple.
We can also add an insertion. This means when the player shift-clicks on the text, that text is copied into the chat.
Its kinda like the click event for suggesting a command, but this will copy to where their cursor is currently in the chat bar.
ex:

set insertion format of {_a} to " this will be copied"

Let's pretend the players chat field currently looks like this:

hi I'm typing a message

When they shift-click that text, they will see this:

hi I'm typing a message this will be copied

They can also click it many times:

hi I'm typing a message this will be copied this will be copied this will be copied this will be copied

The remaining format options just use booleans, ex:

set bold format of {_a} to true
set italic format of {_a} to true
set obfuscated format of {_a} to true

Hover Event

By adding a hover event to your text component, when the player hovers over the text in the chat, they will see another message or even an item.
Syntax:
(Please note a basecomponent is actually just a text component)

[the] hover event of %basecomponents%

So what kind of hover events can we do?

[a] [new] hover event showing %strings%
[a] [new] hover event showing %itemtype%

oooOOOooo fun!
Let's try adding a hover event to our previously made text component.

set hover event of {_a} to a new hover event showing "Look mah I'm hovering"

Pretty simple ya? YA?
Here is what it would look like:

How about with an item?

set hover event of {_a} to a new hover event showing player's tool

How's it look?

OOOOO That's cool!

Click Event

We can also make things happen when you click, let's check out the syntax
(Please note a basecomponent is actually just a text component)

[the] click event of %basecomponents%

What kind of click events can we do? Well I'm glad you asked

[a] [new] click event to run command %string%
[a] [new] click event to suggest command %string%
[a] [new] click event to open (link|url) %string%
[a] [new] click event to copy %string% to clipboard
[a] [new] click event to change to page %number%

Theres are pretty self explanatory.
Run command will make the player run the command when clicked.
Suggest command will put the command in the chat bar for the player, all setup and ready to go.
Open link will open a link for a player.
Copy to clipboard will copy the given text to the clipboard of the player, for easy pasting wherever they need.

So let's try adding a click event.

set click event of {_a} to a new click event to suggest command "/spawn"

How's it look?
(As you can see here, when the player clicks the message, it suggests the "/spawn" command for them)

SENDING

Well now that we have all these cool new components, what do we do with them?
You can send one or many components to a player

send component {_a} to player
send components {_a}, {_b} and {_c} to player

BRINGING IT ALL TOGETHER

Let's bring this all together in one big example.
In this example, we're going to send a message, where only part of it will show the player's tool.
We will save their tool in a variable, so when the player clicks that part of the message, they will receive one of the item.
(Clearly you're going to want to do some checks and such to make sure the player doesn't get an unlimited amount of said item, but this is just an example).

command /item [<player>]:
	trigger:
		if arg-1 is not set:
			set {item::%player%} to 1 of player's tool
			set {_c::1} to a new text component from "Hey guys check out my cool "
			set {_c::2} to a new text component from "&7[&bITEM&7] &r"
			set {_c::3} to a new text component from "ain't she a beauty?"
			set hover event of {_c::2} to hover event showing player's tool
			set click event of {_c::2} to click event to run command "/item %player%"
			set {_c} to merge components {_c::*}
			send component {_c} to player all players
		else:
			give player 1 of {item::%arg-1%}

Here is an example showing some translations in a simple "showoff" command

command /showoff:
	trigger:
		set {_c::1} to text component from "&7[&a&lShowOff&7 - &b&l%player%&7] &rCheck out my &7&l["
		set {_c::2} to translate component from player's tool
		set color format of {_c::2} to rgb(100, 150, 100)
		set bold format of {_c::2} to true
		set hover event of {_c::2} to hover event showing player's tool
		set {_c::3} to text component from "&7&l] &raint she a beaut?"

		set {_c} to merge components {_c::*}
		send components {_c} to all players

BOOK PAGES:

You can set pages of a book using text components. Same methods as above for creating new components with hover and click events.

Simple Example:

set {_item} to a written book
set {_a} to text component of "RULES"
set hover event of {_a} to hover event showing "make sure to read our rules"
set click event of {_a} to click event to run command "/rules"
set page 1 of book {_item} to {_a}
set book author of {_item} to "Bob"
set book title of {_item} to "MyBook"
give player 1 of {_item}

From my testing (on SOME Minecraft versions), book author/title needs to be set AFTER setting the pages. Not sure why, but thats just how it works.

Books have a fancy click event too:

[a] [new] click event to change to page %number%

This allows a click to change pages... neat right?!?!

ITEM CHAT

I get asked this so many times, so I'm sharing a quick little chat event, that will show the player's item when they type any message that includes '[item]'

Code:

on chat:
	set chat format to "&7[&b%world of player%&7] &a%player% &7>> &r%uncolored message%"
	if message contains "[item]":
		cancel event
		set {_m::*} to message split at "[item]"
		set {_t::1} to text component from "&7[&b%world of player%&7] &a%player% &7>> "
		set {_t::2} to text component from {_m::1}
		set {_t::3} to text component from "&7["
		if name of player's tool is set:
			set {_t::4} to text component from name of player's tool
		else:
			set {_t::4} to translate component from player's tool
			set color format of {_t::4} to light blue
		set hover event of {_t::4} to hover event showing player's tool
		set {_t::5} to text component from "&7]&r"
		set {_t::6} to text component from {_m::2}

		# SkBee 2.8.3+
		set {_t} to merge components {_t::*}
		broadcast component {_t}

		# SkBee 1.16.0-3.6.0
		broadcast components {_t::*}

		# SkBee 1.15.3-
		send components {_t::*} to all players and console

Example:

⚠️ **GitHub.com Fallback** ⚠️