Improve placeholder API

This commit is contained in:
filoghost 2021-09-01 20:30:19 +02:00
parent cd6dd1b4d2
commit 899ade7ce2
6 changed files with 32 additions and 7 deletions

View File

@ -8,6 +8,7 @@ package me.filoghost.holographicdisplays.api;
import me.filoghost.holographicdisplays.api.hologram.Hologram; import me.filoghost.holographicdisplays.api.hologram.Hologram;
import me.filoghost.holographicdisplays.api.internal.HolographicDisplaysAPIProvider; import me.filoghost.holographicdisplays.api.internal.HolographicDisplaysAPIProvider;
import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholderReplacer; import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholderReplacer;
import me.filoghost.holographicdisplays.api.placeholder.RegisteredPlaceholder;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -92,7 +93,7 @@ public interface HolographicDisplaysAPI {
* @return a collection of placeholder identifiers * @return a collection of placeholder identifiers
* @since 1 * @since 1
*/ */
@NotNull Collection<String> getRegisteredPlaceholders(); @NotNull Collection<RegisteredPlaceholder> getRegisteredPlaceholders();
/** /**
* Unregisters a placeholder. * Unregisters a placeholder.

View File

@ -0,0 +1,12 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.holographicdisplays.api.placeholder;
public interface RegisteredPlaceholder {
String getIdentifier();
}

View File

@ -10,6 +10,7 @@ import me.filoghost.holographicdisplays.api.HolographicDisplaysAPI;
import me.filoghost.holographicdisplays.api.hologram.Hologram; import me.filoghost.holographicdisplays.api.hologram.Hologram;
import me.filoghost.holographicdisplays.api.Position; import me.filoghost.holographicdisplays.api.Position;
import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholderReplacer; import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholderReplacer;
import me.filoghost.holographicdisplays.api.placeholder.RegisteredPlaceholder;
import me.filoghost.holographicdisplays.plugin.hologram.base.ImmutablePosition; import me.filoghost.holographicdisplays.plugin.hologram.base.ImmutablePosition;
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderRegistry; import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderRegistry;
import org.bukkit.Location; import org.bukkit.Location;
@ -86,8 +87,8 @@ class DefaultHolographicDisplaysAPI implements HolographicDisplaysAPI {
} }
@Override @Override
public @NotNull Collection<String> getRegisteredPlaceholders() { public @NotNull Collection<RegisteredPlaceholder> getRegisteredPlaceholders() {
return placeholderRegistry.getRegisteredIdentifiers(plugin); return placeholderRegistry.getRegisteredPlaceholders(plugin);
} }
@Override @Override

View File

@ -9,6 +9,8 @@ import com.gmail.filoghost.holographicdisplays.api.Hologram;
import com.gmail.filoghost.holographicdisplays.api.internal.HologramsAPIProvider; import com.gmail.filoghost.holographicdisplays.api.internal.HologramsAPIProvider;
import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer; import com.gmail.filoghost.holographicdisplays.api.placeholder.PlaceholderReplacer;
import me.filoghost.fcommons.Preconditions; import me.filoghost.fcommons.Preconditions;
import me.filoghost.fcommons.collection.CollectionUtils;
import me.filoghost.holographicdisplays.api.placeholder.RegisteredPlaceholder;
import me.filoghost.holographicdisplays.plugin.hologram.base.ImmutablePosition; import me.filoghost.holographicdisplays.plugin.hologram.base.ImmutablePosition;
import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderRegistry; import me.filoghost.holographicdisplays.plugin.placeholder.registry.PlaceholderRegistry;
import org.bukkit.Location; import org.bukkit.Location;
@ -81,7 +83,8 @@ public class V2HologramsAPIProvider extends HologramsAPIProvider {
public Collection<String> getRegisteredPlaceholders(Plugin plugin) { public Collection<String> getRegisteredPlaceholders(Plugin plugin) {
Preconditions.notNull(plugin, "plugin"); Preconditions.notNull(plugin, "plugin");
return placeholderRegistry.getRegisteredIdentifiers(plugin); List<RegisteredPlaceholder> registeredPlaceholders = placeholderRegistry.getRegisteredPlaceholders(plugin);
return CollectionUtils.toArrayList(registeredPlaceholders, RegisteredPlaceholder::getIdentifier);
} }
@Override @Override

View File

@ -5,6 +5,7 @@
*/ */
package me.filoghost.holographicdisplays.plugin.placeholder.registry; package me.filoghost.holographicdisplays.plugin.placeholder.registry;
import me.filoghost.holographicdisplays.api.placeholder.RegisteredPlaceholder;
import me.filoghost.holographicdisplays.plugin.placeholder.PlaceholderException; import me.filoghost.holographicdisplays.plugin.placeholder.PlaceholderException;
import me.filoghost.holographicdisplays.plugin.placeholder.StandardPlaceholder; import me.filoghost.holographicdisplays.plugin.placeholder.StandardPlaceholder;
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PlaceholderIdentifier; import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PlaceholderIdentifier;
@ -16,10 +17,12 @@ public abstract class PlaceholderExpansion {
private final PluginName pluginName; private final PluginName pluginName;
private final PlaceholderIdentifier identifier; private final PlaceholderIdentifier identifier;
private final RegisteredPlaceholder registeredPlaceholder;
public PlaceholderExpansion(Plugin plugin, String identifier) { public PlaceholderExpansion(Plugin plugin, String identifier) {
this.pluginName = new PluginName(plugin); this.pluginName = new PluginName(plugin);
this.identifier = new PlaceholderIdentifier(identifier); this.identifier = new PlaceholderIdentifier(identifier);
this.registeredPlaceholder = identifier::toString;
} }
public PluginName getPluginName() { public PluginName getPluginName() {
@ -30,6 +33,10 @@ public abstract class PlaceholderExpansion {
return identifier; return identifier;
} }
public RegisteredPlaceholder asRegisteredPlaceholder() {
return registeredPlaceholder;
}
public abstract boolean isIndividual(); public abstract boolean isIndividual();
public abstract @Nullable StandardPlaceholder createPlaceholder(String argument) throws PlaceholderException; public abstract @Nullable StandardPlaceholder createPlaceholder(String argument) throws PlaceholderException;

View File

@ -14,6 +14,7 @@ import me.filoghost.holographicdisplays.api.placeholder.IndividualPlaceholderRep
import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholder; import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholder;
import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholderFactory; import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholderFactory;
import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholderReplacer; import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholderReplacer;
import me.filoghost.holographicdisplays.api.placeholder.RegisteredPlaceholder;
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PlaceholderIdentifier; import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PlaceholderIdentifier;
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PlaceholderOccurrence; import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PlaceholderOccurrence;
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PluginName; import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PluginName;
@ -96,12 +97,12 @@ public class PlaceholderRegistry {
} }
} }
public List<String> getRegisteredIdentifiers(Plugin plugin) { public List<RegisteredPlaceholder> getRegisteredPlaceholders(Plugin plugin) {
PluginName pluginName = new PluginName(plugin); PluginName pluginName = new PluginName(plugin);
List<String> identifiers = new ArrayList<>(); List<RegisteredPlaceholder> identifiers = new ArrayList<>();
for (PlaceholderExpansion expansion : placeholderExpansions.column(pluginName).values()) { for (PlaceholderExpansion expansion : placeholderExpansions.column(pluginName).values()) {
identifiers.add(expansion.getIdentifier().toString()); identifiers.add(expansion.asRegisteredPlaceholder());
} }
return identifiers; return identifiers;