public final class ComponentBuilder extends Object
ComponentBuilder simplifies creating basic messages by allowing the use of a chainable builder.
new ComponentBuilder("Hello ").color(ChatColor.RED).
append("World").color(ChatColor.BLUE). append("!").bold(true).create();
All methods (excluding append(String) and create() work on
the last part appended to the builder, so in the example above "Hello " would
be ChatColor.RED and "World" would be
ChatColor.BLUE but "!" would be bold and
ChatColor.BLUE because append copies the previous
part's formatting
| Modifier and Type | Class and Description |
|---|---|
static class |
ComponentBuilder.FormatRetention |
static interface |
ComponentBuilder.Joiner
Functional interface to join additional components to a ComponentBuilder.
|
| Constructor and Description |
|---|
ComponentBuilder() |
ComponentBuilder(BaseComponent component)
Creates a ComponentBuilder with the given component as the first part.
|
ComponentBuilder(ComponentBuilder original)
Creates a ComponentBuilder from the other given ComponentBuilder to clone
it.
|
ComponentBuilder(String text)
Creates a ComponentBuilder with the given text as the first part.
|
| Modifier and Type | Method and Description |
|---|---|
ComponentBuilder |
append(BaseComponent component)
Appends a component to the builder and makes it the current target for
formatting.
|
ComponentBuilder |
append(BaseComponent[] components)
Appends the components to the builder and makes the last element the
current target for formatting.
|
ComponentBuilder |
append(BaseComponent[] components,
ComponentBuilder.FormatRetention retention)
Appends the components to the builder and makes the last element the
current target for formatting.
|
ComponentBuilder |
append(BaseComponent component,
ComponentBuilder.FormatRetention retention)
Appends a component to the builder and makes it the current target for
formatting.
|
ComponentBuilder |
append(ComponentBuilder.Joiner joiner)
Allows joining additional components to this builder using the given
ComponentBuilder.Joiner and ComponentBuilder.FormatRetention.ALL. |
ComponentBuilder |
append(ComponentBuilder.Joiner joiner,
ComponentBuilder.FormatRetention retention)
Allows joining additional components to this builder using the given
ComponentBuilder.Joiner. |
ComponentBuilder |
append(String text)
Appends the text to the builder and makes it the current target for
formatting.
|
ComponentBuilder |
append(String text,
ComponentBuilder.FormatRetention retention)
Appends the text to the builder and makes it the current target for
formatting.
|
ComponentBuilder |
append(TranslationProvider translatable)
Appends the
TranslationProvider object to the builder and makes
the last element the current target for formatting. |
ComponentBuilder |
append(TranslationProvider translatable,
ComponentBuilder.FormatRetention retention)
Appends the
TranslationProvider object to the builder and makes
the last element the current target for formatting. |
ComponentBuilder |
appendLegacy(String text)
Parse text to BaseComponent[] with colors and format, appends the text to
the builder and makes it the current target for formatting.
|
ComponentBuilder |
bold(boolean bold)
Sets whether the current part is bold.
|
BaseComponent |
build()
Returns the component built by this builder.
|
ComponentBuilder |
color(ChatColor color)
Sets the color of the current part.
|
BaseComponent[] |
create()
Returns the components needed to display the message created by this
builder.git
|
ComponentBuilder |
event(ClickEvent clickEvent)
Sets the click event for the current part.
|
ComponentBuilder |
event(HoverEvent hoverEvent)
Sets the hover event for the current part.
|
ComponentBuilder |
font(String font)
Sets the font of the current part.
|
BaseComponent |
getComponent(int pos)
Gets the component part at the position of given index.
|
BaseComponent |
getCurrentComponent()
Gets the component at the position of the cursor.
|
int |
getCursor()
The position for the current part to modify.
|
List<BaseComponent> |
getParts() |
ComponentBuilder |
insertion(String insertion)
Sets the insertion text for the current part.
|
ComponentBuilder |
italic(boolean italic)
Sets whether the current part is italic.
|
ComponentBuilder |
obfuscated(boolean obfuscated)
Sets whether the current part is obfuscated.
|
void |
removeComponent(int pos)
Remove the component part at the position of given index.
|
ComponentBuilder |
reset()
Sets the current part back to normal settings.
|
ComponentBuilder |
resetCursor()
Resets the cursor to index of the last element.
|
ComponentBuilder |
retain(ComponentBuilder.FormatRetention retention)
Retains only the specified formatting.
|
ComponentBuilder |
setCursor(int pos)
Sets the position of the current component to be modified
|
ComponentBuilder |
strikethrough(boolean strikethrough)
Sets whether the current part is strikethrough.
|
ComponentBuilder |
style(ComponentStyle style)
Applies the provided
ComponentStyle to the current part. |
ComponentBuilder |
underlined(boolean underlined)
Sets whether the current part is underlined.
|
public ComponentBuilder(ComponentBuilder original)
original - the original for the new ComponentBuilder.public ComponentBuilder(String text)
text - the first text elementpublic ComponentBuilder(BaseComponent component)
component - the first component elementpublic ComponentBuilder()
public ComponentBuilder resetCursor()
public ComponentBuilder setCursor(int pos) throws IndexOutOfBoundsException
pos - the cursor position synonymous to an element position for a
listIndexOutOfBoundsException - if the index is out of range
(index < 0 || index >= size())public ComponentBuilder append(BaseComponent component)
component - the component to appendpublic ComponentBuilder append(BaseComponent component, ComponentBuilder.FormatRetention retention)
component - the component to appendretention - the formatting to retainpublic ComponentBuilder append(BaseComponent[] components)
components - the components to appendpublic ComponentBuilder append(BaseComponent[] components, ComponentBuilder.FormatRetention retention)
components - the components to appendretention - the formatting to retainpublic ComponentBuilder append(TranslationProvider translatable)
TranslationProvider object to the builder and makes
the last element the current target for formatting. The components will
have all the formatting from previous part.translatable - the translatable object to appendpublic ComponentBuilder append(TranslationProvider translatable, ComponentBuilder.FormatRetention retention)
TranslationProvider object to the builder and makes
the last element the current target for formatting. You can specify the
amount of formatting retained from previous part.translatable - the translatable object to appendretention - the formatting to retainpublic ComponentBuilder append(String text)
text - the text to appendpublic ComponentBuilder appendLegacy(String text)
text - the text to appendpublic ComponentBuilder append(String text, ComponentBuilder.FormatRetention retention)
text - the text to appendretention - the formatting to retainpublic ComponentBuilder append(ComponentBuilder.Joiner joiner)
ComponentBuilder.Joiner and ComponentBuilder.FormatRetention.ALL.
Simply executes the provided joiner on this instance to facilitate a
chain pattern.joiner - joiner used for operationpublic ComponentBuilder append(ComponentBuilder.Joiner joiner, ComponentBuilder.FormatRetention retention)
ComponentBuilder.Joiner.
Simply executes the provided joiner on this instance to facilitate a
chain pattern.joiner - joiner used for operationretention - the formatting to retainpublic void removeComponent(int pos)
throws IndexOutOfBoundsException
pos - the index to remove atIndexOutOfBoundsException - if the index is out of range
(index < 0 || index >= size())public BaseComponent getComponent(int pos) throws IndexOutOfBoundsException
pos - the index to findIndexOutOfBoundsException - if the index is out of range
(index < 0 || index >= size())public BaseComponent getCurrentComponent()
public ComponentBuilder color(ChatColor color)
color - the new colorpublic ComponentBuilder font(String font)
font - the new fontpublic ComponentBuilder bold(boolean bold)
bold - whether this part is boldpublic ComponentBuilder italic(boolean italic)
italic - whether this part is italicpublic ComponentBuilder underlined(boolean underlined)
underlined - whether this part is underlinedpublic ComponentBuilder strikethrough(boolean strikethrough)
strikethrough - whether this part is strikethroughpublic ComponentBuilder obfuscated(boolean obfuscated)
obfuscated - whether this part is obfuscatedpublic ComponentBuilder style(ComponentStyle style)
ComponentStyle to the current part.style - the style to applypublic ComponentBuilder insertion(String insertion)
insertion - the insertion textpublic ComponentBuilder event(ClickEvent clickEvent)
clickEvent - the click eventpublic ComponentBuilder event(HoverEvent hoverEvent)
hoverEvent - the hover eventpublic ComponentBuilder reset()
public ComponentBuilder retain(ComponentBuilder.FormatRetention retention)
retention - the formatting to retainpublic BaseComponent build()
public BaseComponent[] create()
NOTE: build() is preferred as it will
consolidate all components into a single BaseComponent with extra
contents as opposed to an array of components which is non-standard and
may result in unexpected behavior.
public int getCursor()
public List<BaseComponent> getParts()
Copyright © 2012–2024 SpigotMC. All rights reserved.