Skip to content

Instantly share code, notes, and snippets.

@mtbarr
Created August 29, 2021 21:15
Show Gist options
  • Save mtbarr/a58a4b01be322216c53c9599fee682c8 to your computer and use it in GitHub Desktop.
Save mtbarr/a58a4b01be322216c53c9599fee682c8 to your computer and use it in GitHub Desktop.
Chained function to replace string labels easily.
package br.com.thelegion.legioncommons.chat.replacer;
import org.bukkit.ChatColor;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* <code>
* Player player = Players.getPlayer("Sasuked");
* String firstJoinMessage = "Olá @player, bem vindo ao servidor @serverName (id @serverId)";
*
* String replacedMessage = StringReplacer.replacing("@player", player::getName)
* .and("@serverName", "RedeNetwork")
* .and("@serverId", "01")
* .transform(firstJoinMessage);
*
* </code>
*/
public class StringReplacer {
private Function<String, String> baseFunction;
private StringReplacer(Function<String, String> baseFunction) {
this.baseFunction = baseFunction;
}
private StringReplacer() {
this(s -> ChatColor.translateAlternateColorCodes('&', s));
}
public static StringReplacer replacing(char oldChar, char replacement) {
return new StringReplacer().and(oldChar, replacement);
}
public static StringReplacer replacing(String placeholder, String replacement) {
return new StringReplacer().and(placeholder, replacement);
}
public static StringReplacer replacing(String placeholder, Object object) {
return replacing(placeholder, String.valueOf(object));
}
public static StringReplacer replacing(String placeholder, Supplier<String> stringSupplier) {
return new StringReplacer().and(placeholder, stringSupplier.get());
}
public StringReplacer and(String placeholder, String replacement) {
return new StringReplacer(baseFunction.andThen(string -> string.replace(placeholder, replacement)));
}
public StringReplacer and(String placeholder, Object object) {
return and(placeholder, String.valueOf(object));
}
public StringReplacer and(char oldChar, char replacement) {
return new StringReplacer(baseFunction.andThen(string -> string.replace(oldChar, replacement)));
}
public StringReplacer and(String replacer, Supplier<String> replacementSupplier) {
return and(replacer, replacementSupplier.get());
}
public String transform(String input) {
return toFunction().apply(input);
}
public Function<String, String> toFunction() {
return baseFunction;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment