diff --git a/Plan/src/main/java/com/djrapitops/plan/Plan.java b/Plan/src/main/java/com/djrapitops/plan/Plan.java index 3a0fa1f1b..0b4a6804c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/src/main/java/com/djrapitops/plan/Plan.java @@ -29,7 +29,6 @@ import com.djrapitops.plugin.api.utility.log.DebugLog; import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.settings.ColorScheme; import com.djrapitops.plugin.task.AbsRunnable; -import com.djrapitops.plugin.task.ITask; import com.djrapitops.plugin.task.RunnableFactory; import main.java.com.djrapitops.plan.api.API; import main.java.com.djrapitops.plan.api.IPlan; @@ -56,6 +55,7 @@ import main.java.com.djrapitops.plan.systems.processing.Processor; import main.java.com.djrapitops.plan.systems.processing.importing.importers.OfflinePlayerImporter; import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue; import main.java.com.djrapitops.plan.systems.tasks.TPSCountTimer; +import main.java.com.djrapitops.plan.systems.tasks.TaskSystem; import main.java.com.djrapitops.plan.systems.update.VersionCheckSystem; import main.java.com.djrapitops.plan.systems.webserver.WebServer; import main.java.com.djrapitops.plan.systems.webserver.WebServerSystem; @@ -188,7 +188,8 @@ public class Plan extends BukkitPlugin implements IPlan { registerListeners(); } PlanPlayerListener.setCountKicks(true); - registerTasks(); + + TaskSystem.getInstance().init(); this.api = new API(this); @@ -229,51 +230,6 @@ public class Plan extends BukkitPlugin implements IPlan { } } - private void registerTasks() { - String bootAnalysisMsg = Locale.get(Msg.ENABLE_BOOT_ANALYSIS_INFO).toString(); - String bootAnalysisRunMsg = Locale.get(Msg.ENABLE_BOOT_ANALYSIS_RUN_INFO).toString(); - - Benchmark.start("Task Registration"); - tpsCountTimer = new TPSCountTimer(this); - RunnableFactory.createNew(tpsCountTimer).runTaskTimer(1000, TimeAmount.SECOND.ticks()); - - // Analysis refresh settings - int analysisRefreshMinutes = Settings.ANALYSIS_AUTO_REFRESH.getNumber(); - boolean analysisRefreshTaskIsEnabled = analysisRefreshMinutes > 0; - long analysisPeriod = analysisRefreshMinutes * TimeAmount.MINUTE.ticks(); - - Log.info(bootAnalysisMsg); - - ITask bootAnalysisTask = RunnableFactory.createNew("BootAnalysisTask", new AbsRunnable() { - @Override - public void run() { - Log.info(bootAnalysisRunMsg); - infoManager.refreshAnalysis(getServerUUID()); - this.cancel(); - } - }).runTaskLaterAsynchronously(30 * TimeAmount.SECOND.ticks()); - - bootAnalysisTaskID = bootAnalysisTask.getTaskId(); - - if (analysisRefreshTaskIsEnabled) { - RunnableFactory.createNew("PeriodicalAnalysisTask", new AbsRunnable() { - @Override - public void run() { - infoManager.refreshAnalysis(getServerUUID()); - } - }).runTaskTimerAsynchronously(analysisPeriod, analysisPeriod); - } - - RunnableFactory.createNew("PeriodicNetworkBoxRefreshTask", new AbsRunnable() { - @Override - public void run() { - infoManager.updateNetworkPageContent(); - } - }).runTaskTimerAsynchronously(TimeAmount.SECOND.ticks(), TimeAmount.MINUTE.ticks() * 5L); - - Benchmark.stop("Enable", "Task Registration"); - } - @Override public ColorScheme getColorScheme() { try { @@ -380,15 +336,6 @@ public class Plan extends BukkitPlugin implements IPlan { return hookHandler; } - /** - * Used to get the ID of the BootAnalysisTask, so that it can be disabled. - * - * @return ID of the bootAnalysisTask - */ - public int getBootAnalysisTaskID() { - return bootAnalysisTaskID; - } - /** * Used to get the object storing server variables that are constant after * boot. diff --git a/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java b/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java index 9a31d5a2c..9f2702e89 100644 --- a/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java +++ b/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java @@ -7,11 +7,9 @@ package main.java.com.djrapitops.plan; import com.djrapitops.plugin.BungeePlugin; import com.djrapitops.plugin.StaticHolder; import com.djrapitops.plugin.api.Benchmark; -import com.djrapitops.plugin.api.TimeAmount; import com.djrapitops.plugin.api.config.Config; import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.settings.ColorScheme; -import com.djrapitops.plugin.task.AbsRunnable; import com.djrapitops.plugin.task.RunnableFactory; import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.command.PlanBungeeCommand; @@ -30,7 +28,7 @@ import main.java.com.djrapitops.plan.systems.info.server.BungeeServerInfoManager import main.java.com.djrapitops.plan.systems.listeners.BungeePlayerListener; import main.java.com.djrapitops.plan.systems.processing.Processor; import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue; -import main.java.com.djrapitops.plan.systems.tasks.TPSCountTimer; +import main.java.com.djrapitops.plan.systems.tasks.TaskSystem; import main.java.com.djrapitops.plan.systems.update.VersionCheckSystem; import main.java.com.djrapitops.plan.systems.webserver.WebServer; import main.java.com.djrapitops.plan.systems.webserver.WebServerSystem; @@ -72,7 +70,6 @@ public class PlanBungee extends BungeePlugin implements IPlan { new Locale().loadLocale(); Theme.getInstance().init(); - DBSystem.getInstance().init(); registerCommand("planbungee", new PlanBungeeCommand(this)); @@ -92,21 +89,7 @@ public class PlanBungee extends BungeePlugin implements IPlan { WebServerSystem.getInstance().init(); serverInfoManager.loadServerInfo(); - RunnableFactory.createNew("Enable Bukkit Connection Task", new AbsRunnable() { - @Override - public void run() { - infoManager.attemptConnection(); - infoManager.sendConfigSettings(); - } - }).runTaskAsynchronously(); - RunnableFactory.createNew("Player Count task", new TPSCountTimer(this)) - .runTaskTimerAsynchronously(1000, TimeAmount.SECOND.ticks()); - RunnableFactory.createNew("NetworkPageContentUpdateTask", new AbsRunnable("NetworkPageContentUpdateTask") { - @Override - public void run() { - infoManager.updateNetworkPageContent(); - } - }).runTaskTimerAsynchronously(1500, TimeAmount.MINUTE.ticks()); + TaskSystem.getInstance().init(); processingQueue = new ProcessingQueue(); diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/AnalyzeCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/AnalyzeCommand.java index d8f294105..63f9c6c08 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/AnalyzeCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/AnalyzeCommand.java @@ -131,10 +131,6 @@ public class AnalyzeCommand extends SubCommand { } private void updateCache(ISender sender, UUID serverUUID) { - int bootAnID = plugin.getBootAnalysisTaskID(); - if (bootAnID != -1) { - plugin.getServer().getScheduler().cancelTask(bootAnID); - } infoManager.addAnalysisNotification(sender, serverUUID); infoManager.refreshAnalysis(serverUUID); } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/Systems.java b/Plan/src/main/java/com/djrapitops/plan/systems/Systems.java index 5d4c279f2..1c6387a2b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/Systems.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/Systems.java @@ -15,6 +15,9 @@ import main.java.com.djrapitops.plan.systems.file.config.PlanConfigSystem; import main.java.com.djrapitops.plan.systems.file.database.DBSystem; import main.java.com.djrapitops.plan.systems.file.database.PlanBungeeDBSystem; import main.java.com.djrapitops.plan.systems.file.database.PlanDBSystem; +import main.java.com.djrapitops.plan.systems.tasks.PlanBungeeTaskSystem; +import main.java.com.djrapitops.plan.systems.tasks.PlanTaskSystem; +import main.java.com.djrapitops.plan.systems.tasks.TaskSystem; import main.java.com.djrapitops.plan.systems.update.VersionCheckSystem; import main.java.com.djrapitops.plan.systems.webserver.WebServerSystem; import main.java.com.djrapitops.plan.utilities.MiscUtils; @@ -34,6 +37,8 @@ public class Systems { private WebServerSystem webServerSystem; + private TaskSystem taskSystem; + private VersionCheckSystem versionCheckSystem; /** @@ -47,6 +52,8 @@ public class Systems { databaseSystem = new PlanDBSystem(); versionCheckSystem = new VersionCheckSystem(plugin.getVersion()); + taskSystem = new PlanTaskSystem(); + webServerSystem = new WebServerSystem(plugin); themeSystem = new Theme(); } @@ -62,6 +69,8 @@ public class Systems { databaseSystem = new PlanBungeeDBSystem(); versionCheckSystem = new VersionCheckSystem(plugin.getVersion()); + taskSystem = new PlanBungeeTaskSystem(); + webServerSystem = new WebServerSystem(plugin); themeSystem = new Theme(); } @@ -114,4 +123,8 @@ public class Systems { public Theme getThemeSystem() { return themeSystem; } + + public TaskSystem getTaskSystem() { + return taskSystem; + } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/tasks/PlanBungeeTaskSystem.java b/Plan/src/main/java/com/djrapitops/plan/systems/tasks/PlanBungeeTaskSystem.java new file mode 100644 index 000000000..089bd8bfb --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/tasks/PlanBungeeTaskSystem.java @@ -0,0 +1,44 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan.systems.tasks; + +import com.djrapitops.plugin.api.TimeAmount; +import com.djrapitops.plugin.task.AbsRunnable; +import main.java.com.djrapitops.plan.PlanBungee; +import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException; +import main.java.com.djrapitops.plan.systems.info.BungeeInformationManager; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class PlanBungeeTaskSystem extends TaskSystem { + + @Override + public void init() throws PlanEnableException { + registerTasks(); + } + + private void registerTasks() { + BungeeInformationManager infoManager = ((BungeeInformationManager) PlanBungee.getInstance().getInfoManager()); + + registerTask("Enable Bukkit Connection Task", new AbsRunnable() { + @Override + public void run() { + infoManager.attemptConnection(); + infoManager.sendConfigSettings(); + } + }).runTaskAsynchronously(); + registerTask("Player Count task", new TPSCountTimer(PlanBungee.getInstance())) + .runTaskTimerAsynchronously(1000, TimeAmount.SECOND.ticks()); + registerTask("NetworkPageContentUpdateTask", new AbsRunnable("NetworkPageContentUpdateTask") { + @Override + public void run() { + infoManager.updateNetworkPageContent(); + } + }).runTaskTimerAsynchronously(1500, TimeAmount.MINUTE.ticks()); + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/tasks/PlanTaskSystem.java b/Plan/src/main/java/com/djrapitops/plan/systems/tasks/PlanTaskSystem.java new file mode 100644 index 000000000..7a1363348 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/tasks/PlanTaskSystem.java @@ -0,0 +1,89 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan.systems.tasks; + +import com.djrapitops.plugin.api.Benchmark; +import com.djrapitops.plugin.api.TimeAmount; +import com.djrapitops.plugin.api.utility.log.Log; +import com.djrapitops.plugin.task.AbsRunnable; +import com.djrapitops.plugin.task.ITask; +import com.djrapitops.plugin.task.RunnableFactory; +import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException; +import main.java.com.djrapitops.plan.settings.Settings; +import main.java.com.djrapitops.plan.settings.locale.Locale; +import main.java.com.djrapitops.plan.settings.locale.Msg; +import main.java.com.djrapitops.plan.systems.info.InformationManager; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class PlanTaskSystem extends TaskSystem { + + // TODO Remove Plan.getInstance requirement. + + private ITask bootAnalysisTask; + + @Override + public void init() throws PlanEnableException { + registerTasks(); + } + + // TODO Clean Up + private void registerTasks() { + String bootAnalysisMsg = Locale.get(Msg.ENABLE_BOOT_ANALYSIS_INFO).toString(); + String bootAnalysisRunMsg = Locale.get(Msg.ENABLE_BOOT_ANALYSIS_RUN_INFO).toString(); + + Benchmark.start("Task Registration"); + tpsCountTimer = new TPSCountTimer(Plan.getInstance()); + registerTask(tpsCountTimer).runTaskTimer(1000, TimeAmount.SECOND.ticks()); + + // Analysis refresh settings + int analysisRefreshMinutes = Settings.ANALYSIS_AUTO_REFRESH.getNumber(); + boolean analysisRefreshTaskIsEnabled = analysisRefreshMinutes > 0; + long analysisPeriod = analysisRefreshMinutes * TimeAmount.MINUTE.ticks(); + + Log.info(bootAnalysisMsg); + + InformationManager infoManager = Plan.getInstance().getInfoManager(); + + bootAnalysisTask = RunnableFactory.createNew("BootAnalysisTask", new AbsRunnable() { + @Override + public void run() { + Log.info(bootAnalysisRunMsg); + infoManager.refreshAnalysis(Plan.getServerUUID()); + this.cancel(); + } + }).runTaskLaterAsynchronously(30 * TimeAmount.SECOND.ticks()); + + if (analysisRefreshTaskIsEnabled) { + RunnableFactory.createNew("PeriodicalAnalysisTask", new AbsRunnable() { + @Override + public void run() { + infoManager.refreshAnalysis(Plan.getServerUUID()); + } + }).runTaskTimerAsynchronously(analysisPeriod, analysisPeriod); + } + + registerTask("PeriodicNetworkBoxRefreshTask", new AbsRunnable() { + @Override + public void run() { + infoManager.updateNetworkPageContent(); + } + }).runTaskTimerAsynchronously(TimeAmount.SECOND.ticks(), TimeAmount.MINUTE.ticks() * 5L); + + Benchmark.stop("Enable", "Task Registration"); + } + + public void cancelBootAnalysis() { + try { + bootAnalysisTask.cancel(); + } catch (Exception ignored) { + /* Ignored */ + } + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/tasks/TaskSystem.java b/Plan/src/main/java/com/djrapitops/plan/systems/tasks/TaskSystem.java new file mode 100644 index 000000000..81d25d95b --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/tasks/TaskSystem.java @@ -0,0 +1,45 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan.systems.tasks; + +import com.djrapitops.plugin.api.systems.TaskCenter; +import com.djrapitops.plugin.task.AbsRunnable; +import com.djrapitops.plugin.task.IRunnable; +import com.djrapitops.plugin.task.RunnableFactory; +import main.java.com.djrapitops.plan.systems.SubSystem; +import main.java.com.djrapitops.plan.systems.Systems; +import main.java.com.djrapitops.plan.utilities.MiscUtils; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public abstract class TaskSystem implements SubSystem { + + protected TPSCountTimer tpsCountTimer; + + public static TaskSystem getInstance() { + return Systems.getInstance().getTaskSystem(); + } + + protected IRunnable registerTask(AbsRunnable runnable) { + String taskName = runnable.getName(); + return registerTask(taskName != null ? taskName : runnable.getClass().getSimpleName(), runnable); + } + + protected IRunnable registerTask(String name, AbsRunnable runnable) { + return RunnableFactory.createNew(name, runnable); + } + + @Override + public void close() { + TaskCenter.cancelAllKnownTasks(MiscUtils.getIPlan().getClass()); + } + + public TPSCountTimer getTpsCountTimer() { + return tpsCountTimer; + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java index cb219f3cb..5e5d4a029 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/analysis/Analysis.java @@ -20,6 +20,8 @@ import main.java.com.djrapitops.plan.systems.cache.DataCache; import main.java.com.djrapitops.plan.systems.cache.SessionCache; import main.java.com.djrapitops.plan.systems.info.BukkitInformationManager; import main.java.com.djrapitops.plan.systems.info.InformationManager; +import main.java.com.djrapitops.plan.systems.tasks.PlanTaskSystem; +import main.java.com.djrapitops.plan.systems.tasks.TaskSystem; import main.java.com.djrapitops.plan.systems.webserver.response.ErrorResponse; import main.java.com.djrapitops.plan.systems.webserver.response.InternalErrorResponse; @@ -54,6 +56,8 @@ public class Analysis { return; } + ((PlanTaskSystem) TaskSystem.getInstance()).cancelBootAnalysis(); + Benchmark.start("Analysis"); log(Locale.get(Msg.ANALYSIS_START).toString()); // Async task for Analysis @@ -218,10 +222,6 @@ public class Analysis { return taskId != -1; } - public void setTaskId(int id) { - taskId = id; - } - /** * Only available during Analysis. *