feat: add expansion type to PlaceholderExpansion

This commit is contained in:
iGabyTM 2023-07-09 00:07:03 +03:00
parent 6465aea539
commit 7e7646ebdf
No known key found for this signature in database
GPG Key ID: 4F14082F3710B7CE
2 changed files with 53 additions and 22 deletions

View File

@ -40,6 +40,14 @@ import org.jetbrains.annotations.Nullable;
*/
public abstract class PlaceholderExpansion extends PlaceholderHook {
/**
* The type is {@link Type#INTERNAL} by default.
* For external expansions, the type is updated on {@link me.clip.placeholderapi.expansion.manager.LocalExpansionManager#register(Class) register}.
* @since 2.11.4
*/
@ApiStatus.Internal
protected Type expansionType = Type.INTERNAL;
/**
* The placeholder identifier of this expansion. May not contain {@literal %},
* {@literal {}} or _
@ -136,7 +144,7 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
* @return true if this expansion is now registered with PlaceholderAPI
*/
public boolean register() {
return getPlaceholderAPI().getLocalExpansionManager().register(this, false);
return getPlaceholderAPI().getLocalExpansionManager().register(this);
}
/**
@ -159,6 +167,27 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
return PlaceholderAPIPlugin.getInstance();
}
/**
* Get the type of the expansion
*
* @return the type of the expansion
* @since 2.11.4
*/
@ApiStatus.Internal
public Type getExpansionType() {
return expansionType;
}
/**
* Set the type of the expansion
* @param expansionType the new type
* @since 2.11.4
*/
@ApiStatus.Internal
public void setExpansionType(Type expansionType) {
this.expansionType = expansionType;
}
// === Configuration ===
/**
@ -166,7 +195,7 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
* null when not specified.
* <br>You may use the {@link Configurable} interface to define default values set
*
* @return ConfigurationSection that this epxpansion has.
* @return ConfigurationSection that this expansion has.
*/
@Nullable
public final ConfigurationSection getConfigSection() {
@ -394,8 +423,8 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
*/
@Override
public final String toString() {
return String.format("PlaceholderExpansion[name: '%s', author: '%s', version: '%s']", getName(),
getAuthor(), getVersion());
return String.format("PlaceholderExpansion[name: '%s', author: '%s', version: '%s', type: '%s']", getName(),
getAuthor(), getVersion(), getExpansionType());
}
// === Deprecated API ===
@ -432,4 +461,19 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
public String getLink() {
return null;
}
public enum Type {
/**
* An expansion provided by a plugin is considered internal
*/
INTERNAL,
/**
* An expansion loaded from the expansions folder is considered external
*/
EXTERNAL
}
}

View File

@ -187,6 +187,8 @@ public final class LocalExpansionManager implements Listener {
return Optional.empty();
}
}
expansion.setExpansionType(PlaceholderExpansion.Type.EXTERNAL);
if (!expansion.register()) {
Msg.warn("Cannot load expansion %s due to an unknown issue.", expansion.getIdentifier());
@ -212,11 +214,10 @@ public final class LocalExpansionManager implements Listener {
/**
* Attempt to register a {@link PlaceholderExpansion}
* @param expansion the expansion to register
* @param isExternalExpansion whether the expansion is external (loaded from the {@link LocalExpansionManager#EXPANSIONS_FOLDER_NAME expansions folder})
* @return if the expansion was registered
*/
@ApiStatus.Internal
public boolean register(@NotNull final PlaceholderExpansion expansion, final boolean isExternalExpansion) {
public boolean register(@NotNull final PlaceholderExpansion expansion) {
final String identifier = expansion.getIdentifier().toLowerCase(Locale.ROOT);
if (!expansion.canRegister()) {
@ -224,9 +225,8 @@ public final class LocalExpansionManager implements Listener {
}
// Avoid loading two external expansions with the same identifier
if (isExternalExpansion && expansions.containsKey(identifier)) {
Msg.warn("Failed to load expansion %s. Identifier is already in use.",
expansion.getIdentifier());
if (expansion.getExpansionType() == PlaceholderExpansion.Type.EXTERNAL && expansions.containsKey(identifier)) {
Msg.warn("Failed to load external expansion %s. Identifier is already in use.", expansion.getIdentifier());
return false;
}
@ -315,19 +315,6 @@ public final class LocalExpansionManager implements Listener {
return true;
}
/**
* Overload for {@link #register(PlaceholderExpansion, boolean)} to provide backwards compatibility for expansions / plugins
* that call this method directly. It is the equivalent of {@code register(expansion, false)}
* @param expansion the expansion to register
* @return if the expansion was registered
* @deprecated use {@link #register(PlaceholderExpansion, boolean)} directly
*/
@Deprecated
@ApiStatus.Internal
public boolean register(@NotNull final PlaceholderExpansion expansion) {
return register(expansion, false);
}
@ApiStatus.Internal
public boolean unregister(@NotNull final PlaceholderExpansion expansion) {
if (expansions.remove(expansion.getIdentifier().toLowerCase(Locale.ROOT)) == null) {