public abstract class StringFormat
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
protected char |
colorChar |
| Constructor and Description |
|---|
StringFormat(char colorChar) |
| Modifier and Type | Method and Description |
|---|---|
static StringFormat |
adventure()
|
static StringFormat |
adventure(char colorChar)
Create a new
AdventureRgbStringFormat with the given color character. |
static StringFormat |
ansi()
Create a new
AnsiStringFormat with true color enabled. |
static StringFormat |
ansi(boolean trueColor)
Create a new
AnsiStringFormat with the given true color parameter. |
static StringFormat |
bungee()
|
static StringFormat |
bungee(char colorChar)
Create a new
BungeeRgbStringFormat with the given color character. |
boolean |
canRead(TextStringReader reader)
Check if the reader can read the color character of this format.
It is recommended to check this before calling matches(TextStringReader). |
abstract boolean |
canWrite(TextFormatting formatting)
Check if this format can write the given text formatting.
|
java.lang.String |
convertTo(java.lang.String s,
StringFormat target)
Convert a string from one format to another.
Some string formats may require a reset code at the end of the string. |
TextComponent |
fromString(java.lang.String s,
ColorHandling colorHandling,
DeserializerUnknownHandling unknownHandling)
Convert a string to a
TextComponent.The color handling is used to determine if color codes should be treated as reset codes. The unknown handling is used to determine what should happen if an unsupported formatting is encountered. The string will be split into multiple components if multiple formatting codes are present. Empty sections will be ignored. |
TextComponent |
fromString(java.lang.String s,
ColorHandling colorHandling,
DeserializerUnknownHandling unknownHandling,
boolean ignoreEmptySections)
Convert a string to a
TextComponent.The color handling is used to determine if color codes should be treated as reset codes. The unknown handling is used to determine what should happen if an unsupported formatting is encountered. The string will be split into multiple components if multiple formatting codes are present. Empty sections are ignored if ignoreEmptySections is true, otherwise they are included as empty components. |
protected static <T> T |
init(T t,
java.util.function.Consumer<T> initializer) |
abstract boolean |
matches(TextStringReader reader)
Check if the reader matches the format of this string format.
This method is allowed to modify the current position of the reader. |
java.lang.String |
prependStyle(java.lang.String s,
Style style,
SerializerUnknownHandling unknownHandling)
Prepend a style to the given string.
This only works with formatting codes (colors, bold, italic, etc.). If the style contains an unknown formatting code the SerializerUnknownHandling will be used to resolve it. |
abstract TextFormatting |
read(TextStringReader reader)
Read the next formatting code from the reader.
The reader should be at the start of the formatting code when this method is called. |
ResolvedFormatting |
resolve(TextStringReader reader)
Resolve the next formatting code in the string.
null will be returned if no formatting code is at the current position.If the formatting code is invalid the ResolvedFormatting.get() method will return null.This is a convenience method which combines canRead(TextStringReader), matches(TextStringReader) and read(TextStringReader). |
boolean |
shouldResetAtEnd() |
java.lang.String[] |
split(java.lang.String s,
java.lang.String split,
ColorHandling colorHandling,
SerializerUnknownHandling serializerUnknownHandling,
DeserializerUnknownHandling deserializerUnknownHandling)
Split a string by a given separator and keep the formatting of the previous part.
The color handling is used to determine if color codes should be treated as reset codes. The unknown handling is used to determine what should happen if an unsupported formatting is encountered. |
Style |
styleAt(java.lang.String s,
int index,
ColorHandling colorHandling,
DeserializerUnknownHandling unknownHandling)
Get the style of a string at the given index.
If the index is smaller or equal to 0 the style will be empty. If the index is greater than the length of the string the last style will be returned. If the index is in the middle of a formatting code the index will be moved to the end of the formatting code. The color handling is used to determine if color codes should be treated as reset codes. The unknown handling is used to determine what should happen if an unsupported formatting is encountered. |
java.lang.String |
toString(TextComponent component,
ColorHandling colorHandling,
SerializerUnknownHandling unknownHandling)
Convert a
TextComponent to a string.The unknown handling is used to determine what should happen if an unsupported formatting is encountered. A reset code will be added at the start of every new section if the previous section had any formattings. Some string formats may require a reset code at the end of the string. |
java.lang.String |
toString(TextComponent component,
ColorHandling colorHandling,
SerializerUnknownHandling unknownHandling,
boolean ignoreEmptyComponents)
Convert a
TextComponent to a string.The unknown handling is used to determine what should happen if an unsupported formatting is encountered. A reset code will be added at the start of every new section if the previous section had any formattings. Some string formats may require a reset code at the end of the string. |
static StringFormat |
vanilla()
Create a new
VanillaStringFormat.The color character is set to '§' and rgb down sampling is disabled. |
static StringFormat |
vanilla(boolean downsampleRgbColors)
Create a new
VanillaStringFormat with the given down sampling parameter.The color character is set to '§'. |
static StringFormat |
vanilla(char colorChar)
Create a new
VanillaStringFormat with the given color character.The downsampleRgbColors parameter is set to false. |
static StringFormat |
vanilla(char colorChar,
boolean downsampleRgbColors)
Create a new
VanillaStringFormat with the given color character and down sampling parameter. |
abstract void |
write(java.lang.StringBuilder builder,
TextFormatting formatting)
Write the given text formatting to the builder.
This method should only be called if canWrite(TextFormatting) returns true. |
public static StringFormat vanilla()
VanillaStringFormat.'§' and rgb down sampling is disabled.VanillaStringFormatpublic static StringFormat vanilla(char colorChar)
VanillaStringFormat with the given color character.downsampleRgbColors parameter is set to false.colorChar - The color characterVanillaStringFormatpublic static StringFormat vanilla(boolean downsampleRgbColors)
VanillaStringFormat with the given down sampling parameter.'§'.downsampleRgbColors - Downsample rgb colors to the closest vanilla colorVanillaStringFormatpublic static StringFormat vanilla(char colorChar, boolean downsampleRgbColors)
VanillaStringFormat with the given color character and down sampling parameter.colorChar - The color characterdownsampleRgbColors - Downsample rgb colors to the closest vanilla colorVanillaStringFormatpublic static StringFormat bungee()
BungeeRgbStringFormatpublic static StringFormat bungee(char colorChar)
BungeeRgbStringFormat with the given color character.colorChar - The color characterBungeeRgbStringFormatpublic static StringFormat adventure()
AdventureRgbStringFormatpublic static StringFormat adventure(char colorChar)
AdventureRgbStringFormat with the given color character.colorChar - The color characterAdventureRgbStringFormatpublic static StringFormat ansi()
AnsiStringFormat with true color enabled.public static StringFormat ansi(boolean trueColor)
AnsiStringFormat with the given true color parameter.trueColor - If true color should be enabledpublic TextComponent fromString(java.lang.String s, ColorHandling colorHandling, DeserializerUnknownHandling unknownHandling)
TextComponent.s - The string to convertcolorHandling - The color handlingunknownHandling - The unknown handlingpublic TextComponent fromString(java.lang.String s, ColorHandling colorHandling, DeserializerUnknownHandling unknownHandling, boolean ignoreEmptySections)
TextComponent.ignoreEmptySections is true, otherwise they are included as empty components.s - The string to convertcolorHandling - The color handlingunknownHandling - The unknown handlingpublic java.lang.String toString(TextComponent component, ColorHandling colorHandling, SerializerUnknownHandling unknownHandling)
TextComponent to a string.shouldResetAtEnd() return true and the last section had any formattings.component - The component to convertcolorHandling - The color handlingunknownHandling - The unknown handlingpublic java.lang.String toString(TextComponent component, ColorHandling colorHandling, SerializerUnknownHandling unknownHandling, boolean ignoreEmptyComponents)
TextComponent to a string.shouldResetAtEnd() return true and the last section had any formattings.ignoreEmptyComponents is true, otherwise their style will be applied (and immediately overwritten by the next component's style).component - The component to convertcolorHandling - The color handlingunknownHandling - The unknown handlingpublic Style styleAt(java.lang.String s, int index, ColorHandling colorHandling, DeserializerUnknownHandling unknownHandling)
s - The string to get the style fromindex - The index to get the style atcolorHandling - The color handlingunknownHandling - The unknown handlingpublic java.lang.String convertTo(java.lang.String s,
StringFormat target)
shouldResetAtEnd() return true and the last section had any formattings.s - The string in the current formattarget - The target formatpublic java.lang.String prependStyle(java.lang.String s,
Style style,
SerializerUnknownHandling unknownHandling)
SerializerUnknownHandling will be used to resolve it.s - The string to append the style tostyle - The style to appendunknownHandling - The unknown handlingpublic java.lang.String[] split(java.lang.String s,
java.lang.String split,
ColorHandling colorHandling,
SerializerUnknownHandling serializerUnknownHandling,
DeserializerUnknownHandling deserializerUnknownHandling)
s - The string to splitsplit - The split stringcolorHandling - The color handling (for styleAt(String, int, ColorHandling, DeserializerUnknownHandling))serializerUnknownHandling - The serializer unknown handling (for prependStyle(String, Style, SerializerUnknownHandling))deserializerUnknownHandling - The deserializer unknown handling (for styleAt(String, int, ColorHandling, DeserializerUnknownHandling))@Nullable public ResolvedFormatting resolve(TextStringReader reader)
null will be returned if no formatting code is at the current position.ResolvedFormatting.get() method will return null.canRead(TextStringReader), matches(TextStringReader) and read(TextStringReader).reader - The reader to read fromnull if no formatting code was foundpublic boolean canRead(TextStringReader reader)
matches(TextStringReader).reader - The reader to checkpublic abstract boolean matches(TextStringReader reader)
TextStringReader.mark() and TextStringReader.reset().reader - The reader to check@Nullable public abstract TextFormatting read(TextStringReader reader)
matches(TextStringReader) before calling this method.null should be returned.reader - The reader to read fromnull if the formatting code is invalidpublic abstract boolean canWrite(TextFormatting formatting)
formatting - The formatting to checkpublic abstract void write(java.lang.StringBuilder builder,
TextFormatting formatting)
canWrite(TextFormatting) returns true.builder - The builder to write toformatting - The formatting to writepublic boolean shouldResetAtEnd()
protected static <T> T init(T t,
java.util.function.Consumer<T> initializer)