From 86002f50e690a450c28ba065cb488e061371e9fc Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sun, 26 Jul 2020 18:03:31 -0400 Subject: [PATCH] updated to phase out PlaceholderHook --- .../placeholderapi/PlaceholderAPIPlugin.java | 40 ++--- .../replacer/CharsReplacer.java | 7 +- .../replacer/RegexReplacer.java | 11 +- .../placeholderapi/replacer/Replacer.java | 4 +- .../java/me/clip/placeholderapi/Values.java | 153 +++--------------- .../replacer/ReplacerUnitTester.java | 8 +- 6 files changed, 48 insertions(+), 175 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/PlaceholderAPIPlugin.java b/src/main/java/me/clip/placeholderapi/PlaceholderAPIPlugin.java index fa32fe9..e973fb3 100644 --- a/src/main/java/me/clip/placeholderapi/PlaceholderAPIPlugin.java +++ b/src/main/java/me/clip/placeholderapi/PlaceholderAPIPlugin.java @@ -181,40 +181,20 @@ public final class PlaceholderAPIPlugin extends JavaPlugin private void setupMetrics() { final Metrics metrics = new Metrics(this); - metrics.addCustomChart( - new Metrics.SimplePie( - "using_expansion_cloud", - () -> getPlaceholderAPIConfig().isCloudEnabled() ? "yes" : "no")); + metrics.addCustomChart(new Metrics.SimplePie("using_expansion_cloud", () -> getPlaceholderAPIConfig().isCloudEnabled() ? "yes" : "no")); - metrics.addCustomChart( - new Metrics.SimplePie("using_spigot", () -> getServerVersion().isSpigot() ? "yes" : "no")); + metrics.addCustomChart(new Metrics.SimplePie("using_spigot", () -> getServerVersion().isSpigot() ? "yes" : "no")); - metrics.addCustomChart( - new Metrics.AdvancedPie( - "expansions_used", - () -> { - Map map = new HashMap<>(); - Map hooks = PlaceholderAPI.getPlaceholders(); + metrics.addCustomChart(new Metrics.AdvancedPie("expansions_used", () -> { + final Map values = new HashMap<>(); - if (!hooks.isEmpty()) - { + for (final PlaceholderExpansion expansion : getLocalExpansionManager().getExpansions()) + { + values.put(expansion.getRequiredPlugin() == null ? expansion.getIdentifier() : expansion.getRequiredPlugin(), 1); + } - for (PlaceholderHook hook : hooks.values()) - { - if (hook instanceof PlaceholderExpansion) - { - PlaceholderExpansion expansion = (PlaceholderExpansion) hook; - map.put( - expansion.getRequiredPlugin() == null - ? expansion.getIdentifier() - : expansion.getRequiredPlugin(), - 1); - } - } - } - - return map; - })); + return values; + })); } private void setupExpansions() diff --git a/src/main/java/me/clip/placeholderapi/replacer/CharsReplacer.java b/src/main/java/me/clip/placeholderapi/replacer/CharsReplacer.java index db9f6d8..4de1f4b 100644 --- a/src/main/java/me/clip/placeholderapi/replacer/CharsReplacer.java +++ b/src/main/java/me/clip/placeholderapi/replacer/CharsReplacer.java @@ -1,6 +1,6 @@ package me.clip.placeholderapi.replacer; -import me.clip.placeholderapi.PlaceholderHook; +import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; import org.jetbrains.annotations.NotNull; @@ -20,8 +20,9 @@ public final class CharsReplacer implements Replacer } + @NotNull @Override - public @NotNull String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function lookup) + public String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function lookup) { final char[] chars = text.toCharArray(); final StringBuilder builder = new StringBuilder(text.length()); @@ -142,7 +143,7 @@ public final class CharsReplacer implements Replacer continue; } - final PlaceholderHook placeholder = lookup.apply(identifierString); + final PlaceholderExpansion placeholder = lookup.apply(identifierString); if (placeholder == null) { builder.append(closure.head).append(identifierString); diff --git a/src/main/java/me/clip/placeholderapi/replacer/RegexReplacer.java b/src/main/java/me/clip/placeholderapi/replacer/RegexReplacer.java index 8218384..dbb3b39 100644 --- a/src/main/java/me/clip/placeholderapi/replacer/RegexReplacer.java +++ b/src/main/java/me/clip/placeholderapi/replacer/RegexReplacer.java @@ -1,6 +1,6 @@ package me.clip.placeholderapi.replacer; -import me.clip.placeholderapi.PlaceholderHook; +import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; import org.jetbrains.annotations.NotNull; @@ -22,8 +22,9 @@ public final class RegexReplacer implements Replacer } + @NotNull @Override - public @NotNull String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function lookup) + public String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function lookup) { final Matcher matcher = pattern.matcher(text); if (!matcher.find()) @@ -38,13 +39,13 @@ public final class RegexReplacer implements Replacer final String identifier = matcher.group("identifier"); final String parameters = matcher.group("parameters"); - final PlaceholderHook hook = lookup.apply(identifier); - if (hook == null) + final PlaceholderExpansion expansion = lookup.apply(identifier); + if (expansion == null) { continue; } - final String requested = hook.onRequest(player, parameters); + final String requested = expansion.onRequest(player, parameters); matcher.appendReplacement(builder, requested != null ? requested : matcher.group(0)); } while (matcher.find()); diff --git a/src/main/java/me/clip/placeholderapi/replacer/Replacer.java b/src/main/java/me/clip/placeholderapi/replacer/Replacer.java index 005c0f7..31ca757 100644 --- a/src/main/java/me/clip/placeholderapi/replacer/Replacer.java +++ b/src/main/java/me/clip/placeholderapi/replacer/Replacer.java @@ -1,6 +1,6 @@ package me.clip.placeholderapi.replacer; -import me.clip.placeholderapi.PlaceholderHook; +import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.OfflinePlayer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -11,7 +11,7 @@ public interface Replacer { @NotNull - String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function lookup); + String apply(@NotNull final String text, @Nullable final OfflinePlayer player, @NotNull final Function lookup); enum Closure diff --git a/src/test/java/me/clip/placeholderapi/Values.java b/src/test/java/me/clip/placeholderapi/Values.java index 37b393c..f27905b 100644 --- a/src/test/java/me/clip/placeholderapi/Values.java +++ b/src/test/java/me/clip/placeholderapi/Values.java @@ -1,17 +1,11 @@ package me.clip.placeholderapi; 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.RegexReplacer; import me.clip.placeholderapi.replacer.Replacer; -import org.bukkit.ChatColor; 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 { @@ -19,137 +13,16 @@ public interface Values 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%"; - ImmutableMap PLACEHOLDERS = ImmutableMap.builder() - .put("player", new MockPlayerPlaceholderHook()) + ImmutableMap PLACEHOLDERS = ImmutableMap.builder() + .put("player", new MockPlayerPlaceholderExpansion()) .build(); Replacer CHARS_REPLACER = new CharsReplacer(Replacer.Closure.PERCENT); Replacer REGEX_REPLACER = new RegexReplacer(Replacer.Closure.PERCENT); - Replacer TESTS_REPLACER = new Replacer() - { - private final Set 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 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); @@ -158,6 +31,24 @@ public interface Values 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 public String onRequest(final OfflinePlayer player, final String params) { diff --git a/src/test/java/me/clip/placeholderapi/replacer/ReplacerUnitTester.java b/src/test/java/me/clip/placeholderapi/replacer/ReplacerUnitTester.java index 02ab33a..c20da41 100644 --- a/src/test/java/me/clip/placeholderapi/replacer/ReplacerUnitTester.java +++ b/src/test/java/me/clip/placeholderapi/replacer/ReplacerUnitTester.java @@ -3,10 +3,10 @@ package me.clip.placeholderapi.replacer; import me.clip.placeholderapi.Values; import org.junit.jupiter.api.Test; -import static me.clip.placeholderapi.Values.MockPlayerPlaceholderHook.PLAYER_NAME; -import static me.clip.placeholderapi.Values.MockPlayerPlaceholderHook.PLAYER_X; -import static me.clip.placeholderapi.Values.MockPlayerPlaceholderHook.PLAYER_Y; -import static me.clip.placeholderapi.Values.MockPlayerPlaceholderHook.PLAYER_Z; +import static me.clip.placeholderapi.Values.MockPlayerPlaceholderExpansion.PLAYER_NAME; +import static me.clip.placeholderapi.Values.MockPlayerPlaceholderExpansion.PLAYER_X; +import static me.clip.placeholderapi.Values.MockPlayerPlaceholderExpansion.PLAYER_Y; +import static me.clip.placeholderapi.Values.MockPlayerPlaceholderExpansion.PLAYER_Z; import static org.junit.jupiter.api.Assertions.assertEquals; public final class ReplacerUnitTester