Package net.lucypoulton.squirtgun.format
Class TextFormatter
java.lang.Object
net.lucypoulton.squirtgun.format.TextFormatter
Formats text, supporting gradients and hex codes.
This was inspired by IridiumColorAPI and CMI
https://github.com/Iridium-Development/IridiumColorAPI
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic net.kyori.adventure.text.ComponentapplyLegacyDecorations(@NotNull net.kyori.adventure.text.Component in, @Nullable String decorations) Apply a string of legacy decoration codes to a component.static net.kyori.adventure.text.ComponentcentreText(@NotNull String in, @NotNull FormatProvider format, @NotNull String character) Inserts characters either side of an input string to centre-align it in chat.static net.kyori.adventure.text.ComponentcentreText(@NotNull String in, @NotNull FormatProvider format, @NotNull String character, @NotNull net.kyori.adventure.text.format.TextDecoration[] formatters) Inserts characters either side of an input string to centre-align it in chat.static @Nullable net.kyori.adventure.text.format.TextColorcolourFromText(@NotNull String in) Parse a string colour representation to aTextColor.static int[]fade(int count, int val1, int val2) Create an array of evenly-spaced integers between two values.static net.kyori.adventure.text.ComponentParses a string to a set of coloured components, calculating gradients.static net.kyori.adventure.text.ComponentParses a string to a set of coloured components, calculating gradients.static net.kyori.adventure.text.ComponentformatTitle(@NotNull String in, @NotNull FormatProvider format) Deprecated.static net.kyori.adventure.text.Componentinvert(net.kyori.adventure.text.Component component) Inverts a component, flipping the extra components and adding the original component on the end.static StringDeprecated.
-
Constructor Details
-
TextFormatter
public TextFormatter()
-
-
Method Details
-
repeat
@Contract(pure=true) @Deprecated public static String repeat(@NotNull @NotNull String charToRepeat, int count) Deprecated.Repeats a character to create a string.- Parameters:
charToRepeat- the character to repeatcount- the amount of times to repeat it
-
invert
@Contract(pure=true) public static net.kyori.adventure.text.Component invert(net.kyori.adventure.text.Component component) Inverts a component, flipping the extra components and adding the original component on the end. -
formatTitle
@Contract(pure=true) @Deprecated public static net.kyori.adventure.text.Component formatTitle(@NotNull @NotNull String in, @NotNull @NotNull FormatProvider format) Deprecated.Creates a centre-aligned title bar for use in commands. Input is formatted using the main colour, the bars either side are formatted using the accent colour.- Parameters:
in- the text to use as a titleformat- the format to apply to the string- Returns:
- a formatted string, ready to send to the player
-
centreText
@Contract(pure=true) public static net.kyori.adventure.text.Component centreText(@NotNull @NotNull String in, @NotNull @NotNull FormatProvider format, @NotNull @NotNull String character) Inserts characters either side of an input string to centre-align it in chat.- Parameters:
in- the string to centreformat- what to format the input with. Centred text is main-formatted, edges are accent-formattedcharacter- the character to repeat to centre-align the text- Returns:
- a formatted string containing the centred text
-
centreText
@Contract(pure=true) public static net.kyori.adventure.text.Component centreText(@NotNull @NotNull String in, @NotNull @NotNull FormatProvider format, @NotNull @NotNull String character, @NotNull @NotNull net.kyori.adventure.text.format.TextDecoration[] formatters) Inserts characters either side of an input string to centre-align it in chat.- Parameters:
in- the string to centreformat- what to format the input with. Centred text is main-formatted, edges are accent-fornattedcharacter- the character to repeat to centre-align the textformatters- a list of vanilla formatter codes to apply to the edges- Returns:
- a formatted string containing the centred text
-
colourFromText
@Contract(pure=true) @Nullable public static @Nullable net.kyori.adventure.text.format.TextColor colourFromText(@NotNull @NotNull String in) Parse a string colour representation to aTextColor.- Parameters:
in- a string representation, either as:- a single character as in a standard formatter code
- a 6-digit HTML hex code, prepended with # ie #ff00ff
- Returns:
- the ChatColor representation, or null if it could not be parsed
-
format
@Contract(pure=true) public static net.kyori.adventure.text.Component format(@NotNull @NotNull String input) Parses a string to a set of coloured components, calculating gradients. By default, any section-coded legacy colours in the string will be removed. Ampersand-coded colours will be parsed into components. This behaviour can be changed withformat(String, String, boolean)- Parameters:
input- the string to parse. encoded with a sort of markup language:- &ctext - vanilla format codes, using & as per bukkit conventions
- {#FFFFFF}text - static HTML code, similar to vanilla codes
- {#FFFFFF>}text{#000000<} - RGB gradient between the two specified HTML codes
- {hsv:FF0000>}text{FF<} - HSV gradient. The hex value in the start tag is not an HTML code - the first two characters are hue, next two are saturation, last two are value. In the closing tag the value is the finishing hue. All values are in the range 0-255 (00-FF) inclusive.
- {flag:some-kind-of-flag>}text{flag<} - blocked flags. Still WIP, more info to come Soon™. Currently supports miscellaneous common pride flags.
Gradient formats support extra format tags, as a list of vanilla characters following a colon. For example, a gradient from #FFFFFF, in bold and italic, would start {#FFFFFF:lo}>.
Tags should not be nested - doing so will result in undefined behaviour.
- Returns:
- a component containing the parsed code.
-
format
@Contract(pure=true) public static net.kyori.adventure.text.Component format(@NotNull @NotNull String input, @Nullable @Nullable String overrides, boolean usePredefinedFormatters) Parses a string to a set of coloured components, calculating gradients.- Parameters:
input- as forformat(String)overrides- a string of vanilla formatters to add to the textusePredefinedFormatters- whether to use ยง-prefixed codes. if true then they will take priority over LCL formatters, if false then they will be removed prior to formatting- Returns:
- the formatted text
-
fade
@Contract(pure=true) public static int[] fade(int count, int val1, int val2) Create an array of evenly-spaced integers between two values. Values are calculated as floats and are then rounded.- Parameters:
count- the length of the returned arrayval1- the value to start fromval2- the value to end at- Returns:
- an integer array of length count
-
applyLegacyDecorations
@Contract(pure=true) public static net.kyori.adventure.text.Component applyLegacyDecorations(@NotNull @NotNull net.kyori.adventure.text.Component in, @Nullable @Nullable String decorations) Apply a string of legacy decoration codes to a component.- Parameters:
in- the component to apply decorations todecorations- a list of legacy decorations to apply ie "lo" will apply bold and italic- Throws:
IllegalArgumentException- if a decoration provided is invalid
-
FormatProvider.formatTitle(String)