Made saving async and added one-lift saving to edit command. #57

Merged
stijnb1234 merged 1 commit from dev into master 2021-07-15 11:26:24 +00:00
3 changed files with 51 additions and 37 deletions
Showing only changes of commit 0ca25dbad0 - Show all commits

View file

@ -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();

View file

@ -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;

View file

@ -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,57 +56,41 @@ 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...");
String query = "DELETE FROM lifts WHERE liftName = ?";
PreparedStatement statement = con.prepareStatement(query);
statement.setString(1, liftSet.getString("liftName"));
statement.executeUpdate();
}
}
} catch(SQLException e) {
e.printStackTrace();
}
}
public void removeFromData(String name) {
try {
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, name);
statement.executeUpdate();
}
} catch(SQLException e) {
} catch (SQLException e) {
e.printStackTrace();
}
});
}
}
public void save() {
for (Map.Entry<String, Lift> 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) {}
} catch (SQLException ignored) {
}
});
Bukkit.getScheduler().runTaskAsynchronously(V10LiftPlugin.getInstance(), () -> {
try {
String query2 = "UPDATE lifts SET liftData = ? WHERE liftName = ?";
PreparedStatement statement2 = con.prepareStatement(query2);
@ -114,9 +100,39 @@ public class DBManager {
} 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, name);
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, name);
statement2.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
});
}
public void closeConnection() {
data.closeSource();
}