diff --git a/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java b/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java index dab34b02..15cccf7f 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java +++ b/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java @@ -8,6 +8,7 @@ import com.google.common.collect.ImmutableMap; import com.google.gson.JsonSyntaxException; import com.willfp.eco.core.Eco; import com.willfp.eco.core.integrations.placeholder.PlaceholderManager; +import com.willfp.eco.core.placeholder.parsing.PlaceholderContext; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; @@ -21,6 +22,7 @@ import org.jetbrains.annotations.Nullable; import java.awt.Color; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -200,6 +202,26 @@ public final class StringUtils { return translated; } + /** + * Format a list of strings. + *

+ * Coverts color codes and placeholders. + * + * @param list The messages to format. + * @param context The context. + * @return The message, format. + */ + @NotNull + public static List formatList(@NotNull final List list, + @NotNull PlaceholderContext context) { + List translated = new ArrayList<>(); + for (String string : list) { + translated.add(format(string, context)); + } + + return translated; + } + /** * Format a string. *

@@ -242,7 +264,7 @@ public final class StringUtils { @NotNull public static String format(@NotNull final String message, @NotNull final FormatOption option) { - return format(message, null, option); + return format(message, (Player) null, option); } /** @@ -287,7 +309,7 @@ public final class StringUtils { @NotNull public static Component formatToComponent(@NotNull final String message, @NotNull final FormatOption option) { - return formatToComponent(message, null, option); + return formatToComponent(message, (Player) null, option); } /** @@ -321,10 +343,54 @@ public final class StringUtils { public static String format(@NotNull final String message, @Nullable final Player player, @NotNull final FormatOption option) { - String processedMessage = message; if (option == FormatOption.WITH_PLACEHOLDERS) { - processedMessage = PlaceholderManager.translatePlaceholders(processedMessage, player); + return format( + message, + new PlaceholderContext( + player, + null, + PlaceholderManager.EMPTY_INJECTABLE, + Collections.emptyList() + ) + ); } + + return STRING_FORMAT_CACHE.get(message); + } + + /** + * Format a string to a component. + *

+ * Converts color codes and placeholders if specified. + * + * @param message The message to translate. + * @param context The placeholder context. + * @return The message, formatted, as a component. + * @see StringUtils#format(String, Player) + */ + @NotNull + public static Component formatToComponent(@NotNull final String message, + @NotNull final PlaceholderContext context) { + return toComponent(format(message, context)); + } + + /** + * Format a string. + *

+ * Coverts color codes and placeholders if specified. + * + * @param message The message to format. + * @param context The context to translate placeholders with respect to. + * @return The message, formatted. + */ + @NotNull + public static String format(@NotNull final String message, + @NotNull final PlaceholderContext context) { + String processedMessage = message; + processedMessage = PlaceholderManager.translatePlaceholders( + processedMessage, + context + ); return STRING_FORMAT_CACHE.get(processedMessage); } diff --git a/eco-api/src/main/kotlin/com/willfp/eco/util/StringUtils.kt b/eco-api/src/main/kotlin/com/willfp/eco/util/StringUtils.kt index 7ba84917..c2cdf0e9 100644 --- a/eco-api/src/main/kotlin/com/willfp/eco/util/StringUtils.kt +++ b/eco-api/src/main/kotlin/com/willfp/eco/util/StringUtils.kt @@ -2,6 +2,7 @@ package com.willfp.eco.util +import com.willfp.eco.core.placeholder.parsing.PlaceholderContext import net.kyori.adventure.text.Component import org.bukkit.entity.Player @@ -31,6 +32,14 @@ fun String.formatEco( if (formatPlaceholders) StringUtils.FormatOption.WITH_PLACEHOLDERS else StringUtils.FormatOption.WITHOUT_PLACEHOLDERS ) +/** @see StringUtils.format */ +fun String.formatEco( + context: PlaceholderContext +) = StringUtils.format( + this, + context +) + /** @see StringUtils.formatList */ fun List.formatEco( player: Player? = null, @@ -41,6 +50,14 @@ fun List.formatEco( if (formatPlaceholders) StringUtils.FormatOption.WITH_PLACEHOLDERS else StringUtils.FormatOption.WITHOUT_PLACEHOLDERS ) +/** @see StringUtils.formatList */ +fun List.formatEco( + context: PlaceholderContext +): List = StringUtils.formatList( + this, + context +) + /** @see StringUtils.splitAround */ fun String.splitAround(separator: String): Array = StringUtils.splitAround(this, separator)