PlayerStats/src/main/java/com/artemis/the/gr8/playerstats/Main.java

182 lines
6.3 KiB
Java

package com.artemis.the.gr8.playerstats;
import com.artemis.the.gr8.playerstats.api.PlayerStats;
import com.artemis.the.gr8.playerstats.api.StatFormatter;
import com.artemis.the.gr8.playerstats.api.StatManager;
import com.artemis.the.gr8.playerstats.commands.*;
import com.artemis.the.gr8.playerstats.multithreading.ThreadManager;
import com.artemis.the.gr8.playerstats.statistic.RequestManager;
import com.artemis.the.gr8.playerstats.msg.OutputManager;
import com.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.artemis.the.gr8.playerstats.listeners.JoinListener;
import com.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler;
import com.artemis.the.gr8.playerstats.share.ShareManager;
import com.artemis.the.gr8.playerstats.utils.MyLogger;
import com.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
/**
* PlayerStats' Main class
*/
public final class Main extends JavaPlugin implements PlayerStats {
private static JavaPlugin pluginInstance;
private static PlayerStats playerStatsAPI;
private static BukkitAudiences adventure;
private static ConfigHandler config;
private static ThreadManager threadManager;
private static LanguageKeyHandler languageKeyHandler;
private static OfflinePlayerHandler offlinePlayerHandler;
private static RequestManager requestManager;
private static OutputManager outputManager;
private static ShareManager shareManager;
@Override
public void onEnable() {
initializeMainClasses();
registerCommands();
setupMetrics();
//register the listener
Bukkit.getPluginManager().registerEvents(new JoinListener(threadManager), this);
//finish up
this.getLogger().info("Enabled PlayerStats!");
}
@Override
public void onDisable() {
if (adventure != null) {
adventure.close();
adventure = null;
}
this.getLogger().info("Disabled PlayerStats!");
}
public void reloadPlugin() {
config.reload();
MyLogger.setDebugLevel(config.getDebugLevel());
languageKeyHandler.reload();
offlinePlayerHandler.reload();
outputManager.updateSettings();
shareManager.updateSettings();
}
/**
*
* @return the JavaPlugin instance associated with PlayerStats
* @throws IllegalStateException if PlayerStats is not enabled
*/
public static @NotNull JavaPlugin getPluginInstance() throws IllegalStateException {
if (pluginInstance == null) {
throw new IllegalStateException("PlayerStats is not loaded!");
}
return pluginInstance;
}
public static @NotNull PlayerStats getPlayerStatsAPI() throws IllegalStateException {
if (playerStatsAPI == null) {
throw new IllegalStateException("PlayerStats does not seem to be loaded!");
}
return playerStatsAPI;
}
/**
* Initialize all classes that need initializing,
* and store references to classes that are
* needed for the Command classes or the API.
*/
private void initializeMainClasses() {
pluginInstance = this;
playerStatsAPI = this;
adventure = BukkitAudiences.create(this);
config = ConfigHandler.getInstance();
languageKeyHandler = LanguageKeyHandler.getInstance();
offlinePlayerHandler = OfflinePlayerHandler.getInstance();
shareManager = ShareManager.getInstance();
outputManager = new OutputManager(adventure);
requestManager = new RequestManager(outputManager);
threadManager = new ThreadManager(this, outputManager);
}
/**
* Register all commands and assign the tabCompleter
* to the relevant commands.
*/
private void registerCommands() {
TabCompleter tabCompleter = new TabCompleter();
PluginCommand statcmd = this.getCommand("statistic");
if (statcmd != null) {
statcmd.setExecutor(new StatCommand(outputManager, threadManager));
statcmd.setTabCompleter(tabCompleter);
}
PluginCommand excludecmd = this.getCommand("statisticexclude");
if (excludecmd != null) {
excludecmd.setExecutor(new ExcludeCommand());
excludecmd.setTabCompleter(tabCompleter);
}
PluginCommand reloadcmd = this.getCommand("statisticreload");
if (reloadcmd != null) {
reloadcmd.setExecutor(new ReloadCommand(threadManager));
}
PluginCommand sharecmd = this.getCommand("statisticshare");
if (sharecmd != null) {
sharecmd.setExecutor(new ShareCommand(outputManager));
}
}
/**
* Setup bstats
*/
private void setupMetrics() {
new BukkitRunnable() {
@Override
public void run() {
final Metrics metrics = new Metrics(pluginInstance, 15923);
final boolean placeholderExpansionActive;
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
PlaceholderExpansion expansion = PlaceholderAPIPlugin
.getInstance()
.getLocalExpansionManager()
.getExpansion("playerstats");
placeholderExpansionActive = expansion != null;
} else {
placeholderExpansionActive = false;
}
metrics.addCustomChart(new SimplePie("using_placeholder_expansion", () -> placeholderExpansionActive ? "yes" : "no"));
}
}.runTaskLaterAsynchronously(this, 200);
}
@Contract(pure = true)
@Override
public @NotNull String getVersion() {
return "1.8";
}
@Override
public StatManager getStatManager() {
return requestManager;
}
@Override
public StatFormatter getFormatter() {
return outputManager.getMainMessageBuilder();
}
}