mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2026-02-04 23:53:20 +01:00
Add config option to use adventure replacer, add bracket {} support
This commit is contained in:
@@ -54,6 +54,10 @@ public final class PlaceholderAPIConfig {
|
|||||||
return plugin.getConfig().getBoolean("debug", false);
|
return plugin.getConfig().getBoolean("debug", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean useAdventureReplacer() {
|
||||||
|
return plugin.getConfig().getBoolean("useAdventureProvidedReplacer", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public Optional<ExpansionSort> getExpansionSort() {
|
public Optional<ExpansionSort> getExpansionSort() {
|
||||||
final String option = plugin.getConfig()
|
final String option = plugin.getConfig()
|
||||||
|
|||||||
@@ -49,10 +49,12 @@ public final class PAPIComponents {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public static Component setPlaceholders(final OfflinePlayer player, @NotNull final Component component) {
|
public static Component setPlaceholders(final OfflinePlayer player, @NotNull final Component component) {
|
||||||
// TODO: explore a custom TextReplacementRenderer which doesn't use regex for performance benefits i.e. merge CharsReplacer with kyori TextReplacementRenderer
|
if (PlaceholderAPIPlugin.getInstance().getPlaceholderAPIConfig().useAdventureProvidedReplacer()) {
|
||||||
return ComponentReplacer.replace(player, component);
|
return component.replaceText(config -> config.match(PlaceholderAPI.PLACEHOLDER_PATTERN).replacement((result, builder) ->
|
||||||
// return component.replaceText(config -> config.match(PlaceholderAPI.PLACEHOLDER_PATTERN).replacement((result, builder) ->
|
builder.content(PERCENT_EXACT_REPLACER.apply(result.group(), player, PlaceholderAPIPlugin.getInstance().getLocalExpansionManager()::getExpansion))));
|
||||||
// builder.content(PERCENT_EXACT_REPLACER.apply(result.group(), player, PlaceholderAPIPlugin.getInstance().getLocalExpansionManager()::getExpansion))));
|
}
|
||||||
|
|
||||||
|
return ComponentReplacer.replace(component, str -> PlaceholderAPI.setPlaceholders(player, str));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -104,8 +106,12 @@ public final class PAPIComponents {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public static Component setBracketPlaceholders(final OfflinePlayer player, @NotNull final Component component) {
|
public static Component setBracketPlaceholders(final OfflinePlayer player, @NotNull final Component component) {
|
||||||
return component.replaceText(config -> config.match(PlaceholderAPI.BRACKET_PLACEHOLDER_PATTERN).replacement((result, builder) ->
|
if (PlaceholderAPIPlugin.getInstance().getPlaceholderAPIConfig().useAdventureReplacer()) {
|
||||||
builder.content(BRACKET_EXACT_REPLACER.apply(result.group(), player, PlaceholderAPIPlugin.getInstance().getLocalExpansionManager()::getExpansion))));
|
return component.replaceText(config -> config.match(PlaceholderAPI.BRACKET_PLACEHOLDER_PATTERN).replacement((result, builder) ->
|
||||||
|
builder.content(BRACKET_EXACT_REPLACER.apply(result.group(), player, PlaceholderAPIPlugin.getInstance().getLocalExpansionManager()::getExpansion))));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ComponentReplacer.replace(component, str -> PlaceholderAPI.setBracketPlaceholders(player, str));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -157,6 +163,7 @@ public final class PAPIComponents {
|
|||||||
* @return The Component containing the parsed relational placeholders
|
* @return The Component containing the parsed relational placeholders
|
||||||
*/
|
*/
|
||||||
public static Component setRelationalPlaceholders(Player one, Player two, Component component) {
|
public static Component setRelationalPlaceholders(Player one, Player two, Component component) {
|
||||||
|
//todo: custom replacer
|
||||||
return component.replaceText(config -> config.match(RELATIONAL_PLACEHOLDER_PATTERN).replacement((result, builder) ->
|
return component.replaceText(config -> config.match(RELATIONAL_PLACEHOLDER_PATTERN).replacement((result, builder) ->
|
||||||
builder.content(RELATIONAL_EXACT_REPLACER.apply(result.group(2), one, two, PlaceholderAPIPlugin.getInstance().getLocalExpansionManager()::getExpansion))));
|
builder.content(RELATIONAL_EXACT_REPLACER.apply(result.group(2), one, two, PlaceholderAPIPlugin.getInstance().getLocalExpansionManager()::getExpansion))));
|
||||||
}
|
}
|
||||||
@@ -174,9 +181,4 @@ public final class PAPIComponents {
|
|||||||
return components.stream().map(line -> setRelationalPlaceholders(one, two, line))
|
return components.stream().map(line -> setRelationalPlaceholders(one, two, line))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
// kyori doesn't seem to have a method that can do a contains with regex, we don't want to do a more expensive replace
|
|
||||||
// public static boolean containsPlaceholders(@Nullable final Component text) {
|
|
||||||
// return text != null && text.replaceText()
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package me.clip.placeholderapi.replacer;
|
package me.clip.placeholderapi.replacer;
|
||||||
|
|
||||||
import me.clip.placeholderapi.PlaceholderAPI;
|
|
||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.key.Key;
|
||||||
import net.kyori.adventure.nbt.api.BinaryTagHolder;
|
import net.kyori.adventure.nbt.api.BinaryTagHolder;
|
||||||
import net.kyori.adventure.text.*;
|
import net.kyori.adventure.text.*;
|
||||||
@@ -8,29 +7,27 @@ import net.kyori.adventure.text.event.ClickEvent;
|
|||||||
import net.kyori.adventure.text.event.DataComponentValue;
|
import net.kyori.adventure.text.event.DataComponentValue;
|
||||||
import net.kyori.adventure.text.event.HoverEvent;
|
import net.kyori.adventure.text.event.HoverEvent;
|
||||||
import net.kyori.adventure.text.format.Style;
|
import net.kyori.adventure.text.format.Style;
|
||||||
import org.bukkit.OfflinePlayer;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class ComponentReplacer {
|
public class ComponentReplacer {
|
||||||
@NotNull
|
@NotNull
|
||||||
public static Component replace(@Nullable final OfflinePlayer player, @NotNull final Component component) {
|
public static Component replace(@NotNull final Component component, @NotNull final Function<String, String> replacer) {
|
||||||
return rebuild(player, component);
|
return rebuild(component, replacer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static Component rebuild(@Nullable final OfflinePlayer player, @NotNull final Component component) {
|
private static Component rebuild(@NotNull final Component component, @NotNull final Function<String, String> replacer) {
|
||||||
Component rebuilt;
|
Component rebuilt;
|
||||||
|
|
||||||
if (component instanceof TextComponent) {
|
if (component instanceof TextComponent) {
|
||||||
final TextComponent text = (TextComponent) component;
|
final TextComponent text = (TextComponent) component;
|
||||||
final String replaced = PlaceholderAPI.setPlaceholders(player, text.content());
|
final String replaced = replacer.apply(text.content());
|
||||||
|
|
||||||
rebuilt = Component.text(replaced);
|
rebuilt = Component.text(replaced);
|
||||||
} else if (component instanceof TranslatableComponent) {
|
} else if (component instanceof TranslatableComponent) {
|
||||||
@@ -38,7 +35,7 @@ public class ComponentReplacer {
|
|||||||
final List<Component> arguments = new ArrayList<>();
|
final List<Component> arguments = new ArrayList<>();
|
||||||
|
|
||||||
for (final ComponentLike arg : translatable.arguments()) {
|
for (final ComponentLike arg : translatable.arguments()) {
|
||||||
arguments.add(rebuild(player, arg.asComponent()));
|
arguments.add(rebuild(arg.asComponent(), replacer));
|
||||||
}
|
}
|
||||||
|
|
||||||
rebuilt = Component.translatable(translatable.key(), arguments);
|
rebuilt = Component.translatable(translatable.key(), arguments);
|
||||||
@@ -55,12 +52,12 @@ public class ComponentReplacer {
|
|||||||
rebuilt = Component.empty();
|
rebuilt = Component.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
rebuilt = rebuilt.style(rebuildStyle(player, component.style()));
|
rebuilt = rebuilt.style(rebuildStyle(component.style(), replacer));
|
||||||
|
|
||||||
if (!component.children().isEmpty()) {
|
if (!component.children().isEmpty()) {
|
||||||
final List<Component> children = new ArrayList<>();
|
final List<Component> children = new ArrayList<>();
|
||||||
for (Component child : component.children()) {
|
for (Component child : component.children()) {
|
||||||
children.add(rebuild(player, child));
|
children.add(rebuild(child, replacer));
|
||||||
}
|
}
|
||||||
rebuilt = rebuilt.children(children);
|
rebuilt = rebuilt.children(children);
|
||||||
}
|
}
|
||||||
@@ -69,25 +66,25 @@ public class ComponentReplacer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static Style rebuildStyle(@Nullable final OfflinePlayer player, @NotNull final Style style) {
|
private static Style rebuildStyle(@NotNull final Style style, @NotNull final Function<String, String> replacer) {
|
||||||
final Style.Builder builder = style.toBuilder();
|
final Style.Builder builder = style.toBuilder();
|
||||||
final ClickEvent click = style.clickEvent();
|
final ClickEvent click = style.clickEvent();
|
||||||
|
|
||||||
if (click != null) {
|
if (click != null) {
|
||||||
builder.clickEvent(rebuildClickEvent(player, click));
|
builder.clickEvent(rebuildClickEvent(click, replacer));
|
||||||
}
|
}
|
||||||
|
|
||||||
final HoverEvent<?> hover = style.hoverEvent();
|
final HoverEvent<?> hover = style.hoverEvent();
|
||||||
|
|
||||||
if (hover != null) {
|
if (hover != null) {
|
||||||
builder.hoverEvent(rebuildHoverEvent(player, hover));
|
builder.hoverEvent(rebuildHoverEvent(hover, replacer));
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static ClickEvent rebuildClickEvent(@Nullable final OfflinePlayer player, @NotNull final ClickEvent click) {
|
private static ClickEvent rebuildClickEvent(@NotNull final ClickEvent click, @NotNull final Function<String, String> replacer) {
|
||||||
final ClickEvent.Payload payload = click.payload();
|
final ClickEvent.Payload payload = click.payload();
|
||||||
|
|
||||||
if (!(payload instanceof ClickEvent.Payload.Text)) {
|
if (!(payload instanceof ClickEvent.Payload.Text)) {
|
||||||
@@ -95,7 +92,7 @@ public class ComponentReplacer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final String original = ((ClickEvent.Payload.Text) payload).value();
|
final String original = ((ClickEvent.Payload.Text) payload).value();
|
||||||
final String replaced = PlaceholderAPI.setPlaceholders(player, original);
|
final String replaced = replacer.apply(original);
|
||||||
|
|
||||||
final ClickEvent.Action action = click.action();
|
final ClickEvent.Action action = click.action();
|
||||||
|
|
||||||
@@ -116,16 +113,16 @@ public class ComponentReplacer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static HoverEvent<?> rebuildHoverEvent(@Nullable final OfflinePlayer player, @NotNull final HoverEvent<?> hover) {
|
private static HoverEvent<?> rebuildHoverEvent(@NotNull final HoverEvent<?> hover, @NotNull final Function<String, String> replacer) {
|
||||||
final Object value = hover.value();
|
final Object value = hover.value();
|
||||||
|
|
||||||
if (value instanceof Component) {
|
if (value instanceof Component) {
|
||||||
final Component rebuilt = rebuild(player, (Component) value);
|
final Component rebuilt = rebuild((Component) value, replacer);
|
||||||
return HoverEvent.showText(rebuilt);
|
return HoverEvent.showText(rebuilt);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value instanceof HoverEvent.ShowItem) {
|
if (value instanceof HoverEvent.ShowItem) {
|
||||||
return rebuildShowItem(player, (HoverEvent.ShowItem) value);
|
return rebuildShowItem((HoverEvent.ShowItem) value, replacer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value instanceof HoverEvent.ShowEntity) {
|
if (value instanceof HoverEvent.ShowEntity) {
|
||||||
@@ -133,7 +130,7 @@ public class ComponentReplacer {
|
|||||||
|
|
||||||
Component rebuiltName = null;
|
Component rebuiltName = null;
|
||||||
if (entity.name() != null) {
|
if (entity.name() != null) {
|
||||||
rebuiltName = rebuild(player, entity.name());
|
rebuiltName = rebuild(entity.name(), replacer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return HoverEvent.showEntity(entity.type(), entity.id(), rebuiltName);
|
return HoverEvent.showEntity(entity.type(), entity.id(), rebuiltName);
|
||||||
@@ -143,11 +140,11 @@ public class ComponentReplacer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static HoverEvent<?> rebuildShowItem(@Nullable final OfflinePlayer player, @NotNull final HoverEvent.ShowItem item) {
|
private static HoverEvent<?> rebuildShowItem(@NotNull final HoverEvent.ShowItem item, @NotNull final Function<String, String> replacer) {
|
||||||
final BinaryTagHolder nbt = item.nbt();
|
final BinaryTagHolder nbt = item.nbt();
|
||||||
|
|
||||||
if (nbt != null && !nbt.string().isEmpty()) {
|
if (nbt != null && !nbt.string().isEmpty()) {
|
||||||
final String replaced = PlaceholderAPI.setPlaceholders(player, nbt.string());
|
final String replaced = replacer.apply(nbt.string());
|
||||||
|
|
||||||
return HoverEvent.showItem(item.item(), item.count(), BinaryTagHolder.binaryTagHolder(replaced));
|
return HoverEvent.showItem(item.item(), item.count(), BinaryTagHolder.binaryTagHolder(replaced));
|
||||||
}
|
}
|
||||||
@@ -166,7 +163,7 @@ public class ComponentReplacer {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
rebuilt.put(entry.getKey(), BinaryTagHolder.binaryTagHolder(PlaceholderAPI.setPlaceholders(player, ((BinaryTagHolder) value).string())));
|
rebuilt.put(entry.getKey(), BinaryTagHolder.binaryTagHolder(replacer.apply(((BinaryTagHolder) value).string())));
|
||||||
}
|
}
|
||||||
|
|
||||||
return HoverEvent.showItem(item.item(), item.count(), rebuilt);
|
return HoverEvent.showItem(item.item(), item.count(), rebuilt);
|
||||||
|
|||||||
Reference in New Issue
Block a user