Class TextFormatting

java.lang.Object
tk.airshipcraft.commonlib.utils.TextFormatting

public class TextFormatting extends Object

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 Details

    • TextFormatting

      public TextFormatting()
  • Method Details

    • formatDuration

      public static String formatDuration(long time)
      Formats a duration in milliseconds into a human-readable string.
      Parameters:
      time - Duration in milliseconds.
      Returns:
      Formatted duration as a string.
    • formatDuration

      public static String formatDuration(long time, TimeUnit unit)
      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

      public static String parse(String str, Object... args)
      Parses a string with formatting and arguments.
      Parameters:
      str - The string to be formatted.
      args - Arguments for formatting.
      Returns:
      The formatted string.
    • parse

      public static String parse(String str)
      Parses a string for color codes.
      Parameters:
      str - The string to be parsed.
      Returns:
      The string with color codes parsed.
    • parseColor

      public static String parseColor(String string)
      Parses a string for Minecraft color codes.
      Parameters:
      string - The string to be parsed.
      Returns:
      The parsed string with Minecraft color codes.
    • parseColorAmp

      public static String parseColorAmp(String string)
      Converts ampersand color codes to Minecraft color codes.
      Parameters:
      string - The string to be converted.
      Returns:
      The string with converted color codes.
    • parseColorAcc

      public static String parseColorAcc(String string)
      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

      public static String parseColorTags(String string)
      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 public static String parseTags(String sting)
      Deprecated.
      This method is deprecated and replaced by parseColorTags(String) .
      Parameters:
      sting - The string to be parsed.
      Returns:
      The string with color tags parsed.
    • upperCaseFirst

      public static String upperCaseFirst(String string)
      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

      public static String repeat(String string, int times)
      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

      public static String implode(List<String> list, String glue)
      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

      public 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. 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

      public 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. 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

      public static String titleize(String str)
      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

      public static String titleize(String colorCode, String str)
      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

      public static ArrayList<String> getPage(List<String> lines, int pageHumanBased, String title)
      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 public static void msg(org.bukkit.entity.Player player, String message)
      Deprecated.
      Static utility method for easily sending formatting strings to players.
      Parameters:
      player - The player
      message - The message
    • msg

      @Deprecated public static void msg(org.bukkit.entity.Player player, String message, Object... args)
      Deprecated.
      Static utility method for easily sending formatting strings to players.
      Parameters:
      player - The player
      message - The message
      args - Additional arguments which are used later in a String.format()
    • stringEquals

      public static boolean stringEquals(String former, String latter)
      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

      public static boolean stringEqualsIgnoreCase(String former, String latter)
      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

      public static boolean startsWith(String container, String contained)
      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.