diff --git a/common/src/main/java/net/william278/huskhomes/data/Database.java b/common/src/main/java/net/william278/huskhomes/data/Database.java index ffd2fafe..bdd9db69 100644 --- a/common/src/main/java/net/william278/huskhomes/data/Database.java +++ b/common/src/main/java/net/william278/huskhomes/data/Database.java @@ -181,19 +181,43 @@ public abstract class Database { /** * (Internal use only) - Sets a position to the position table in the database * - * @param position The {@link Position} to set + * @param position The {@link Position} to set + * @param connection SQL connection * @return The newly inserted row ID + * @throws SQLException if an SQL exception occurs doing this */ protected abstract int setPosition(@NotNull Position position, @NotNull Connection connection) throws SQLException; + /** + * (Internal use only) - Updates position data + * + * @param positionId ID of the position to update + * @param position the new position + * @param connection SQL connection + * @throws SQLException if an SQL exception occurs doing this + */ + protected abstract void updatePosition(int positionId, @NotNull Position position, @NotNull Connection connection) throws SQLException; + /** * (Internal use only) - Sets position meta to the position metadata table in the database * - * @param meta The {@link PositionMeta} to set + * @param meta The {@link PositionMeta} to set + * @param connection SQL connection * @return The newly inserted row ID + * @throws SQLException if an SQL exception occurs doing this */ protected abstract int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException; + /** + * (Internal use only) - Updates position metadata + * + * @param metaId ID of the metadata to update + * @param meta the new metadata + * @param connection SQL connection + * @throws SQLException if an SQL exception occurs doing this + */ + protected abstract void updatePositionMeta(int metaId, @NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException; + /** * Ensure a {@link Player} has a {@link UserData} entry in the database and that their username is up-to-date * diff --git a/common/src/main/java/net/william278/huskhomes/data/MySqlDatabase.java b/common/src/main/java/net/william278/huskhomes/data/MySqlDatabase.java index 9418be41..600f01ba 100644 --- a/common/src/main/java/net/william278/huskhomes/data/MySqlDatabase.java +++ b/common/src/main/java/net/william278/huskhomes/data/MySqlDatabase.java @@ -145,6 +145,32 @@ public class MySqlDatabase extends Database { } } + @Override + protected void updatePosition(int positionId, @NotNull Position position, @NotNull Connection connection) throws SQLException { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + UPDATE `%positions_table%` + SET `x`=?, + `y`=?, + `z`=?, + `yaw`=?, + `pitch`=?, + `world_uuid`=?, + `world_name`=?, + `server_name`=? + WHERE `id`=?"""))) { + statement.setDouble(1, position.x); + statement.setDouble(2, position.y); + statement.setDouble(3, position.z); + statement.setFloat(4, position.yaw); + statement.setFloat(5, position.pitch); + statement.setString(6, position.world.uuid.toString()); + statement.setString(7, position.world.name); + statement.setString(8, position.server.name); + statement.setDouble(9, positionId); + statement.executeUpdate(); + } + } + @Override protected int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" @@ -161,6 +187,20 @@ public class MySqlDatabase extends Database { } } + @Override + protected void updatePositionMeta(int metaId, @NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + UPDATE `%position_metadata_table%` + SET `name`=?, + `description`=? + WHERE `id`=?;"""))) { + statement.setString(1, meta.name); + statement.setString(2, meta.description); + statement.setInt(3, metaId); + statement.executeUpdate(); + } + } + @Override public CompletableFuture ensureUser(@NotNull Player player) { return CompletableFuture.runAsync(() -> getUser(player.getUuid()).thenAccept(optionalUser -> @@ -776,16 +816,16 @@ public class MySqlDatabase extends Database { return CompletableFuture.runAsync(() -> getHome(home.uuid) .thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> { try (Connection connection = getConnection()) { - try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - UPDATE `%homes_table%` - SET `position_id`=?, `metadata_id`=?, `public`=? + try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + SELECT `position_id`, `metadata_id` FROM `%homes_table%` WHERE `uuid`=?;"""))) { - statement.setInt(1, setPosition(home, connection)); - statement.setInt(2, setPositionMeta(home.meta, connection)); - statement.setBoolean(3, home.isPublic); - statement.setString(4, home.uuid.toString()); + statement.setString(1, home.uuid.toString()); - statement.executeUpdate(); + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + updatePosition(resultSet.getInt("position_id"), home, connection); + updatePositionMeta(resultSet.getInt("metadata_id"), home.meta, connection); + } } } catch (SQLException e) { getLogger().log(Level.SEVERE, @@ -816,15 +856,16 @@ public class MySqlDatabase extends Database { return CompletableFuture.runAsync(() -> getWarp(warp.uuid) .thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> { try (Connection connection = getConnection()) { - try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" - UPDATE `%warps_table%` - SET `position_id`=?, `metadata_id`=? + try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" + SELECT `position_id`, `metadata_id` FROM `%warps_table%` WHERE `uuid`=?;"""))) { - statement.setInt(1, setPosition(warp, connection)); - statement.setInt(2, setPositionMeta(warp.meta, connection)); - statement.setString(3, warp.uuid.toString()); + statement.setString(1, warp.uuid.toString()); - statement.executeUpdate(); + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + updatePosition(resultSet.getInt("position_id"), warp, connection); + updatePositionMeta(resultSet.getInt("metadata_id"), warp.meta, connection); + } } } catch (SQLException e) { getLogger().log(Level.SEVERE, "Failed to update a warp in the database", e); diff --git a/common/src/main/java/net/william278/huskhomes/data/SqLiteDatabase.java b/common/src/main/java/net/william278/huskhomes/data/SqLiteDatabase.java index 361126c4..8440f713 100644 --- a/common/src/main/java/net/william278/huskhomes/data/SqLiteDatabase.java +++ b/common/src/main/java/net/william278/huskhomes/data/SqLiteDatabase.java @@ -115,6 +115,32 @@ public class SqLiteDatabase extends Database { } } + @Override + protected void updatePosition(int positionId, @NotNull Position position, @NotNull Connection connection) throws SQLException { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + UPDATE `%positions_table%` + SET `x`=?, + `y`=?, + `z`=?, + `yaw`=?, + `pitch`=?, + `world_uuid`=?, + `world_name`=?, + `server_name`=? + WHERE `id`=?"""))) { + statement.setDouble(1, position.x); + statement.setDouble(2, position.y); + statement.setDouble(3, position.z); + statement.setFloat(4, position.yaw); + statement.setFloat(5, position.pitch); + statement.setString(6, position.world.uuid.toString()); + statement.setString(7, position.world.name); + statement.setString(8, position.server.name); + statement.setDouble(9, positionId); + statement.executeUpdate(); + } + } + @Override protected int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" @@ -131,6 +157,20 @@ public class SqLiteDatabase extends Database { } } + @Override + protected void updatePositionMeta(int metaId, @NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + UPDATE `%position_metadata_table%` + SET `name`=?, + `description`=? + WHERE `id`=?;"""))) { + statement.setString(1, meta.name); + statement.setString(2, meta.description); + statement.setInt(3, metaId); + statement.executeUpdate(); + } + } + @Override public CompletableFuture ensureUser(@NotNull Player player) { return CompletableFuture.runAsync(() -> getUser(player.getUuid()).thenAccept(optionalUser -> @@ -748,15 +788,15 @@ public class SqLiteDatabase extends Database { .thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> { try { try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" - UPDATE `%homes_table%` - SET `position_id`=?, `metadata_id`=?, `public`=? + SELECT `position_id`, `metadata_id` FROM `%homes_table%` WHERE `uuid`=?;"""))) { - statement.setInt(1, setPosition(home, getConnection())); - statement.setInt(2, setPositionMeta(home.meta, getConnection())); - statement.setBoolean(3, home.isPublic); - statement.setString(4, home.uuid.toString()); + statement.setString(1, home.uuid.toString()); - statement.executeUpdate(); + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + updatePosition(resultSet.getInt("position_id"), home, connection); + updatePositionMeta(resultSet.getInt("metadata_id"), home.meta, connection); + } } } catch (SQLException e) { getLogger().log(Level.SEVERE, @@ -785,17 +825,18 @@ public class SqLiteDatabase extends Database { @Override public CompletableFuture setWarp(@NotNull Warp warp) { return CompletableFuture.runAsync(() -> getWarp(warp.uuid) - .thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> { + .thenAccept(existingHome -> existingHome.ifPresentOrElse(presentWarp -> { try { try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" - UPDATE `%warps_table%` - SET `position_id`=?, `metadata_id`=? + SELECT `position_id`, `metadata_id` FROM `%warps_table%` WHERE `uuid`=?;"""))) { - statement.setInt(1, setPosition(warp, getConnection())); - statement.setInt(2, setPositionMeta(warp.meta, getConnection())); - statement.setString(3, warp.uuid.toString()); + statement.setString(1, warp.uuid.toString()); - statement.executeUpdate(); + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + updatePosition(resultSet.getInt("position_id"), warp, connection); + updatePositionMeta(resultSet.getInt("metadata_id"), warp.meta, connection); + } } } catch (SQLException e) { getLogger().log(Level.SEVERE, "Failed to update a warp in the database", e); diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java index 6d893e3a..b0388d76 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java @@ -11,10 +11,8 @@ import net.william278.huskhomes.position.Position; import net.william278.huskhomes.position.Warp; import net.william278.huskhomes.util.MatcherUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.Optional; -import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; /** @@ -131,9 +129,7 @@ public abstract class TeleportManager { public CompletableFuture teleport(@NotNull Player player, @NotNull Position position) { return CompletableFuture.supplyAsync(() -> { final int teleportWarmupTime = plugin.getSettings().getIntegerValue(Settings.ConfigOption.TELEPORT_WARMUP_TIME); - System.out.println("teleport 1"); if (!player.hasPermission(WARMUP_BYPASS_PERMISSION) && teleportWarmupTime > 0) { - System.out.println("teleport 2"); return processTimedTeleport(new TimedTeleport(player, position, teleportWarmupTime)) .thenApply(teleport -> { if (!teleport.cancelled) { @@ -143,7 +139,6 @@ public abstract class TeleportManager { } }).join(); } else { - System.out.println("teleport b2"); return teleportNow(player, position).join(); } }); @@ -180,11 +175,9 @@ public abstract class TeleportManager { private CompletableFuture teleportNow(@NotNull Player player, @NotNull Position position) { final User user = new User(player); final Teleport teleport = new Teleport(user, position); - System.out.println("teleport b3"); - return CompletableFuture.supplyAsync(() -> player.getPosition().thenApply(preTeleportPosition -> plugin.getDatabase(). - setLastPosition(user, preTeleportPosition). - thenApply(ignored -> plugin.getServer(player).thenApply(server -> { - System.out.println("teleport b4"); + return CompletableFuture.supplyAsync(() -> player.getPosition().thenApply(preTeleportPosition -> plugin.getDatabase() + .setLastPosition(user, preTeleportPosition) // Update the player's last position + .thenApply(ignored -> plugin.getServer(player).thenApply(server -> { // Teleport player locally, or across server depending on need if (position.server.equals(server)) { return player.teleport(teleport.target).join(); @@ -211,10 +204,7 @@ public abstract class TeleportManager { */ protected final Optional tickTimedTeleport(@NotNull final TimedTeleport teleport) { if (teleport.isDone()) { - System.out.println("done... "); return Optional.of(teleport); - } else { - System.out.println("countdown... "); } // Cancel the timed teleport if the player takes damage