Package tk.airshipcraft.commonlib.utils
Class TextFormatting
java.lang.Object
tk.airshipcraft.commonlib.utils.TextFormatting
Utility class for text formatting in Minecraft, providing methods for color parsing, duration formatting, and string manipulation.
This class offers a variety of static methods to handle common text formatting tasks such as coloring text, formatting time durations, and various string utilities.
- Since:
- 2023-04-02
- Version:
- 1.0.0
- Author:
- notzune
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
formatDuration
(long time) Formats a duration in milliseconds into a human-readable string.static String
formatDuration
(long time, TimeUnit unit) Formats a duration in a specified time unit into a human-readable string.Retrieves a specific page of text from a larger list, formatted for display.static String
Concatenates the elements of a list into a single string with a specified delimiter.static String
implodeCommaAnd
(List<String> list) Concatenates the elements of a list into a single string, using a comma as a delimiter, and "and" before the last element.static String
implodeCommaAnd
(List<String> list, String comma, String and) Concatenates the elements of a list into a single string, using a comma as a delimiter, and "and" before the last element.static boolean
isNullOrEmpty
(net.kyori.adventure.text.Component component) Determines whether a given base component is null or empty.static void
Deprecated.static void
Deprecated.static String
Parses a string for color codes.static String
Parses a string with formatting and arguments.static String
parseColor
(String string) Parses a string for Minecraft color codes.static String
parseColorAcc
(String string) Replaces specific accent color codes within a string with their corresponding ChatColor values.static String
parseColorAmp
(String string) Converts ampersand color codes to Minecraft color codes.static String
parseColorTags
(String string) Replaces color tag placeholders within a string with their corresponding ChatColor values.static String
Deprecated.static String
Repeats the given string a specified number of times.static boolean
startsWith
(String container, String contained) Determines if one string starts with another.static boolean
stringEquals
(String former, String latter) Determines if two strings match in value.static boolean
stringEqualsIgnoreCase
(String former, String latter) Determines if two strings match regardless of case.static String
Creates a title string with a specific format including color codes and padding.static String
Creates a title string with a specific color code, format including color codes, and padding.static String
upperCaseFirst
(String string) Capitalizes the first letter of the provided string.
-
Constructor Details
-
TextFormatting
public TextFormatting()
-
-
Method Details
-
formatDuration
Formats a duration in milliseconds into a human-readable string.- Parameters:
time
- Duration in milliseconds.- Returns:
- Formatted duration as a string.
-
formatDuration
Formats a duration in a specified time unit into a human-readable string.- Parameters:
time
- Duration.unit
- TimeUnit of the duration.- Returns:
- Formatted duration as a string.
-
parse
Parses a string with formatting and arguments.- Parameters:
str
- The string to be formatted.args
- Arguments for formatting.- Returns:
- The formatted string.
-
parse
Parses a string for color codes.- Parameters:
str
- The string to be parsed.- Returns:
- The string with color codes parsed.
-
parseColor
Parses a string for Minecraft color codes.- Parameters:
string
- The string to be parsed.- Returns:
- The parsed string with Minecraft color codes.
-
parseColorAmp
Converts ampersand color codes to Minecraft color codes.- Parameters:
string
- The string to be converted.- Returns:
- The string with converted color codes.
-
parseColorAcc
Replaces specific accent color codes within a string with their corresponding ChatColor values. This method is used to parse strings containing custom accent color codes (`0, `1, `2, etc.) and replace them with the color codes used by the ChatColor class.- Parameters:
string
- The string to be parsed.- Returns:
- The string with custom accent color codes replaced by ChatColor color codes.
-
parseColorTags
Replaces color tag placeholders within a string with their corresponding ChatColor values. This method is used to parse strings containing color tags(<black>, <red>, etc.)
and replace them with the color codes provided by the ChatColor class.- Parameters:
string
- The string to be parsed.- Returns:
- The string with color tag placeholders replaced by ChatColor color codes.
-
parseTags
Deprecated.This method is deprecated and replaced byparseColorTags(String)
.- Parameters:
sting
- The string to be parsed.- Returns:
- The string with color tags parsed.
-
upperCaseFirst
Capitalizes the first letter of the provided string.- Parameters:
string
- The string to capitalize.- Returns:
- The string with the first character converted to uppercase.
- Throws:
IllegalArgumentException
- If the input string is null.
-
repeat
Repeats the given string a specified number of times.- Parameters:
string
- The string to repeat.times
- The number of times to repeat the string.- Returns:
- A new string composed of the input string repeated the specified number of times.
- Throws:
IllegalArgumentException
- If the input string is null or times is less than 1.
-
implode
Concatenates the elements of a list into a single string with a specified delimiter.- Parameters:
list
- The list of strings to concatenate.glue
- The delimiter to insert between each element.- Returns:
- A string resulting from concatenating the elements of the list, separated by the given delimiter.
- Throws:
IllegalArgumentException
- If the input list or delimiter is null.
-
implodeCommaAnd
Concatenates the elements of a list into a single string, using a comma as a delimiter, and "and" before the last element. Intended for human-readable lists.- Parameters:
list
- The list of strings to concatenate.comma
- The delimiter to use between elements, typically a comma.and
- The string to use before the last element, typically "and".- Returns:
- A human-readable string representing the list.
-
implodeCommaAnd
Concatenates the elements of a list into a single string, using a comma as a delimiter, and "and" before the last element. This is a convenience method that uses default delimiters.- Parameters:
list
- The list of strings to concatenate.- Returns:
- A human-readable string representing the list with default delimiters.
-
titleize
Creates a title string with a specific format including color codes and padding. This method is typically used for stylizing titles in text-based UIs where the title is centered within a line of decorative characters.- Parameters:
str
- The string to be stylized as a title.- Returns:
- The titleized string with color and format applied.
-
titleize
Creates a title string with a specific color code, format including color codes, and padding. This method allows for custom color codes to be applied to the title.- Parameters:
colorCode
- The color code to apply to the title.str
- The string to be stylized as a title.- Returns:
- The titleized string with the specified color and format applied.
-
getPage
Retrieves a specific page of text from a larger list, formatted for display. This method is used for paginating text for easier reading and display in a UI, where each page contains a subset of the total lines.- Parameters:
lines
- The list of strings to paginate.pageHumanBased
- The human-readable page number (1-based index).title
- The title to be displayed at the top of the page.- Returns:
- A list of strings representing the page of text.
-
msg
Deprecated.Static utility method for easily sending formatting strings to players.- Parameters:
player
- The playermessage
- The message
-
msg
Deprecated.Static utility method for easily sending formatting strings to players.- Parameters:
player
- The playermessage
- The messageargs
- Additional arguments which are used later in a String.format()
-
stringEquals
Determines if two strings match in value. This has been created to avoid those times when you want to check for equality without having to do null checks yourself, such as after pulling a value from a map or list.- Parameters:
former
- The first value.latter
- The second value.- Returns:
- Returns true if the first and second values match.
-
stringEqualsIgnoreCase
Determines if two strings match regardless of case. This has been created to avoid those times when you want to check for equality without having to do null checks yourself, such as after pulling a value from a map or list.- Parameters:
former
- The first value.latter
- The second value.- Returns:
- Returns true if the first and second values match regardless of case.
-
startsWith
Determines if one string starts with another.- Parameters:
container
- The string to check the start of.contained
- The string to search for.- Returns:
- Returns true if the contained is contained within the container.
-
isNullOrEmpty
public static boolean isNullOrEmpty(net.kyori.adventure.text.Component component) Determines whether a given base component is null or empty.
This is determined by converting the component into plain text, so a non-null component filled with nothing but colour codes and hover text will likely return true.
- Parameters:
component
- The component to test if null or empty.- Returns:
- Returns true if the component is null or has no visible content.
-