metrics! (and placeholders)
This commit is contained in:
106
src/main/java/cc/fascinated/placeholder/PlaceholderManager.java
Normal file
106
src/main/java/cc/fascinated/placeholder/PlaceholderManager.java
Normal file
@ -0,0 +1,106 @@
|
||||
package cc.fascinated.placeholder;
|
||||
|
||||
import cc.fascinated.Aetheria;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Log4j2
|
||||
public class PlaceholderManager {
|
||||
|
||||
private static final Map<String, AetheriaPlaceholder> PLACEHOLDERS = new ConcurrentHashMap<>();
|
||||
|
||||
public PlaceholderManager() {
|
||||
new PlaceholderAdapter().register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the provided placeholder
|
||||
*
|
||||
* @param placeholder the placeholder to register
|
||||
*/
|
||||
public static void registerPlaceholder(AetheriaPlaceholder placeholder) {
|
||||
PLACEHOLDERS.put(placeholder.getIdentifier(), placeholder);
|
||||
log.info("Registered placeholder " + placeholder.getIdentifier());
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister the provided placeholder
|
||||
*
|
||||
* @param placeholder the placeholder to unregister
|
||||
*/
|
||||
public static void unregisterPlaceholder(AetheriaPlaceholder placeholder) {
|
||||
PLACEHOLDERS.remove(placeholder.getIdentifier());
|
||||
log.info("Unregistered placeholder " + placeholder.getIdentifier());
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace PAPI placeholders in the given content.
|
||||
*
|
||||
* @param player the player to replace the placeholders for, null if offline
|
||||
* @param content the content to replace the placeholders in
|
||||
* @return the content with the placeholders replaced
|
||||
*/
|
||||
public static String replace(@Nullable Player player, String content) {
|
||||
return PlaceholderAPI.setPlaceholders(player != null && (player.isOnline()) ? player : null, content);
|
||||
}
|
||||
|
||||
static class PlaceholderAdapter extends PlaceholderExpansion {
|
||||
|
||||
@Override
|
||||
public @NotNull String getIdentifier() {
|
||||
return "aetheria";
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
@Override
|
||||
public @NotNull String getAuthor() {
|
||||
return String.join(", ", Aetheria.INSTANCE.getPluginMeta().getAuthors());
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
@Override
|
||||
public @NotNull String getVersion() {
|
||||
return Aetheria.INSTANCE.getPluginMeta().getVersion();
|
||||
}
|
||||
|
||||
/**
|
||||
* From PlaceholderAPI:
|
||||
* <p>
|
||||
* Expansions that do not use the e-cloud and instead register from the dependency should set this to true
|
||||
* to ensure that your placeholder expansion is not unregistered when the papi reload command is used
|
||||
*/
|
||||
public boolean persist() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String onRequest(@Nullable OfflinePlayer player, @NotNull String params) {
|
||||
String[] parameters = params.split("_");
|
||||
AetheriaPlaceholder placeholder = PLACEHOLDERS.get(parameters[0]);
|
||||
// Attempt to fetch the placeholder with the provided id
|
||||
if (placeholder == null) {
|
||||
log.error("Tried to fetch an unregistered placeholder: " + parameters[0]);
|
||||
return null;
|
||||
}
|
||||
// Ensure the correct amount of parameters is provided
|
||||
if (parameters.length - 1 < placeholder.getRequiredParameterCount()) {
|
||||
log.error("Invalid placeholders provided for {}, provided = {}, required = {}",
|
||||
placeholder.getIdentifier(),
|
||||
parameters.length - 1,
|
||||
placeholder.getRequiredParameterCount());
|
||||
return null;
|
||||
}
|
||||
// Trim the placeholder identifier from the parameters before parsing
|
||||
return placeholder.parse((Player) player, parameters.length > 1 ? Arrays.copyOfRange(parameters, 1, parameters.length) : parameters);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user