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

189 lines
7.3 KiB
Java
Raw Normal View History

package com.github.artemis.the.gr8.playerstats;
import com.github.artemis.the.gr8.playerstats.api.PlayerStats;
import com.github.artemis.the.gr8.playerstats.msg.OutputManager;
import com.github.artemis.the.gr8.playerstats.api.PlayerStatsAPI;
import com.github.artemis.the.gr8.playerstats.commands.ReloadCommand;
import com.github.artemis.the.gr8.playerstats.commands.ShareCommand;
import com.github.artemis.the.gr8.playerstats.commands.StatCommand;
import com.github.artemis.the.gr8.playerstats.commands.TabCompleter;
import com.github.artemis.the.gr8.playerstats.config.ConfigHandler;
import com.github.artemis.the.gr8.playerstats.listeners.JoinListener;
import com.github.artemis.the.gr8.playerstats.msg.InternalFormatter;
import com.github.artemis.the.gr8.playerstats.msg.MessageBuilder;
import com.github.artemis.the.gr8.playerstats.msg.msgutils.LanguageKeyHandler;
import com.github.artemis.the.gr8.playerstats.statistic.StatCalculator;
import com.github.artemis.the.gr8.playerstats.utils.EnumHandler;
import com.github.artemis.the.gr8.playerstats.utils.OfflinePlayerHandler;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
2022-05-25 17:29:51 +02:00
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
2022-07-26 01:17:33 +02:00
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;
2022-05-25 17:29:51 +02:00
import org.jetbrains.annotations.NotNull;
/**
* PlayerStats' Main class
*/
public final class Main extends JavaPlugin {
private static Main instance;
private static BukkitAudiences adventure;
2022-08-01 16:57:20 +02:00
private static ConfigHandler config;
private static LanguageKeyHandler languageKeyHandler;
2022-08-01 16:57:20 +02:00
private static OfflinePlayerHandler offlinePlayerHandler;
private static EnumHandler enumHandler;
2022-07-23 16:08:35 +02:00
private static OutputManager outputManager;
private static ShareManager shareManager;
2022-08-08 17:24:22 +02:00
private static StatCalculator statCalculator;
2022-07-23 16:08:35 +02:00
private static ThreadManager threadManager;
2022-05-25 17:29:51 +02:00
2022-08-01 16:57:20 +02:00
private static PlayerStats playerStatsAPI;
2022-07-22 21:20:29 +02:00
@Override
public void onEnable() {
2022-08-01 16:57:20 +02:00
//initialize all the Managers, singletons, ConfigHandler and the API
initializeMainClasses();
setupMetrics();
2022-07-23 03:00:02 +02:00
//register all commands and the tabCompleter
PluginCommand statcmd = this.getCommand("statistic");
if (statcmd != null) {
2022-08-01 16:57:20 +02:00
statcmd.setExecutor(new StatCommand(outputManager, threadManager));
statcmd.setTabCompleter(new TabCompleter(enumHandler, offlinePlayerHandler));
}
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(shareManager, outputManager));
//register the listener
Bukkit.getPluginManager().registerEvents(new JoinListener(threadManager), this);
//finish up
2022-05-09 21:00:39 +02:00
this.getLogger().info("Enabled PlayerStats!");
}
@Override
public void onDisable() {
2022-05-25 17:29:51 +02:00
if (adventure != null) {
adventure.close();
adventure = null;
}
this.getLogger().info("Disabled PlayerStats!");
}
2022-07-23 16:08:35 +02:00
/**
* @return Adventure's BukkitAudiences object
* @throws IllegalStateException if PlayerStats is not enabled
*/
2022-07-23 16:08:35 +02:00
public static @NotNull BukkitAudiences getAdventure() throws IllegalStateException {
if (adventure == null) {
throw new IllegalStateException("Tried to access Adventure without PlayerStats being enabled!");
}
return adventure;
}
/**
* @return PlayerStats' ConfigHandler
* @throws IllegalStateException if PlayerStats is not enabled
*/
public static @NotNull ConfigHandler getConfigHandler() throws IllegalStateException {
if (config == null) {
throw new IllegalStateException("PlayerStats does not seem to be loaded!");
}
return config;
}
2022-08-08 17:24:22 +02:00
public static @NotNull OfflinePlayerHandler getOfflinePlayerHandler() throws IllegalStateException {
if (offlinePlayerHandler == null) {
throw new IllegalStateException("PlayerStats does not seem to be loaded!");
2022-08-08 17:24:22 +02:00
}
return offlinePlayerHandler;
}
public static @NotNull LanguageKeyHandler getLanguageKeyHandler() {
if (languageKeyHandler == null) {
languageKeyHandler = new LanguageKeyHandler(instance);
}
return languageKeyHandler;
}
/**
* Gets the EnumHandler. If there is no EnumHandler, one will be created.
* @return PlayerStat's EnumHandler
*/
2022-08-01 16:57:20 +02:00
public static @NotNull EnumHandler getEnumHandler() {
if (enumHandler == null) {
enumHandler = new EnumHandler();
}
return enumHandler;
}
2022-08-08 17:24:22 +02:00
public static @NotNull StatCalculator getStatCalculator() throws IllegalStateException {
if (statCalculator == null) {
throw new IllegalStateException("PlayerStats does not seem to be loaded!");
2022-08-01 16:57:20 +02:00
}
2022-08-08 17:24:22 +02:00
return statCalculator;
}
public static @NotNull InternalFormatter getStatFormatter() throws IllegalStateException {
2022-08-08 17:24:22 +02:00
if (outputManager == null) {
throw new IllegalStateException("PlayerStats does not seem to be loaded!");
}
return outputManager;
2022-08-01 16:57:20 +02:00
}
2022-07-23 16:08:35 +02:00
public static @NotNull PlayerStats getPlayerStatsAPI() throws IllegalStateException {
if (playerStatsAPI == null) {
throw new IllegalStateException("PlayerStats does not seem to be loaded!");
}
return playerStatsAPI;
}
2022-08-01 16:57:20 +02:00
private void initializeMainClasses() {
instance = this;
2022-07-23 16:08:35 +02:00
adventure = BukkitAudiences.create(this);
config = new ConfigHandler(this);
2022-08-01 16:57:20 +02:00
enumHandler = new EnumHandler();
languageKeyHandler = new LanguageKeyHandler(instance);
offlinePlayerHandler = new OfflinePlayerHandler();
2022-08-01 13:16:03 +02:00
shareManager = new ShareManager(config);
2022-08-08 17:24:22 +02:00
statCalculator = new StatCalculator(offlinePlayerHandler);
outputManager = new OutputManager(adventure, config, shareManager);
2022-08-08 17:24:22 +02:00
threadManager = new ThreadManager(config, statCalculator, outputManager);
2022-07-23 16:08:35 +02:00
MessageBuilder apiMessageBuilder = MessageBuilder.defaultBuilder(config);
playerStatsAPI = new PlayerStatsAPI(apiMessageBuilder, offlinePlayerHandler);
2022-07-23 16:08:35 +02:00
}
private void setupMetrics() {
new BukkitRunnable() {
@Override
public void run() {
final Metrics metrics = new Metrics(instance, 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);
}
2022-06-27 14:21:35 +02:00
}