mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI.git
synced 2025-01-02 14:28:53 +01:00
updated plugin class to be less painful to look at,
This commit is contained in:
parent
9631b087b3
commit
f47eef7442
@ -30,281 +30,302 @@ import me.clip.placeholderapi.external.EZPlaceholderHook;
|
|||||||
import me.clip.placeholderapi.listeners.PlaceholderListener;
|
import me.clip.placeholderapi.listeners.PlaceholderListener;
|
||||||
import me.clip.placeholderapi.listeners.ServerLoadEventListener;
|
import me.clip.placeholderapi.listeners.ServerLoadEventListener;
|
||||||
import me.clip.placeholderapi.updatechecker.UpdateChecker;
|
import me.clip.placeholderapi.updatechecker.UpdateChecker;
|
||||||
import me.clip.placeholderapi.util.TimeUtil;
|
import me.clip.placeholderapi.util.Msg;
|
||||||
import org.bstats.bukkit.Metrics;
|
import org.bstats.bukkit.Metrics;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.command.PluginCommand;
|
||||||
|
import org.bukkit.event.HandlerList;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.logging.Level;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Yes I have a shit load of work to do...
|
* Yes I have a shit load of work to do...
|
||||||
*
|
*
|
||||||
* @author Ryan McCarthy
|
* @author Ryan McCarthy
|
||||||
*/
|
*/
|
||||||
public class PlaceholderAPIPlugin extends JavaPlugin {
|
public final class PlaceholderAPIPlugin extends JavaPlugin
|
||||||
|
{
|
||||||
|
|
||||||
private static PlaceholderAPIPlugin instance;
|
@NotNull
|
||||||
private static SimpleDateFormat dateFormat;
|
private static final Version version = resolveServerVersion();
|
||||||
private static String booleanTrue;
|
|
||||||
private static String booleanFalse;
|
|
||||||
private static Version serverVersion;
|
|
||||||
private PlaceholderAPIConfig config;
|
|
||||||
private ExpansionManager expansionManager;
|
|
||||||
private ExpansionCloudManager expansionCloud;
|
|
||||||
private long startTime;
|
|
||||||
|
|
||||||
private static Version getVersion() {
|
|
||||||
String v = "unknown";
|
|
||||||
boolean spigot = false;
|
|
||||||
|
|
||||||
try {
|
private static PlaceholderAPIPlugin instance;
|
||||||
v = Bukkit.getServer().getClass().getPackage().getName()
|
|
||||||
.split("\\.")[3];
|
|
||||||
} catch (ArrayIndexOutOfBoundsException ex) {
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
Class.forName("org.spigotmc.SpigotConfig");
|
|
||||||
Class.forName("net.md_5.bungee.api.chat.BaseComponent");
|
|
||||||
spigot = true;
|
|
||||||
} catch (ExceptionInInitializerError | ClassNotFoundException ignored) {
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Version(v, spigot);
|
@NotNull
|
||||||
}
|
private final PlaceholderAPIConfig config = new PlaceholderAPIConfig(this);
|
||||||
|
@NotNull
|
||||||
|
private final ExpansionCloudManager cloud = new ExpansionCloudManager(this);
|
||||||
|
@NotNull
|
||||||
|
private final ExpansionManager manager = new ExpansionManager(this);
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the static instance of the main class for PlaceholderAPI. This class is not the actual API
|
|
||||||
* class, this is the main class that extends JavaPlugin. For most API methods, use static methods
|
|
||||||
* available from the class: {@link PlaceholderAPI}
|
|
||||||
*
|
|
||||||
* @return PlaceholderAPIPlugin instance
|
|
||||||
*/
|
|
||||||
public static PlaceholderAPIPlugin getInstance() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Get the configurable {@linkplain SimpleDateFormat} object that is used to parse time for
|
public void onLoad()
|
||||||
* generic time based placeholders
|
{
|
||||||
*
|
instance = this;
|
||||||
* @return date format
|
|
||||||
*/
|
|
||||||
public static SimpleDateFormat getDateFormat() {
|
|
||||||
return dateFormat != null ? dateFormat : new SimpleDateFormat(
|
|
||||||
"MM/dd/yy HH:mm:ss");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
saveDefaultConfig();
|
||||||
* Get the configurable {@linkplain String} value that should be returned when a boolean is true
|
}
|
||||||
*
|
|
||||||
* @return string value of true
|
|
||||||
*/
|
|
||||||
public static String booleanTrue() {
|
|
||||||
return booleanTrue != null ? booleanTrue : "true";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Get the configurable {@linkplain String} value that should be returned when a boolean is false
|
public void onEnable()
|
||||||
*
|
{
|
||||||
* @return string value of false
|
setupCommand();
|
||||||
*/
|
setupMetrics();
|
||||||
public static String booleanFalse() {
|
setupExpansions();
|
||||||
return booleanFalse != null ? booleanFalse : "false";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Version getServerVersion() {
|
new PlaceholderListener(this);
|
||||||
return serverVersion != null ? serverVersion : getVersion();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
if (config.isCloudEnabled())
|
||||||
public void onLoad() {
|
{
|
||||||
startTime = System.currentTimeMillis();
|
enableCloud();
|
||||||
instance = this;
|
}
|
||||||
serverVersion = getVersion();
|
|
||||||
config = new PlaceholderAPIConfig(this);
|
|
||||||
expansionManager = new ExpansionManager(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
if (config.checkUpdates())
|
||||||
public void onEnable() {
|
{
|
||||||
config.loadDefConfig();
|
new UpdateChecker(this).fetch();
|
||||||
setupOptions();
|
}
|
||||||
|
|
||||||
Objects.requireNonNull(getCommand("placeholderapi")).setExecutor(new CommandHandler());
|
getServer().getScheduler().runTaskLater(this, this::serveWarning, 40);
|
||||||
new PlaceholderListener(this);
|
}
|
||||||
|
|
||||||
try {
|
@Override
|
||||||
Class.forName("org.bukkit.event.server.ServerLoadEvent");
|
public void onDisable()
|
||||||
new ServerLoadEventListener(this);
|
{
|
||||||
} catch (ExceptionInInitializerError | ClassNotFoundException exception) {
|
disableCloud();
|
||||||
Bukkit.getScheduler().runTaskLater(this, () -> {
|
|
||||||
getLogger().info("Placeholder expansion registration initializing...");
|
|
||||||
|
|
||||||
//fetch any hooks that may have registered externally onEnable first otherwise they will be lost
|
PlaceholderAPI.unregisterAll();
|
||||||
final Map<String, PlaceholderHook> alreadyRegistered = PlaceholderAPI.getPlaceholders();
|
|
||||||
getExpansionManager().registerAllExpansions();
|
|
||||||
|
|
||||||
if (alreadyRegistered != null && !alreadyRegistered.isEmpty()) {
|
HandlerList.unregisterAll(this);
|
||||||
alreadyRegistered.forEach(PlaceholderAPI::registerPlaceholderHook);
|
Bukkit.getScheduler().cancelTasks(this);
|
||||||
}
|
|
||||||
}, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.checkUpdates()) {
|
instance = null;
|
||||||
new UpdateChecker(this).fetch();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (config.isCloudEnabled()) {
|
|
||||||
enableCloud();
|
|
||||||
}
|
|
||||||
|
|
||||||
setupMetrics();
|
public void reloadConf(@NotNull final CommandSender sender)
|
||||||
getServer().getScheduler().runTaskLater(this, this::checkHook, 40);
|
{
|
||||||
}
|
PlaceholderAPI.unregisterAllProvidedExpansions();
|
||||||
|
|
||||||
@Override
|
reloadConfig();
|
||||||
public void onDisable() {
|
|
||||||
disableCloud();
|
|
||||||
PlaceholderAPI.unregisterAll();
|
|
||||||
expansionManager = null;
|
|
||||||
Bukkit.getScheduler().cancelTasks(this);
|
|
||||||
serverVersion = null;
|
|
||||||
instance = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reloadConf(CommandSender s) {
|
manager.registerAllExpansions();
|
||||||
boolean cloudEnabled = this.expansionCloud != null;
|
|
||||||
PlaceholderAPI.unregisterAllProvidedExpansions();
|
|
||||||
reloadConfig();
|
|
||||||
setupOptions();
|
|
||||||
expansionManager.registerAllExpansions();
|
|
||||||
|
|
||||||
if (!config.isCloudEnabled()) {
|
if (config.isCloudEnabled())
|
||||||
disableCloud();
|
{
|
||||||
} else if (!cloudEnabled) {
|
enableCloud();
|
||||||
enableCloud();
|
}
|
||||||
}
|
else
|
||||||
|
{
|
||||||
|
disableCloud();
|
||||||
|
}
|
||||||
|
|
||||||
s.sendMessage(ChatColor.translateAlternateColorCodes('&',
|
Msg.msg(sender,
|
||||||
PlaceholderAPI.getRegisteredIdentifiers().size()
|
PlaceholderAPI.getRegisteredIdentifiers().size() + " &aplaceholder hooks successfully registered!");
|
||||||
+ " &aplaceholder hooks successfully registered!"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void checkHook() {
|
public void enableCloud()
|
||||||
Map<String, PlaceholderHook> loaded = PlaceholderAPI.getPlaceholders();
|
{
|
||||||
|
disableCloud();
|
||||||
|
cloud.fetch(config.cloudAllowUnverifiedExpansions());
|
||||||
|
}
|
||||||
|
|
||||||
loaded.values().forEach(h -> {
|
public void disableCloud()
|
||||||
if (h instanceof EZPlaceholderHook) {
|
{
|
||||||
String author;
|
cloud.clean();
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
author = Bukkit.getPluginManager().getPlugin(((EZPlaceholderHook) h).getPluginName()).getDescription().getAuthors().toString();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
author = "the author of the hook's plugin";
|
|
||||||
}
|
|
||||||
|
|
||||||
getLogger().severe(((EZPlaceholderHook) h).getPluginName() +
|
/**
|
||||||
" is currently using a deprecated method to hook into PlaceholderAPI. Placeholders for that plugin no longer work. " +
|
* Obtain the configuration class for PlaceholderAPI.
|
||||||
"Please consult {author} and urge them to update it ASAP.".replace("{author}", author));
|
*
|
||||||
|
* @return PlaceholderAPIConfig instance
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public PlaceholderAPIConfig getPlaceholderAPIConfig()
|
||||||
|
{
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
// disable the hook on startup
|
@NotNull
|
||||||
PlaceholderAPI.unregisterPlaceholderHook(((EZPlaceholderHook) h).getPlaceholderName());
|
public ExpansionManager getExpansionManager()
|
||||||
}
|
{
|
||||||
});
|
return manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupOptions() {
|
@NotNull
|
||||||
booleanTrue = config.booleanTrue();
|
public ExpansionCloudManager getExpansionCloud()
|
||||||
|
{
|
||||||
|
return cloud;
|
||||||
|
}
|
||||||
|
|
||||||
if (booleanTrue == null) {
|
|
||||||
booleanTrue = "true";
|
|
||||||
}
|
|
||||||
|
|
||||||
booleanFalse = config.booleanFalse();
|
private void setupCommand()
|
||||||
|
{
|
||||||
|
final PluginCommand pluginCommand = getCommand("placeholderapi");
|
||||||
|
if (pluginCommand == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (booleanFalse == null) {
|
final CommandHandler evaluator = new CommandHandler();
|
||||||
booleanFalse = "false";
|
pluginCommand.setExecutor(evaluator);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
private void setupMetrics()
|
||||||
dateFormat = new SimpleDateFormat(config.dateFormat());
|
{
|
||||||
} catch (Exception e) {
|
final Metrics metrics = new Metrics(this);
|
||||||
dateFormat = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
|
metrics.addCustomChart(new Metrics.SimplePie("using_expansion_cloud", () -> getPlaceholderAPIConfig().isCloudEnabled() ? "yes" : "no"));
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setupMetrics() {
|
metrics.addCustomChart(new Metrics.SimplePie("using_spigot", () -> getServerVersion().isSpigot() ? "yes" : "no"));
|
||||||
Metrics m = new Metrics(this);
|
|
||||||
m.addCustomChart(new Metrics.SimplePie("using_expansion_cloud", () -> getExpansionCloud() != null ? "yes" : "no"));
|
|
||||||
|
|
||||||
m.addCustomChart(new Metrics.SimplePie("using_spigot", () -> getServerVersion().isSpigot() ? "yes" : "no"));
|
metrics.addCustomChart(new Metrics.AdvancedPie("expansions_used", () -> {
|
||||||
|
|
||||||
m.addCustomChart(new Metrics.AdvancedPie("expansions_used", () -> {
|
Map<String, Integer> map = new HashMap<>();
|
||||||
Map<String, Integer> map = new HashMap<>();
|
Map<String, PlaceholderHook> hooks = PlaceholderAPI.getPlaceholders();
|
||||||
Map<String, PlaceholderHook> hooks = PlaceholderAPI.getPlaceholders();
|
|
||||||
|
|
||||||
if (!hooks.isEmpty()) {
|
if (!hooks.isEmpty())
|
||||||
|
{
|
||||||
|
|
||||||
for (PlaceholderHook hook : hooks.values()) {
|
for (PlaceholderHook hook : hooks.values())
|
||||||
if (hook instanceof PlaceholderExpansion) {
|
{
|
||||||
PlaceholderExpansion expansion = (PlaceholderExpansion) hook;
|
if (hook instanceof PlaceholderExpansion)
|
||||||
map.put(expansion.getRequiredPlugin() == null ? expansion.getIdentifier() : expansion.getRequiredPlugin(), 1);
|
{
|
||||||
}
|
PlaceholderExpansion expansion = (PlaceholderExpansion) hook;
|
||||||
}
|
map.put(expansion.getRequiredPlugin() == null ? expansion.getIdentifier() : expansion.getRequiredPlugin(), 1);
|
||||||
}
|
}
|
||||||
return map;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}));
|
return map;
|
||||||
}
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
public void enableCloud() {
|
private void serveWarning()
|
||||||
if (expansionCloud == null) {
|
{
|
||||||
expansionCloud = new ExpansionCloudManager(this);
|
for (final PlaceholderHook hook : PlaceholderAPI.getPlaceholders().values())
|
||||||
} else {
|
{
|
||||||
expansionCloud.clean();
|
if (!(hook instanceof EZPlaceholderHook))
|
||||||
}
|
{
|
||||||
expansionCloud.fetch(config.cloudAllowUnverifiedExpansions());
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disableCloud() {
|
final EZPlaceholderHook legacy = (EZPlaceholderHook) hook;
|
||||||
if (expansionCloud != null) {
|
final Plugin plugin = Bukkit.getPluginManager().getPlugin(legacy.getPluginName());
|
||||||
expansionCloud.clean();
|
|
||||||
expansionCloud = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Obtain the configuration class for PlaceholderAPI.
|
|
||||||
*
|
|
||||||
* @return PlaceholderAPIConfig instance
|
|
||||||
*/
|
|
||||||
public PlaceholderAPIConfig getPlaceholderAPIConfig() {
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExpansionManager getExpansionManager() {
|
getLogger().severe(String.format("%s is using a legacy PlaceholderAPI hook, these placeholders will no longer work.\nPlease consult %s and urge them to update it ASAP.",
|
||||||
return expansionManager;
|
legacy.getPluginName(),
|
||||||
}
|
plugin == null ? "the author of the hook's plugin" : plugin.getDescription().getAuthors().toString()));
|
||||||
|
|
||||||
public ExpansionCloudManager getExpansionCloud() {
|
// disable the hook on startup
|
||||||
return expansionCloud;
|
PlaceholderAPI.unregisterPlaceholderHook(legacy.getPlaceholderName());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public String getUptime() {
|
private void setupExpansions()
|
||||||
return TimeUtil
|
{
|
||||||
.getTime((int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime));
|
try
|
||||||
}
|
{
|
||||||
|
Class.forName("org.bukkit.event.server.ServerLoadEvent");
|
||||||
|
new ServerLoadEventListener(this);
|
||||||
|
}
|
||||||
|
catch (final ExceptionInInitializerError | ClassNotFoundException exception)
|
||||||
|
{
|
||||||
|
Bukkit.getScheduler().runTaskLater(this, getExpansionManager()::initializeExpansions, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the static instance of the main class for PlaceholderAPI. This class is not the actual API
|
||||||
|
* class, this is the main class that extends JavaPlugin. For most API methods, use static methods
|
||||||
|
* available from the class: {@link PlaceholderAPI}
|
||||||
|
*
|
||||||
|
* @return PlaceholderAPIPlugin instance
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public static PlaceholderAPIPlugin getInstance()
|
||||||
|
{
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the configurable {@linkplain SimpleDateFormat} object that is used to parse time for
|
||||||
|
* generic time based placeholders
|
||||||
|
*
|
||||||
|
* @return date format
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public static SimpleDateFormat getDateFormat()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return new SimpleDateFormat(getInstance().getPlaceholderAPIConfig().dateFormat());
|
||||||
|
}
|
||||||
|
catch (final IllegalArgumentException ex)
|
||||||
|
{
|
||||||
|
|
||||||
|
getInstance().getLogger().log(Level.WARNING, "configured date format is invalid", ex);
|
||||||
|
|
||||||
|
return new SimpleDateFormat("MM/dd/yy HH:mm:ss");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the configurable {@linkplain String} value that should be returned when a boolean is true
|
||||||
|
*
|
||||||
|
* @return string value of true
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public static String booleanTrue()
|
||||||
|
{
|
||||||
|
return getInstance().getPlaceholderAPIConfig().booleanTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the configurable {@linkplain String} value that should be returned when a boolean is false
|
||||||
|
*
|
||||||
|
* @return string value of false
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public static String booleanFalse()
|
||||||
|
{
|
||||||
|
return getInstance().getPlaceholderAPIConfig().booleanFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Version getServerVersion()
|
||||||
|
{
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Version resolveServerVersion()
|
||||||
|
{
|
||||||
|
final String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
||||||
|
|
||||||
|
boolean isSpigot;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Class.forName("org.spigotmc.SpigotConfig");
|
||||||
|
isSpigot = true;
|
||||||
|
}
|
||||||
|
catch (final ExceptionInInitializerError | ClassNotFoundException ignored)
|
||||||
|
{
|
||||||
|
isSpigot = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Version(version, isSpigot);
|
||||||
|
}
|
||||||
|
|
||||||
public long getUptimeMillis() {
|
|
||||||
return (System.currentTimeMillis() - startTime);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user