public abstract class Text extends Object implements TextRepresentable, DataSerializable, Comparable<Text>
Text is primarily used for sending formatted chat messages to players, but also in other places like books or signs.
Text instances can be created through the available of() methods
or using one of the Text.Builders available through one of the
builder() methods.
builder(),
Text.Builder,
LiteralText,
TranslatableText,
SelectorText,
ScoreText| Modifier and Type | Class and Description |
|---|---|
static class |
Text.Builder
Represents a builder class to create immutable
Text instances. |
| Modifier and Type | Field and Description |
|---|---|
static Text |
EMPTY
The empty, unformatted
Text instance. |
static LiteralText |
NEW_LINE
An unformatted
Text that will start a new line (if supported). |
static Comparator<Text> |
PLAIN_COMPARATOR
A
Comparator for texts that compares the plain text of two text
instances. |
| Modifier and Type | Method and Description |
|---|---|
static Text.Builder |
builder()
Creates a
Text.Builder with empty text. |
static LiteralText.Builder |
builder(char content)
Creates a new unformatted
LiteralText.Builder with the specified
content. |
static ScoreText.Builder |
builder(Score score)
Creates a new unformatted
ScoreText.Builder with the given score. |
static SelectorText.Builder |
builder(Selector selector)
Creates a new unformatted
SelectorText.Builder with the given
selector. |
static LiteralText.Builder |
builder(String content)
Creates a new unformatted
LiteralText.Builder with the specified
content. |
static ScoreText.Builder |
builder(Text text,
Score score)
Creates a new
ScoreText.Builder with the formatting and actions
of the specified Text and the given score. |
static SelectorText.Builder |
builder(Text text,
Selector selector)
Creates a new
SelectorText.Builder with the formatting and
actions of the specified Text and the given selector. |
static LiteralText.Builder |
builder(Text text,
String content)
Creates a new
LiteralText.Builder with the formatting and actions
of the specified Text and the given content. |
static TranslatableText.Builder |
builder(Text text,
Translatable translatable,
Object... args)
Creates a new
TranslatableText.Builder with the formatting and
actions of the specified Text and the given Translatable. |
static TranslatableText.Builder |
builder(Text text,
Translation translation,
Object... args)
Creates a new
TranslatableText.Builder with the formatting and
actions of the specified Text and the given Translation
and arguments. |
static TranslatableText.Builder |
builder(Translatable translatable,
Object... args)
Creates a new unformatted
TranslatableText.Builder from the given
Translatable. |
static TranslatableText.Builder |
builder(Translation translation,
Object... args)
Creates a new unformatted
TranslatableText.Builder with the given
Translation and arguments. |
int |
compareTo(Text o) |
Text |
concat(Text other)
Concatenates the specified
Text to this Text and returns the
result. |
boolean |
equals(Object o) |
ImmutableList<Text> |
getChildren()
Returns the immutable list of children appended after the content of this
Text. |
Optional<ClickAction<?>> |
getClickAction()
Returns the
ClickAction executed on the client when this
Text gets clicked. |
TextColor |
getColor()
Returns the color of this
Text. |
int |
getContentVersion()
Gets the content version of this
DataSerializable. |
TextFormat |
getFormat()
Returns the format of this
Text. |
Optional<HoverAction<?>> |
getHoverAction()
Returns the
HoverAction executed on the client when this
Text gets hovered. |
Optional<ShiftClickAction<?>> |
getShiftClickAction()
Returns the
ShiftClickAction executed on the client when this
Text gets shift-clicked. |
TextStyle |
getStyle()
Returns the style of this
Text. |
int |
hashCode() |
boolean |
isEmpty()
Returns whether this
Text is empty. |
static Text |
join(Iterable<? extends Text> texts)
Joins a sequence of text objects together.
|
static Text |
join(Iterator<? extends Text> texts)
Joins a sequence of text objects together.
|
static Text |
join(Text... texts)
Joins a sequence of text objects together.
|
static Text |
joinWith(Text separator,
Iterable<? extends Text> texts)
Joins a sequence of text objects together along with a separator.
|
static Text |
joinWith(Text separator,
Iterator<? extends Text> texts)
Joins a sequence of text objects together along with a separator.
|
static Text |
joinWith(Text separator,
Text... texts)
Joins a sequence of text objects together along with a separator.
|
static Text |
of()
Returns an empty, unformatted
Text instance. |
static LiteralText |
of(char content)
Creates a
Text with the specified char as plain text. |
static Text |
of(Object... objects)
Builds a
Text from a given array of objects. |
static ScoreText |
of(Score score)
Creates a new unformatted
ScoreText with the given score. |
static SelectorText |
of(Selector selector)
Creates a new unformatted
SelectorText with the given selector. |
static LiteralText |
of(String content)
Creates a
Text with the specified plain text. |
static TranslatableText |
of(Translatable translatable,
Object... args)
Creates a new unformatted
TranslatableText from the given
Translatable. |
static TranslatableText |
of(Translation translation,
Object... args)
Creates a new unformatted
TranslatableText with the given
Translation and arguments. |
Text |
replace(Pattern oldValue,
Text newValue)
Replaces a pattern in this text with a
Text, preserving all
formatting. |
Text |
replace(Pattern oldValue,
Text newValue,
boolean lossy)
Replaces a pattern in this text with a
Text, preserving
formatting where possible. |
Text |
replace(String oldValue,
Text newValue)
Replaces a string within this text with a
Text, preserving all
formatting. |
Text |
replace(String oldValue,
Text newValue,
boolean lossy)
Replaces a string within this text with a
Text, preserving
formatting where possible. |
abstract Text.Builder |
toBuilder()
Returns a new
Text.Builder with the content, formatting and actions of
this text. |
DataContainer |
toContainer()
Serializes this object into a comprehensible
DataContainer. |
String |
toPlain()
Returns a plain text representation of this
Text without any
formatting. |
String |
toPlainSingle()
Returns a plain text representation of this
Text without any
children. |
String |
toString() |
Text |
toText()
Gets the textual representation of this instance for its usage in other
Text objects. |
Text |
trim()
Removes all empty texts from the beginning and end of this
text.
|
Iterable<Text> |
withChildren()
Returns an immutable
Iterable over this text and all of its
children. |
clone, finalize, getClass, notify, notifyAll, wait, wait, waitapplyTopublic static final LiteralText NEW_LINE
Text that will start a new line (if supported).public static Comparator<Text> PLAIN_COMPARATOR
Comparator for texts that compares the plain text of two text
instances.public final TextFormat getFormat()
Text.public final TextColor getColor()
Text.public final TextStyle getStyle()
Text. This will return a compound
TextStyle if multiple different styles have been set.public final ImmutableList<Text> getChildren()
Text.public final Iterable<Text> withChildren()
Iterable over this text and all of its
children. This is recursive, the children of the children will be also
included.public final Optional<ClickAction<?>> getClickAction()
ClickAction executed on the client when this
Text gets clicked.Optional.empty() if not
setpublic final Optional<HoverAction<?>> getHoverAction()
HoverAction executed on the client when this
Text gets hovered.Optional.empty() if not
setpublic final Optional<ShiftClickAction<?>> getShiftClickAction()
ShiftClickAction executed on the client when this
Text gets shift-clicked.Optional.empty()
if not setpublic final boolean isEmpty()
Text is empty.true if this text is emptypublic abstract Text.Builder toBuilder()
Text.Builder with the content, formatting and actions of
this text. This can be used to edit an otherwise immutable Text
instance.public final String toPlain()
Text without any
formatting.public final String toPlainSingle()
Text without any
children.public final Text concat(Text other)
Text to this Text and returns the
result.other - To concatenatepublic final Text trim()
public Text replace(Pattern oldValue, Text newValue, boolean lossy)
Text, preserving
formatting where possible.
If lossy is true, this will replace the pattern if this finds
it spanning multiple child Texts, resulting in a potential loss
of formatting; if false, it will only replace the pattern where it is
contained within one child Text, to preserve all formatting.
This method produces a copy with the specified replacements. The original is not modified.
oldValue - The pattern to replacenewValue - The value to replace withlossy - Whether lossy mode is enabledpublic Text replace(Pattern oldValue, Text newValue)
Text, preserving all
formatting.
This method produces a copy with the specified replacements. The original is not modified.
oldValue - The pattern to replacenewValue - The value to replace withpublic Text replace(String oldValue, Text newValue, boolean lossy)
Text, preserving
formatting where possible.
If lossy is true, this will replace the pattern if this finds
it spanning multiple child Texts, resulting in a potential loss
of formatting; if false, it will only replace the pattern where it is
contained within one child Text, to preserve all formatting.
This method produces a copy with the specified replacements. The original is not modified.
oldValue - The string to replacenewValue - The value to replace withlossy - Whether lossy mode is enabledpublic Text replace(String oldValue, Text newValue)
Text, preserving all
formatting.
This method produces a copy with the specified replacements. The original is not modified.
oldValue - The string to replacenewValue - The value to replace withpublic int getContentVersion()
DataSerializableDataSerializable. The version
may differ between instances of plugins and implementations such that
the DataView from DataSerializable.toContainer() may include different
information, or remove other information as they are no longer deemed
necessary. The version goes hand in hand with DataContentUpdater
as it is required when there exists any DataView of this
DataSerializable with an "older" version.getContentVersion in interface DataSerializablepublic DataContainer toContainer()
DataSerializableDataContainer.toContainer in interface DataSerializablepublic int compareTo(Text o)
compareTo in interface Comparable<Text>public final Text toText()
TextRepresentableText objects. This may but does not need to include
hover texts or other actions. This
method is basically the toString() equivalent
for Texts.toText in interface TextRepresentablepublic static LiteralText of(String content)
Text with the specified plain text. The created text
won't have any formatting or events configured.content - The content of the textLiteralTextpublic static LiteralText of(char content)
Text with the specified char as plain text. The created
text won't have any formatting or events configured.content - The content of the text as charLiteralTextpublic static TranslatableText of(Translation translation, Object... args)
TranslatableText with the given
Translation and arguments.translation - The translation for the textargs - The arguments for the translationTranslatableTextpublic static TranslatableText of(Translatable translatable, Object... args)
TranslatableText from the given
Translatable.translatable - The translatable for the textargs - The arguments for the translationTranslatableTextpublic static SelectorText of(Selector selector)
SelectorText with the given selector.selector - The selector for the textSelectorTextpublic static ScoreText of(Score score)
ScoreText with the given score.score - The score for the textScoreTextpublic static Text of(Object... objects)
Text from a given array of objects.
For instance, you can use this like
Text.of(TextColors.DARK_AQUA, "Hi", TextColors.AQUA, "Bye")
This will create the correct Text instance if the input object
is the input for one of the Text types or convert the object to a
string otherwise.
For instances of type TextRepresentable (e.g. Text,
Text.Builder, ...) the formatting of appended text has priority over
the current formatting in the method, e.g. the following results in a
green, then yellow and at the end green again Text:
Text.of(TextColors.GREEN, "Hello ", Text.of(TextColors.YELLOW,
"Spongie"), '!');objects - The object arraypublic static Text.Builder builder()
Text.Builder with empty text.public static LiteralText.Builder builder(String content)
LiteralText.Builder with the specified
content.content - The content of the textLiteralText,
LiteralText.Builderpublic static LiteralText.Builder builder(char content)
LiteralText.Builder with the specified
content.content - The content of the text as charLiteralText,
LiteralText.Builderpublic static LiteralText.Builder builder(Text text, String content)
LiteralText.Builder with the formatting and actions
of the specified Text and the given content.text - The text to apply the properties fromcontent - The content for the text builderLiteralText,
LiteralText.Builderpublic static TranslatableText.Builder builder(Translation translation, Object... args)
TranslatableText.Builder with the given
Translation and arguments.translation - The translation for the builderargs - The arguments for the translationTranslatableText,
TranslatableText.Builderpublic static TranslatableText.Builder builder(Translatable translatable, Object... args)
TranslatableText.Builder from the given
Translatable.translatable - The translatable for the builderargs - The arguments for the translationTranslatableText,
TranslatableText.Builderpublic static TranslatableText.Builder builder(Text text, Translation translation, Object... args)
TranslatableText.Builder with the formatting and
actions of the specified Text and the given Translation
and arguments.text - The text to apply the properties fromtranslation - The translation for the builderargs - The arguments for the translationTranslatableText,
TranslatableText.Builderpublic static TranslatableText.Builder builder(Text text, Translatable translatable, Object... args)
TranslatableText.Builder with the formatting and
actions of the specified Text and the given Translatable.text - The text to apply the properties fromtranslatable - The translatable for the builderargs - The arguments for the translationTranslatableText,
TranslatableText.Builderpublic static SelectorText.Builder builder(Selector selector)
SelectorText.Builder with the given
selector.selector - The selector for the builderSelectorText,
SelectorText.Builderpublic static SelectorText.Builder builder(Text text, Selector selector)
SelectorText.Builder with the formatting and
actions of the specified Text and the given selector.text - The text to apply the properties fromselector - The selector for the builderSelectorText,
SelectorText.Builderpublic static ScoreText.Builder builder(Score score)
ScoreText.Builder with the given score.score - The score for the text builderScoreText,
ScoreText.Builderpublic static ScoreText.Builder builder(Text text, Score score)
ScoreText.Builder with the formatting and actions
of the specified Text and the given score.text - The text to apply the properties fromscore - The score for the text builderScoreText,
ScoreText.Builderpublic static Text join(Text... texts)
texts - The texts to joinpublic static Text join(Iterable<? extends Text> texts)
texts - The texts to joinpublic static Text join(Iterator<? extends Text> texts)
texts - The texts to joinpublic static Text joinWith(Text separator, Text... texts)
separator - The separatortexts - The texts to joinpublic static Text joinWith(Text separator, Iterable<? extends Text> texts)
separator - The separatortexts - The texts to join