TextComponentBuilder
Create complex clickable messages using the Spigot TextComponent API — in just one line!
With this little builder, you can create clickable messages by combining multiple TextComponents into a single one.
[hr]
Usage Example
Here's a few examples to help you understand how it works
Simple clickable message (The whole message will be clickable)
Code (Java):
player.
spigot
(
).
sendMessage
(
new TextComponentBuilder
(
).
setMessage
(
"§dThis is clickable !!!"
).
build
(
)
)
;
Complex clickable message (Only chosen part will be clickable)
Code (Java):
player.
spigot
(
).
sendMessage
(
new TextComponentBuilder
(
)
.
compose
(
new TextComponentBuilder
(
).
setMessage
(
"§dThis is clickable !!!"
)
.
setHoverEvent
(HoverEvent.
Action.
SHOW_TEXT,
String.
format
(
"§7Hey §d%s §!", player.
getName
(
)
)
)
.
setClickAction
(ClickEvent.
Action.
RUN_COMMAND,
"/say Heyyy !"
)
.
build
(
),
new TextComponentBuilder
(
).
setMessage
(
" §7This is non clickable :("
).
build
(
)
).
build
(
)
)
;
[hr]
Method Explanation
This method is just the constructor — it creates the TextComponent and sets it to empty.
If a message were put there, your whole message would be clickable, and to avoid that, the compose method exists!
Code (Java):
public TextComponentBuilder
(
)
{
this.
textComponent
=
new
TextComponent
(
""
)
;
}
As its name suggests, this method defines the message that will be shown to your player. Nothing special here — put whatever you want!
Code (Java):
public TextComponentBuilder setMessage
(
String message
)
{
textComponent.
addExtra
(message
)
;
return
this
;
}
Those two methods, setHoverEvent() and setClickAction(), allow you to add interactivity to your messages.
setHoverEvent() — lets you show something when hovering the message.
It takes two arguments:
- A HoverEvent Action — one of:
- HoverEvent.Action.SHOW_TEXT : Show text when hovering
- HoverEvent.Action.SHOW_ACHIEVEMENT : Show an achievement (UNTESTED)
- HoverEvent.Action.SHOW_ENTITY : Show entity info (UNTESTED)
- HoverEvent.Action.SHOW_ITEM : Show item info (UNTESTED)
- A String — the text shown when using SHOW_TEXT
setClickAction() — lets you trigger an action when clicking the message.
It also takes two arguments:
- A ClickEvent Action — one of:
- ClickEvent.Action.COPY_TO_CLIPBOARD : Copy text to clipboard (UNTESTED)
- ClickEvent.Action.OPEN_FILE : Open a local file (UNTESTED)
- ClickEvent.Action.OPEN_URL : Open a webpage
- ClickEvent.Action.RUN_COMMAND : Run a command
- ClickEvent.Action.SUGGEST_COMMAND : Suggest a command in chat
- A String — the value passed to the action
Code (Java):
public TextComponentBuilder setHoverEvent
(net.
md_5.
bungee.
api.
chat.
HoverEvent.
Action hoverAction,
String value
)
{
textComponent.
setHoverEvent
(
new HoverEvent
(hoverAction,
new Text
(value
)
)
)
;
return
this
;
}
public TextComponentBuilder setClickAction
(net.
md_5.
bungee.
api.
chat.
ClickEvent.
Action clickAction,
String value
)
{
textComponent.
setClickEvent
(
new ClickEvent
(clickAction, value
)
)
;
return
this
;
}
The compose() method is used to build complex clickable messages.
It allows you to chain multiple TextComponentBuilder calls to build any number of clickable (or non-clickable) segments, combining them into a single message.
Finally, the build() method just does what its name implies — it builds and returns the final TextComponent.
Code (Java):
public TextComponentBuilder compose
(
TextComponent...
components
)
{
for
(
TextComponent component
: components
)
{
textComponent.
addExtra
(component
)
;
}
return
this
;
}
public
TextComponent build
(
)
{
return textComponent
;
}
[hr]
That's it, enjoy!