Class MessageUtils

java.lang.Object
ua.mcchickenstudio.opencreative.utils.MessageUtils

public final class MessageUtils extends Object

MessageUtils

This class contains utils, that can return messages, modify and format them. Uses translation files.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
     
    static boolean
    cantSendOnceMessage(@NotNull Planet planet, int onceInSeconds)
     
    static void
     
    static @NotNull String
    convertTime(long duration)
    Converts duration in millis to user-friendly text.
    static @NotNull net.kyori.adventure.text.Component
    fromInputToComponent(@NotNull String input)
    Converts text into component by deserializing it with legacy serializer (if message has invalid input: '&' or § symbol), or with minimessage format, but without hover and click events.
    static @NotNull String
    Converts legacy text with § or invalid input: '&' to MiniMessage format, so this text can be deserialized to MiniMessage.
    static @NotNull String
    Converts legacy text with § or invalid input: '&' to book's MiniMessage format, so this text can be deserialized to MiniMessage and used in written books.
    static @NotNull List<net.kyori.adventure.text.Component>
    getBookPages(@NotNull org.bukkit.entity.Player player, @NotNull String localizationID)
    Returns book pages from translation.
    static @NotNull net.kyori.adventure.text.Component
    getComponentWithPlaceholders(@NotNull String messageID, @NotNull org.bukkit.OfflinePlayer player, @Nullable Object... placeholdersAndValues)
    Returns component message from translation with parsed player placeholders and custom placeholders.
    static @NotNull String
    getElapsedTime(long currentTime, long oldTime)
    Returns elapsed time from old time to current with localized message.
    static String
    Returns current language of OpenCreative+.
    static @NotNull net.kyori.adventure.text.Component
    Returns component message from translation.
    static net.kyori.adventure.text.Component
    getLocaleComponent(String messageID, boolean returnDetailedError)
    Returns component message from translation with parsed player placeholders.
    static List<String>
    Returns item description from translation
    static String
    Returns item name from translation.
    static String
    Returns message from translation.
    static String
    getLocaleMessage(String messageID, boolean returnDetailedError)
    Returns message from translation with parsed player placeholders.
    static org.bukkit.configuration.file.FileConfiguration
    Returns translation stored in FileConfiguration.
    static @NotNull net.kyori.adventure.text.Component
    getPlayerLocaleComponent(@NotNull String messageID, @NotNull org.bukkit.OfflinePlayer player)
    Returns component message from translation with parsed player placeholders.
    static String
    getPlayerLocaleMessage(String messageID, org.bukkit.OfflinePlayer player)
    Returns message from translation with parsed player placeholders.
    static boolean
    Checks if text has invalid input: '&' or § symbol.
    static void
    Loads localization file, that located in ./plugins/OpenCreative/locales/ folder.
    static boolean
    Checks if localization file exists in locales directory.
    static boolean
    messageExists(String messageID)
    Checks whether message exists in localization file.
    static String
    parseModuleLines(Module module, String string)
    Returns string with parsed module lines (
    static String
    parsePAPI(org.bukkit.OfflinePlayer player, String string)
    Returns string, that parsed player's placeholders if PlaceholderAPI is working.
    static String
    parsePlanetLines(Planet planet, String string)
    Returns string, that parsed planet lines: planet name, description, online, reputation, owner, id, category, uniques, last activity time, creation time.
    static net.kyori.adventure.text.Component
    parsePlanetLines(Planet planet, net.kyori.adventure.text.Component component)
     
    static @Nullable Double
    parseTicks(@NotNull String message)
     
    static double
    parseTicks(@NotNull String message, double defaultTicks)
     
    static void
    sendMessageOnce(@NotNull Planet planet, @NotNull String path, @NotNull PlaceholderReplacer placeholder, @Nullable String clickCommand, @Nullable String hoverPath, int onceInSeconds)
    Sends message by path for every player in planet, if it was not sent recently.
    static void
    setMessage(@NotNull String path, @Nullable Object object)
    Sets message in localization file and saves changes.
    static String
    substring(String text, int length)
    Returns text that could be shortened to specified length, if text length is greater than specified one.
    static @NotNull net.kyori.adventure.text.Component
    Converts text into component by deserializing it with legacy serializer (if message has invalid input: '&' or § symbol), or with minimessage format.
    static @NotNull String
    toKebabCase(@NotNull String input)
    Converts text from snake_case, camelCase and PascalCase to kebab-case, that can be used in YAML.

    Methods inherited from class Object

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

    • MessageUtils

      public MessageUtils()
  • Method Details

    • toComponent

      @NotNull public static @NotNull net.kyori.adventure.text.Component toComponent(String text)
      Converts text into component by deserializing it with legacy serializer (if message has invalid input: '&' or § symbol), or with minimessage format.
      Parameters:
      text - text to convert.
      Returns:
      text component.
    • fromInputToComponent

      @NotNull public static @NotNull net.kyori.adventure.text.Component fromInputToComponent(@NotNull @NotNull String input)
      Converts text into component by deserializing it with legacy serializer (if message has invalid input: '&' or § symbol), or with minimessage format, but without hover and click events.
      Parameters:
      input - text to convert.
      Returns:
      text component without hover and click events.
    • isLegacyFormat

      public static boolean isLegacyFormat(String text)
      Checks if text has invalid input: '&' or § symbol.
      Parameters:
      text - text to check.
      Returns:
      true - if contains invalid input: '&' or §, false - otherwise.
    • substring

      public static String substring(String text, int length)
      Returns text that could be shortened to specified length, if text length is greater than specified one.
      
      substring("Hello",5); // "Hello"
      substring("World",4); // "Wo..."
      substring("World",0); // ""
      
      
      Parameters:
      text - text to substring.
      length - maximum length of text.
      Returns:
      shortened text.
    • loadLocalizationFile

      public static void loadLocalizationFile()
      Loads localization file, that located in ./plugins/OpenCreative/locales/ folder.
    • addMissingMessageLines

      public static int addMissingMessageLines()
    • setMessage

      public static void setMessage(@NotNull @NotNull String path, @Nullable @Nullable Object object)
      Sets message in localization file and saves changes.
      Parameters:
      path - path of message.
      object - message.
    • localizationFileExists

      public static boolean localizationFileExists(String languageName)
      Checks if localization file exists in locales directory.
      Parameters:
      languageName - name of language, without ".yml".
      Returns:
      true - exists, false - not exists.
    • getLanguage

      public static String getLanguage()
      Returns current language of OpenCreative+.
      Returns:
      global language of plugin.
    • getLocalization

      public static org.bukkit.configuration.file.FileConfiguration getLocalization()
      Returns translation stored in FileConfiguration.
      Returns:
      translation config.
    • getLocaleComponent

      @NotNull public static @NotNull net.kyori.adventure.text.Component getLocaleComponent(String messageID)
      Returns component message from translation.
      Parameters:
      messageID - id of message.
      Returns:
      component message, or "Error | Not found message.path...", if message was not found.
    • getPlayerLocaleComponent

      @NotNull public static @NotNull net.kyori.adventure.text.Component getPlayerLocaleComponent(@NotNull @NotNull String messageID, @NotNull @NotNull org.bukkit.OfflinePlayer player)
      Returns component message from translation with parsed player placeholders.
      Parameters:
      messageID - id of message.
      player - player to parse.
      Returns:
      component message, or "Error | Not found message.path...", if message was not found.
    • getComponentWithPlaceholders

      @NotNull public static @NotNull net.kyori.adventure.text.Component getComponentWithPlaceholders(@NotNull @NotNull String messageID, @NotNull @NotNull org.bukkit.OfflinePlayer player, @Nullable @Nullable Object... placeholdersAndValues)
      Returns component message from translation with parsed player placeholders and custom placeholders.
      Parameters:
      messageID - id of message.
      player - player to parse.
      placeholdersAndValues - placeholders and values

      It must start with placeholder and end with value.

      "placeholder", value, "placeholder-2", value-2

      Returns:
      component message, or "Error | Not found message.path...", if message was not found.
    • getLocaleComponent

      public static net.kyori.adventure.text.Component getLocaleComponent(String messageID, boolean returnDetailedError)
      Returns component message from translation with parsed player placeholders.
      Parameters:
      messageID - id of message.
      returnDetailedError - if true - returns detailed error when message was not found, false - will return only path.
      Returns:
      component message.
    • getLocaleMessage

      public static String getLocaleMessage(String messageID)
      Returns message from translation.
      Parameters:
      messageID - id of message.
      Returns:
      translated message, or "Error | Not found message.path...", if message was not found.
    • getPlayerLocaleMessage

      public static String getPlayerLocaleMessage(String messageID, org.bukkit.OfflinePlayer player)
      Returns message from translation with parsed player placeholders.
      Parameters:
      messageID - id of message.
      player - player to parse.
      Returns:
      translated message, or "Error | Not found message.path...", if message was not found.
    • getLocaleMessage

      public static String getLocaleMessage(String messageID, boolean returnDetailedError)
      Returns message from translation with parsed player placeholders.
      Parameters:
      messageID - id of message.
      returnDetailedError - if true - returns detailed error when message was not found, false - will return only path.
      Returns:
      translated message.
    • getLocaleItemName

      public static String getLocaleItemName(String nameID)
      Returns item name from translation.
      Parameters:
      nameID - id of message.
      Returns:
      translated item name, or "Not found: message.path", if message was not found.
    • getLocaleItemDescription

      public static List<String> getLocaleItemDescription(String descriptionID)
      Returns item description from translation
      Parameters:
      descriptionID - id of message.
      Returns:
      translated item description, or "Not found item description...", if message was not found.
    • getBookPages

      @NotNull public static @NotNull List<net.kyori.adventure.text.Component> getBookPages(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull String localizationID)
      Returns book pages from translation.
      Parameters:
      localizationID - id of message.
      Returns:
      translated book pages, or "Not found pages...", if message was not found.
    • getElapsedTime

      @NotNull public static @NotNull String getElapsedTime(long currentTime, long oldTime)
      Returns elapsed time from old time to current with localized message. For example: if elapsed time is 2 seconds, it will return "2 sec ago".
    • convertTime

      @NotNull public static @NotNull String convertTime(long duration)
      Converts duration in millis to user-friendly text.
      
      convertTime(1000); // 1 sec
      convertTime(1); // less a second
      convertTime(60000); // 1 min
      convertTime(121000); // 2 min 1 sec
      
      
      Parameters:
      duration - duration to convert
      Returns:
      user-friendly duration text.
    • sendMessageOnce

      public static void sendMessageOnce(@NotNull @NotNull Planet planet, @NotNull @NotNull String path, @NotNull @NotNull PlaceholderReplacer placeholder, @Nullable @Nullable String clickCommand, @Nullable @Nullable String hoverPath, int onceInSeconds)
      Sends message by path for every player in planet, if it was not sent recently.
      Parameters:
      planet - planet to send message.
      path - path of message.
      placeholder - placeholder for message.
      clickCommand - command for click.
      hoverPath - path of hover message.
      onceInSeconds - cooldown of messages.
    • cantSendOnceMessage

      public static boolean cantSendOnceMessage(@NotNull @NotNull Planet planet, int onceInSeconds)
    • clearOnceMessages

      public static void clearOnceMessages(Planet planet)
    • parsePlanetLines

      public static String parsePlanetLines(Planet planet, String string)
      Returns string, that parsed planet lines: planet name, description, online, reputation, owner, id, category, uniques, last activity time, creation time.
    • parsePlanetLines

      public static net.kyori.adventure.text.Component parsePlanetLines(Planet planet, net.kyori.adventure.text.Component component)
    • parseModuleLines

      public static String parseModuleLines(Module module, String string)
      Returns string with parsed module lines (
    • parsePAPI

      public static String parsePAPI(org.bukkit.OfflinePlayer player, String string)
      Returns string, that parsed player's placeholders if PlaceholderAPI is working.
    • parseTicks

      public static double parseTicks(@NotNull @NotNull String message, double defaultTicks)
    • parseTicks

      @Nullable public static @Nullable Double parseTicks(@NotNull @NotNull String message)
    • messageExists

      public static boolean messageExists(String messageID)
      Checks whether message exists in localization file.
      Parameters:
      messageID - id of message.
      Returns:
      true - message exists, false - not found.
    • toKebabCase

      @NotNull public static @NotNull String toKebabCase(@NotNull @NotNull String input)
      Converts text from snake_case, camelCase and PascalCase to kebab-case, that can be used in YAML.
      
      toKebabCase("EnderDragon"); // "ender-dragon"
      toKebabCase("ARMOR_STAND"); // "armor-stand"
      toKebabCase("Creeper"); // "creeper"
      
      
      Parameters:
      input - text to convert.
      Returns:
      kebab-cased text.
    • fromLegacyToMiniMessage

      @NotNull public static @NotNull String fromLegacyToMiniMessage(@NotNull @NotNull String input)
      Converts legacy text with § or invalid input: '&' to MiniMessage format, so this text can be deserialized to MiniMessage.
      
      fromLegacyToMiniMessage("&4Hello"); // "<red>Hello"
      fromLegacyToMiniMessage("&nHello"); // "<underlined>Hello"
      fromLegacyToMiniMessage("&rHello<red>"); // "<reset>Hello<red>"
      
      
      Parameters:
      input - text to convert.
      Returns:
      text, that can be used as MiniMessage.
    • fromLegacyToMiniMessageBook

      @NotNull public static @NotNull String fromLegacyToMiniMessageBook(@NotNull @NotNull String input)
      Converts legacy text with § or invalid input: '&' to book's MiniMessage format, so this text can be deserialized to MiniMessage and used in written books.
      
      fromLegacyToMiniMessage("&4Hello"); // "<red>Hello"
      fromLegacyToMiniMessage("&nHello"); // "<underlined>Hello"
      fromLegacyToMiniMessage("&rHello<red>"); // "<reset>Hello<red>"
      
      
      Parameters:
      input - text to convert.
      Returns:
      text, that can be used as MiniMessage in books.