PlaceholderAPI/src/main/java/me/clip/placeholderapi/PlaceholderAPIPlugin.java

307 lines
7.0 KiB
Java
Raw Normal View History

2018-03-21 14:15:51 +01:00
/*
*
* PlaceholderAPI
2019-06-21 18:30:12 +02:00
* Copyright (C) 2019 Ryan McCarthy
2018-03-21 14:15:51 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
2018-03-21 00:04:14 +01:00
package me.clip.placeholderapi;
Commands rewrite (#357) * began rewriting command system * began rewriting command system * updated formatting * added new info command * added new reload command * updated new parse command to support all three parsing types * added new commands to command router * deleted old unused commands * removed parserel * added new expansion register and unregister commands * deleted unused commands * fixed annotation order * added labels helper to command * updated alias method to return an immutable view * updated params param with unmodifiable annotation * updated router to build an immutable map of the commands * began rewriting command system * updated formatting * added new info command * added new reload command * updated new parse command to support all three parsing types * added new commands to command router * deleted old unused commands * removed parserel * added new expansion register and unregister commands * deleted unused commands * fixed annotation order * added labels helper to command * updated alias method to return an immutable view * updated params param with unmodifiable annotation * updated router to build an immutable map of the commands * updated plugin class to use new command router * updated switch to break on parse match * updated register completions to suggest file names * updated router to allow entering labels in any case * updated parse command to send message to players as components * added command dispatching parsing * moved new commands into local package * added helper functions for filtering and suggesting * updated imports, updated tab completion to use helper functions * added start of ecloud commands * replace ecloud enable and disable commands with single toggle command evaluator * deleted unused commands * updated commands to use helper functions for suggesting * updated downloading to use completablefuture, updated all methods to use streams exclusively * updated to use the config instead of a null check * deleted old commands system * finished new command system * updated to use new method from cloud manager * fixed annotation ordering and added missing annotations * updated ecloud subcommands to have a more specific permission * updated plugin.yml with the new permissions, (and also fixed its formatting) * fixed annotations intellij missed * this should probably be there...
2020-07-24 07:29:11 +02:00
import me.clip.placeholderapi.commands.PlaceholderCommandRouter;
2018-03-21 00:04:14 +01:00
import me.clip.placeholderapi.configuration.PlaceholderAPIConfig;
import me.clip.placeholderapi.expansion.ExpansionManager;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.clip.placeholderapi.expansion.Version;
import me.clip.placeholderapi.expansion.cloud.ExpansionCloudManager;
import me.clip.placeholderapi.listeners.PlaceholderListener;
import me.clip.placeholderapi.listeners.ServerLoadEventListener;
import me.clip.placeholderapi.updatechecker.UpdateChecker;
import me.clip.placeholderapi.util.Msg;
import org.bstats.bukkit.Metrics;
2018-03-21 00:04:14 +01:00
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.event.HandlerList;
2018-03-21 00:04:14 +01:00
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
2018-03-21 00:04:14 +01:00
2019-06-21 18:30:12 +02:00
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
2018-03-21 00:04:14 +01:00
/**
* Yes I have a shit load of work to do...
*
* @author Ryan McCarthy
2018-03-21 00:04:14 +01:00
*/
public final class PlaceholderAPIPlugin extends JavaPlugin
{
@NotNull
private static final Version version = resolveServerVersion();
private static PlaceholderAPIPlugin instance;
@NotNull
private final PlaceholderAPIConfig config = new PlaceholderAPIConfig(this);
@NotNull
private final ExpansionCloudManager cloud = new ExpansionCloudManager(this);
@NotNull
private final ExpansionManager manager = new ExpansionManager(this);
@Override
public void onLoad()
{
instance = this;
saveDefaultConfig();
}
@Override
public void onEnable()
{
setupCommand();
setupMetrics();
setupExpansions();
new PlaceholderListener(this);
if (config.isCloudEnabled())
{
enableCloud();
}
if (config.checkUpdates())
{
new UpdateChecker(this).fetch();
}
}
@Override
public void onDisable()
{
disableCloud();
PlaceholderAPI.unregisterAll();
HandlerList.unregisterAll(this);
Bukkit.getScheduler().cancelTasks(this);
instance = null;
}
public void reloadConf(@NotNull final CommandSender sender)
{
PlaceholderAPI.unregisterAllProvidedExpansions();
reloadConfig();
manager.registerAllExpansions();
if (config.isCloudEnabled())
{
enableCloud();
}
else
{
disableCloud();
}
Msg.msg(sender,
PlaceholderAPI.getRegisteredIdentifiers().size() + " &aplaceholder hooks successfully registered!");
}
public void enableCloud()
{
disableCloud();
cloud.fetch(config.cloudAllowUnverifiedExpansions());
}
public void disableCloud()
{
cloud.clean();
}
/**
* Obtain the configuration class for PlaceholderAPI.
*
* @return PlaceholderAPIConfig instance
*/
@NotNull
public PlaceholderAPIConfig getPlaceholderAPIConfig()
{
return config;
}
@NotNull
public ExpansionManager getExpansionManager()
{
return manager;
}
@NotNull
public ExpansionCloudManager getExpansionCloud()
{
return cloud;
}
private void setupCommand()
{
final PluginCommand pluginCommand = getCommand("placeholderapi");
if (pluginCommand == null)
{
return;
}
Commands rewrite (#357) * began rewriting command system * began rewriting command system * updated formatting * added new info command * added new reload command * updated new parse command to support all three parsing types * added new commands to command router * deleted old unused commands * removed parserel * added new expansion register and unregister commands * deleted unused commands * fixed annotation order * added labels helper to command * updated alias method to return an immutable view * updated params param with unmodifiable annotation * updated router to build an immutable map of the commands * began rewriting command system * updated formatting * added new info command * added new reload command * updated new parse command to support all three parsing types * added new commands to command router * deleted old unused commands * removed parserel * added new expansion register and unregister commands * deleted unused commands * fixed annotation order * added labels helper to command * updated alias method to return an immutable view * updated params param with unmodifiable annotation * updated router to build an immutable map of the commands * updated plugin class to use new command router * updated switch to break on parse match * updated register completions to suggest file names * updated router to allow entering labels in any case * updated parse command to send message to players as components * added command dispatching parsing * moved new commands into local package * added helper functions for filtering and suggesting * updated imports, updated tab completion to use helper functions * added start of ecloud commands * replace ecloud enable and disable commands with single toggle command evaluator * deleted unused commands * updated commands to use helper functions for suggesting * updated downloading to use completablefuture, updated all methods to use streams exclusively * updated to use the config instead of a null check * deleted old commands system * finished new command system * updated to use new method from cloud manager * fixed annotation ordering and added missing annotations * updated ecloud subcommands to have a more specific permission * updated plugin.yml with the new permissions, (and also fixed its formatting) * fixed annotations intellij missed * this should probably be there...
2020-07-24 07:29:11 +02:00
final PlaceholderCommandRouter router = new PlaceholderCommandRouter(this);
pluginCommand.setExecutor(router);
pluginCommand.setTabCompleter(router);
}
private void setupMetrics()
{
final Metrics metrics = new Metrics(this);
metrics.addCustomChart(new Metrics.SimplePie("using_expansion_cloud", () -> getPlaceholderAPIConfig().isCloudEnabled() ? "yes" : "no"));
metrics.addCustomChart(new Metrics.SimplePie("using_spigot", () -> getServerVersion().isSpigot() ? "yes" : "no"));
metrics.addCustomChart(new Metrics.AdvancedPie("expansions_used", () -> {
Map<String, Integer> map = new HashMap<>();
Map<String, PlaceholderHook> hooks = PlaceholderAPI.getPlaceholders();
if (!hooks.isEmpty())
{
for (PlaceholderHook hook : hooks.values())
{
if (hook instanceof PlaceholderExpansion)
{
PlaceholderExpansion expansion = (PlaceholderExpansion) hook;
map.put(expansion.getRequiredPlugin() == null ? expansion.getIdentifier() : expansion.getRequiredPlugin(), 1);
}
}
}
return map;
}));
}
private void setupExpansions()
{
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);
}
2018-03-21 00:04:14 +01:00
}