Start merging chars replacer logic into adventure replacer

very rough no judge
This commit is contained in:
PiggyPiglet
2025-10-25 13:04:24 +08:00
parent 683dc6e8e3
commit 2a8bb3695c
2 changed files with 82 additions and 2 deletions

View File

@@ -29,6 +29,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.Optional; import java.util.Optional;
public abstract class PlaceholderHook { public abstract class PlaceholderHook {
@Deprecated
@Nullable @Nullable
public String onRequest(final OfflinePlayer player, @NotNull final String params) { public String onRequest(final OfflinePlayer player, @NotNull final String params) {
if (player != null && player.isOnline()) { if (player != null && player.isOnline()) {
@@ -45,8 +46,8 @@ public abstract class PlaceholderHook {
} }
@Nullable @Nullable
public Component onPlaceholderComponentRequest(final Player player, @NotNull final String params) { public Component onPlaceholderComponentRequest(final OfflinePlayer player, @NotNull final String params) {
final String result = onPlaceholderRequest(player, params); final String result = onRequest(player, params);
return result == null ? null : Component.text(result); return result == null ? null : Component.text(result);
} }

View File

@@ -24,13 +24,16 @@
package me.clip.placeholderapi.replacer; package me.clip.placeholderapi.replacer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.MatchResult; import java.util.regex.MatchResult;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.google.common.collect.Lists;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.kyori.adventure.text.*; import net.kyori.adventure.text.*;
import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEvent;
@@ -47,6 +50,20 @@ final class ComponentCharsReplacer implements ComponentRenderer<ComponentCharsRe
//static final TextReplacementRenderer INSTANCE = new TextReplacementRenderer(); //static final TextReplacementRenderer INSTANCE = new TextReplacementRenderer();
private final OfflinePlayer player; private final OfflinePlayer player;
private final Function<String, @Nullable PlaceholderExpansion> lookup; private final Function<String, @Nullable PlaceholderExpansion> lookup;
private static final Closure CLOSURE = Closure.PERCENT;
enum Closure {
BRACKET('{', '}'),
PERCENT('%', '%');
public final char head, tail;
Closure(final char head, final char tail) {
this.head = head;
this.tail = tail;
}
}
public ComponentCharsReplacer(@Nullable final OfflinePlayer player, public ComponentCharsReplacer(@Nullable final OfflinePlayer player,
@NotNull final Function<String, @Nullable PlaceholderExpansion> lookup) { @NotNull final Function<String, @Nullable PlaceholderExpansion> lookup) {
@@ -70,6 +87,68 @@ final class ComponentCharsReplacer implements ComponentRenderer<ComponentCharsRe
TextComponent tc = (TextComponent) component; TextComponent tc = (TextComponent) component;
final String content = tc.content(); final String content = tc.content();
final char[] chars = content.toCharArray();
final StringBuilder identifier = new StringBuilder();
final StringBuilder parameters = new StringBuilder();
final Map<List<Integer>, Component> replacements = new HashMap<>();
for (int i = 0; i < chars.length; i++) {
final char l = chars[i];
if (l != CLOSURE.head || i + 1 >= chars.length) {
continue;
}
boolean identified = false;
boolean invalid = true;
boolean hadSpace = false;
while (++i < chars.length) {
final char p = chars[i];
if (p == ' ' && !identified) {
hadSpace = true;
break;
}
if (p == CLOSURE.tail) {
invalid = false;
break;
}
if (p == '_' && !identified) {
identified = true;
break;
}
if (identified) {
parameters.append(p);
} else {
identifier.append(p);
}
}
final String identifierString = identifier.toString();
final String lowerIdentifiedString = identifierString.toLowerCase();
final String parametersString = parameters.toString();
identifier.setLength(0);
parameters.setLength(0);
if (invalid) {
continue;
}
replacements.put(Lists.newArrayList(i, i + identifierString.length() + parametersString.length()), lookup.apply(lowerIdentifiedString).onPlaceholderComponentRequest(player, parametersString));
}
// do something with our replacements
final Matcher matcher = state.pattern.matcher(content); final Matcher matcher = state.pattern.matcher(content);