From 9cbfbb6f27c8b4a3fd19b8bad991eb0a889b7729 Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Wed, 15 Jun 2022 11:14:15 +0200 Subject: [PATCH] :bug: Fixed async shutdown task exception, closes #60 --- .../nl/SBDeveloper/V10Lift/V10LiftPlugin.java | 2 +- .../V10Lift/commands/V10LiftCommand.java | 4 +-- .../V10Lift/managers/DBManager.java | 29 +++++-------------- 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java b/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java index 03ef1e8..2c52bbd 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java @@ -123,7 +123,7 @@ public class V10LiftPlugin extends JavaPlugin { @Override public void onDisable() { - dbManager.save(true); + dbManager.save(); dbManager.closeConnection(); instance = null; diff --git a/src/main/java/nl/SBDeveloper/V10Lift/commands/V10LiftCommand.java b/src/main/java/nl/SBDeveloper/V10Lift/commands/V10LiftCommand.java index 1254cac..2335d8c 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/commands/V10LiftCommand.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/commands/V10LiftCommand.java @@ -361,7 +361,7 @@ public class V10LiftCommand implements CommandExecutor { DataManager.clearMovingTasks(); V10LiftPlugin.getSConfig().reloadConfig(); try { - V10LiftPlugin.getDBManager().save(true); + V10LiftPlugin.getDBManager().save(); V10LiftPlugin.getDBManager().load(); } catch (SQLException e) { e.printStackTrace(); @@ -955,7 +955,7 @@ public class V10LiftCommand implements CommandExecutor { DataManager.removeRopeRemovesPlayer(p.getUniqueId()); DataManager.removeDoorEditPlayer(p.getUniqueId()); - V10LiftPlugin.getDBManager().saveLift(liftName, lift); + V10LiftPlugin.getDBManager().saveLift(liftName, lift, false); BlockState bs; Sign sign; diff --git a/src/main/java/nl/SBDeveloper/V10Lift/managers/DBManager.java b/src/main/java/nl/SBDeveloper/V10Lift/managers/DBManager.java index f38876a..2f276a3 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/managers/DBManager.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/managers/DBManager.java @@ -93,27 +93,10 @@ public class DBManager { /** * Save all lifts to data - * This is done async */ public void save() { - save(false); - } - - /** - * Save all lifts to data - * @param force true if sync, false if async - */ - public void save(boolean force) { - if (!force) { - Bukkit.getScheduler().runTaskAsynchronously(V10LiftPlugin.getInstance(), () -> { - for (Map.Entry entry : DataManager.getLifts().entrySet()) { - saveLift(entry.getKey(), entry.getValue()); - } - }); - } else { - for (Map.Entry entry : DataManager.getLifts().entrySet()) { - saveLift(entry.getKey(), entry.getValue()); - } + for (Map.Entry entry : DataManager.getLifts().entrySet()) { + saveLift(entry.getKey(), entry.getValue(), true); } } @@ -123,11 +106,15 @@ public class DBManager { * @param liftName The name of the lift * @param lift The lift itself */ - public void saveLift(String liftName, Lift lift) { + public void saveLift(String liftName, Lift lift, boolean sync) { Bukkit.getLogger().info("[V10Lift] Saving lift " + liftName + " to data..."); byte[] blob = gson.toJson(lift).getBytes(); - Bukkit.getScheduler().runTaskAsynchronously(V10LiftPlugin.getInstance(), () -> updateLift(liftName, blob)); + if (sync) { + updateLift(liftName, blob); + } else { + Bukkit.getScheduler().runTaskAsynchronously(V10LiftPlugin.getInstance(), () -> updateLift(liftName, blob)); + } } /**