From 8cf22de4b68477d7bbe0127c7b3872499c47d813 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 4 Jul 2017 11:24:50 +0300 Subject: [PATCH] Improvements & Fix #147 - RslPlugin v1.4.0 - RunnableFactory - Prevented a collision if multiple RslPlugin implementations are installed. - Queues now wait for each other. --- .../main/java/com/djrapitops/plan/Log.java | 2 +- .../main/java/com/djrapitops/plan/Plan.java | 13 +++--- .../plan/command/commands/AnalyzeCommand.java | 6 +-- .../plan/command/commands/InspectCommand.java | 9 +++-- .../command/commands/QuickAnalyzeCommand.java | 5 ++- .../command/commands/QuickInspectCommand.java | 9 +++-- .../plan/command/commands/SearchCommand.java | 7 ++-- .../commands/manage/ManageBackupCommand.java | 5 +-- .../commands/manage/ManageCleanCommand.java | 3 +- .../commands/manage/ManageClearCommand.java | 5 +-- .../commands/manage/ManageImportCommand.java | 12 +++--- .../commands/manage/ManageMoveCommand.java | 5 +-- .../commands/manage/ManageRemoveCommand.java | 8 ++-- .../commands/manage/ManageRestoreCommand.java | 7 ++-- .../plan/data/cache/DataCacheHandler.java | 18 ++++++--- .../plan/data/cache/queue/Consumer.java | 5 +-- .../data/cache/queue/DataCacheGetQueue.java | 12 ++++-- .../cache/queue/DataCacheProcessQueue.java | 10 ++++- .../data/cache/queue/DataCacheSaveQueue.java | 40 +++++++++++-------- .../plan/data/cache/queue/Setup.java | 9 ++--- .../data/listeners/PlanPlayerListener.java | 5 ++- .../plan/data/listeners/TPSCountTimer.java | 4 +- .../plan/database/databases/SQLDB.java | 9 +++-- .../plan/ui/webserver/WebSocketServer.java | 3 +- .../plan/utilities/analysis/Analysis.java | 3 +- Plan/src/test/java/utils/TestInit.java | 2 +- 26 files changed, 122 insertions(+), 94 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/Log.java b/Plan/src/main/java/com/djrapitops/plan/Log.java index a9ab50eced..a57cd76f87 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Log.java +++ b/Plan/src/main/java/com/djrapitops/plan/Log.java @@ -16,7 +16,7 @@ public class Log { * @param message "Message" will show up as [INFO][Plan]: Message */ public static void info(String message) { - Plan.getPluginInstance().getPluginLogger().info(message); + Plan.getInstance().getPluginLogger().info(message); } /** diff --git a/Plan/src/main/java/com/djrapitops/plan/Plan.java b/Plan/src/main/java/com/djrapitops/plan/Plan.java index 7ff9959964..faf4d917ec 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/src/main/java/com/djrapitops/plan/Plan.java @@ -22,6 +22,7 @@ import com.djrapitops.javaplugin.api.ColorScheme; import com.djrapitops.javaplugin.RslPlugin; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import com.djrapitops.javaplugin.task.RslTask; import java.io.File; import java.io.FileNotFoundException; @@ -114,7 +115,7 @@ public void onEnable() { this.inspectCache = new InspectCacheHandler(this); this.analysisCache = new AnalysisCacheHandler(this); registerListeners(); - new TPSCountTimer(this).runTaskTimer(1000, 20); + getRunnableFactory().createNew(new TPSCountTimer(this)).runTaskTimer(1000, 20); registerCommand(new PlanCommand(this)); @@ -231,7 +232,7 @@ public boolean initDatabase() { } private void startAnalysisRefreshTask(int analysisRefreshMinutes) throws IllegalStateException, IllegalArgumentException { - RslTask task = new RslBukkitRunnable("PeriodicalAnalysisTask") { + RslTask task = getRunnableFactory().createNew("PeriodicalAnalysisTask", new RslRunnable() { @Override public void run() { if (!analysisCache.isCached()) { @@ -240,19 +241,19 @@ public void run() { analysisCache.updateCache(); } } - }.runTaskTimerAsynchronously(analysisRefreshMinutes * 60 * 20, analysisRefreshMinutes * 60 * 20); + }).runTaskTimerAsynchronously(analysisRefreshMinutes * 60 * 20, analysisRefreshMinutes * 60 * 20); } private void startBootAnalysisTask() throws IllegalStateException, IllegalArgumentException { Log.info(Phrase.ANALYSIS_BOOT_NOTIFY + ""); - RslTask bootAnalysisTask = new RslBukkitRunnable("BootAnalysisTask") { + RslTask bootAnalysisTask = getRunnableFactory().createNew("BootAnalysisTask", new RslRunnable() { @Override public void run() { Log.info(Phrase.ANALYSIS_BOOT + ""); analysisCache.updateCache(); this.cancel(); } - }.runTaskLaterAsynchronously(30 * 20); + }).runTaskLaterAsynchronously(30 * 20); bootAnalysisTaskID = bootAnalysisTask.getTaskId(); } @@ -429,6 +430,6 @@ public static API getPlanAPI() throws IllegalStateException { * @return this object. */ public static Plan getInstance() { - return (Plan) getPluginInstance(); + return (Plan) getPluginInstance(Plan.class); } } 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 7f8d790fb8..9f1431b023 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 @@ -3,7 +3,7 @@ import com.djrapitops.javaplugin.command.CommandType; import com.djrapitops.javaplugin.command.SubCommand; import com.djrapitops.javaplugin.command.sender.ISender; -import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import com.djrapitops.javaplugin.task.RslTask; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; @@ -61,7 +61,7 @@ public boolean onCommand(ISender sender, String commandLabel, String[] args) { } analysisCache.updateCache(); } - final RslTask task = new RslBukkitRunnable("AnalysisMessageSenderTask") { + final RslTask task = plugin.getRunnableFactory().createNew("AnalysisMessageSenderTask", new RslRunnable() { private int timesrun = 0; @Override @@ -78,7 +78,7 @@ public void run() { this.cancel(); } } - }.runTaskTimer(1 * 20, 5 * 20); + }).runTaskTimer(1 * 20, 5 * 20); return true; } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java index 3771550d81..2e5af04b99 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java @@ -4,6 +4,7 @@ import com.djrapitops.javaplugin.command.SubCommand; import com.djrapitops.javaplugin.command.sender.ISender; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import com.djrapitops.javaplugin.task.RslTask; import main.java.com.djrapitops.plan.command.ConditionUtils; import java.util.UUID; @@ -50,7 +51,7 @@ public boolean onCommand(ISender sender, String commandLabel, String[] args) { return true; } String playerName = MiscUtils.getPlayerName(args, sender); - final RslTask inspectTask = new RslBukkitRunnable("InspectTask") { + final RslTask inspectTask = plugin.getRunnableFactory().createNew(new RslRunnable("InspectTask") { @Override public void run() { UUID uuid = ConditionUtils.getUUID(playerName); @@ -69,7 +70,7 @@ public void run() { } sender.sendMessage(Phrase.GRABBING_DATA_MESSAGE + ""); inspectCache.cache(uuid); - final RslTask inspectMessageSenderTask = new RslBukkitRunnable("InspectMessageSenderTask") { + final RslTask inspectMessageSenderTask = plugin.getRunnableFactory().createNew(new RslRunnable("InspectMessageSenderTask") { private int timesrun = 0; @Override @@ -87,10 +88,10 @@ public void run() { } } - }.runTaskTimer(1 * 20, 5 * 20); + }).runTaskTimer(1 * 20, 5 * 20); this.cancel(); } - }.runTaskAsynchronously(); + }).runTaskAsynchronously(); return true; } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickAnalyzeCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickAnalyzeCommand.java index ba02e2c9f6..b279ee811d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickAnalyzeCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickAnalyzeCommand.java @@ -4,6 +4,7 @@ import com.djrapitops.javaplugin.command.SubCommand; import com.djrapitops.javaplugin.command.sender.ISender; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import com.djrapitops.javaplugin.task.RslTask; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; @@ -56,7 +57,7 @@ public boolean onCommand(ISender sender, String commandLabel, String[] args) { analysisCache.updateCache(); } - RslTask analysisMessageSenderTask = new RslBukkitRunnable("QanalysisMessageSenderTask") { + RslTask analysisMessageSenderTask = plugin.getRunnableFactory().createNew(new RslRunnable("QanalysisMessageSenderTask") { private int timesrun = 0; @Override @@ -74,7 +75,7 @@ public void run() { this.cancel(); } } - }.runTaskTimer(1 * 20, 5 * 20); + }).runTaskTimer(1 * 20, 5 * 20); return true; } } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickInspectCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickInspectCommand.java index 0120472aea..430ad0e338 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickInspectCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/QuickInspectCommand.java @@ -4,6 +4,7 @@ import com.djrapitops.javaplugin.command.SubCommand; import com.djrapitops.javaplugin.command.sender.ISender; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import com.djrapitops.javaplugin.task.RslTask; import java.util.UUID; import main.java.com.djrapitops.plan.Log; @@ -45,7 +46,7 @@ public QuickInspectCommand(Plan plugin) { @Override public boolean onCommand(ISender sender, String commandLabel, String[] args) { String playerName = MiscUtils.getPlayerName(args, sender, Permissions.QUICK_INSPECT_OTHER); - final RslTask inspectTask = new RslBukkitRunnable("QinspectTask") { + final RslTask inspectTask = plugin.getRunnableFactory().createNew(new RslRunnable("QinspectTask") { @Override public void run() { UUID uuid = ConditionUtils.getUUID(playerName); @@ -64,7 +65,7 @@ public void run() { } sender.sendMessage(Phrase.GRABBING_DATA_MESSAGE + ""); inspectCache.cache(uuid); - final RslTask inspectMessageSenderTask = new RslBukkitRunnable("QinspectMessageSenderTask") { + final RslTask inspectMessageSenderTask = plugin.getRunnableFactory().createNew(new RslRunnable("QinspectMessageSenderTask") { private int timesrun = 0; @Override @@ -82,10 +83,10 @@ public void run() { this.cancel(); } } - }.runTaskTimer(1 * 20, 5 * 20); + }).runTaskTimer(1 * 20, 5 * 20); this.cancel(); } - }.runTaskAsynchronously(); + }).runTaskAsynchronously(); return true; } } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java index 08bcce13a5..5695e3c590 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/SearchCommand.java @@ -4,6 +4,7 @@ import com.djrapitops.javaplugin.command.SubCommand; import com.djrapitops.javaplugin.command.sender.ISender; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import com.djrapitops.javaplugin.task.RslTask; import com.djrapitops.javaplugin.utilities.FormattingUtils; import java.util.Arrays; @@ -18,8 +19,6 @@ import main.java.com.djrapitops.plan.data.cache.InspectCacheHandler; import main.java.com.djrapitops.plan.utilities.MiscUtils; import org.bukkit.OfflinePlayer; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; /** * This subcommand is used to search for a user, and to view all matches' data. @@ -51,7 +50,7 @@ public boolean onCommand(ISender sender, String commandLabel, String[] args) { return true; } sender.sendMessage(Phrase.CMD_SEARCH_SEARCHING + ""); - final RslTask searchTask = new RslBukkitRunnable("SearchTask: " + Arrays.toString(args)) { + final RslTask searchTask =plugin.getRunnableFactory().createNew(new RslRunnable("SearchTask: " + Arrays.toString(args)) { @Override public void run() { try { @@ -70,7 +69,7 @@ public void run() { this.cancel(); } } - }.runTaskAsynchronously(); + }).runTaskAsynchronously(); return true; } } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageBackupCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageBackupCommand.java index 3b54d6e391..79795f8679 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageBackupCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageBackupCommand.java @@ -4,14 +4,13 @@ import com.djrapitops.javaplugin.command.SubCommand; import com.djrapitops.javaplugin.command.sender.ISender; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.utilities.ManageUtils; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; /** * This manage subcommand is used to backup a database to a .db file. @@ -62,7 +61,7 @@ public boolean onCommand(ISender sender, String commandLabel, String[] args) { return true; } final Database copyFromDB = database; - (new RslBukkitRunnable("BackupTask") { + plugin.getRunnableFactory().createNew(new RslRunnable("BackupTask") { @Override public void run() { sender.sendMessage(Phrase.MANAGE_PROCESS_START.parse()); diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageCleanCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageCleanCommand.java index 79e1ffcdc1..5d9a23f1c8 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageCleanCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageCleanCommand.java @@ -4,6 +4,7 @@ import com.djrapitops.javaplugin.command.SubCommand; import com.djrapitops.javaplugin.command.sender.ISender; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Phrase; @@ -59,7 +60,7 @@ public boolean onCommand(ISender sender, String commandLabel, String[] args) { } final Database clearThisDB = clearDB; - (new RslBukkitRunnable("DBCleanTask") { + plugin.getRunnableFactory().createNew(new RslRunnable("DBCleanTask") { @Override public void run() { sender.sendMessage(Phrase.MANAGE_PROCESS_START.parse()); diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageClearCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageClearCommand.java index e59ed03b31..f7e77774d8 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageClearCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageClearCommand.java @@ -4,6 +4,7 @@ import com.djrapitops.javaplugin.command.SubCommand; import com.djrapitops.javaplugin.command.sender.ISender; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import java.sql.SQLException; import java.util.Arrays; import main.java.com.djrapitops.plan.Log; @@ -11,8 +12,6 @@ import main.java.com.djrapitops.plan.Phrase; import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.database.Database; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; /** * This manage subcommand is used to clear a database of all data. @@ -65,7 +64,7 @@ public boolean onCommand(ISender sender, String commandLabel, String[] args) { } final Database clearThisDB = clearDB; - (new RslBukkitRunnable("DBClearTask") { + plugin.getRunnableFactory().createNew(new RslRunnable("DBClearTask") { @Override public void run() { sender.sendMessage(Phrase.MANAGE_PROCESS_START.parse()); diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageImportCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageImportCommand.java index 68b7d7444e..ddac4f65df 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageImportCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageImportCommand.java @@ -3,7 +3,7 @@ import com.djrapitops.javaplugin.command.CommandType; import com.djrapitops.javaplugin.command.SubCommand; import com.djrapitops.javaplugin.command.sender.ISender; -import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import com.djrapitops.javaplugin.task.RslTask; import java.util.Arrays; import java.util.List; @@ -63,12 +63,12 @@ public boolean onCommand(ISender sender, String commandLabel, String[] args) { return true; } - String[] arguments = new String[args.length-1]; + String[] arguments = new String[args.length - 1]; for (int i = 1; i < args.length; i++) { - arguments[i-1] = args[i]; + arguments[i - 1] = args[i]; } final Importer importer = importPlugins.get(importFromPlugin); - RslTask asyncImportTask = new RslBukkitRunnable("ImportTask") { + RslTask asyncImportTask = plugin.getRunnableFactory().createNew(new RslRunnable("ImportTask") { @Override public void run() { sender.sendMessage(Phrase.MANAGE_IMPORTING + ""); @@ -80,7 +80,7 @@ public void run() { } this.cancel(); } - }.runTaskAsynchronously(); + }).runTaskAsynchronously(); return true; } @@ -88,7 +88,7 @@ private void list(ISender sender) { sender.sendMessage(Phrase.CMD_FOOTER.parse()); Map importers = ImportUtils.getImporters(); for (String key : importers.keySet()) { - sender.sendMessage(Phrase.CMD_BALL+" "+key+": "+importers.get(key).getInfo()); + sender.sendMessage(Phrase.CMD_BALL + " " + key + ": " + importers.get(key).getInfo()); } sender.sendMessage(Phrase.CMD_FOOTER.parse()); } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageMoveCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageMoveCommand.java index cd68bee77a..4679fff363 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageMoveCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageMoveCommand.java @@ -4,6 +4,7 @@ import com.djrapitops.javaplugin.command.SubCommand; import com.djrapitops.javaplugin.command.sender.ISender; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import java.util.Arrays; import java.util.Collection; import java.util.UUID; @@ -13,8 +14,6 @@ import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.utilities.ManageUtils; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; /** * This manage subcommand is used to move all data from one database to another. @@ -89,7 +88,7 @@ public boolean onCommand(ISender sender, String commandLabel, String[] args) { final Database moveFromDB = fromDatabase; final Database moveToDB = toDatabase; - (new RslBukkitRunnable("DBMoveTask") { + plugin.getRunnableFactory().createNew(new RslRunnable("DBMoveTask") { @Override public void run() { final Collection uuids = ManageUtils.getUUIDS(moveFromDB); diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRemoveCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRemoveCommand.java index 07bdba5e9d..f75dfc933c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRemoveCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRemoveCommand.java @@ -3,7 +3,7 @@ import com.djrapitops.javaplugin.command.CommandType; import com.djrapitops.javaplugin.command.SubCommand; import com.djrapitops.javaplugin.command.sender.ISender; -import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import java.sql.SQLException; import java.util.Arrays; import java.util.UUID; @@ -13,8 +13,6 @@ import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.uuid.UUIDUtility; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; /** * This manage subcommand is used to remove a single player's data from the @@ -46,7 +44,7 @@ public boolean onCommand(ISender sender, String commandLabel, String[] args) { String playerName = MiscUtils.getPlayerName(args, sender, Permissions.MANAGE); - (new RslBukkitRunnable("DBRemoveTask " + playerName) { + plugin.getRunnableFactory().createNew(new RslRunnable("DBRemoveTask " + playerName) { @Override public void run() { UUID uuid; @@ -85,7 +83,7 @@ public void run() { } this.cancel(); } - }).runTaskAsynchronously(plugin); + }).runTaskAsynchronously(); return true; } } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRestoreCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRestoreCommand.java index e64f2095f1..cdd33ece15 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRestoreCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageRestoreCommand.java @@ -4,6 +4,7 @@ import com.djrapitops.javaplugin.command.SubCommand; import com.djrapitops.javaplugin.command.sender.ISender; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import com.djrapitops.javaplugin.task.RslTask; import java.io.File; import java.util.Arrays; @@ -16,8 +17,6 @@ import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.databases.SQLiteDB; import main.java.com.djrapitops.plan.utilities.ManageUtils; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; /** * This manage subcommand is used to restore a backup.db file in the @@ -72,7 +71,7 @@ public boolean onCommand(ISender sender, String commandLabel, String[] args) { return true; } final Database copyToDB = database; - RslTask asyncRestoreTask = new RslBukkitRunnable("RestoreTask") { + RslTask asyncRestoreTask = plugin.getRunnableFactory().createNew(new RslRunnable("RestoreTask") { @Override public void run() { String backupDBName = args[0]; @@ -109,7 +108,7 @@ public void run() { } this.cancel(); } - }.runTaskAsynchronously(); + }).runTaskAsynchronously(); } catch (NullPointerException e) { sender.sendMessage(Phrase.MANAGE_DATABASE_FAILURE + ""); } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/DataCacheHandler.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/DataCacheHandler.java index 9a534d6833..69ebab22fc 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/DataCacheHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/DataCacheHandler.java @@ -1,6 +1,7 @@ package main.java.com.djrapitops.plan.data.cache; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import com.djrapitops.javaplugin.task.RslTask; import java.sql.SQLException; import java.util.ArrayList; @@ -34,6 +35,13 @@ import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import static org.bukkit.Bukkit.getOfflinePlayer; +import static org.bukkit.Bukkit.getOfflinePlayer; +import static org.bukkit.Bukkit.getOfflinePlayer; +import static org.bukkit.Bukkit.getOfflinePlayer; +import static org.bukkit.Bukkit.getOfflinePlayer; +import static org.bukkit.Bukkit.getOfflinePlayer; +import static org.bukkit.Bukkit.getOfflinePlayer; +import static org.bukkit.Bukkit.getOfflinePlayer; /** * This Class contains the Cache. @@ -115,7 +123,7 @@ public boolean getCommandUseFromDb() { */ public void startQueues() { clearTask = new DataCacheClearQueue(this); - saveTask = new DataCacheSaveQueue(plugin, clearTask); + saveTask = new DataCacheSaveQueue(plugin, this); getTask = new DataCacheGetQueue(plugin); processTask = new DataCacheProcessQueue(this); } @@ -139,7 +147,7 @@ public void startAsyncPeriodicSaveTask() throws IllegalArgumentException, Illega } else { clearAfterXsaves = configValue; } - RslTask asyncPeriodicCacheSaveTask = new RslBukkitRunnable("PeriodicCacheSaveTask") { + RslTask asyncPeriodicCacheSaveTask = plugin.getRunnableFactory().createNew(new RslRunnable("PeriodicCacheSaveTask") { private int timesSaved = 0; @Override @@ -159,12 +167,12 @@ public void run() { saveUnsavedTPSHistory(); timesSaved++; } catch (Exception e) { - Log.toLog(this.getClass().getName() + "(" + this.getTaskName() + ")", e); + Log.toLog(this.getClass().getName() + "(" + this.getName() + ")", e); } finally { periodicTaskIsSaving = false; } } - }.runTaskTimerAsynchronously(60 * 20 * minutes, 60 * 20 * minutes); + }).runTaskTimerAsynchronously(60 * 20 * minutes, 60 * 20 * minutes); } /** @@ -518,7 +526,7 @@ public Map getCommandUse() { * Calls all the methods that are ran when PlayerJoinEvent is fired */ public void handleReload() { - RslTask asyncReloadCacheUpdateTask = (new RslBukkitRunnable("ReloadCacheUpdateTask") { + RslTask asyncReloadCacheUpdateTask = plugin.getRunnableFactory().createNew(new RslRunnable("ReloadCacheUpdateTask") { @Override public void run() { for (Player player : Bukkit.getOnlinePlayers()) { diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/Consumer.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/Consumer.java index cd393662d7..e1dc361602 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/Consumer.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/Consumer.java @@ -1,8 +1,7 @@ package main.java.com.djrapitops.plan.data.cache.queue; -import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import java.util.concurrent.BlockingQueue; -import main.java.com.djrapitops.plan.Plan; /** * Abstract class representing a queue consumer. @@ -10,7 +9,7 @@ * @author Rsl1122 * @param */ -public abstract class Consumer extends RslBukkitRunnable { +public abstract class Consumer extends RslRunnable { boolean run; final BlockingQueue queue; diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheGetQueue.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheGetQueue.java index d2dd7b7abd..9fe06e1291 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheGetQueue.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheGetQueue.java @@ -46,15 +46,19 @@ public void scheduleForGet(UUID uuid, DBCallableProcessor... processors) { Log.debug(uuid + ": Scheduling for get"); try { Map> map = new HashMap<>(); - if (map.get(uuid) == null) { - map.put(uuid, new ArrayList<>()); - } - map.get(uuid).addAll(Arrays.asList(processors)); + map.put(uuid, Arrays.asList(processors)); queue.add(map); } catch (IllegalStateException e) { Log.error(Phrase.ERROR_TOO_SMALL_QUEUE.parse("Get Queue", Settings.PROCESS_GET_LIMIT.getNumber() + "")); } } + + public boolean containsUUIDtoBeCached(UUID uuid) { + if (uuid == null) { + return false; + } + return new ArrayList<>(queue).stream().anyMatch((map) -> (map.get(uuid) != null && map.get(uuid).size() >= 2)); // Map has 2 processors if being cached + } } class GetConsumer extends Consumer>> { diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheProcessQueue.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheProcessQueue.java index b0988894bb..8b1389fbf2 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheProcessQueue.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheProcessQueue.java @@ -66,7 +66,10 @@ public void addToPool(Collection info) { * @return true/false */ public boolean containsUUID(UUID uuid) { - return new ArrayList<>(queue).stream().map(d -> d.getUuid()).collect(Collectors.toList()).contains(uuid); + if (uuid == null) { + return false; + } + return new ArrayList<>(queue).stream().anyMatch(info -> info.getUuid().equals(uuid)); } } @@ -84,6 +87,10 @@ void consume(HandlingInfo info) { if (handler == null) { return; } + if (handler.getGetTask().containsUUIDtoBeCached(info.getUuid())) { // Wait for get queue. + queue.add(info); + return; + } Log.debug(info.getUuid() + ": Processing type: " + info.getType().name()); DBCallableProcessor p = new DBCallableProcessor() { @Override @@ -105,6 +112,7 @@ void clearVariables() { } class ProcessSetup extends Setup { + ProcessSetup(BlockingQueue q, DataCacheHandler h) { super(new ProcessConsumer(q, h), new ProcessConsumer(q, h)); } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheSaveQueue.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheSaveQueue.java index aaf458007a..55aeebd6bb 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheSaveQueue.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/DataCacheSaveQueue.java @@ -12,6 +12,7 @@ import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Settings; import main.java.com.djrapitops.plan.data.UserData; +import main.java.com.djrapitops.plan.data.cache.DataCacheHandler; import main.java.com.djrapitops.plan.database.Database; /** @@ -20,18 +21,17 @@ * @author Rsl1122 * @since 3.0.0 */ -public class DataCacheSaveQueue extends Queue{ +public class DataCacheSaveQueue extends Queue { /** * Class constructor, starts the new Thread for saving. * * @param plugin current instance of Plan - * @param clear current instance of the Clear task to schedule clear if - * UserData.clearAfterSave() is true + * @param handler DataCacheHandler */ - public DataCacheSaveQueue(Plan plugin, DataCacheClearQueue clear) { + public DataCacheSaveQueue(Plan plugin, DataCacheHandler handler) { super(new ArrayBlockingQueue(Settings.PROCESS_SAVE_LIMIT.getNumber())); - setup = new SaveSetup(queue, clear, plugin.getDB()); + setup = new SaveSetup(queue, handler, plugin.getDB()); setup.go(); } @@ -84,19 +84,22 @@ public void scheduleNewPlayer(UserData data) { * @return true/false */ public boolean containsUUID(UUID uuid) { - return new ArrayList<>(queue).stream().map(d -> d.getUuid()).collect(Collectors.toList()).contains(uuid); + if (uuid == null) { + return false; + } + return new ArrayList<>(queue).stream().anyMatch(d -> d.getUuid().equals(uuid)); } } class SaveConsumer extends Consumer { private Database db; - private DataCacheClearQueue clear; + private DataCacheHandler handler; - SaveConsumer(BlockingQueue q, DataCacheClearQueue clear, Database db) { + SaveConsumer(BlockingQueue q, DataCacheHandler handler, Database db) { super(q, "SaveQueueConsumer"); this.db = db; - this.clear = clear; + this.handler = handler; run = true; } @@ -106,14 +109,18 @@ void consume(UserData data) { return; } UUID uuid = data.getUuid(); + if (handler.getProcessTask().containsUUID(uuid)) { // Wait for process queue. + queue.add(data); + return; + } Log.debug(uuid + ": Saving: " + uuid); try { db.saveUserData(data); data.stopAccessing(); Log.debug(uuid + ": Saved!"); if (data.shouldClearAfterSave()) { - if (clear != null) { - clear.scheduleForClear(uuid); + if (handler != null) { + handler.getClearTask().scheduleForClear(uuid); } } } catch (SQLException ex) { @@ -127,14 +134,15 @@ void clearVariables() { if (db != null) { db = null; } - if (clear != null) { - clear = null; + if (handler != null) { + handler = null; } } } -class SaveSetup extends Setup{ - SaveSetup(BlockingQueue q, DataCacheClearQueue clear, Database db) { - super(new SaveConsumer(q, clear, db), new SaveConsumer(q, clear, db)); +class SaveSetup extends Setup { + + SaveSetup(BlockingQueue q, DataCacheHandler handler, Database db) { + super(new SaveConsumer(q, handler, db), new SaveConsumer(q, handler, db)); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/Setup.java b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/Setup.java index 5744cd7538..153819e3f7 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/Setup.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/cache/queue/Setup.java @@ -1,6 +1,5 @@ package main.java.com.djrapitops.plan.data.cache.queue; -import com.djrapitops.javaplugin.task.RslBukkitRunnable; import main.java.com.djrapitops.plan.Plan; /** @@ -10,7 +9,7 @@ * @param Object this queue consumes. */ public abstract class Setup { - + private final Consumer[] consumers; /** @@ -21,13 +20,13 @@ public abstract class Setup { public Setup(Consumer... consumers) { this.consumers = consumers; } - + void go() { for (Consumer consumer : consumers) { - consumer.runTaskAsynchronously(); + Plan.getInstance().getRunnableFactory().createNew(consumer).runTaskAsynchronously(); } } - + void stop() { for (Consumer consumer : consumers) { consumer.stop(); diff --git a/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanPlayerListener.java b/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanPlayerListener.java index fb710ad5a6..9d92280569 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanPlayerListener.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/listeners/PlanPlayerListener.java @@ -1,6 +1,7 @@ package main.java.com.djrapitops.plan.data.listeners; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import com.djrapitops.javaplugin.task.RslTask; import java.util.UUID; import main.java.com.djrapitops.plan.Log; @@ -57,7 +58,7 @@ public void onPlayerLogin(PlayerJoinEvent event) { UUID uuid = player.getUniqueId(); handler.startSession(uuid); Log.debug(uuid + ": PlayerJoinEvent"); - RslTask asyncNewPlayerCheckTask = new RslBukkitRunnable("NewPlayerCheckTask") { + RslTask asyncNewPlayerCheckTask = plugin.getRunnableFactory().createNew(new RslRunnable("NewPlayerCheckTask") { @Override public void run() { LoginInfo loginInfo = new LoginInfo(uuid, MiscUtils.getTime(), player.getAddress().getAddress(), player.isBanned(), player.getDisplayName(), player.getGameMode(), 1); @@ -72,7 +73,7 @@ public void run() { Log.debug(uuid + ": PlayerJoinEvent_AsyncTask_END, New:" + isNewPlayer); this.cancel(); } - }.runTaskAsynchronously(); + }).runTaskAsynchronously(); Log.debug(uuid + ": PlayerJoinEvent_END"); } diff --git a/Plan/src/main/java/com/djrapitops/plan/data/listeners/TPSCountTimer.java b/Plan/src/main/java/com/djrapitops/plan/data/listeners/TPSCountTimer.java index 0e7dcdb9d9..5008ae9fde 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/listeners/TPSCountTimer.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/listeners/TPSCountTimer.java @@ -1,7 +1,7 @@ package main.java.com.djrapitops.plan.data.listeners; import com.djrapitops.javaplugin.api.TimeAmount; -import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import java.util.ArrayList; import java.util.List; import main.java.com.djrapitops.plan.Plan; @@ -14,7 +14,7 @@ * * @author Rsl1122 */ -public class TPSCountTimer extends RslBukkitRunnable { +public class TPSCountTimer extends RslRunnable { private long lastCheckNano; private final Plan plugin; diff --git a/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java b/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java index 92675f5ddf..807f489161 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/databases/SQLDB.java @@ -1,6 +1,7 @@ package main.java.com.djrapitops.plan.database.databases; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import java.net.InetAddress; import java.sql.Connection; import java.sql.SQLException; @@ -67,7 +68,7 @@ public SQLDB(Plan plugin, boolean supportsModification) { */ public void startConnectionPingTask(Plan plugin) throws IllegalArgumentException, IllegalStateException { // Maintains Connection. - new RslBukkitRunnable("DBConnectionPingTask " + getName()) { + plugin.getRunnableFactory().createNew(new RslRunnable("DBConnectionPingTask " + getName()) { @Override public void run() { try { @@ -78,7 +79,7 @@ public void run() { connection = getNewConnection(); } } - }.runTaskTimerAsynchronously(60 * 20, 60 * 20); + }).runTaskTimerAsynchronously(60 * 20, 60 * 20); } /** @@ -150,7 +151,7 @@ public boolean checkConnection() throws SQLException { * */ public void convertBukkitDataToDB() { - new RslBukkitRunnable("BukkitDataConversionTask") { + plugin.getRunnableFactory().createNew(new RslRunnable("BukkitDataConversionTask") { @Override public void run() { try { @@ -177,7 +178,7 @@ public void run() { this.cancel(); } } - }.runTaskAsynchronously(); + }).runTaskAsynchronously(); } /** diff --git a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebSocketServer.java b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebSocketServer.java index e6a20d5584..b7afefbac9 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebSocketServer.java +++ b/Plan/src/main/java/com/djrapitops/plan/ui/webserver/WebSocketServer.java @@ -1,6 +1,7 @@ package main.java.com.djrapitops.plan.ui.webserver; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; @@ -60,7 +61,7 @@ public void initServer() { System.exit(1); } //Run server in seperate thread - (new RslBukkitRunnable("WebServerTask") { + plugin.getRunnableFactory().createNew(new RslRunnable("WebServerTask") { @Override public void run() { while (!shutdown) { 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 33691f35b5..0db1f6376b 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 @@ -2,6 +2,7 @@ import com.djrapitops.javaplugin.api.TimeAmount; import com.djrapitops.javaplugin.task.RslBukkitRunnable; +import com.djrapitops.javaplugin.task.RslRunnable; import com.djrapitops.javaplugin.task.RslTask; import java.util.ArrayList; import main.java.com.djrapitops.plan.data.additional.HookHandler; @@ -77,7 +78,7 @@ public void runAnalysis(AnalysisCacheHandler analysisCache) { plugin.processStatus().startExecution("Analysis"); log(Phrase.ANALYSIS_START + ""); // Async task for Analysis - RslTask asyncAnalysisTask = (new RslBukkitRunnable("AnalysisTask") { + RslTask asyncAnalysisTask = plugin.getRunnableFactory().createNew(new RslRunnable("AnalysisTask") { @Override public void run() { taskId = this.getTaskId(); diff --git a/Plan/src/test/java/utils/TestInit.java b/Plan/src/test/java/utils/TestInit.java index a879bd99eb..9c5e05554a 100644 --- a/Plan/src/test/java/utils/TestInit.java +++ b/Plan/src/test/java/utils/TestInit.java @@ -75,7 +75,7 @@ public boolean setUp() { when(planMock.processStatus()).thenReturn(process); BenchmarkUtil bench = new BenchmarkUtil(); when(planMock.benchmark()).thenReturn(bench); - Plan.setInstance(planMock); + Plan.setInstance(Plan.class, planMock); // Mockito.doReturn("0.0.0.0").when(planMock).getServer().getIp(); Settings.DEBUG.setValue(true); return true;