wip: further code cleanup

This commit is contained in:
2024-07-23 22:25:00 +02:00
parent e5e1f39989
commit 93dc9d8a80
25 changed files with 556 additions and 407 deletions
@@ -4,8 +4,6 @@ import eu.mhsl.minecraft.pixelblocks.utils.Direction;
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import java.sql.*;
import java.util.ArrayList;
@@ -13,194 +11,182 @@ import java.util.List;
import java.util.UUID;
public class PixelBlockDatabase {
public Connection db;
private final Connection db;
private final PreparedStatement getAllPixelBlocks;
private final PreparedStatement deletePixelBlock;
private final PreparedStatement insertNewPixelBlock;
private final PreparedStatement updateExistingPixelBlock;
public PixelBlockDatabase(String url) {
try {
Class.forName("org.sqlite.JDBC");
this.db = DriverManager.getConnection(url);
} catch (SQLException e) {
this.db.createStatement().execute(
"CREATE TABLE IF NOT EXISTS pixelblocks (" +
"uuid CHAR(36) PRIMARY KEY, " +
"owner CHAR(36), " +
"locationWorldName CHAR(36), " +
"locationX DOUBLE, " +
"locationY DOUBLE, " +
"locationZ DOUBLE, " +
"entryLocationWorldName CHAR(36), " +
"entryLocationX DOUBLE, " +
"entryLocationY DOUBLE, " +
"entryLocationZ DOUBLE, " +
"direction CHAR(36)" +
")"
);
this.deletePixelBlock = this.db.prepareStatement("DELETE FROM pixelblocks WHERE uuid = ?");
this.getAllPixelBlocks = this.db.prepareStatement("SELECT * FROM pixelblocks");
this.insertNewPixelBlock = this.db.prepareStatement(
"INSERT INTO pixelblocks(uuid, owner, " +
"locationWorldName, locationX, locationY, locationZ, " +
"entryLocationWorldName, entryLocationX, entryLocationY, entryLocationZ, direction) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
);
this.updateExistingPixelBlock = this.db.prepareStatement(
"UPDATE pixelblocks " +
"SET owner=?, locationWorldName=?, locationX=?, locationY=?, locationZ=?," +
"entryLocationWorldName=?, entryLocationX=?, entryLocationY=?, entryLocationZ=?, direction=? " +
"WHERE uuid=?;"
);
} catch (SQLException | RuntimeException | ClassNotFoundException e) {
throw new RuntimeException("Failed to load Database", e);
}
}
public Statement getStatement() throws SQLException {
return this.db.createStatement();
public void close() throws SQLException {
deletePixelBlock.close();
getAllPixelBlocks.close();
insertNewPixelBlock.close();
updateExistingPixelBlock.close();
db.close();
}
public void removePixelBlock(PixelBlock pixelBlock) {
Bukkit.getScheduler().runTask(PixelBlocksPlugin.plugin, () -> {
public void deletePixelBlock(PixelBlock pixelBlock) {
Bukkit.getScheduler().runTaskAsynchronously(PixelBlocksPlugin.plugin, () -> {
try {
PreparedStatement prep = this.db.prepareStatement("DELETE FROM pixelblocks WHERE uuid = ?");
prep.setString(1, pixelBlock.blockUUID.toString());
prep.executeUpdate();
prep.close();
this.deletePixelBlock.setString(1, pixelBlock.blockUUID.toString());
this.deletePixelBlock.executeUpdate();
PixelBlocksPlugin.plugin.getLogger().info("DB: Deleted PixelBlock: " + pixelBlock.blockUUID);
} catch (SQLException e) {
System.err.println(e.getMessage());
throw new RuntimeException("Failed to delete PixelBlock", e);
}
});
}
public void savePixelBlock(PixelBlock pixelBlock) {
Bukkit.getScheduler().runTask(PixelBlocksPlugin.plugin, () -> {
List<UUID> uuids = new ArrayList<>();
List<UUID> storedPixelBlocks = new ArrayList<>();
try (var statement = getStatement()) {
ResultSet pixelBlocksResult = statement.executeQuery("SELECT * FROM pixelblocks;");
try {
ResultSet pixelBlocksResult = this.getAllPixelBlocks.executeQuery();
while (pixelBlocksResult.next()) {
uuids.add(UUID.fromString(pixelBlocksResult.getString("uuid")));
storedPixelBlocks.add(UUID.fromString(pixelBlocksResult.getString("uuid")));
}
} catch (SQLException e) {
System.err.println(e.getMessage());
throw new RuntimeException("Failed to fetch PixelBlock list", e);
}
if(!uuids.contains(pixelBlock.blockUUID)) {
if(!storedPixelBlocks.contains(pixelBlock.blockUUID)) {
// create new entry if it does not exist
try {
PreparedStatement prep = this.db.prepareStatement(
"INSERT INTO pixelblocks(uuid, owner, " +
"locationWorldName, locationX, locationY, locationZ, " +
"entryLocationWorldName, entryLocationX, entryLocationY, entryLocationZ, direction) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
);
prep.setString(1, pixelBlock.blockUUID.toString());
prep.setString(2, pixelBlock.ownerUUID.toString());
this.insertNewPixelBlock.setString(1, pixelBlock.blockUUID.toString());
this.insertNewPixelBlock.setString(2, pixelBlock.ownerUUID.toString());
prep.setString(3, pixelBlock.pixelBlockLocation.getWorld().getName());
prep.setDouble(4, pixelBlock.pixelBlockLocation.getX());
prep.setDouble(5, pixelBlock.pixelBlockLocation.getY());
prep.setDouble(6, pixelBlock.pixelBlockLocation.getZ());
this.insertNewPixelBlock.setString(3, pixelBlock.pixelBlockLocation.getWorld().getName());
this.insertNewPixelBlock.setDouble(4, pixelBlock.pixelBlockLocation.getX());
this.insertNewPixelBlock.setDouble(5, pixelBlock.pixelBlockLocation.getY());
this.insertNewPixelBlock.setDouble(6, pixelBlock.pixelBlockLocation.getZ());
if(pixelBlock.lastEntryLocation != null) {
prep.setString(7, pixelBlock.lastEntryLocation.getWorld().getName());
prep.setDouble(8, pixelBlock.lastEntryLocation.getX());
prep.setDouble(9, pixelBlock.lastEntryLocation.getY());
prep.setDouble(10, pixelBlock.lastEntryLocation.getZ());
this.insertNewPixelBlock.setString(7, pixelBlock.lastEntryLocation.getWorld().getName());
this.insertNewPixelBlock.setDouble(8, pixelBlock.lastEntryLocation.getX());
this.insertNewPixelBlock.setDouble(9, pixelBlock.lastEntryLocation.getY());
this.insertNewPixelBlock.setDouble(10, pixelBlock.lastEntryLocation.getZ());
} else {
prep.setString(7, Bukkit.getWorlds().getFirst().getName());
prep.setDouble(8, Bukkit.getWorlds().getFirst().getSpawnLocation().getX());
prep.setDouble(9, Bukkit.getWorlds().getFirst().getSpawnLocation().getY());
prep.setDouble(10, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ());
this.insertNewPixelBlock.setString(7, Bukkit.getWorlds().getFirst().getName());
this.insertNewPixelBlock.setDouble(8, Bukkit.getWorlds().getFirst().getSpawnLocation().getX());
this.insertNewPixelBlock.setDouble(9, Bukkit.getWorlds().getFirst().getSpawnLocation().getY());
this.insertNewPixelBlock.setDouble(10, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ());
}
prep.setString(11, pixelBlock.facingDirection.toString());
this.insertNewPixelBlock.setString(11, pixelBlock.facingDirection.toString());
prep.executeUpdate();
prep.close();
this.insertNewPixelBlock.executeUpdate();
PixelBlocksPlugin.plugin.getLogger().info("DB: Created PixelBlock: " + pixelBlock.blockUUID);
} catch (SQLException e) {
System.err.println(e.getMessage());
throw new RuntimeException("Failed to save PixelBlock", e);
}
} else {
// update existing entry
try {
PreparedStatement prep = this.db.prepareStatement(
"UPDATE pixelblocks " +
"SET owner=?, locationWorldName=?, locationX=?, locationY=?, locationZ=?," +
"entryLocationWorldName=?, entryLocationX=?, entryLocationY=?, entryLocationZ=?, direction=? " +
"WHERE uuid=?;"
);
prep.setString(1, pixelBlock.ownerUUID.toString());
this.updateExistingPixelBlock.setString(1, pixelBlock.ownerUUID.toString());
prep.setString(2, pixelBlock.pixelBlockLocation.getWorld().getName());
prep.setDouble(3, pixelBlock.pixelBlockLocation.getX());
prep.setDouble(4, pixelBlock.pixelBlockLocation.getY());
prep.setDouble(5, pixelBlock.pixelBlockLocation.getZ());
this.updateExistingPixelBlock.setString(2, pixelBlock.pixelBlockLocation.getWorld().getName());
this.updateExistingPixelBlock.setDouble(3, pixelBlock.pixelBlockLocation.getX());
this.updateExistingPixelBlock.setDouble(4, pixelBlock.pixelBlockLocation.getY());
this.updateExistingPixelBlock.setDouble(5, pixelBlock.pixelBlockLocation.getZ());
if(pixelBlock.lastEntryLocation != null) {
prep.setString(6, pixelBlock.lastEntryLocation.getWorld().getName());
prep.setDouble(7, pixelBlock.lastEntryLocation.getX());
prep.setDouble(8, pixelBlock.lastEntryLocation.getY());
prep.setDouble(9, pixelBlock.lastEntryLocation.getZ());
this.updateExistingPixelBlock.setString(6, pixelBlock.lastEntryLocation.getWorld().getName());
this.updateExistingPixelBlock.setDouble(7, pixelBlock.lastEntryLocation.getX());
this.updateExistingPixelBlock.setDouble(8, pixelBlock.lastEntryLocation.getY());
this.updateExistingPixelBlock.setDouble(9, pixelBlock.lastEntryLocation.getZ());
} else {
prep.setString(6, Bukkit.getWorlds().getFirst().getName());
prep.setDouble(7, Bukkit.getWorlds().getFirst().getSpawnLocation().getX());
prep.setDouble(8, Bukkit.getWorlds().getFirst().getSpawnLocation().getY());
prep.setDouble(9, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ());
this.updateExistingPixelBlock.setString(6, Bukkit.getWorlds().getFirst().getName());
this.updateExistingPixelBlock.setDouble(7, Bukkit.getWorlds().getFirst().getSpawnLocation().getX());
this.updateExistingPixelBlock.setDouble(8, Bukkit.getWorlds().getFirst().getSpawnLocation().getY());
this.updateExistingPixelBlock.setDouble(9, Bukkit.getWorlds().getFirst().getSpawnLocation().getZ());
}
prep.setString(10, pixelBlock.blockUUID.toString());
prep.setString(11, pixelBlock.facingDirection.toString());
this.updateExistingPixelBlock.setString(10, pixelBlock.blockUUID.toString());
this.updateExistingPixelBlock.setString(11, pixelBlock.facingDirection.toString());
prep.executeUpdate();
prep.close();
this.updateExistingPixelBlock.executeUpdate();
PixelBlocksPlugin.plugin.getLogger().info("DB: Updated PixelBlock: " + pixelBlock.blockUUID);
} catch (SQLException e) {
System.err.println(e.getMessage());
throw new RuntimeException("Failed updating PixelBlock", e);
}
}
});
}
public void loadPixelBlocks() {
List<Entity> entities = new ArrayList<>();
entities.addAll(Bukkit.getWorlds().get(0).getEntities().stream()
.filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION) || entity.getType().equals(EntityType.ITEM_DISPLAY))
.toList());
entities.addAll(Bukkit.getWorlds().get(1).getEntities().stream()
.filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION) || entity.getType().equals(EntityType.ITEM_DISPLAY))
.toList());
entities.addAll(Bukkit.getWorlds().get(2).getEntities().stream()
.filter(entity -> entity.getType().equals(EntityType.BLOCK_DISPLAY) || entity.getType().equals(EntityType.INTERACTION) || entity.getType().equals(EntityType.ITEM_DISPLAY))
.toList());
try {
ResultSet allPixelBlocks = this.getAllPixelBlocks.executeQuery();
String sql = "CREATE TABLE IF NOT EXISTS pixelblocks (" +
"uuid CHAR(36) PRIMARY KEY, " +
"owner CHAR(36), " +
"locationWorldName CHAR(36), " +
"locationX DOUBLE, " +
"locationY DOUBLE, " +
"locationZ DOUBLE, " +
"entryLocationWorldName CHAR(36), " +
"entryLocationX DOUBLE, " +
"entryLocationY DOUBLE, " +
"entryLocationZ DOUBLE, " +
"direction CHAR(36)" +
");";
try (var statement = getStatement()) {
statement.execute(sql);
ResultSet pixelBlocksResult = statement.executeQuery("SELECT * FROM pixelblocks;");
while (pixelBlocksResult.next()) {
Location newPixelBlockLocation = new Location(
Bukkit.getWorld(pixelBlocksResult.getString("locationWorldName")),
pixelBlocksResult.getDouble("locationX"),
pixelBlocksResult.getDouble("locationY"),
pixelBlocksResult.getDouble("locationZ")
while (allPixelBlocks.next()) {
Location blockLocation = new Location(
Bukkit.getWorld(allPixelBlocks.getString("locationWorldName")),
allPixelBlocks.getDouble("locationX"),
allPixelBlocks.getDouble("locationY"),
allPixelBlocks.getDouble("locationZ")
);
Location newEntryLocation = new Location(
Bukkit.getWorld(pixelBlocksResult.getString("entryLocationWorldName")),
pixelBlocksResult.getDouble("entryLocationX"),
pixelBlocksResult.getDouble("entryLocationY"),
pixelBlocksResult.getDouble("entryLocationZ")
Location entryLocation = new Location(
Bukkit.getWorld(allPixelBlocks.getString("entryLocationWorldName")),
allPixelBlocks.getDouble("entryLocationX"),
allPixelBlocks.getDouble("entryLocationY"),
allPixelBlocks.getDouble("entryLocationZ")
);
if(!newPixelBlockLocation.getChunk().isEntitiesLoaded()) {
newPixelBlockLocation.getChunk().load(true);
newPixelBlockLocation.getChunk().getEntities();
}
entities.stream().filter(entity -> entity
.getLocation()
.add(0, PixelBlocksPlugin.configuration.hitboxOffset(), 0)
.toBlockLocation()
.equals(newPixelBlockLocation))
.forEach(Entity::remove);
PixelBlock newPixelBlock = new PixelBlock(
newPixelBlockLocation,
UUID.fromString(pixelBlocksResult.getString("owner")),
UUID.fromString(pixelBlocksResult.getString("uuid"))
PixelBlock block = new PixelBlock(
blockLocation,
UUID.fromString(allPixelBlocks.getString("owner")),
UUID.fromString(allPixelBlocks.getString("uuid")),
Direction.valueOf(allPixelBlocks.getString("direction"))
);
newPixelBlock.lastEntryLocation = newEntryLocation;
newPixelBlock.place(newPixelBlockLocation, Direction.valueOf(pixelBlocksResult.getString("direction")));
block.setLastEntryLocation(entryLocation);
PixelBlocksPlugin.plugin.getLogger().info("DB: Loaded PixelBlock: " + block.blockUUID);
}
} catch (SQLException e) {
System.err.println(e.getMessage());
throw new RuntimeException("Failed loading PixelBlocks", e);
}
}
}