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

279 lines
6.7 KiB
Java
Raw Normal View History

2018-03-21 14:15:51 +01:00
/*
* This file is part of PlaceholderAPI
2018-03-21 14:15:51 +01:00
*
* PlaceholderAPI
* Copyright (c) 2015 - 2020 PlaceholderAPI Team
2018-03-21 14:15:51 +01:00
*
* PlaceholderAPI free software: you can redistribute it and/or modify
2018-03-21 14:15:51 +01:00
* 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.
*
* PlaceholderAPI is distributed in the hope that it will be useful,
2018-03-21 14:15:51 +01:00
* 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.PlaceholderExpansion;
import me.clip.placeholderapi.expansion.Version;
import me.clip.placeholderapi.expansion.manager.CloudExpansionManager;
import me.clip.placeholderapi.expansion.manager.LocalExpansionManager;
import me.clip.placeholderapi.listeners.ServerLoadEventListener;
import me.clip.placeholderapi.updatechecker.UpdateChecker;
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;
static
{
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;
}
VERSION = new Version(version, isSpigot);
}
private static PlaceholderAPIPlugin instance;
@NotNull
private final PlaceholderAPIConfig config = new PlaceholderAPIConfig(this);
@NotNull
private final LocalExpansionManager localExpansionManager = new LocalExpansionManager(this);
@NotNull
private final CloudExpansionManager cloudExpansionManager = new CloudExpansionManager(this);
@Override
public void onLoad()
{
instance = this;
saveDefaultConfig();
}
@Override
public void onEnable()
{
setupCommand();
setupMetrics();
setupExpansions();
if (config.isCloudEnabled())
{
getCloudExpansionManager().load();
}
if (config.checkUpdates())
{
new UpdateChecker(this).fetch();
}
}
@Override
public void onDisable()
{
getCloudExpansionManager().kill();
getLocalExpansionManager().kill();
HandlerList.unregisterAll(this);
Bukkit.getScheduler().cancelTasks(this);
instance = null;
}
public void reloadConf(@NotNull final CommandSender sender)
{
getLocalExpansionManager().kill();
reloadConfig();
getLocalExpansionManager().load(sender);
if (config.isCloudEnabled())
{
getCloudExpansionManager().load();
}
else
{
getCloudExpansionManager().kill();
}
}
@NotNull
public LocalExpansionManager getLocalExpansionManager()
{
return localExpansionManager;
}
@NotNull
public CloudExpansionManager getCloudExpansionManager()
{
return cloudExpansionManager;
}
/**
* Obtain the configuration class for PlaceholderAPI.
*
* @return PlaceholderAPIConfig instance
*/
@NotNull
public PlaceholderAPIConfig getPlaceholderAPIConfig()
{
return config;
}
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);
2020-07-27 00:03:31 +02:00
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", () -> {
final Map<String, Integer> values = new HashMap<>();
for (final PlaceholderExpansion expansion : getLocalExpansionManager().getExpansions())
{
values.put(expansion.getRequiredPlugin() == null ? expansion.getIdentifier() : expansion.getRequiredPlugin(), 1);
}
return values;
}));
}
private void setupExpansions()
{
Bukkit.getPluginManager().registerEvents(getLocalExpansionManager(), this);
try
{
Class.forName("org.bukkit.event.server.ServerLoadEvent");
new ServerLoadEventListener(this);
}
catch (final ExceptionInInitializerError | ClassNotFoundException ignored)
{
Bukkit.getScheduler().runTaskLater(this, () -> getLocalExpansionManager().load(Bukkit.getConsoleSender()), 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 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();
}
/**
* 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");
}
}
public static Version getServerVersion()
{
return VERSION;
}
2018-03-21 00:04:14 +01:00
}