Ensure redundant data is properly cleared

This commit is contained in:
William committed 2022-06-09 13:58:50 +01:00
1 parent 0c8f2eb530
commit aa0dbeff4b
2 files changed
+222 -145

No files matched your search

@@ -190,25 +190,27 @@ public class MySqlDatabase extends Database {
@Override @Override
protected void updateSavedPosition(int savedPositionId, @NotNull SavedPosition position, @NotNull Connection connection) throws SQLException { protected void updateSavedPosition(int savedPositionId, @NotNull SavedPosition position, @NotNull Connection connection) throws SQLException {
try (PreparedStatement selectStatement = connection.prepareStatement(""" try (PreparedStatement selectStatement = connection.prepareStatement(formatStatementTables("""
SELECT `position_id` SELECT `position_id`
FROM `%saved_positions_table%` FROM `%saved_positions_table%`
WHERE `id`=?;""")) { WHERE `id`=?;"""))) {
selectStatement.setInt(1, savedPositionId); selectStatement.setInt(1, savedPositionId);
final ResultSet resultSet = selectStatement.executeQuery(); final ResultSet resultSet = selectStatement.executeQuery();
final int positionId = resultSet.getInt("position_id"); if (resultSet.next()) {
updatePosition(positionId, position, connection); final int positionId = resultSet.getInt("position_id");
updatePosition(positionId, position, connection);
try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables("""
UPDATE `%saved_positions_table%` UPDATE `%saved_positions_table%`
SET `name`=?, SET `name`=?,
`description`=? `description`=?
WHERE `id`=?;"""))) { WHERE `id`=?;"""))) {
updateStatement.setString(1, position.meta.name); updateStatement.setString(1, position.meta.name);
updateStatement.setString(2, position.meta.description); updateStatement.setString(2, position.meta.description);
updateStatement.setInt(3, savedPositionId); updateStatement.setInt(3, savedPositionId);
updateStatement.executeUpdate(); updateStatement.executeUpdate();
}
} }
} }
} }
@@ -628,21 +630,24 @@ public class MySqlDatabase extends Database {
@Override @Override
public CompletableFuture<Void> setCurrentTeleport(@NotNull User user, @Nullable Teleport teleport) { public CompletableFuture<Void> setCurrentTeleport(@NotNull User user, @Nullable Teleport teleport) {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
if (teleport == null) { // Clear the user's current teleport
// Clear the user's current teleport try (Connection connection = getConnection()) {
try (Connection connection = getConnection()) { try (PreparedStatement deleteStatement = connection.prepareStatement(formatStatementTables("""
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" DELETE FROM `%positions_table%`
DELETE FROM `%teleports_table%` WHERE `id`=(
WHERE `player_uuid`=?;"""))) { SELECT `destination_id`
statement.setString(1, user.uuid.toString()); FROM `%teleports_table%`
WHERE `%teleports_table%`.`player_uuid`=?
statement.executeUpdate(); );"""))) {
} deleteStatement.setString(1, user.uuid.toString());
} catch (SQLException e) { deleteStatement.executeUpdate();
getLogger().log(Level.SEVERE, "Failed to clear the current teleport of " + user.username, e);
} }
} else { } catch (SQLException e) {
// Set the user's teleport into the database getLogger().log(Level.SEVERE, "Failed to clear the current teleport of " + user.username, e);
}
// Set the user's teleport into the database (if it's not null)
if (teleport != null) {
try (Connection connection = getConnection()) { try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
INSERT INTO `%teleports_table%` (`player_uuid`, `destination_id`) INSERT INTO `%teleports_table%` (`player_uuid`, `destination_id`)
@@ -693,14 +698,25 @@ public class MySqlDatabase extends Database {
public CompletableFuture<Void> setLastPosition(@NotNull User user, @NotNull Position position) { public CompletableFuture<Void> setLastPosition(@NotNull User user, @NotNull Position position) {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
try (Connection connection = getConnection()) { try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement queryStatement = connection.prepareStatement(formatStatementTables("""
UPDATE `%players_table%` SELECT `last_position` FROM `%players_table%`
SET `last_position`=?
WHERE `uuid`=?;"""))) { WHERE `uuid`=?;"""))) {
statement.setInt(1, setPosition(position, connection)); queryStatement.setString(1, user.uuid.toString());
statement.setString(2, user.uuid.toString());
statement.executeUpdate(); final ResultSet resultSet = queryStatement.executeQuery();
if (resultSet.next()) {
// Update the last position
updatePosition(resultSet.getInt("last_position"), position, connection);
} else {
// Set the last position
try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables("""
UPDATE `%players_table%`
SET `last_position`=?
WHERE `uuid`=?;"""))) {
updateStatement.setInt(1, setPosition(position, connection));
updateStatement.setString(2, user.uuid.toString());
}
}
} }
} catch (SQLException e) { } catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to set the last position of " + user.username, e); getLogger().log(Level.SEVERE, "Failed to set the last position of " + user.username, e);
@@ -742,14 +758,25 @@ public class MySqlDatabase extends Database {
public CompletableFuture<Void> setOfflinePosition(@NotNull User user, @NotNull Position position) { public CompletableFuture<Void> setOfflinePosition(@NotNull User user, @NotNull Position position) {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
try (Connection connection = getConnection()) { try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement queryStatement = connection.prepareStatement(formatStatementTables("""
UPDATE `%players_table%` SELECT `offline_position` FROM `%players_table%`
SET `offline_position`=?
WHERE `uuid`=?;"""))) { WHERE `uuid`=?;"""))) {
statement.setInt(1, setPosition(position, connection)); queryStatement.setString(1, user.uuid.toString());
statement.setString(2, user.uuid.toString());
statement.executeUpdate(); final ResultSet resultSet = queryStatement.executeQuery();
if (resultSet.next()) {
// Update the offline position
updatePosition(resultSet.getInt("offline_position"), position, connection);
} else {
// Set the offline position
try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables("""
UPDATE `%players_table%`
SET `offline_position`=?
WHERE `uuid`=?;"""))) {
updateStatement.setInt(1, setPosition(position, connection));
updateStatement.setString(2, user.uuid.toString());
}
}
} }
} catch (SQLException e) { } catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to set the offline position of " + user.username, e); getLogger().log(Level.SEVERE, "Failed to set the offline position of " + user.username, e);
@@ -790,35 +817,45 @@ public class MySqlDatabase extends Database {
@Override @Override
public CompletableFuture<Void> setRespawnPosition(@NotNull User user, @Nullable Position position) { public CompletableFuture<Void> setRespawnPosition(@NotNull User user, @Nullable Position position) {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
if (position == null) { try (Connection connection = getConnection()) {
// Clear the respawn position try (PreparedStatement queryStatement = connection.prepareStatement(formatStatementTables("""
try (Connection connection = getConnection()) { SELECT `respawn_position` FROM `%players_table%`
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" WHERE `uuid`=?;"""))) {
UPDATE `%players_table%` queryStatement.setString(1, user.uuid.toString());
SET `respawn_position`=NULL
WHERE `uuid`=?;"""))) {
statement.setString(1, user.uuid.toString());
statement.executeUpdate(); final ResultSet resultSet = queryStatement.executeQuery();
if (resultSet.next()) {
if (position == null) {
// Delete a respawn position
try (PreparedStatement deleteStatement = connection.prepareStatement(formatStatementTables("""
DELETE FROM `%positions_table%`
WHERE `id`=(
SELECT `respawn_position`
FROM `%players_table%`
WHERE `%players_table%`.`uuid`=?
);"""))) {
deleteStatement.setString(1, user.uuid.toString());
deleteStatement.executeUpdate();
}
} else {
// Update the respawn position
updatePosition(resultSet.getInt("respawn_position"), position, connection);
}
} else {
if (position != null) {
// Set a respawn position
try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables("""
UPDATE `%players_table%`
SET `respawn_position`=?
WHERE `uuid`=?;"""))) {
updateStatement.setInt(1, setPosition(position, connection));
updateStatement.setString(2, user.uuid.toString());
}
}
} }
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to set the respawn position of " + user.username, e);
}
} else {
// Set the respawn position
try (Connection connection = getConnection()) {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
UPDATE `%players_table%`
SET `respawn_position`=?
WHERE `uuid`=?;"""))) {
statement.setInt(1, setPosition(position, connection));
statement.setString(2, user.uuid.toString());
statement.executeUpdate();
}
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to set the respawn position of " + user.username, e);
} }
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to set the respawn position of " + user.username, e);
} }
}); });
} }
@@ -61,19 +61,21 @@ public class SqLiteDatabase extends Database {
private void setConnection() { private void setConnection() {
try { try {
// Ensures the database file exists // Ensure that the database file exists
if (databaseFile.createNewFile()) { if (databaseFile.createNewFile()) {
getLogger().log(Level.INFO, "Created the SQLite database file"); getLogger().log(Level.INFO, "Created the SQLite database file");
} }
// Establish the connection using the JDBC SQLite driver // Specify use of the JDBC SQLite driver
Class.forName("org.sqlite.JDBC"); Class.forName("org.sqlite.JDBC");
// Set SQLite database properties
SQLiteConfig config = new SQLiteConfig(); SQLiteConfig config = new SQLiteConfig();
config.enforceForeignKeys(true); config.enforceForeignKeys(true);
config.setEncoding(SQLiteConfig.Encoding.UTF8); config.setEncoding(SQLiteConfig.Encoding.UTF8);
config.setSynchronous(SQLiteConfig.SynchronousMode.FULL); config.setSynchronous(SQLiteConfig.SynchronousMode.FULL);
// Establish the connection
connection = DriverManager.getConnection("jdbc:sqlite:" + databaseFile.getAbsolutePath(), config.toProperties()); connection = DriverManager.getConnection("jdbc:sqlite:" + databaseFile.getAbsolutePath(), config.toProperties());
} catch (IOException e) { } catch (IOException e) {
getLogger().log(Level.SEVERE, "An exception occurred creating the database file", e); getLogger().log(Level.SEVERE, "An exception occurred creating the database file", e);
@@ -87,6 +89,7 @@ public class SqLiteDatabase extends Database {
@Override @Override
public CompletableFuture<Void> initialize() { public CompletableFuture<Void> initialize() {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
// Setup the connection
setConnection(); setConnection();
// Prepare database schema; make tables if they don't exist // Prepare database schema; make tables if they don't exist
@@ -99,14 +102,14 @@ public class SqLiteDatabase extends Database {
} }
} }
} catch (SQLException | IOException e) { } catch (SQLException | IOException e) {
getLogger().log(Level.SEVERE, "An error occurred creating tables on the MySQL database: ", e); getLogger().log(Level.SEVERE, "An error occurred creating tables on the SQLite database: ", e);
} }
}); });
} }
@Override @Override
protected int setPosition(@NotNull Position position, @NotNull Connection connection) throws SQLException { protected int setPosition(@NotNull Position position, @NotNull Connection connection) throws SQLException {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
INSERT INTO `%positions_table%` (`x`,`y`,`z`,`yaw`,`pitch`,`world_name`,`world_uuid`,`server_name`) INSERT INTO `%positions_table%` (`x`,`y`,`z`,`yaw`,`pitch`,`world_name`,`world_uuid`,`server_name`)
VALUES (?,?,?,?,?,?,?,?);"""), VALUES (?,?,?,?,?,?,?,?);"""),
Statement.RETURN_GENERATED_KEYS)) { Statement.RETURN_GENERATED_KEYS)) {
@@ -170,25 +173,27 @@ public class SqLiteDatabase extends Database {
@Override @Override
protected void updateSavedPosition(int savedPositionId, @NotNull SavedPosition position, @NotNull Connection connection) throws SQLException { protected void updateSavedPosition(int savedPositionId, @NotNull SavedPosition position, @NotNull Connection connection) throws SQLException {
try (PreparedStatement selectStatement = connection.prepareStatement(""" try (PreparedStatement selectStatement = connection.prepareStatement(formatStatementTables("""
SELECT `position_id` SELECT `position_id`
FROM `%saved_positions_table%` FROM `%saved_positions_table%`
WHERE `id`=?;""")) { WHERE `id`=?;"""))) {
selectStatement.setInt(1, savedPositionId); selectStatement.setInt(1, savedPositionId);
final ResultSet resultSet = selectStatement.executeQuery(); final ResultSet resultSet = selectStatement.executeQuery();
final int positionId = resultSet.getInt("position_id"); if (resultSet.next()) {
updatePosition(positionId, position, connection); final int positionId = resultSet.getInt("position_id");
updatePosition(positionId, position, connection);
try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables("""
UPDATE `%saved_positions_table%` UPDATE `%saved_positions_table%`
SET `name`=?, SET `name`=?,
`description`=? `description`=?
WHERE `id`=?;"""))) { WHERE `id`=?;"""))) {
updateStatement.setString(1, position.meta.name); updateStatement.setString(1, position.meta.name);
updateStatement.setString(2, position.meta.description); updateStatement.setString(2, position.meta.description);
updateStatement.setInt(3, savedPositionId); updateStatement.setInt(3, savedPositionId);
updateStatement.executeUpdate(); updateStatement.executeUpdate();
}
} }
} }
} }
@@ -200,7 +205,7 @@ public class SqLiteDatabase extends Database {
if (!existingUser.username.equals(player.getName())) { if (!existingUser.username.equals(player.getName())) {
// Update a player's name if it has changed in the database // Update a player's name if it has changed in the database
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
UPDATE `%players_table%` UPDATE `%players_table%`
SET `username`=? SET `username`=?
WHERE `uuid`=?"""))) { WHERE `uuid`=?"""))) {
@@ -218,7 +223,7 @@ public class SqLiteDatabase extends Database {
() -> { () -> {
// Insert new player data into the database // Insert new player data into the database
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
INSERT INTO `%players_table%` (`uuid`,`username`) INSERT INTO `%players_table%` (`uuid`,`username`)
VALUES (?,?);"""))) { VALUES (?,?);"""))) {
@@ -236,7 +241,7 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Optional<UserData>> getUserByName(@NotNull String name) { public CompletableFuture<Optional<UserData>> getUserByName(@NotNull String name) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `uuid`, `username`, `home_slots`, `ignoring_requests`, `rtp_cooldown` SELECT `uuid`, `username`, `home_slots`, `ignoring_requests`, `rtp_cooldown`
FROM `%players_table%` FROM `%players_table%`
WHERE `username`=?"""))) { WHERE `username`=?"""))) {
@@ -263,7 +268,7 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Optional<UserData>> getUser(@NotNull UUID uuid) { public CompletableFuture<Optional<UserData>> getUser(@NotNull UUID uuid) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `uuid`, `username`, `home_slots`, `ignoring_requests`, `rtp_cooldown` SELECT `uuid`, `username`, `home_slots`, `ignoring_requests`, `rtp_cooldown`
FROM `%players_table%` FROM `%players_table%`
WHERE `uuid`=?"""))) { WHERE `uuid`=?"""))) {
@@ -292,7 +297,7 @@ public class SqLiteDatabase extends Database {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
final List<Home> userHomes = new ArrayList<>(); final List<Home> userHomes = new ArrayList<>();
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public` SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public`
FROM `%homes_table%` FROM `%homes_table%`
INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id`
@@ -333,7 +338,7 @@ public class SqLiteDatabase extends Database {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
final List<Warp> warps = new ArrayList<>(); final List<Warp> warps = new ArrayList<>();
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`
FROM `%warps_table%` FROM `%warps_table%`
INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id` INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id`
@@ -368,7 +373,7 @@ public class SqLiteDatabase extends Database {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
final List<Home> userHomes = new ArrayList<>(); final List<Home> userHomes = new ArrayList<>();
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public` SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public`
FROM `%homes_table%` FROM `%homes_table%`
INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id`
@@ -407,7 +412,7 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Optional<Home>> getHome(@NotNull User user, @NotNull String homeName) { public CompletableFuture<Optional<Home>> getHome(@NotNull User user, @NotNull String homeName) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public` SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public`
FROM `%homes_table%` FROM `%homes_table%`
INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id`
@@ -448,7 +453,7 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Optional<Home>> getHome(@NotNull UUID uuid) { public CompletableFuture<Optional<Home>> getHome(@NotNull UUID uuid) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public` SELECT `%homes_table%`.`uuid` AS `home_uuid`, `owner_uuid`, `username` AS `owner_username`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`, `public`
FROM `%homes_table%` FROM `%homes_table%`
INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id`
@@ -487,7 +492,7 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Optional<Warp>> getWarp(@NotNull String warpName) { public CompletableFuture<Optional<Warp>> getWarp(@NotNull String warpName) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`
FROM `%warps_table%` FROM `%warps_table%`
INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id` INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id`
@@ -522,7 +527,7 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Optional<Warp>> getWarp(@NotNull UUID uuid) { public CompletableFuture<Optional<Warp>> getWarp(@NotNull UUID uuid) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`
FROM `%warps_table%` FROM `%warps_table%`
INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id` INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id`
@@ -557,7 +562,7 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Optional<Teleport>> getCurrentTeleport(@NotNull User user) { public CompletableFuture<Optional<Teleport>> getCurrentTeleport(@NotNull User user) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`
FROM `%teleports_table%` FROM `%teleports_table%`
INNER JOIN `%positions_table%` ON `%teleports_table%`.`destination_id` = `%positions_table%`.`id` INNER JOIN `%positions_table%` ON `%teleports_table%`.`destination_id` = `%positions_table%`.`id`
@@ -588,7 +593,7 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Void> updateUserData(@NotNull UserData userData) { public CompletableFuture<Void> updateUserData(@NotNull UserData userData) {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
UPDATE `%players_table%` UPDATE `%players_table%`
SET `home_slots`=?, `ignoring_requests`=?, `rtp_cooldown`=? SET `home_slots`=?, `ignoring_requests`=?, `rtp_cooldown`=?
WHERE `uuid`=?"""))) { WHERE `uuid`=?"""))) {
@@ -608,23 +613,26 @@ public class SqLiteDatabase extends Database {
@Override @Override
public CompletableFuture<Void> setCurrentTeleport(@NotNull User user, @Nullable Teleport teleport) { public CompletableFuture<Void> setCurrentTeleport(@NotNull User user, @Nullable Teleport teleport) {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
if (teleport == null) { // Clear the user's current teleport
// Clear the user's current teleport try {
try { try (PreparedStatement deleteStatement = getConnection().prepareStatement(formatStatementTables("""
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" DELETE FROM `%positions_table%`
DELETE FROM `%teleports_table%` WHERE `id`=(
WHERE `player_uuid`=?;"""))) { SELECT `destination_id`
statement.setString(1, user.uuid.toString()); FROM `%teleports_table%`
WHERE `%teleports_table%`.`player_uuid`=?
statement.executeUpdate(); );"""))) {
} deleteStatement.setString(1, user.uuid.toString());
} catch (SQLException e) { deleteStatement.executeUpdate();
getLogger().log(Level.SEVERE, "Failed to clear the current teleport of " + user.username, e);
} }
} else { } catch (SQLException e) {
// Set the user's teleport into the database getLogger().log(Level.SEVERE, "Failed to clear the current teleport of " + user.username, e);
}
// Set the user's teleport into the database (if it's not null)
if (teleport != null) {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
INSERT INTO `%teleports_table%` (`player_uuid`, `destination_id`) INSERT INTO `%teleports_table%` (`player_uuid`, `destination_id`)
VALUES (?,?);"""))) { VALUES (?,?);"""))) {
statement.setString(1, user.uuid.toString()); statement.setString(1, user.uuid.toString());
@@ -643,7 +651,7 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Optional<Position>> getLastPosition(@NotNull User user) { public CompletableFuture<Optional<Position>> getLastPosition(@NotNull User user) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`
FROM `%players_table%` FROM `%players_table%`
INNER JOIN `%positions_table%` ON `%players_table%`.`last_position` = `%positions_table%`.`id` INNER JOIN `%positions_table%` ON `%players_table%`.`last_position` = `%positions_table%`.`id`
@@ -673,14 +681,25 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Void> setLastPosition(@NotNull User user, @NotNull Position position) { public CompletableFuture<Void> setLastPosition(@NotNull User user, @NotNull Position position) {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement queryStatement = getConnection().prepareStatement(formatStatementTables("""
UPDATE `%players_table%` SELECT `last_position` FROM `%players_table%`
SET `last_position`=?
WHERE `uuid`=?;"""))) { WHERE `uuid`=?;"""))) {
statement.setInt(1, setPosition(position, connection)); queryStatement.setString(1, user.uuid.toString());
statement.setString(2, user.uuid.toString());
statement.executeUpdate(); final ResultSet resultSet = queryStatement.executeQuery();
if (resultSet.next()) {
// Update the last position
updatePosition(resultSet.getInt("last_position"), position, connection);
} else {
// Set the last position
try (PreparedStatement updateStatement = getConnection().prepareStatement(formatStatementTables("""
UPDATE `%players_table%`
SET `last_position`=?
WHERE `uuid`=?;"""))) {
updateStatement.setInt(1, setPosition(position, connection));
updateStatement.setString(2, user.uuid.toString());
}
}
} }
} catch (SQLException e) { } catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to set the last position of " + user.username, e); getLogger().log(Level.SEVERE, "Failed to set the last position of " + user.username, e);
@@ -692,7 +711,7 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Optional<Position>> getOfflinePosition(@NotNull User user) { public CompletableFuture<Optional<Position>> getOfflinePosition(@NotNull User user) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`
FROM `%players_table%` FROM `%players_table%`
INNER JOIN `%positions_table%` ON `%players_table%`.`offline_position` = `%positions_table%`.`id` INNER JOIN `%positions_table%` ON `%players_table%`.`offline_position` = `%positions_table%`.`id`
@@ -722,14 +741,25 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Void> setOfflinePosition(@NotNull User user, @NotNull Position position) { public CompletableFuture<Void> setOfflinePosition(@NotNull User user, @NotNull Position position) {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement queryStatement = getConnection().prepareStatement(formatStatementTables("""
UPDATE `%players_table%` SELECT `offline_position` FROM `%players_table%`
SET `offline_position`=?
WHERE `uuid`=?;"""))) { WHERE `uuid`=?;"""))) {
statement.setInt(1, setPosition(position, connection)); queryStatement.setString(1, user.uuid.toString());
statement.setString(2, user.uuid.toString());
statement.executeUpdate(); final ResultSet resultSet = queryStatement.executeQuery();
if (resultSet.next()) {
// Update the offline position
updatePosition(resultSet.getInt("offline_position"), position, connection);
} else {
// Set the offline position
try (PreparedStatement updateStatement = getConnection().prepareStatement(formatStatementTables("""
UPDATE `%players_table%`
SET `offline_position`=?
WHERE `uuid`=?;"""))) {
updateStatement.setInt(1, setPosition(position, connection));
updateStatement.setString(2, user.uuid.toString());
}
}
} }
} catch (SQLException e) { } catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to set the offline position of " + user.username, e); getLogger().log(Level.SEVERE, "Failed to set the offline position of " + user.username, e);
@@ -741,7 +771,7 @@ public class SqLiteDatabase extends Database {
public CompletableFuture<Optional<Position>> getRespawnPosition(@NotNull User user) { public CompletableFuture<Optional<Position>> getRespawnPosition(@NotNull User user) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
try { try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name`
FROM `%players_table%` FROM `%players_table%`
INNER JOIN `%positions_table%` ON `%players_table%`.`respawn_position` = `%positions_table%`.`id` INNER JOIN `%positions_table%` ON `%players_table%`.`respawn_position` = `%positions_table%`.`id`
@@ -770,35 +800,45 @@ public class SqLiteDatabase extends Database {
@Override @Override
public CompletableFuture<Void> setRespawnPosition(@NotNull User user, @Nullable Position position) { public CompletableFuture<Void> setRespawnPosition(@NotNull User user, @Nullable Position position) {
return CompletableFuture.runAsync(() -> { return CompletableFuture.runAsync(() -> {
if (position == null) { try {
// Clear the respawn position try (PreparedStatement queryStatement = getConnection().prepareStatement(formatStatementTables("""
try { SELECT `respawn_position` FROM `%players_table%`
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" WHERE `uuid`=?;"""))) {
UPDATE `%players_table%` queryStatement.setString(1, user.uuid.toString());
SET `respawn_position`=NULL
WHERE `uuid`=?;"""))) {
statement.setString(1, user.uuid.toString());
statement.executeUpdate(); final ResultSet resultSet = queryStatement.executeQuery();
if (resultSet.next()) {
if (position == null) {
// Delete a respawn position
try (PreparedStatement deleteStatement = getConnection().prepareStatement(formatStatementTables("""
DELETE FROM `%positions_table%`
WHERE `id`=(
SELECT `respawn_position`
FROM `%players_table%`
WHERE `%players_table%`.`uuid`=?
);"""))) {
deleteStatement.setString(1, user.uuid.toString());
deleteStatement.executeUpdate();
}
} else {
// Update the respawn position
updatePosition(resultSet.getInt("respawn_position"), position, connection);
}
} else {
if (position != null) {
// Set a respawn position
try (PreparedStatement updateStatement = getConnection().prepareStatement(formatStatementTables("""
UPDATE `%players_table%`
SET `respawn_position`=?
WHERE `uuid`=?;"""))) {
updateStatement.setInt(1, setPosition(position, connection));
updateStatement.setString(2, user.uuid.toString());
}
}
} }
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to set the respawn position of " + user.username, e);
}
} else {
// Set the respawn position
try {
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
UPDATE `%players_table%`
SET `respawn_position`=?
WHERE `uuid`=?;"""))) {
statement.setInt(1, setPosition(position, connection));
statement.setString(2, user.uuid.toString());
statement.executeUpdate();
}
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to set the respawn position of " + user.username, e);
} }
} catch (SQLException e) {
getLogger().log(Level.SEVERE, "Failed to set the respawn position of " + user.username, e);
} }
}); });
} }
@@ -815,7 +855,7 @@ public class SqLiteDatabase extends Database {
final ResultSet resultSet = statement.executeQuery(); final ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) { if (resultSet.next()) {
updateSavedPosition(resultSet.getInt("saved_position_id"), home, getConnection()); updateSavedPosition(resultSet.getInt("saved_position_id"), home, connection);
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
@@ -828,7 +868,7 @@ public class SqLiteDatabase extends Database {
INSERT INTO `%homes_table%` (`uuid`, `saved_position_id`, `owner_uuid`, `public`) INSERT INTO `%homes_table%` (`uuid`, `saved_position_id`, `owner_uuid`, `public`)
VALUES (?,?,?,?);"""))) { VALUES (?,?,?,?);"""))) {
statement.setString(1, home.uuid.toString()); statement.setString(1, home.uuid.toString());
statement.setInt(2, setSavedPosition(home, getConnection())); statement.setInt(2, setSavedPosition(home, connection));
statement.setString(3, home.owner.uuid.toString()); statement.setString(3, home.owner.uuid.toString());
statement.setBoolean(4, home.isPublic); statement.setBoolean(4, home.isPublic);