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 Map replacements = new HashMap<>(); private String replacementInput; /** * Instantiates a new string replacer */ public StringReplacer() { } /** * Instantiates a new string replacer * * @param stringFormatter

The string formatter to use for translation

* @param translatableMessage

The translatable message to replace for

*/ public StringReplacer(@NotNull StringFormatter stringFormatter, @NotNull TranslatableMessage translatableMessage) { this.replacementInput = stringFormatter.getUnFormattedMessage(translatableMessage); } /** * Instantiates a new string replacer * * @param replacementInput

The input string to replace placeholders for

*/ public StringReplacer(@NotNull String replacementInput) { this.replacementInput = replacementInput; } /** * Adds a new string replacement * * @param placeholder

The placeholder to replace

* @param value

The replacement value

*/ public void add(@NotNull String placeholder, @NotNull String value) { this.replacements.put(placeholder, value); } /** * Adds a new string replacement * * @param stringFormatter

The string formatter to use for translation

* @param placeholder

The placeholder to replace

* @param value

The 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 stringFormatter

The string formatter to use for translation

* @param placeholder

The placeholder to replace

* @param value

The 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 stringFormatter

The string formatter to use for translation

* @param placeholder

The placeholder to replace

* @param value

The 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 placeholder

The 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.

* * @return

The 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 input

The string to replace placeholders in

* @return

The string with placeholders replaced

*/ public @NotNull String replace(@NotNull String input) { return StringFormatter.replacePlaceholders(input, new ArrayList<>(replacements.keySet()), new ArrayList<>(replacements.values())); } }