From f61b6acfa875ddce40a2d4fe6c300510254144c3 Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Sun, 26 Jul 2020 23:01:12 -0400 Subject: [PATCH] updated placeholderexpansion to define its own unregister method --- .../local/CommandExpansionUnregister.java | 2 +- .../expansion/PlaceholderExpansion.java | 46 ++++++++++++++++- .../manager/LocalExpansionManager.java | 51 ++++++++++--------- 3 files changed, 72 insertions(+), 27 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/commands/impl/local/CommandExpansionUnregister.java b/src/main/java/me/clip/placeholderapi/commands/impl/local/CommandExpansionUnregister.java index ccc0d2f..42936c2 100644 --- a/src/main/java/me/clip/placeholderapi/commands/impl/local/CommandExpansionUnregister.java +++ b/src/main/java/me/clip/placeholderapi/commands/impl/local/CommandExpansionUnregister.java @@ -39,7 +39,7 @@ public final class CommandExpansionUnregister extends PlaceholderCommand } - final String message = !plugin.getLocalExpansionManager().unregister(expansion.get()) ? + final String message = !expansion.get().unregister() ? "&cFailed to unregister expansion: &f" : "&aSuccessfully unregistered expansion: &f"; diff --git a/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java b/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java index 4b313ee..6eab658 100644 --- a/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java +++ b/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java @@ -20,7 +20,6 @@ */ package me.clip.placeholderapi.expansion; -import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.PlaceholderHook; import org.bukkit.Bukkit; @@ -32,6 +31,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Collections; import java.util.List; +import java.util.Objects; public abstract class PlaceholderExpansion extends PlaceholderHook { @@ -122,7 +122,7 @@ public abstract class PlaceholderExpansion extends PlaceholderHook */ public final boolean isRegistered() { - return PlaceholderAPI.isRegistered(getIdentifier()); + return getPlaceholderAPI().getLocalExpansionManager().findExpansionByIdentifier(getIdentifier()).map(it -> it.equals(this)).orElse(false); } @@ -141,12 +141,24 @@ public abstract class PlaceholderExpansion extends PlaceholderHook * Attempt to register this PlaceholderExpansion * * @return true if this expansion is now registered with PlaceholderAPI + * @deprecated This is going to be final in the future, startup and shutdown logic will have their own methods soon. */ + @Deprecated public boolean register() { return canRegister() && getPlaceholderAPI().getLocalExpansionManager().register(this); } + /** + * Attempt to unregister this PlaceholderExpansion + * + * @return true if this expansion is now unregistered with PlaceholderAPI + */ + public final boolean unregister() + { + return getPlaceholderAPI().getLocalExpansionManager().unregister(this); + } + /** * Quick getter for the {@link PlaceholderAPIPlugin} instance @@ -222,6 +234,36 @@ public abstract class PlaceholderExpansion extends PlaceholderHook return section != null && section.contains(path); } + @Override + public final boolean equals(final Object o) + { + if (this == o) + { + return true; + } + if (!(o instanceof PlaceholderExpansion)) + { + return false; + } + + final PlaceholderExpansion expansion = (PlaceholderExpansion) o; + + return getIdentifier().equals(expansion.getIdentifier()) && + getAuthor().equals(expansion.getAuthor()) && + getVersion().equals(expansion.getVersion()); + } + + @Override + public final int hashCode() + { + return Objects.hash(getIdentifier(), getAuthor(), getVersion()); + } + + @Override + public final String toString() + { + return String.format("PlaceholderExpansion[name: '%s', author: '%s', version: '%s']", getName(), getAuthor(), getVersion()); + } // === Deprecated API === diff --git a/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java b/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java index f5689cc..efd1e8e 100644 --- a/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java +++ b/src/main/java/me/clip/placeholderapi/expansion/manager/LocalExpansionManager.java @@ -124,6 +124,27 @@ public final class LocalExpansionManager implements Listener } + public Optional register(@NotNull final Class clazz) + { + try + { + final PlaceholderExpansion expansion = createExpansionInstance(clazz); + if (expansion == null || !expansion.register()) + { + return Optional.empty(); + } + + return Optional.of(expansion); + } + catch (final LinkageError ex) + { + plugin.getLogger().severe("expansion class " + clazz.getSimpleName() + " is outdated: \n" + + "Failed to load due to a [" + ex.getClass().getSimpleName() + "], attempted to use " + ex.getMessage()); + } + + return Optional.empty(); + } + /** * Do not call this method yourself, use {@link PlaceholderExpansion#register()} */ @@ -188,7 +209,7 @@ public final class LocalExpansionManager implements Listener } final PlaceholderExpansion removed = expansions.get(expansion.getIdentifier()); - if (removed != null && !unregister(removed)) + if (removed != null && !removed.unregister()) { return false; } @@ -228,27 +249,9 @@ public final class LocalExpansionManager implements Listener return true; } - public Optional register(@NotNull final Class clazz) - { - try - { - final PlaceholderExpansion expansion = createExpansionInstance(clazz); - if (expansion == null || !expansion.register()) - { - return Optional.empty(); - } - - return Optional.of(expansion); - } - catch (final LinkageError ex) - { - plugin.getLogger().severe("expansion class " + clazz.getSimpleName() + " is outdated: \n" + - "Failed to load due to a [" + ex.getClass().getSimpleName() + "], attempted to use " + ex.getMessage()); - } - - return Optional.empty(); - } - + /** + * Do not call this method yourself, use {@link PlaceholderExpansion#unregister()} + */ public boolean unregister(@NotNull final PlaceholderExpansion expansion) { if (expansions.remove(expansion.getIdentifier()) == null) @@ -314,7 +317,7 @@ public final class LocalExpansionManager implements Listener continue; } - unregister(expansion); + expansion.unregister(); } } @@ -399,7 +402,7 @@ public final class LocalExpansionManager implements Listener continue; } - unregister(expansion); + expansion.unregister(); plugin.getLogger().info("Unregistered placeholder expansion: " + expansion.getName()); } }