Class Text

java.lang.Object
dev.demeng.pluginbase.text.Text

public final class Text extends Object
Message-related utilities, including console and chat messages.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Separation line for players (in-game chat).
    static final String
    Separation line for console.
    static final Pattern
    Pattern to match our HEX color format for MC 1.16+.
    static final Pattern
    Pattern to match our localized placeholders.
    static final net.kyori.adventure.text.minimessage.MiniMessage
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    broadcast(@Nullable String permission, @Nullable String str)
    Broadcasts the message after coloring and formatting it.
    static void
    broadcastRaw(@Nullable String permission, @Nullable String str)
    Broadcasts a colorized message without the prefix.
    static @NotNull String
    capitalizeFirst(@NotNull String str)
    Makes the first character uppercase and the rest lowercase.
    static void
    clearTitle(@NotNull org.bukkit.entity.Player player)
    Clears the current title and subtitle of the player.
    static @NotNull String
    colorize(@Nullable String str)
    Converts plain string with color codes into a colorized message.
    static @NotNull List<String>
    colorize(@Nullable List<String> strList)
    Colorizes a list of plain strings.
    static void
    console(@Nullable String str)
    Sends a formatted console message.
    static void
    consoleRaw(@Nullable String str)
    Sends a colorized console message without the prefix.
    static @NotNull String
    error(@Nullable String str)
    Appends the prefix, a red chat color, and the message, and then colorizes.
    static @NotNull String
    format(@Nullable String str)
    Appends the prefix, and then colorizes.
    static @NotNull Locale
    getLocale(@Nullable org.bukkit.command.CommandSender sender)
    Gets the locale for the specified console or player.
    static @NotNull String
    Convenience method to get the current prefix of the plugin.
    static @NotNull String
    legacyParseMini(@Nullable String str)
    Parses the string using the MiniMessage library, and then uses the legacy Bukkit Component Serializer to return a String rather than a Component.
    static @NotNull String
    legacySerialize(@NotNull net.kyori.adventure.text.Component component)
    Serializes an Adventure Component using the legacy Bukkit Component Serializer, which can be useful for displaying components in areas other than the chat (ex.
    static @NotNull String
    line(@NotNull org.bukkit.command.CommandSender sender)
    Gets either CHAT_LINE or CONSOLE_LINE, depending on if the CommandSender is a player or console.
    static @NotNull String
    localized(@Nullable String key, @Nullable Locale locale, @NotNull Object... args)
    Gets the localized string with the given key using the provided locale.
    static @NotNull String
    localized(@Nullable String key, @Nullable org.bukkit.command.CommandSender sender, @NotNull Object... args)
    Gets the localized string with the given key using the provided sender's locale.
    static @NotNull String
    localizedDef(@Nullable String key, @NotNull Object... args)
    Gets the localized string with the given key using the default locale.
    static @NotNull String
    localizedOrDefault(@Nullable org.bukkit.command.CommandSender sender, @Nullable String key, @NotNull String defaultMessage, @NotNull Object... args)
    Gets a localized error message with fallback to a default message.
    static @NotNull String
    localizePlaceholders(@Nullable String str, @Nullable Locale locale, @NotNull Object... args)
    Localizes the placeholders in the string using the sender's locale.
    static @NotNull String
    localizePlaceholders(@Nullable String str, @Nullable org.bukkit.command.CommandSender sender, @NotNull Object... args)
     
    static @NotNull String
    localizePlaceholdersDef(@Nullable String str, @NotNull Object... args)
    Localizes the placeholders in the string using the default locale.
    static void
    log(@Nullable String str)
    Logs a plain message into the console.
    static void
    log(Level level, String str)
    Logs a plain message into the console.
    static @NotNull net.kyori.adventure.text.Component
    parseMini(@Nullable String str)
    Parses the string using the MiniMessage library.
    static void
    sendTitle(@NotNull org.bukkit.entity.Player player, @Nullable String title, @Nullable String subtitle)
    Sends the title to the player.
    static void
    sendTitle(@NotNull org.bukkit.entity.Player player, @Nullable String title, @Nullable String subtitle, int fadeIn, int stay, int fadeOut)
    Sends the title to the player.
    static @NotNull List<String>
    splitLines(@Nullable String str)
    Creates a list of lines from a string line-broken with \n.
    static @NotNull String
    strip(@Nullable String str)
    Fully strip all color from the string.
    static @NotNull List<String>
    strip(@Nullable List<String> strList)
    Fully strip all color from strings.
    static void
    tell(@NotNull org.bukkit.command.CommandSender sender, @Nullable String str)
    Sends a colored and prefixed message to the command sender.
    static void
    tellCentered(@NotNull org.bukkit.entity.Player player, @Nullable String str)
    Sends a colored and centered message.
    static void
    tellComponent(@NotNull org.bukkit.entity.Player player, @NotNull net.kyori.adventure.text.Component component)
    Sends the Component to the player as a chat message.
    static void
    tellLocalized(@NotNull org.bukkit.command.CommandSender sender, @Nullable String key, Object... args)
    Sends a localized(String, CommandSender, Object...), colored, and prefixed message to the command sender.
    static void
    tellLocalizedRaw(@NotNull org.bukkit.command.CommandSender sender, @Nullable String key, Object... args)
    Sends a localized, colorized message without the prefix.
    static void
    tellRaw(@NotNull org.bukkit.command.CommandSender sender, @Nullable String str)
    Sends a colorized message without the prefix.
    static @NotNull String
    titleCase(@NotNull String str, @NotNull String delimiter)
    Makes a string Title Case.
    static @NotNull String
    tl(@Nullable String key, @Nullable Locale locale, @NotNull Object... args)
    static @NotNull String
    tl(@Nullable String key, @Nullable org.bukkit.command.CommandSender sender, @NotNull Object... args)

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HEX_PATTERN

      public static final Pattern HEX_PATTERN
      Pattern to match our HEX color format for MC 1.16+.
    • LOCALIZED_PLACEHOLDER_PATTERN

      public static final Pattern LOCALIZED_PLACEHOLDER_PATTERN
      Pattern to match our localized placeholders.
    • CHAT_LINE

      public static final String CHAT_LINE
      Separation line for players (in-game chat).
      See Also:
    • CONSOLE_LINE

      public static final String CONSOLE_LINE
      Separation line for console.
      See Also:
    • MINI_MESSAGE

      public static final net.kyori.adventure.text.minimessage.MiniMessage MINI_MESSAGE
  • Constructor Details

    • Text

      public Text()
  • Method Details

    • getLocale

      @NotNull public static @NotNull Locale getLocale(@Nullable @Nullable org.bukkit.command.CommandSender sender)
      Gets the locale for the specified console or player. If the player's locale cannot be resolved, the sender is a console, or the sender is null, the default locale is returned.
      Parameters:
      sender - The sender to get the locale of
      Returns:
      The sender's locale or the default locale if the sender's locale could not be resolved
    • localized

      @NotNull public static @NotNull String localized(@Nullable @Nullable String key, @Nullable @Nullable org.bukkit.command.CommandSender sender, @NotNull @NotNull Object... args)
      Gets the localized string with the given key using the provided sender's locale. Uses the default locale if the localized string could not be resolved with the provided sender's locale.

      Uses MessageFormat internally for argument substitution. Single quotes in translation values must be doubled ('') to appear literally, and curly braces used as literal text (not placeholders) must be quoted: '{literal}'.

      Parameters:
      key - The key of the message
      sender - The sender to get the locale of
      args - Arguments for positioned placeholders ({n})
      Returns:
      The localized message, or key if unable to resolve
      See Also:
    • localized

      @NotNull public static @NotNull String localized(@Nullable @Nullable String key, @Nullable @Nullable Locale locale, @NotNull @NotNull Object... args)
      Gets the localized string with the given key using the provided locale. Uses the default locale if the localized string could not be resolved with the provided locale.

      Uses MessageFormat internally for argument substitution. Single quotes in translation values must be doubled ('') to appear literally, and curly braces used as literal text (not placeholders) must be quoted: '{literal}'.

      Parameters:
      key - The key of the message
      locale - The locale
      args - Arguments for positioned placeholders ({n})
      Returns:
      The localized message, or key if unable to resolve
    • localizedDef

      @NotNull public static @NotNull String localizedDef(@Nullable @Nullable String key, @NotNull @NotNull Object... args)
      Gets the localized string with the given key using the default locale.

      Uses MessageFormat internally for argument substitution. Single quotes in translation values must be doubled ('') to appear literally.

      Parameters:
      key - The key of the message
      args - Arguments for positioned placeholders ({n})
      Returns:
      The localized message, or key if unable to resolve
    • localizedOrDefault

      @NotNull public static @NotNull String localizedOrDefault(@Nullable @Nullable org.bukkit.command.CommandSender sender, @Nullable @Nullable String key, @NotNull @NotNull String defaultMessage, @NotNull @NotNull Object... args)
      Gets a localized error message with fallback to a default message. If the localization key cannot be resolved (returns the key itself), the default message is used instead.
      Parameters:
      sender - The command sender to get the locale of
      key - The localization key
      defaultMessage - The default message if localization is not available
      args - Arguments for placeholders in the localized message
      Returns:
      The localized or default message (not colorized)
    • tl

      @NotNull public static @NotNull String tl(@Nullable @Nullable String key, @Nullable @Nullable org.bukkit.command.CommandSender sender, @NotNull @NotNull Object... args)
      See Also:
    • tl

      @NotNull public static @NotNull String tl(@Nullable @Nullable String key, @Nullable @Nullable Locale locale, @NotNull @NotNull Object... args)
      See Also:
    • localizePlaceholders

      @NotNull public static @NotNull String localizePlaceholders(@Nullable @Nullable String str, @Nullable @Nullable Locale locale, @NotNull @NotNull Object... args)
      Localizes the placeholders in the string using the sender's locale. Note that the same arguments are used for EVERY placeholder.

      Each resolved placeholder is processed through MessageFormat. Single quotes in translation values must be doubled ('') to appear literally.

      Parameters:
      str - The string to localize
      locale - The locale to use
      args - The arguments to replace in the localized string
      Returns:
      The localized string
    • localizePlaceholders

      @NotNull public static @NotNull String localizePlaceholders(@Nullable @Nullable String str, @Nullable @Nullable org.bukkit.command.CommandSender sender, @NotNull @NotNull Object... args)
    • localizePlaceholdersDef

      @NotNull public static @NotNull String localizePlaceholdersDef(@Nullable @Nullable String str, @NotNull @NotNull Object... args)
      Localizes the placeholders in the string using the default locale. Note that the same arguments are used for EVERY placeholder.
      Parameters:
      str - The string to localize
      args - The arguments to replace in the localized string
      Returns:
      The localized string
    • getPrefix

      @NotNull public static @NotNull String getPrefix()
      Convenience method to get the current prefix of the plugin.
      Returns:
      The prefix
    • line

      @NotNull public static @NotNull String line(@NotNull @NotNull org.bukkit.command.CommandSender sender)
      Gets either CHAT_LINE or CONSOLE_LINE, depending on if the CommandSender is a player or console.
      Parameters:
      sender - The command sender
      Returns:
      The separation line
    • colorize

      @NotNull public static @NotNull String colorize(@Nullable @Nullable String str)
      Converts plain string with color codes into a colorized message. Supports HEX colors in the format of <#HEX>. 1.16+ HEX support requires the server software to be Spigot, or a fork of Spigot.
      Parameters:
      str - The plain string
      Returns:
      Colorized string, or empty if the provided string is null
    • colorize

      @NotNull public static @NotNull List<String> colorize(@Nullable @Nullable List<String> strList)
      Colorizes a list of plain strings.
      Parameters:
      strList - The plain strings
      Returns:
      Colorized strings, or an empty collection if the provided list is null
      See Also:
    • format

      @NotNull public static @NotNull String format(@Nullable @Nullable String str)
      Appends the prefix, and then colorizes.
      Parameters:
      str - The plain, non-prefixed string
      Returns:
      Formatted string, or empty if the provided string is null
    • error

      @NotNull public static @NotNull String error(@Nullable @Nullable String str)
      Appends the prefix, a red chat color, and the message, and then colorizes.
      Parameters:
      str - The plain string
      Returns:
      Formatted string with red message
    • parseMini

      @NotNull public static @NotNull net.kyori.adventure.text.Component parseMini(@Nullable @Nullable String str)
      Parses the string using the MiniMessage library. Format: ...
      Parameters:
      str - The raw string
      Returns:
      The result component for the string, or empty if the provided string is null
    • legacyParseMini

      @NotNull public static @NotNull String legacyParseMini(@Nullable @Nullable String str)
      Parses the string using the MiniMessage library, and then uses the legacy Bukkit Component Serializer to return a String rather than a Component. Format: ...
      Parameters:
      str - The raw string(s)
      Returns:
      The serialized component for the string, or empty if the provided string is null
    • legacySerialize

      @NotNull public static @NotNull String legacySerialize(@NotNull @NotNull net.kyori.adventure.text.Component component)
      Serializes an Adventure Component using the legacy Bukkit Component Serializer, which can be useful for displaying components in areas other than the chat (ex. item names).
      Parameters:
      component - The component to serialize
      Returns:
      The serialized component
      See Also:
    • strip

      @NotNull public static @NotNull String strip(@Nullable @Nullable String str)
      Fully strip all color from the string.
      Parameters:
      str - The string to strip
      Returns:
      The stripped string, or an empty string if the provided one is null
    • strip

      @NotNull public static @NotNull List<String> strip(@Nullable @Nullable List<String> strList)
      Fully strip all color from strings.
      Parameters:
      strList - The list of string to strip
      Returns:
      The stripped string list, or an empty list if the provided one is null
      See Also:
    • capitalizeFirst

      @NotNull public static @NotNull String capitalizeFirst(@NotNull @NotNull String str)
      Makes the first character uppercase and the rest lowercase.
      Parameters:
      str - The string
      Returns:
      The string with the first letter in upper case and the rest in lower case
    • titleCase

      @NotNull public static @NotNull String titleCase(@NotNull @NotNull String str, @NotNull @NotNull String delimiter)
      Makes a string Title Case.
      Parameters:
      str - The string
      delimiter - The delimiter to use for splitting the string
      Returns:
      The string in Title Case
    • splitLines

      @NotNull public static @NotNull List<String> splitLines(@Nullable @Nullable String str)
      Creates a list of lines from a string line-broken with \n.
      Parameters:
      str - The string to split
      Returns:
      The list of lines
    • console

      public static void console(@Nullable @Nullable String str)
      Sends a formatted console message. Any message equaling null or IGNORE_MESSAGE_VALUE (ignore case) will be ignored.
      Parameters:
      str - The message to send
    • consoleRaw

      public static void consoleRaw(@Nullable @Nullable String str)
      Sends a colorized console message without the prefix. Any message equaling null or IGNORE_MESSAGE_VALUE (ignore case) will be ignored.
      Parameters:
      str - The message to send
    • log

      public static void log(@Nullable @Nullable String str)
      Logs a plain message into the console.
      Parameters:
      str - The message to send
    • log

      public static void log(Level level, String str)
      Logs a plain message into the console.
      Parameters:
      str - The message to send
      level - The logging level
    • tell

      public static void tell(@NotNull @NotNull org.bukkit.command.CommandSender sender, @Nullable @Nullable String str)
      Sends a colored and prefixed message to the command sender. Any message equaling null or IGNORE_MESSAGE_VALUE (ignore case) will be ignored.
      Parameters:
      sender - The command sender that will receive the message
      str - The message to send
    • tellLocalized

      public static void tellLocalized(@NotNull @NotNull org.bukkit.command.CommandSender sender, @Nullable @Nullable String key, Object... args)
      Sends a localized(String, CommandSender, Object...), colored, and prefixed message to the command sender. Any key equaling null or any message equaling to IGNORE_MESSAGE_VALUE (ignore case) will be ignored.

      Uses MessageFormat internally for argument substitution. Single quotes in translation values must be doubled ('') to appear literally.

      Parameters:
      sender - The command sender that will receive the message
      key - The key of the localized string
      args - Arguments to replace in the localized string
      See Also:
    • tellRaw

      public static void tellRaw(@NotNull @NotNull org.bukkit.command.CommandSender sender, @Nullable @Nullable String str)
      Sends a colorized message without the prefix. Any message equaling null or IGNORE_MESSAGE_VALUE (ignore case) will be ignored.
      Parameters:
      sender - The command sender that will receive the message
      str - The message to send
      See Also:
    • tellLocalizedRaw

      public static void tellLocalizedRaw(@NotNull @NotNull org.bukkit.command.CommandSender sender, @Nullable @Nullable String key, Object... args)
      Sends a localized, colorized message without the prefix. Any key equaling null or any message equaling to IGNORE_MESSAGE_VALUE (ignore case) will be ignored.

      Uses MessageFormat internally for argument substitution. Single quotes in translation values must be doubled ('') to appear literally.

      Parameters:
      sender - The command sender that will receive the message
      key - The key of the localized string
      args - Arguments to replace in the localized string
      See Also:
    • tellComponent

      public static void tellComponent(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull net.kyori.adventure.text.Component component)
      Sends the Component to the player as a chat message.
      Parameters:
      player - The player who should receive the component
      component - The component to send
      See Also:
    • tellCentered

      public static void tellCentered(@NotNull @NotNull org.bukkit.entity.Player player, @Nullable @Nullable String str)
      Sends a colored and centered message. May not work if the player has changed their chat size, used a custom font (resource pack), or if the message contains HEX colors. Any message equaling null or IGNORE_MESSAGE_VALUE (ignore case) will be ignored.
      Parameters:
      player - The player that will receive the message
      str - The message to send
    • broadcast

      public static void broadcast(@Nullable @Nullable String permission, @Nullable @Nullable String str)
      Broadcasts the message after coloring and formatting it. Any message equaling null or IGNORE_MESSAGE_VALUE (ignore case) will be ignored.
      Parameters:
      permission - The permission players must have in order to see this broadcast, or null if the broadcast should be seen by everyone
      str - The message to send
    • broadcastRaw

      public static void broadcastRaw(@Nullable @Nullable String permission, @Nullable @Nullable String str)
      Broadcasts a colorized message without the prefix. Any message equaling null or IGNORE_MESSAGE_VALUE (ignore case) will be ignored.
      Parameters:
      permission - The permission players must have in order to see this broadcast, or null if the broadcast should be seen by everyone
      str - The message to send
      See Also:
    • sendTitle

      public static void sendTitle(@NotNull @NotNull org.bukkit.entity.Player player, @Nullable @Nullable String title, @Nullable @Nullable String subtitle)
      Sends the title to the player.
      Parameters:
      player - The player who should receive the title
      title - The title to send, or null for none
      subtitle - The subtitle to send, or null for none
    • sendTitle

      public static void sendTitle(@NotNull @NotNull org.bukkit.entity.Player player, @Nullable @Nullable String title, @Nullable @Nullable String subtitle, int fadeIn, int stay, int fadeOut)
      Sends the title to the player.
      Parameters:
      player - The player who should receive the title
      title - The title to send, or null for none
      subtitle - The subtitle to send, or null for none
      fadeIn - The fade in duration, in ticks
      stay - The stay duration, in ticks
      fadeOut - The fade out duration, in ticks
    • clearTitle

      public static void clearTitle(@NotNull @NotNull org.bukkit.entity.Player player)
      Clears the current title and subtitle of the player.
      Parameters:
      player - The player who should have their title and subtitle cleared