Skip to content

Instantly share code, notes, and snippets.

@gepron1x
Created February 12, 2021 14:55
Show Gist options
  • Save gepron1x/a00ae4c045272deb9809dd8bc8947342 to your computer and use it in GitHub Desktop.
Save gepron1x/a00ae4c045272deb9809dd8bc8947342 to your computer and use it in GitHub Desktop.
package me.gepron1x.decaliumcore;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
public class Placeholder {
private static Set<Placeholder> placeholders = new HashSet<>();
private final String name;
private final Function<Player, String> placeholderFunction;
private Placeholder(String name, Function<Player, String> placeholderFunction) {
this.name = name;
this.placeholderFunction = placeholderFunction;
}
public static void registerPlaceholder(String name, Function<Player, String> replaceFunction) {
placeholders.add(new Placeholder(name, replaceFunction));
}
public static Component applyPlaceholders(Player p, Component component) {
TextReplacementConfig.Builder builder = TextReplacementConfig.builder();
for (Placeholder placeholder : placeholders) {
component = component.replaceText
(
builder.match("%" + placeholder.name + "%")
.replacement(placeholder.placeholderFunction.apply(p))
.build()
);
}
return component;
}
public static String applyPlaceholders(Player p, String s) {
for(Placeholder placeholder : placeholders) {
s = s.replace("%" + placeholder.name + "%", placeholder.placeholderFunction.apply(p));
}
return s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment