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.internal.HolographicDisplaysAPIProvider;
import me.filoghost.holographicdisplays.api.placeholder.GlobalPlaceholderReplacer;
import me.filoghost.holographicdisplays.api.placeholder.RegisteredPlaceholder;
import org.bukkit.Location;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
@ -92,7 +93,7 @@ public interface HolographicDisplaysAPI {
* @return a collection of placeholder identifiers
* @since 1
*/
@NotNull Collection<String> getRegisteredPlaceholders();
@NotNull Collection<RegisteredPlaceholder> getRegisteredPlaceholders();
/**
* 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.Position;
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.placeholder.registry.PlaceholderRegistry;
import org.bukkit.Location;
@ -86,8 +87,8 @@ class DefaultHolographicDisplaysAPI implements HolographicDisplaysAPI {
}
@Override
public @NotNull Collection<String> getRegisteredPlaceholders() {
return placeholderRegistry.getRegisteredIdentifiers(plugin);
public @NotNull Collection<RegisteredPlaceholder> getRegisteredPlaceholders() {
return placeholderRegistry.getRegisteredPlaceholders(plugin);
}
@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.placeholder.PlaceholderReplacer;
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.placeholder.registry.PlaceholderRegistry;
import org.bukkit.Location;
@ -81,7 +83,8 @@ public class V2HologramsAPIProvider extends HologramsAPIProvider {
public Collection<String> getRegisteredPlaceholders(Plugin plugin) {
Preconditions.notNull(plugin, "plugin");
return placeholderRegistry.getRegisteredIdentifiers(plugin);
List<RegisteredPlaceholder> registeredPlaceholders = placeholderRegistry.getRegisteredPlaceholders(plugin);
return CollectionUtils.toArrayList(registeredPlaceholders, RegisteredPlaceholder::getIdentifier);
}
@Override

View File

@ -5,6 +5,7 @@
*/
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.StandardPlaceholder;
import me.filoghost.holographicdisplays.plugin.placeholder.parsing.PlaceholderIdentifier;
@ -16,10 +17,12 @@ public abstract class PlaceholderExpansion {
private final PluginName pluginName;
private final PlaceholderIdentifier identifier;
private final RegisteredPlaceholder registeredPlaceholder;
public PlaceholderExpansion(Plugin plugin, String identifier) {
this.pluginName = new PluginName(plugin);
this.identifier = new PlaceholderIdentifier(identifier);
this.registeredPlaceholder = identifier::toString;
}
public PluginName getPluginName() {
@ -30,6 +33,10 @@ public abstract class PlaceholderExpansion {
return identifier;
}
public RegisteredPlaceholder asRegisteredPlaceholder() {
return registeredPlaceholder;
}
public abstract boolean isIndividual();
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.GlobalPlaceholderFactory;
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.PlaceholderOccurrence;
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);
List<String> identifiers = new ArrayList<>();
List<RegisteredPlaceholder> identifiers = new ArrayList<>();
for (PlaceholderExpansion expansion : placeholderExpansions.column(pluginName).values()) {
identifiers.add(expansion.getIdentifier().toString());
identifiers.add(expansion.asRegisteredPlaceholder());
}
return identifiers;