package net.knarcraft.knarlib.formatting; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** * A stateful string replacer * *
Add placeholder -> value pairs before running replace on the wanted string
*/ @SuppressWarnings("unused") public final class StringReplacer { private final MapThe string formatter to use for translation
* @param translatableMessageThe translatable message to replace for
*/ public StringReplacer(@NotNull StringFormatter stringFormatter, @NotNull TranslatableMessage translatableMessage) { this.replacementInput = stringFormatter.getUnFormattedMessage(translatableMessage); } /** * Instantiates a new string replacer * * @param replacementInputThe input string to replace placeholders for
*/ public StringReplacer(@NotNull String replacementInput) { this.replacementInput = replacementInput; } /** * Adds a new string replacement * * @param placeholderThe placeholder to replace
* @param valueThe replacement value
*/ public void add(@NotNull String placeholder, @NotNull String value) { this.replacements.put(placeholder, value); } /** * Adds a new string replacement * * @param stringFormatterThe string formatter to use for translation
* @param placeholderThe placeholder to replace
* @param valueThe replacement value
*/ public void add(@NotNull StringFormatter stringFormatter, @NotNull TranslatableMessage placeholder, @NotNull TranslatableMessage value) { this.replacements.put(stringFormatter.getUnFormattedMessage(placeholder), stringFormatter.getUnFormattedMessage(value)); } /** * Adds a new string replacement * * @param stringFormatterThe string formatter to use for translation
* @param placeholderThe placeholder to replace
* @param valueThe replacement value
*/ public void add(@NotNull StringFormatter stringFormatter, @NotNull String placeholder, @NotNull TranslatableMessage value) { this.replacements.put(placeholder, stringFormatter.getUnFormattedMessage(value)); } /** * Adds a new string replacement * * @param stringFormatterThe string formatter to use for translation
* @param placeholderThe placeholder to replace
* @param valueThe replacement value
*/ public void add(@NotNull StringFormatter stringFormatter, @NotNull TranslatableMessage placeholder, @NotNull String value) { this.replacements.put(stringFormatter.getUnFormattedMessage(placeholder), value); } /** * Removes a string replacement * * @param placeholderThe placeholder to remove
*/ public void remove(@NotNull String placeholder) { this.replacements.remove(placeholder); } /** * Replaces previously added placeholders in the string given in the constructor * *You must use the constructor with a non-null string to use this method.
* * @returnThe string with placeholders replaced
*/ public @NotNull String replace() { if (replacementInput == null) { throw new IllegalStateException("This method cannot be run without a defined string"); } return replace(replacementInput); } /** * Replaces previously added placeholders in the given string * * @param inputThe string to replace placeholders in
* @returnThe string with placeholders replaced
*/ public @NotNull String replace(@NotNull String input) { return StringFormatter.replacePlaceholders(input, new ArrayList<>(replacements.keySet()), new ArrayList<>(replacements.values())); } }