Merge pull request #57 from SBDPlugins/dev

Made saving async and added one-lift saving to edit command. Closes #56
This commit is contained in:
Stijn Bannink 2021-07-15 13:26:24 +02:00 committed by GitHub
commit 148f990061
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 37 deletions

View file

@ -128,8 +128,6 @@ public class V10LiftPlugin extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
dbManager.removeFromData(); //TODO Find a better way, override?
dbManager.save(); dbManager.save();
dbManager.closeConnection(); dbManager.closeConnection();

View file

@ -954,7 +954,7 @@ public class V10LiftCommand implements CommandExecutor {
DataManager.removeRopeRemovesPlayer(p.getUniqueId()); DataManager.removeRopeRemovesPlayer(p.getUniqueId());
DataManager.removeDoorEditPlayer(p.getUniqueId()); DataManager.removeDoorEditPlayer(p.getUniqueId());
V10LiftPlugin.getDBManager().save(); V10LiftPlugin.getDBManager().saveLift(liftName, lift);
BlockState bs; BlockState bs;
Sign sign; Sign sign;

View file

@ -1,6 +1,7 @@
package nl.SBDeveloper.V10Lift.managers; package nl.SBDeveloper.V10Lift.managers;
import com.google.gson.Gson; import com.google.gson.Gson;
import nl.SBDeveloper.V10Lift.V10LiftPlugin;
import nl.SBDeveloper.V10Lift.api.objects.Lift; import nl.SBDeveloper.V10Lift.api.objects.Lift;
import nl.SBDeveloper.V10Lift.sbutils.SQLiteDB; import nl.SBDeveloper.V10Lift.sbutils.SQLiteDB;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -12,6 +13,8 @@ import java.sql.SQLException;
import java.util.Map; import java.util.Map;
public class DBManager { public class DBManager {
private static final Gson gson = new Gson();
private static SQLiteDB data; private static SQLiteDB data;
private static Connection con; private static Connection con;
@ -46,7 +49,6 @@ public class DBManager {
byte[] blob = liftSet.getBytes("liftData"); byte[] blob = liftSet.getBytes("liftData");
String json = new String(blob); String json = new String(blob);
Gson gson = new Gson();
Lift lift = gson.fromJson(json, Lift.class); Lift lift = gson.fromJson(json, Lift.class);
DataManager.addLift(liftSet.getString("liftName"), lift); DataManager.addLift(liftSet.getString("liftName"), lift);
@ -54,67 +56,81 @@ public class DBManager {
} }
} }
public void removeFromData() { public void removeFromData(String name) {
try { if (!DataManager.containsLift(name)) {
String query0 = "SELECT * FROM lifts"; Bukkit.getLogger().info("[V10Lift] Removing lift " + name + " to data...");
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...");
Bukkit.getScheduler().runTaskAsynchronously(V10LiftPlugin.getInstance(), () -> {
try {
String query = "DELETE FROM lifts WHERE liftName = ?"; String query = "DELETE FROM lifts WHERE liftName = ?";
PreparedStatement statement = con.prepareStatement(query); PreparedStatement statement = con.prepareStatement(query);
statement.setString(1, liftSet.getString("liftName")); statement.setString(1, name);
statement.executeUpdate(); 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() { public void save() {
for (Map.Entry<String, Lift> entry : DataManager.getLifts().entrySet()) { for (Map.Entry<String, Lift> entry : DataManager.getLifts().entrySet()) {
Gson gson = new Gson();
byte[] blob = gson.toJson(entry.getValue()).getBytes(); byte[] blob = gson.toJson(entry.getValue()).getBytes();
Bukkit.getLogger().info("[V10Lift] Saving lift " + entry.getKey() + " to data..."); 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 { try {
String query = "INSERT INTO lifts (liftName, liftData) VALUES (?, ?)"; String query = "INSERT INTO lifts (liftName, liftData) VALUES (?, ?)";
PreparedStatement statement = con.prepareStatement(query); PreparedStatement statement = con.prepareStatement(query);
statement.setString(1, entry.getKey()); statement.setString(1, name);
statement.setBytes(2, blob); statement.setBytes(2, blob);
statement.executeUpdate(); statement.executeUpdate();
} catch (SQLException ignored) {} } catch (SQLException ignored) {
}
});
Bukkit.getScheduler().runTaskAsynchronously(V10LiftPlugin.getInstance(), () -> {
try { try {
String query2 = "UPDATE lifts SET liftData = ? WHERE liftName = ?"; String query2 = "UPDATE lifts SET liftData = ? WHERE liftName = ?";
PreparedStatement statement2 = con.prepareStatement(query2); PreparedStatement statement2 = con.prepareStatement(query2);
statement2.setBytes(1, blob); statement2.setBytes(1, blob);
statement2.setString(2, entry.getKey()); statement2.setString(2, name);
statement2.executeUpdate(); statement2.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} });
} }
public void closeConnection() { public void closeConnection() {