update positions rather than set new one on home/warp update

This commit is contained in:
William committed 2022-05-31 17:44:53 +01:00
1 parent 5b8352411e
commit 8a7bb11c10
4 files changed
+140 -44

No files matched your search

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