mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 14:38:53 +00:00
update positions rather than set new one on home/warp update
This commit is contained in:
1 parent
5b8352411e
commit
8a7bb11c10
4 files changed
+140
-44
No files matched your search
@@ -181,19 +181,43 @@ public abstract class Database {
|
|||||||
/**
|
/**
|
||||||
* <b>(Internal use only)</b> - Sets a position to the position table in the database
|
* <b>(Internal use only)</b> - 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
|
* @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;
|
protected abstract int setPosition(@NotNull Position position, @NotNull Connection connection) throws SQLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <b>(Internal use only)</b> - 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <b>(Internal use only)</b> - Sets position meta to the position metadata table in the database
|
* <b>(Internal use only)</b> - 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
|
* @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;
|
protected abstract int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <b>(Internal use only)</b> - 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
|
* Ensure a {@link Player} has a {@link UserData} entry in the database and that their username is up-to-date
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -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
|
@Override
|
||||||
protected int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException {
|
protected int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException {
|
||||||
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
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
|
@Override
|
||||||
public CompletableFuture<Void> ensureUser(@NotNull Player player) {
|
public CompletableFuture<Void> ensureUser(@NotNull Player player) {
|
||||||
return CompletableFuture.runAsync(() -> getUser(player.getUuid()).thenAccept(optionalUser ->
|
return CompletableFuture.runAsync(() -> getUser(player.getUuid()).thenAccept(optionalUser ->
|
||||||
@@ -776,16 +816,16 @@ public class MySqlDatabase extends Database {
|
|||||||
return CompletableFuture.runAsync(() -> getHome(home.uuid)
|
return CompletableFuture.runAsync(() -> getHome(home.uuid)
|
||||||
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> {
|
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> {
|
||||||
try (Connection connection = getConnection()) {
|
try (Connection connection = getConnection()) {
|
||||||
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
|
||||||
UPDATE `%homes_table%`
|
SELECT `position_id`, `metadata_id` FROM `%homes_table%`
|
||||||
SET `position_id`=?, `metadata_id`=?, `public`=?
|
|
||||||
WHERE `uuid`=?;"""))) {
|
WHERE `uuid`=?;"""))) {
|
||||||
statement.setInt(1, setPosition(home, connection));
|
statement.setString(1, home.uuid.toString());
|
||||||
statement.setInt(2, setPositionMeta(home.meta, connection));
|
|
||||||
statement.setBoolean(3, home.isPublic);
|
|
||||||
statement.setString(4, 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) {
|
} catch (SQLException e) {
|
||||||
getLogger().log(Level.SEVERE,
|
getLogger().log(Level.SEVERE,
|
||||||
@@ -816,15 +856,16 @@ public class MySqlDatabase extends Database {
|
|||||||
return CompletableFuture.runAsync(() -> getWarp(warp.uuid)
|
return CompletableFuture.runAsync(() -> getWarp(warp.uuid)
|
||||||
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> {
|
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> {
|
||||||
try (Connection connection = getConnection()) {
|
try (Connection connection = getConnection()) {
|
||||||
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
|
||||||
UPDATE `%warps_table%`
|
SELECT `position_id`, `metadata_id` FROM `%warps_table%`
|
||||||
SET `position_id`=?, `metadata_id`=?
|
|
||||||
WHERE `uuid`=?;"""))) {
|
WHERE `uuid`=?;"""))) {
|
||||||
statement.setInt(1, setPosition(warp, connection));
|
statement.setString(1, warp.uuid.toString());
|
||||||
statement.setInt(2, setPositionMeta(warp.meta, connection));
|
|
||||||
statement.setString(3, 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) {
|
} catch (SQLException e) {
|
||||||
getLogger().log(Level.SEVERE, "Failed to update a warp in the database", e);
|
getLogger().log(Level.SEVERE, "Failed to update a warp in the database", e);
|
||||||
|
|||||||
@@ -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
|
@Override
|
||||||
protected int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException {
|
protected int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException {
|
||||||
try (PreparedStatement statement = connection.prepareStatement(formatStatementTables("""
|
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
|
@Override
|
||||||
public CompletableFuture<Void> ensureUser(@NotNull Player player) {
|
public CompletableFuture<Void> ensureUser(@NotNull Player player) {
|
||||||
return CompletableFuture.runAsync(() -> getUser(player.getUuid()).thenAccept(optionalUser ->
|
return CompletableFuture.runAsync(() -> getUser(player.getUuid()).thenAccept(optionalUser ->
|
||||||
@@ -748,15 +788,15 @@ public class SqLiteDatabase extends Database {
|
|||||||
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> {
|
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> {
|
||||||
try {
|
try {
|
||||||
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
|
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
|
||||||
UPDATE `%homes_table%`
|
SELECT `position_id`, `metadata_id` FROM `%homes_table%`
|
||||||
SET `position_id`=?, `metadata_id`=?, `public`=?
|
|
||||||
WHERE `uuid`=?;"""))) {
|
WHERE `uuid`=?;"""))) {
|
||||||
statement.setInt(1, setPosition(home, getConnection()));
|
statement.setString(1, home.uuid.toString());
|
||||||
statement.setInt(2, setPositionMeta(home.meta, getConnection()));
|
|
||||||
statement.setBoolean(3, home.isPublic);
|
|
||||||
statement.setString(4, 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) {
|
} catch (SQLException e) {
|
||||||
getLogger().log(Level.SEVERE,
|
getLogger().log(Level.SEVERE,
|
||||||
@@ -785,17 +825,18 @@ public class SqLiteDatabase extends Database {
|
|||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Void> setWarp(@NotNull Warp warp) {
|
public CompletableFuture<Void> setWarp(@NotNull Warp warp) {
|
||||||
return CompletableFuture.runAsync(() -> getWarp(warp.uuid)
|
return CompletableFuture.runAsync(() -> getWarp(warp.uuid)
|
||||||
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentHome -> {
|
.thenAccept(existingHome -> existingHome.ifPresentOrElse(presentWarp -> {
|
||||||
try {
|
try {
|
||||||
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
|
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
|
||||||
UPDATE `%warps_table%`
|
SELECT `position_id`, `metadata_id` FROM `%warps_table%`
|
||||||
SET `position_id`=?, `metadata_id`=?
|
|
||||||
WHERE `uuid`=?;"""))) {
|
WHERE `uuid`=?;"""))) {
|
||||||
statement.setInt(1, setPosition(warp, getConnection()));
|
statement.setString(1, warp.uuid.toString());
|
||||||
statement.setInt(2, setPositionMeta(warp.meta, getConnection()));
|
|
||||||
statement.setString(3, 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) {
|
} catch (SQLException e) {
|
||||||
getLogger().log(Level.SEVERE, "Failed to update a warp in the database", e);
|
getLogger().log(Level.SEVERE, "Failed to update a warp in the database", e);
|
||||||
|
|||||||
@@ -11,10 +11,8 @@ import net.william278.huskhomes.position.Position;
|
|||||||
import net.william278.huskhomes.position.Warp;
|
import net.william278.huskhomes.position.Warp;
|
||||||
import net.william278.huskhomes.util.MatcherUtil;
|
import net.william278.huskhomes.util.MatcherUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.CancellationException;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -131,9 +129,7 @@ public abstract class TeleportManager {
|
|||||||
public CompletableFuture<TeleportResult> teleport(@NotNull Player player, @NotNull Position position) {
|
public CompletableFuture<TeleportResult> teleport(@NotNull Player player, @NotNull Position position) {
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
final int teleportWarmupTime = plugin.getSettings().getIntegerValue(Settings.ConfigOption.TELEPORT_WARMUP_TIME);
|
final int teleportWarmupTime = plugin.getSettings().getIntegerValue(Settings.ConfigOption.TELEPORT_WARMUP_TIME);
|
||||||
System.out.println("teleport 1");
|
|
||||||
if (!player.hasPermission(WARMUP_BYPASS_PERMISSION) && teleportWarmupTime > 0) {
|
if (!player.hasPermission(WARMUP_BYPASS_PERMISSION) && teleportWarmupTime > 0) {
|
||||||
System.out.println("teleport 2");
|
|
||||||
return processTimedTeleport(new TimedTeleport(player, position, teleportWarmupTime))
|
return processTimedTeleport(new TimedTeleport(player, position, teleportWarmupTime))
|
||||||
.thenApply(teleport -> {
|
.thenApply(teleport -> {
|
||||||
if (!teleport.cancelled) {
|
if (!teleport.cancelled) {
|
||||||
@@ -143,7 +139,6 @@ public abstract class TeleportManager {
|
|||||||
}
|
}
|
||||||
}).join();
|
}).join();
|
||||||
} else {
|
} else {
|
||||||
System.out.println("teleport b2");
|
|
||||||
return teleportNow(player, position).join();
|
return teleportNow(player, position).join();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -180,11 +175,9 @@ public abstract class TeleportManager {
|
|||||||
private CompletableFuture<TeleportResult> teleportNow(@NotNull Player player, @NotNull Position position) {
|
private CompletableFuture<TeleportResult> teleportNow(@NotNull Player player, @NotNull Position position) {
|
||||||
final User user = new User(player);
|
final User user = new User(player);
|
||||||
final Teleport teleport = new Teleport(user, position);
|
final Teleport teleport = new Teleport(user, position);
|
||||||
System.out.println("teleport b3");
|
return CompletableFuture.supplyAsync(() -> player.getPosition().thenApply(preTeleportPosition -> plugin.getDatabase()
|
||||||
return CompletableFuture.supplyAsync(() -> player.getPosition().thenApply(preTeleportPosition -> plugin.getDatabase().
|
.setLastPosition(user, preTeleportPosition) // Update the player's last position
|
||||||
setLastPosition(user, preTeleportPosition).
|
.thenApply(ignored -> plugin.getServer(player).thenApply(server -> {
|
||||||
thenApply(ignored -> plugin.getServer(player).thenApply(server -> {
|
|
||||||
System.out.println("teleport b4");
|
|
||||||
// Teleport player locally, or across server depending on need
|
// Teleport player locally, or across server depending on need
|
||||||
if (position.server.equals(server)) {
|
if (position.server.equals(server)) {
|
||||||
return player.teleport(teleport.target).join();
|
return player.teleport(teleport.target).join();
|
||||||
@@ -211,10 +204,7 @@ public abstract class TeleportManager {
|
|||||||
*/
|
*/
|
||||||
protected final Optional<TimedTeleport> tickTimedTeleport(@NotNull final TimedTeleport teleport) {
|
protected final Optional<TimedTeleport> tickTimedTeleport(@NotNull final TimedTeleport teleport) {
|
||||||
if (teleport.isDone()) {
|
if (teleport.isDone()) {
|
||||||
System.out.println("done... ");
|
|
||||||
return Optional.of(teleport);
|
return Optional.of(teleport);
|
||||||
} else {
|
|
||||||
System.out.println("countdown... ");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cancel the timed teleport if the player takes damage
|
// Cancel the timed teleport if the player takes damage
|
||||||
|
|||||||
Reference in new issue
Block a user