All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good
127 lines
4.2 KiB
Java
127 lines
4.2 KiB
Java
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
|
|
*
|
|
* <p>Add placeholder -> value pairs before running replace on the wanted string</p>
|
|
*/
|
|
@SuppressWarnings("unused")
|
|
public final class StringReplacer {
|
|
|
|
private final Map<String, String> replacements = new HashMap<>();
|
|
private String replacementInput;
|
|
|
|
/**
|
|
* Instantiates a new string replacer
|
|
*/
|
|
public StringReplacer() {
|
|
}
|
|
|
|
/**
|
|
* Instantiates a new string replacer
|
|
*
|
|
* @param stringFormatter <p>The string formatter to use for translation</p>
|
|
* @param translatableMessage <p>The translatable message to replace for</p>
|
|
*/
|
|
public StringReplacer(@NotNull StringFormatter stringFormatter, @NotNull TranslatableMessage translatableMessage) {
|
|
this.replacementInput = stringFormatter.getUnFormattedMessage(translatableMessage);
|
|
}
|
|
|
|
/**
|
|
* Instantiates a new string replacer
|
|
*
|
|
* @param replacementInput <p>The input string to replace placeholders for</p>
|
|
*/
|
|
public StringReplacer(@NotNull String replacementInput) {
|
|
this.replacementInput = replacementInput;
|
|
}
|
|
|
|
/**
|
|
* Adds a new string replacement
|
|
*
|
|
* @param placeholder <p>The placeholder to replace</p>
|
|
* @param value <p>The replacement value</p>
|
|
*/
|
|
public void add(@NotNull String placeholder, @NotNull String value) {
|
|
this.replacements.put(placeholder, value);
|
|
}
|
|
|
|
/**
|
|
* Adds a new string replacement
|
|
*
|
|
* @param stringFormatter <p>The string formatter to use for translation</p>
|
|
* @param placeholder <p>The placeholder to replace</p>
|
|
* @param value <p>The replacement value</p>
|
|
*/
|
|
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 <p>The string formatter to use for translation</p>
|
|
* @param placeholder <p>The placeholder to replace</p>
|
|
* @param value <p>The replacement value</p>
|
|
*/
|
|
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 <p>The string formatter to use for translation</p>
|
|
* @param placeholder <p>The placeholder to replace</p>
|
|
* @param value <p>The replacement value</p>
|
|
*/
|
|
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 <p>The placeholder to remove</p>
|
|
*/
|
|
public void remove(@NotNull String placeholder) {
|
|
this.replacements.remove(placeholder);
|
|
}
|
|
|
|
/**
|
|
* Replaces previously added placeholders in the string given in the constructor
|
|
*
|
|
* <p>You must use the constructor with a non-null string to use this method.</p>
|
|
*
|
|
* @return <p>The string with placeholders replaced</p>
|
|
*/
|
|
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 <p>The string to replace placeholders in</p>
|
|
* @return <p>The string with placeholders replaced</p>
|
|
*/
|
|
public @NotNull String replace(@NotNull String input) {
|
|
return StringFormatter.replacePlaceholders(input, new ArrayList<>(replacements.keySet()),
|
|
new ArrayList<>(replacements.values()));
|
|
}
|
|
|
|
}
|