mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2025-02-05 15:55:28 +01:00
updated to phase out PlaceholderHook
This commit is contained in:
parent
c3e0c1fb64
commit
86002f50e6
@ -181,40 +181,20 @@ public final class PlaceholderAPIPlugin extends JavaPlugin
|
|||||||
private void setupMetrics()
|
private void setupMetrics()
|
||||||
{
|
{
|
||||||
final Metrics metrics = new Metrics(this);
|
final Metrics metrics = new Metrics(this);
|
||||||
metrics.addCustomChart(
|
metrics.addCustomChart(new Metrics.SimplePie("using_expansion_cloud", () -> getPlaceholderAPIConfig().isCloudEnabled() ? "yes" : "no"));
|
||||||
new Metrics.SimplePie(
|
|
||||||
"using_expansion_cloud",
|
|
||||||
() -> getPlaceholderAPIConfig().isCloudEnabled() ? "yes" : "no"));
|
|
||||||
|
|
||||||
metrics.addCustomChart(
|
metrics.addCustomChart(new Metrics.SimplePie("using_spigot", () -> getServerVersion().isSpigot() ? "yes" : "no"));
|
||||||
new Metrics.SimplePie("using_spigot", () -> getServerVersion().isSpigot() ? "yes" : "no"));
|
|
||||||
|
|
||||||
metrics.addCustomChart(
|
metrics.addCustomChart(new Metrics.AdvancedPie("expansions_used", () -> {
|
||||||
new Metrics.AdvancedPie(
|
final Map<String, Integer> values = new HashMap<>();
|
||||||
"expansions_used",
|
|
||||||
() -> {
|
|
||||||
Map<String, Integer> map = new HashMap<>();
|
|
||||||
Map<String, PlaceholderHook> hooks = PlaceholderAPI.getPlaceholders();
|
|
||||||
|
|
||||||
if (!hooks.isEmpty())
|
for (final PlaceholderExpansion expansion : getLocalExpansionManager().getExpansions())
|
||||||
{
|
{
|
||||||
|
values.put(expansion.getRequiredPlugin() == null ? expansion.getIdentifier() : expansion.getRequiredPlugin(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
for (PlaceholderHook hook : hooks.values())
|
return values;
|
||||||
{
|
}));
|
||||||
if (hook instanceof PlaceholderExpansion)
|
|
||||||
{
|
|
||||||
PlaceholderExpansion expansion = (PlaceholderExpansion) hook;
|
|
||||||
map.put(
|
|
||||||
expansion.getRequiredPlugin() == null
|
|
||||||
? expansion.getIdentifier()
|
|
||||||
: expansion.getRequiredPlugin(),
|
|
||||||
1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return map;
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupExpansions()
|
private void setupExpansions()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package me.clip.placeholderapi.replacer;
|
package me.clip.placeholderapi.replacer;
|
||||||
|
|
||||||
import me.clip.placeholderapi.PlaceholderHook;
|
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@ -20,8 +20,9 @@ public final class CharsReplacer implements Replacer
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public @NotNull String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function<String, @Nullable PlaceholderHook> lookup)
|
public String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function<String, @Nullable PlaceholderExpansion> lookup)
|
||||||
{
|
{
|
||||||
final char[] chars = text.toCharArray();
|
final char[] chars = text.toCharArray();
|
||||||
final StringBuilder builder = new StringBuilder(text.length());
|
final StringBuilder builder = new StringBuilder(text.length());
|
||||||
@ -142,7 +143,7 @@ public final class CharsReplacer implements Replacer
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
final PlaceholderHook placeholder = lookup.apply(identifierString);
|
final PlaceholderExpansion placeholder = lookup.apply(identifierString);
|
||||||
if (placeholder == null)
|
if (placeholder == null)
|
||||||
{
|
{
|
||||||
builder.append(closure.head).append(identifierString);
|
builder.append(closure.head).append(identifierString);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package me.clip.placeholderapi.replacer;
|
package me.clip.placeholderapi.replacer;
|
||||||
|
|
||||||
import me.clip.placeholderapi.PlaceholderHook;
|
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@ -22,8 +22,9 @@ public final class RegexReplacer implements Replacer
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public @NotNull String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function<String, @Nullable PlaceholderHook> lookup)
|
public String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function<String, @Nullable PlaceholderExpansion> lookup)
|
||||||
{
|
{
|
||||||
final Matcher matcher = pattern.matcher(text);
|
final Matcher matcher = pattern.matcher(text);
|
||||||
if (!matcher.find())
|
if (!matcher.find())
|
||||||
@ -38,13 +39,13 @@ public final class RegexReplacer implements Replacer
|
|||||||
final String identifier = matcher.group("identifier");
|
final String identifier = matcher.group("identifier");
|
||||||
final String parameters = matcher.group("parameters");
|
final String parameters = matcher.group("parameters");
|
||||||
|
|
||||||
final PlaceholderHook hook = lookup.apply(identifier);
|
final PlaceholderExpansion expansion = lookup.apply(identifier);
|
||||||
if (hook == null)
|
if (expansion == null)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String requested = hook.onRequest(player, parameters);
|
final String requested = expansion.onRequest(player, parameters);
|
||||||
matcher.appendReplacement(builder, requested != null ? requested : matcher.group(0));
|
matcher.appendReplacement(builder, requested != null ? requested : matcher.group(0));
|
||||||
}
|
}
|
||||||
while (matcher.find());
|
while (matcher.find());
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package me.clip.placeholderapi.replacer;
|
package me.clip.placeholderapi.replacer;
|
||||||
|
|
||||||
import me.clip.placeholderapi.PlaceholderHook;
|
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
@ -11,7 +11,7 @@ public interface Replacer
|
|||||||
{
|
{
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function<String, @Nullable PlaceholderHook> lookup);
|
String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function<String, @Nullable PlaceholderExpansion> lookup);
|
||||||
|
|
||||||
|
|
||||||
enum Closure
|
enum Closure
|
||||||
|
@ -1,17 +1,11 @@
|
|||||||
package me.clip.placeholderapi;
|
package me.clip.placeholderapi;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||||
import me.clip.placeholderapi.replacer.CharsReplacer;
|
import me.clip.placeholderapi.replacer.CharsReplacer;
|
||||||
import me.clip.placeholderapi.replacer.RegexReplacer;
|
import me.clip.placeholderapi.replacer.RegexReplacer;
|
||||||
import me.clip.placeholderapi.replacer.Replacer;
|
import me.clip.placeholderapi.replacer.Replacer;
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
public interface Values
|
public interface Values
|
||||||
{
|
{
|
||||||
@ -19,137 +13,16 @@ public interface Values
|
|||||||
String SMALL_TEXT = "My name is %player_name%";
|
String SMALL_TEXT = "My name is %player_name%";
|
||||||
String LARGE_TEXT = "My name is %player_name% and my location is (%player_x%, %player_y%, %player_z%), this placeholder is invalid %server_name%";
|
String LARGE_TEXT = "My name is %player_name% and my location is (%player_x%, %player_y%, %player_z%), this placeholder is invalid %server_name%";
|
||||||
|
|
||||||
ImmutableMap<String, PlaceholderHook> PLACEHOLDERS = ImmutableMap.<String, PlaceholderHook>builder()
|
ImmutableMap<String, PlaceholderExpansion> PLACEHOLDERS = ImmutableMap.<String, PlaceholderExpansion>builder()
|
||||||
.put("player", new MockPlayerPlaceholderHook())
|
.put("player", new MockPlayerPlaceholderExpansion())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|
||||||
Replacer CHARS_REPLACER = new CharsReplacer(Replacer.Closure.PERCENT);
|
Replacer CHARS_REPLACER = new CharsReplacer(Replacer.Closure.PERCENT);
|
||||||
Replacer REGEX_REPLACER = new RegexReplacer(Replacer.Closure.PERCENT);
|
Replacer REGEX_REPLACER = new RegexReplacer(Replacer.Closure.PERCENT);
|
||||||
Replacer TESTS_REPLACER = new Replacer()
|
|
||||||
{
|
|
||||||
private final Set<Character> COLOR_CODES = ImmutableSet.of('0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
||||||
'a', 'b', 'c', 'd', 'e', 'f', 'k', 'l', 'm', 'o', 'r', 'x');
|
|
||||||
|
|
||||||
private final boolean colorize = true;
|
|
||||||
private final Replacer.Closure closure = Replacer.Closure.PERCENT;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull String apply(final @NotNull String text, final @Nullable OfflinePlayer player, final @NotNull Function<String, @Nullable PlaceholderHook> lookup)
|
|
||||||
{
|
|
||||||
char[] chars = text.toCharArray();
|
|
||||||
StringBuilder builder = new StringBuilder(chars.length);
|
|
||||||
|
|
||||||
// This won't cause memory leaks. It's inside a method. And we want to use setLength instead of
|
|
||||||
// creating a new string builder to use the maximum capacity and avoid initializing new objects.
|
|
||||||
StringBuilder identifier = new StringBuilder(50);
|
|
||||||
PlaceholderHook handler = null;
|
|
||||||
|
|
||||||
// Stages:
|
|
||||||
// Stage -1: Look for the color code in the next character.
|
|
||||||
// Stage 0: No closures detected, or the detected identifier is invalid. We're going forward while appending the characters normally.
|
|
||||||
// Stage 1: The closure has been detected, looking for the placeholder identifier...
|
|
||||||
// Stage 2: Detected the identifier and the parameter. Translating the placeholder...
|
|
||||||
int stage = 0;
|
|
||||||
|
|
||||||
for (char ch : chars)
|
|
||||||
{
|
|
||||||
if (stage == -1 && COLOR_CODES.contains(ch))
|
|
||||||
{
|
|
||||||
builder.append(ChatColor.COLOR_CHAR).append(ch);
|
|
||||||
stage = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the placeholder starts or ends.
|
|
||||||
if (ch == closure.head || ch == closure.tail)
|
|
||||||
{
|
|
||||||
// If the placeholder ends.
|
|
||||||
if (stage == 2)
|
|
||||||
{
|
|
||||||
String parameter = identifier.toString();
|
|
||||||
String translated = handler.onRequest(player, parameter);
|
|
||||||
|
|
||||||
if (translated == null)
|
|
||||||
{
|
|
||||||
String name = "";
|
|
||||||
builder.append(closure.head).append(name).append('_').append(parameter).append(closure.tail);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
builder.append(translated);
|
|
||||||
}
|
|
||||||
|
|
||||||
identifier.setLength(0);
|
|
||||||
stage = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else if (stage == 1)
|
|
||||||
{ // If it just started | Double closures | If it's still hasn't detected the indentifier, reset.
|
|
||||||
builder.append(closure.head).append(identifier);
|
|
||||||
}
|
|
||||||
|
|
||||||
identifier.setLength(0);
|
|
||||||
stage = 1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Placeholder identifier started.
|
|
||||||
if (stage == 1)
|
|
||||||
{
|
|
||||||
// Compare the current character with the idenfitier's.
|
|
||||||
// We reached the end of our identifier.
|
|
||||||
if (ch == '_')
|
|
||||||
{
|
|
||||||
handler = lookup.apply(identifier.toString());
|
|
||||||
if (handler == null)
|
|
||||||
{
|
|
||||||
builder.append(closure.head).append(identifier).append('_');
|
|
||||||
stage = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
identifier.setLength(0);
|
|
||||||
stage = 2;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep building the identifier name.
|
|
||||||
identifier.append(ch);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Building the placeholder parameter.
|
|
||||||
if (stage == 2)
|
|
||||||
{
|
|
||||||
identifier.append(ch);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Nothing placeholder related was found.
|
|
||||||
if (colorize && ch == '&')
|
|
||||||
{
|
|
||||||
stage = -1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
builder.append(ch);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (identifier != null)
|
|
||||||
{
|
|
||||||
if (stage > 0)
|
|
||||||
{
|
|
||||||
builder.append(closure.tail);
|
|
||||||
}
|
|
||||||
builder.append(identifier);
|
|
||||||
}
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
final class MockPlayerPlaceholderHook extends PlaceholderHook
|
final class MockPlayerPlaceholderExpansion extends PlaceholderExpansion
|
||||||
{
|
{
|
||||||
|
|
||||||
public static final String PLAYER_X = String.valueOf(10);
|
public static final String PLAYER_X = String.valueOf(10);
|
||||||
@ -158,6 +31,24 @@ public interface Values
|
|||||||
public static final String PLAYER_NAME = "Sxtanna";
|
public static final String PLAYER_NAME = "Sxtanna";
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getIdentifier()
|
||||||
|
{
|
||||||
|
return "player";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthor()
|
||||||
|
{
|
||||||
|
return "Sxtanna";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getVersion()
|
||||||
|
{
|
||||||
|
return "1.0";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String onRequest(final OfflinePlayer player, final String params)
|
public String onRequest(final OfflinePlayer player, final String params)
|
||||||
{
|
{
|
||||||
|
@ -3,10 +3,10 @@ package me.clip.placeholderapi.replacer;
|
|||||||
import me.clip.placeholderapi.Values;
|
import me.clip.placeholderapi.Values;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static me.clip.placeholderapi.Values.MockPlayerPlaceholderHook.PLAYER_NAME;
|
import static me.clip.placeholderapi.Values.MockPlayerPlaceholderExpansion.PLAYER_NAME;
|
||||||
import static me.clip.placeholderapi.Values.MockPlayerPlaceholderHook.PLAYER_X;
|
import static me.clip.placeholderapi.Values.MockPlayerPlaceholderExpansion.PLAYER_X;
|
||||||
import static me.clip.placeholderapi.Values.MockPlayerPlaceholderHook.PLAYER_Y;
|
import static me.clip.placeholderapi.Values.MockPlayerPlaceholderExpansion.PLAYER_Y;
|
||||||
import static me.clip.placeholderapi.Values.MockPlayerPlaceholderHook.PLAYER_Z;
|
import static me.clip.placeholderapi.Values.MockPlayerPlaceholderExpansion.PLAYER_Z;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public final class ReplacerUnitTester
|
public final class ReplacerUnitTester
|
||||||
|
Loading…
Reference in New Issue
Block a user