Interface ChatComponent<C extends ChatComponent<C>>
- Type Parameters:
C- concrete component type returned by the fluent methods, enabling type-safe chaining in subinterfaces
- All Known Subinterfaces:
MultiComponent
A ChatComponent wraps a raw message string and lets you attach interactive events
(setClick(me.croabeast.prismatic.chat.ChatComponent.Click, java.lang.String), setHover(java.util.List<java.lang.String>), setHoverItem(java.lang.String)) through a builder-style API. When
you are done configuring the component, call compile(Player) to produce the
BaseComponent array expected by player.spigot().sendMessage(...).
The simplest way to create an instance is through the factory methods:
BaseComponent[] msg = PrismaticAPI.chatComponent("<#ff8800>Click me!")
.setClick("run", "/help")
.setHover("Open help<n>Second line")
.compile(player);
player.spigot().sendMessage(msg);
URLs found inside the message text are automatically converted into
OPEN_URL click events during compile(Player).
- Since:
- 1.5.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumEnumeration of the click actions that aChatComponentcan carry. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PatternURL detector used to automatically attachChatComponent.Click.OPEN_URLclick events to any URL-only segment found inside the raw message duringcompile(Player). -
Method Summary
Modifier and TypeMethodDescription@NotNull net.md_5.bungee.api.chat.BaseComponent[]compile(org.bukkit.entity.Player player) Compiles this component into aBaseComponentarray ready for Spigot/Bungee chat APIs.static ChatComponent<?> fromString(String message) Creates a newChatComponentbacked by the default Prismatic processor.static ChatComponent<?> fromString(ChatProcessor processor, String message) Creates a newChatComponentbacked by the givenChatProcessor.@NotNull StringReturns the raw, un-colorized message text stored in this component.booleanReturnstruewhen this component has at least one click or hover event attached.instance()Returns the concrete implementation instance of this component.default CAttaches a click event from a compact"action:payload"string.default CAttaches a click event to this component using a string alias for the action.setClick(ChatComponent.Click click, String input) Attaches a click event to this component.Sets a text hover event from a single, optionally multi-line string.default CSets a text hover event whose content is drawn from the provided lines.Sets a text hover event whose content is drawn from the provided lines.setHoverItem(String json) Sets an item hover event whose content is described by an NBT/SNBT JSON string.setMessage(@NotNull String message) Replaces the raw message text of this component.
-
Field Details
-
URL_PATTERN
URL detector used to automatically attachChatComponent.Click.OPEN_URLclick events to any URL-only segment found inside the raw message duringcompile(Player).The pattern matches both
http://andhttps://URLs as well as barewww.prefixed links.
-
-
Method Details
-
getMessage
Returns the raw, un-colorized message text stored in this component.- Returns:
- the raw message string; never
null
-
setMessage
Replaces the raw message text of this component.- Parameters:
message- the new raw message; must not benull- Returns:
- this component for fluent chaining
-
setClick
Attaches a click event to this component.- Parameters:
click- the click action to perform when the player clicks the textinput- the payload delivered to the click action (command, URL, etc.)- Returns:
- this component for fluent chaining
-
setClick
Attaches a click event to this component using a string alias for the action.The alias is resolved through
ChatComponent.Click.fromName(String); unknown aliases fall back toChatComponent.Click.SUGGEST.- Parameters:
click- a case-insensitive alias for the desired click action (e.g."run","suggest","url")input- the payload delivered to the click action- Returns:
- this component for fluent chaining
-
setClick
Attaches a click event from a compact"action:payload"string.The string is split on the first
':'character; surrounding quotation marks are stripped before parsing. Example:"run:/spawn"or"url:https://example.com".- Parameters:
input- combined action and payload string- Returns:
- this component for fluent chaining
-
setHover
Sets a text hover event whose content is drawn from the provided lines.Each element in the list is rendered as a separate line in the tooltip shown to the player. The lines are colorized via the component's processor when the component is compiled.
- Parameters:
list- ordered list of hover text lines; must not benull- Returns:
- this component for fluent chaining
-
setHover
Sets a text hover event whose content is drawn from the provided lines.Convenience overload for
setHover(List).- Parameters:
array- ordered hover text lines- Returns:
- this component for fluent chaining
-
setHover
Sets a text hover event from a single, optionally multi-line string.The string is split into lines using the processor's
line separator regex(default:<n>). Surroundinghover:"..."markup is stripped before splitting.- Parameters:
string- raw hover content, may contain<n>separators- Returns:
- this component for fluent chaining
-
setHoverItem
Sets an item hover event whose content is described by an NBT/SNBT JSON string.The payload can be provided either as a raw JSON string or as a Base64-encoded string prefixed with
"b64:". This is useful for including special characters or complex item NBT without escaping issues.- Parameters:
json- raw item JSON or a"b64:<base64>"encoded payload- Returns:
- this component for fluent chaining
-
hasEvents
boolean hasEvents()Returnstruewhen this component has at least one click or hover event attached.This can be used to decide whether to wrap the compiled output in additional markup or to skip unnecessary processing when no interactive events were configured.
- Returns:
trueif a click, text hover or item hover event is present
-
compile
@NotNull @NotNull net.md_5.bungee.api.chat.BaseComponent[] compile(org.bukkit.entity.Player player) Compiles this component into aBaseComponentarray ready for Spigot/Bungee chat APIs.The message text is colorized through the component's
ChatProcessorusing the provided player context so that player-aware hex fallback is applied correctly. URLs found inside the message are automatically converted toChatComponent.Click.OPEN_URLevents.- Parameters:
player- the player who will receive the message, used for version-aware color output; may benullto force the legacy fallback- Returns:
- a non-empty array of compiled
BaseComponentobjects
-
instance
Returns the concrete implementation instance of this component.Implementations return
this; this method exists so that fluent setters defined in default methods on sub-interfaces can return the correct concrete type.- Returns:
- this component instance
-
fromString
Creates a newChatComponentbacked by the givenChatProcessor.- Parameters:
processor- the text processor used to colorize the message and hover contentmessage- the initial raw message text- Returns:
- a new
ChatComponentinstance
-
fromString
Creates a newChatComponentbacked by the default Prismatic processor.This is the recommended factory method for most use cases. It uses
ChatProcessor.prismatic()which delegates colorization toPrismaticAPI.- Parameters:
message- the initial raw message text- Returns:
- a new
ChatComponentinstance
-