Made saving async and added one-lift saving to edit command.
This commit is contained in:
parent
bcd700f451
commit
0ca25dbad0
3 changed files with 51 additions and 37 deletions
|
@ -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();
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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,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) {
|
public void removeFromData(String name) {
|
||||||
try {
|
|
||||||
if (!DataManager.containsLift(name)) {
|
if (!DataManager.containsLift(name)) {
|
||||||
Bukkit.getLogger().info("[V10Lift] Removing lift " + name + " to data...");
|
Bukkit.getLogger().info("[V10Lift] Removing lift " + name + " 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, name);
|
statement.setString(1, name);
|
||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
}
|
} catch (SQLException e) {
|
||||||
} catch(SQLException e) {
|
|
||||||
e.printStackTrace();
|
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 {
|
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, entry.getKey());
|
||||||
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);
|
||||||
|
@ -114,9 +100,39 @@ public class DBManager {
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
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() {
|
public void closeConnection() {
|
||||||
data.closeSource();
|
data.closeSource();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue