updated placeholderexpansion to define its own unregister method

This commit is contained in:
Sxtanna 2020-07-26 23:01:12 -04:00
parent 8360511c50
commit f61b6acfa8
3 changed files with 72 additions and 27 deletions

View File

@ -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";

View File

@ -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 ===

View File

@ -124,6 +124,27 @@ public final class LocalExpansionManager implements Listener
}
public Optional<PlaceholderExpansion> register(@NotNull final Class<? extends PlaceholderExpansion> 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<PlaceholderExpansion> register(@NotNull final Class<? extends PlaceholderExpansion> 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());
}
}