Makes both String[] and List<String> available for string formatting
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-01-11 14:04:36 +01:00
parent 21611dd90d
commit 7dc0315469
3 changed files with 35 additions and 5 deletions

View File

@ -6,6 +6,8 @@ import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List;
/** /**
* A formatter for formatting displayed messages * A formatter for formatting displayed messages
*/ */
@ -169,6 +171,20 @@ public final class StringFormatter {
*/ */
public String replacePlaceholders(@NotNull TranslatableMessage translatableMessage, @NotNull String[] placeholders, public String replacePlaceholders(@NotNull TranslatableMessage translatableMessage, @NotNull String[] placeholders,
@NotNull String[] replacements) { @NotNull String[] replacements) {
return replacePlaceholders(this.translator.getTranslatedMessage(translatableMessage), List.of(placeholders),
List.of(replacements));
}
/**
* Replaces placeholders in a translatable message
*
* @param translatableMessage <p>The translatable message to replace in</p>
* @param placeholders <p>The placeholders to replace</p>
* @param replacements <p>The replacement values</p>
* @return <p>The input string with placeholders replaced</p>
*/
public String replacePlaceholders(@NotNull TranslatableMessage translatableMessage, @NotNull List<String> placeholders,
@NotNull List<String> replacements) {
return replacePlaceholders(this.translator.getTranslatedMessage(translatableMessage), placeholders, replacements); return replacePlaceholders(this.translator.getTranslatedMessage(translatableMessage), placeholders, replacements);
} }
@ -195,8 +211,21 @@ public final class StringFormatter {
*/ */
public static String replacePlaceholders(@NotNull String input, @NotNull String[] placeholders, public static String replacePlaceholders(@NotNull String input, @NotNull String[] placeholders,
@NotNull String[] replacements) { @NotNull String[] replacements) {
for (int i = 0; i < Math.min(placeholders.length, replacements.length); i++) { return replacePlaceholders(input, List.of(placeholders), List.of(replacements));
input = replacePlaceholder(input, placeholders[i], replacements[i]); }
/**
* Replaces placeholders in a string
*
* @param input <p>The input string to replace in</p>
* @param placeholders <p>The placeholders to replace</p>
* @param replacements <p>The replacement values</p>
* @return <p>The input string with placeholders replaced</p>
*/
public static String replacePlaceholders(@NotNull String input, @NotNull List<String> placeholders,
@NotNull List<String> replacements) {
for (int i = 0; i < Math.min(placeholders.size(), replacements.size()); i++) {
input = replacePlaceholder(input, placeholders.get(i), replacements.get(i));
} }
return input; return input;
} }

View File

@ -2,6 +2,7 @@ package net.knarcraft.knarlib.formatting;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -71,8 +72,8 @@ public final class StringReplacer {
* @return <p>The string with placeholders replaced</p> * @return <p>The string with placeholders replaced</p>
*/ */
public @NotNull String replace(@NotNull String input) { public @NotNull String replace(@NotNull String input) {
return StringFormatter.replacePlaceholders(input, replacements.keySet().toArray(new String[0]), return StringFormatter.replacePlaceholders(input, new ArrayList<>(replacements.keySet()),
replacements.values().toArray(new String[0])); new ArrayList<>(replacements.values()));
} }
} }

View File

@ -12,7 +12,7 @@ import org.jetbrains.annotations.NotNull;
public enum TranslatableTimeUnit implements TranslatableMessage { public enum TranslatableTimeUnit implements TranslatableMessage {
/** /**
* The format for displaying the duation of something * The format for displaying the duration of something
*/ */
DURATION_FORMAT, DURATION_FORMAT,