Class MessageUtils
java.lang.Object
ua.mcchickenstudio.opencreative.utils.MessageUtils
MessageUtils
This class contains utils, that can return messages, modify and format them. Uses translation files.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intstatic booleancantSendOnceMessage(@NotNull Planet planet, int onceInSeconds) static voidclearOnceMessages(Planet planet) static @NotNull StringconvertTime(long duration) Converts duration in millis to user-friendly text.static @NotNull net.kyori.adventure.text.ComponentfromInputToComponent(@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 StringfromLegacyToMiniMessage(@NotNull String input) Converts legacy text with § or invalid input: '&' to MiniMessage format, so this text can be deserialized to MiniMessage.static @NotNull StringfromLegacyToMiniMessageBook(@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.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.ComponentgetComponentWithPlaceholders(@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 StringgetElapsedTime(long currentTime, long oldTime) Returns elapsed time from old time to current with localized message.static StringReturns current language of OpenCreative+.static @NotNull net.kyori.adventure.text.ComponentgetLocaleComponent(String messageID) Returns component message from translation.static net.kyori.adventure.text.ComponentgetLocaleComponent(String messageID, boolean returnDetailedError) Returns component message from translation with parsed player placeholders.getLocaleItemDescription(String descriptionID) Returns item description from translationstatic StringgetLocaleItemName(String nameID) Returns item name from translation.static StringgetLocaleMessage(String messageID) Returns message from translation.static StringgetLocaleMessage(String messageID, boolean returnDetailedError) Returns message from translation with parsed player placeholders.static org.bukkit.configuration.file.FileConfigurationReturns translation stored in FileConfiguration.static @NotNull net.kyori.adventure.text.ComponentgetPlayerLocaleComponent(@NotNull String messageID, @NotNull org.bukkit.OfflinePlayer player) Returns component message from translation with parsed player placeholders.static StringgetPlayerLocaleMessage(String messageID, org.bukkit.OfflinePlayer player) Returns message from translation with parsed player placeholders.static booleanisLegacyFormat(String text) Checks if text has invalid input: '&' or § symbol.static voidLoads localization file, that located in ./plugins/OpenCreative/locales/ folder.static booleanlocalizationFileExists(String languageName) Checks if localization file exists in locales directory.static booleanmessageExists(String messageID) Checks whether message exists in localization file.static StringparseModuleLines(Module module, String string) Returns string with parsed module lines (static StringReturns string, that parsed player's placeholders if PlaceholderAPI is working.static StringparsePlanetLines(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.ComponentparsePlanetLines(Planet planet, net.kyori.adventure.text.Component component) static @Nullable DoubleparseTicks(@NotNull String message) static doubleparseTicks(@NotNull String message, double defaultTicks) static voidsendMessageOnce(@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 voidsetMessage(@NotNull String path, @Nullable Object object) Sets message in localization file and saves changes.static StringReturns text that could be shortened to specified length, if text length is greater than specified one.static @NotNull net.kyori.adventure.text.ComponenttoComponent(String text) Converts text into component by deserializing it with legacy serializer (if message has invalid input: '&' or § symbol), or with minimessage format.static @NotNull StringtoKebabCase(@NotNull String input) Converts text from snake_case, camelCase and PascalCase to kebab-case, that can be used in YAML.
-
Constructor Details
-
MessageUtils
public MessageUtils()
-
-
Method Details
-
toComponent
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
Checks if text has invalid input: '&' or § symbol.- Parameters:
text- text to check.- Returns:
- true - if contains invalid input: '&' or §, false - otherwise.
-
substring
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
-
localizationFileExists
Checks if localization file exists in locales directory.- Parameters:
languageName- name of language, without ".yml".- Returns:
- true - exists, false - not exists.
-
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 valuesIt 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
-
getPlayerLocaleMessage
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
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
-
getLocaleItemDescription
-
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
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
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
-
clearOnceMessages
-
parsePlanetLines
-
parsePlanetLines
public static net.kyori.adventure.text.Component parsePlanetLines(Planet planet, net.kyori.adventure.text.Component component) -
parseModuleLines
-
parsePAPI
-
parseTicks
-
parseTicks
-
messageExists
Checks whether message exists in localization file.- Parameters:
messageID- id of message.- Returns:
- true - message exists, false - not found.
-
toKebabCase
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
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
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.
-