From 0ca25dbad05309922358903aba571b4caed46755 Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Thu, 15 Jul 2021 13:25:08 +0200 Subject: [PATCH] Made saving async and added one-lift saving to edit command. --- .../nl/SBDeveloper/V10Lift/V10LiftPlugin.java | 2 - .../V10Lift/commands/V10LiftCommand.java | 2 +- .../V10Lift/managers/DBManager.java | 84 +++++++++++-------- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/V10LiftPlugin.java b/src/main/lombok/nl/SBDeveloper/V10Lift/V10LiftPlugin.java index 36964bc..4cf287d 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/V10LiftPlugin.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/V10LiftPlugin.java @@ -128,8 +128,6 @@ public class V10LiftPlugin extends JavaPlugin { @Override public void onDisable() { - dbManager.removeFromData(); //TODO Find a better way, override? - dbManager.save(); dbManager.closeConnection(); diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/commands/V10LiftCommand.java b/src/main/lombok/nl/SBDeveloper/V10Lift/commands/V10LiftCommand.java index 6dd7250..2b98847 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/commands/V10LiftCommand.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/commands/V10LiftCommand.java @@ -954,7 +954,7 @@ public class V10LiftCommand implements CommandExecutor { DataManager.removeRopeRemovesPlayer(p.getUniqueId()); DataManager.removeDoorEditPlayer(p.getUniqueId()); - V10LiftPlugin.getDBManager().save(); + V10LiftPlugin.getDBManager().saveLift(liftName, lift); BlockState bs; Sign sign; diff --git a/src/main/lombok/nl/SBDeveloper/V10Lift/managers/DBManager.java b/src/main/lombok/nl/SBDeveloper/V10Lift/managers/DBManager.java index a137100..8a9b2ee 100644 --- a/src/main/lombok/nl/SBDeveloper/V10Lift/managers/DBManager.java +++ b/src/main/lombok/nl/SBDeveloper/V10Lift/managers/DBManager.java @@ -1,6 +1,7 @@ package nl.SBDeveloper.V10Lift.managers; import com.google.gson.Gson; +import nl.SBDeveloper.V10Lift.V10LiftPlugin; import nl.SBDeveloper.V10Lift.api.objects.Lift; import nl.SBDeveloper.V10Lift.sbutils.SQLiteDB; import org.bukkit.Bukkit; @@ -12,6 +13,8 @@ import java.sql.SQLException; import java.util.Map; public class DBManager { + private static final Gson gson = new Gson(); + private static SQLiteDB data; private static Connection con; @@ -46,7 +49,6 @@ public class DBManager { byte[] blob = liftSet.getBytes("liftData"); String json = new String(blob); - Gson gson = new Gson(); Lift lift = gson.fromJson(json, Lift.class); DataManager.addLift(liftSet.getString("liftName"), lift); @@ -54,67 +56,81 @@ public class DBManager { } } - public void removeFromData() { - try { - String query0 = "SELECT * FROM lifts"; - PreparedStatement statement0 = con.prepareStatement(query0); - ResultSet liftSet = statement0.executeQuery(); - while (liftSet.next()) { - if (!DataManager.containsLift(liftSet.getString("liftName"))) { - Bukkit.getLogger().info("[V10Lift] Removing lift " + liftSet.getString("liftName") + " to data..."); + public void removeFromData(String name) { + if (!DataManager.containsLift(name)) { + Bukkit.getLogger().info("[V10Lift] Removing lift " + name + " to data..."); + Bukkit.getScheduler().runTaskAsynchronously(V10LiftPlugin.getInstance(), () -> { + try { String query = "DELETE FROM lifts WHERE liftName = ?"; PreparedStatement statement = con.prepareStatement(query); - statement.setString(1, liftSet.getString("liftName")); + statement.setString(1, name); statement.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); } - } - } catch(SQLException e) { - e.printStackTrace(); - } - } - - public void removeFromData(String name) { - try { - if (!DataManager.containsLift(name)) { - Bukkit.getLogger().info("[V10Lift] Removing lift " + name + " to data..."); - - String query = "DELETE FROM lifts WHERE liftName = ?"; - PreparedStatement statement = con.prepareStatement(query); - statement.setString(1, name); - statement.executeUpdate(); - } - } catch(SQLException e) { - e.printStackTrace(); + }); } } public void save() { for (Map.Entry entry : DataManager.getLifts().entrySet()) { - - Gson gson = new Gson(); byte[] blob = gson.toJson(entry.getValue()).getBytes(); Bukkit.getLogger().info("[V10Lift] Saving lift " + entry.getKey() + " to data..."); + Bukkit.getScheduler().runTaskAsynchronously(V10LiftPlugin.getInstance(), () -> { + try { + String query = "INSERT INTO lifts (liftName, liftData) VALUES (?, ?)"; + PreparedStatement statement = con.prepareStatement(query); + statement.setString(1, entry.getKey()); + statement.setBytes(2, blob); + statement.executeUpdate(); + } catch (SQLException ignored) { + } + }); + + Bukkit.getScheduler().runTaskAsynchronously(V10LiftPlugin.getInstance(), () -> { + try { + String query2 = "UPDATE lifts SET liftData = ? WHERE liftName = ?"; + PreparedStatement statement2 = con.prepareStatement(query2); + statement2.setBytes(1, blob); + statement2.setString(2, entry.getKey()); + statement2.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + }); + } + } + + public void saveLift(String name, Lift lift) { + byte[] blob = gson.toJson(lift).getBytes(); + + Bukkit.getLogger().info("[V10Lift] Saving lift " + name + " to data..."); + + Bukkit.getScheduler().runTaskAsynchronously(V10LiftPlugin.getInstance(), () -> { try { String query = "INSERT INTO lifts (liftName, liftData) VALUES (?, ?)"; PreparedStatement statement = con.prepareStatement(query); - statement.setString(1, entry.getKey()); + statement.setString(1, name); statement.setBytes(2, blob); statement.executeUpdate(); - } catch (SQLException ignored) {} + } catch (SQLException ignored) { + } + }); + Bukkit.getScheduler().runTaskAsynchronously(V10LiftPlugin.getInstance(), () -> { try { String query2 = "UPDATE lifts SET liftData = ? WHERE liftName = ?"; PreparedStatement statement2 = con.prepareStatement(query2); statement2.setBytes(1, blob); - statement2.setString(2, entry.getKey()); + statement2.setString(2, name); statement2.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } - } + }); } public void closeConnection() { -- 2.45.2