diff --git a/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java b/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java index f6334ade..56ab1170 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java +++ b/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java @@ -20,7 +20,7 @@ public class BukkitPlayer implements Player { } public static Optional get(@NotNull User user) { - return get(user.uuid()); + return get(user.uuid); } public static Optional get(@NotNull UUID uuid) { 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 22c586f3..246e15ed 100644 --- a/common/src/main/java/net/william278/huskhomes/data/Database.java +++ b/common/src/main/java/net/william278/huskhomes/data/Database.java @@ -3,6 +3,7 @@ package net.william278.huskhomes.data; import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.player.Player; import net.william278.huskhomes.player.User; +import net.william278.huskhomes.player.UserData; import net.william278.huskhomes.position.Home; import net.william278.huskhomes.position.Position; import net.william278.huskhomes.position.PositionMeta; @@ -194,37 +195,37 @@ public abstract class Database { protected abstract int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException; /** - * Ensure a {@link Player} has a {@link User} 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 * * @param player The {@link Player} to ensure * @return A future returning void when complete */ - public abstract CompletableFuture ensurePlayer(@NotNull Player player); + public abstract CompletableFuture ensureUser(@NotNull Player player); /** - * Get a player by their username (case-insensitive) + * Get a user by their username (case-insensitive) * - * @param name Username of the {@link User} to get (case-insensitive) - * @return A future returning an optional with the {@link User} present if they exist + * @param name Username of the {@link UserData} to get (case-insensitive) + * @return A future returning an optional with the {@link UserData} present if they exist */ - public abstract CompletableFuture> getUserByName(@NotNull String name); + public abstract CompletableFuture> getUserByName(@NotNull String name); /** * Get a player by their Minecraft account {@link UUID} * - * @param uuid Minecraft account {@link UUID} of the {@link User} to get - * @return A future returning an optional with the {@link User} present if they exist + * @param uuid Minecraft account {@link UUID} of the {@link UserData} to get + * @return A future returning an optional with the {@link UserData} present if they exist */ - public abstract CompletableFuture> getUser(@NotNull UUID uuid); + public abstract CompletableFuture> getUser(@NotNull UUID uuid); /** * Get a list of {@link Home}s set by a {@link User} * - * @param player {@link User} to get the homes of + * @param user {@link User} to get the homes of * @return A future returning void when complete */ - public abstract CompletableFuture> getHomes(@NotNull User player); + public abstract CompletableFuture> getHomes(@NotNull User user); /** * Get a list of all {@link Warp}s that have been set @@ -243,11 +244,11 @@ public abstract class Database { /** * Get a {@link Home} set by a {@link User} * - * @param player The {@link User} who set the home + * @param user The {@link User} who set the home * @param homeName The case-insensitive name of the home to get * @return A future returning an optional with the {@link Home} present if it exists */ - public abstract CompletableFuture> getHome(@NotNull User player, @NotNull String homeName); + public abstract CompletableFuture> getHome(@NotNull User user, @NotNull String homeName); /** * Get a {@link Home} by its unique id @@ -276,72 +277,80 @@ public abstract class Database { /** * Get the current {@link Teleport} being executed by the specified {@link User} * - * @param player The {@link User} to check + * @param user The {@link User} to check * @return A future returning an optional with the {@link Teleport} present if they are teleporting cross-server */ - public abstract CompletableFuture> getCurrentTeleport(@NotNull User player); + public abstract CompletableFuture> getCurrentTeleport(@NotNull User user); + + /** + * Updates a user in the database with new {@link UserData} + * + * @param userData The {@link UserData} to update + * @return A future returning void when complete + */ + public abstract CompletableFuture updateUserData(@NotNull UserData userData); /** * Sets or clears the current {@link Teleport} being executed by a {@link User} * - * @param player The {@link User} to set the current teleport of. + * @param user The {@link User} to set the current teleport of. * Pass as {@code null} to clear the player's current teleport.

* @param teleport The {@link Teleport} to set as their current cross-server teleport * @return A future returning void when complete */ - public abstract CompletableFuture setCurrentTeleport(@NotNull User player, @Nullable Teleport teleport); + public abstract CompletableFuture setCurrentTeleport(@NotNull User user, @Nullable Teleport teleport); /** * Get the last teleport {@link Position} of a specified {@link User} * - * @param player The {@link User} to check + * @param user The {@link User} to check * @return A future returning an optional with the {@link Position} present if it has been set */ - public abstract CompletableFuture> getLastPosition(@NotNull User player); + public abstract CompletableFuture> getLastPosition(@NotNull User user); /** * Sets the last teleport {@link Position} of a {@link User} * - * @param player The {@link User} to set the last position of + * @param user The {@link User} to set the last position of * @param position The {@link Position} to set as their last position * @return A future returning void when complete */ - public abstract CompletableFuture setLastPosition(@NotNull User player, @NotNull Position position); + public abstract CompletableFuture setLastPosition(@NotNull User user, @NotNull Position position); /** * Get the offline {@link Position} of a specified {@link User} * - * @param player The {@link User} to check + * @param user The {@link User} to check * @return A future returning an optional with the {@link Position} present if it has been set */ - public abstract CompletableFuture> getOfflinePosition(@NotNull User player); + public abstract CompletableFuture> getOfflinePosition(@NotNull User user); /** * Sets the offline {@link Position} of a {@link User} * - * @param player The {@link User} to set the offline position of + * @param user The {@link User} to set the offline position of * @param position The {@link Position} to set as their offline position * @return A future returning void when complete */ - public abstract CompletableFuture setOfflinePosition(@NotNull User player, @NotNull Position position); + public abstract CompletableFuture setOfflinePosition(@NotNull User user, @NotNull Position position); /** * Get the respawn {@link Position} of a specified {@link User} * - * @param player The {@link User} to check + * @param user The {@link User} to check * @return A future returning an optional with the {@link Position} present if it has been set */ - public abstract CompletableFuture> getRespawnPosition(@NotNull User player); + public abstract CompletableFuture> getRespawnPosition(@NotNull User user); /** * Sets or clears the respawn {@link Position} of a {@link User} * - * @param player The {@link User} to set the respawn position of + * @param user The {@link User} to set the respawn position of * @param position The {@link Position} to set as their respawn position * Pass as {@code null} to clear the player's current respawn position.

* @return A future returning void when complete */ - public abstract CompletableFuture setRespawnPosition(@NotNull User player, @Nullable Position position); + public abstract CompletableFuture setRespawnPosition(@NotNull User user, @Nullable Position position); /** * Sets or updates a {@link Home} into the home data table on the database. @@ -373,57 +382,6 @@ public abstract class Database { * @param uuid {@link UUID} of the warp to delete * @return A future returning void when complete */ - public abstract CompletableFuture deleteWarp(@NotNull Warp uuid); - - /** - * Get the rtp cooldown time of a specified {@link User} - * - * @param player The {@link User} to check - * @return A future returning the epoch time of their current cooldown - */ - public abstract CompletableFuture getRtpCooldown(@NotNull User player); - - /** - * Sets the rtp cooldown time of a {@link User} - * - * @param player The {@link User} to update - * @param position The epoch time of when the user can use rtp again. - * @return A future returning void when complete - */ - public abstract CompletableFuture setRtpCooldown(@NotNull User player, long position); - - /** - * Get the number of home slots a {@link User} has consumed (bought) - * - * @param player The {@link User} to check - * @return A future returning the number of home slots - */ - public abstract CompletableFuture getHomeSlots(@NotNull User player); - - /** - * Sets the number of home slots a {@link User} has consumed (bought) - * - * @param player The {@link User} to update - * @param homeSlots The number of home slots to set the user as having consumed. - * @return A future returning void when complete - */ - public abstract CompletableFuture setHomeSlots(@NotNull User player, int homeSlots); - - /** - * Get if a {@link User} is ignoring tp requests - * - * @param player The {@link User} to check - * @return A future returning if the player is ignoring tp requests - */ - public abstract CompletableFuture getIsIgnoringTeleports(@NotNull User player); - - /** - * Sets if a {@link User} is ignoring tp requests - * - * @param player The {@link User} to update - * @param ignoringTeleports If the player is ignoring tp requests - * @return A future returning void when complete - */ - public abstract CompletableFuture setIgnoringTeleports(@NotNull User player, boolean ignoringTeleports); + public abstract CompletableFuture deleteWarp(@NotNull UUID uuid); } 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 7eccc536..edac643d 100644 --- a/common/src/main/java/net/william278/huskhomes/data/MySqlDatabase.java +++ b/common/src/main/java/net/william278/huskhomes/data/MySqlDatabase.java @@ -4,6 +4,7 @@ import com.zaxxer.hikari.HikariDataSource; import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.player.Player; import net.william278.huskhomes.player.User; +import net.william278.huskhomes.player.UserData; import net.william278.huskhomes.position.*; import net.william278.huskhomes.teleport.Teleport; import net.william278.huskhomes.util.Logger; @@ -21,8 +22,6 @@ import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.logging.Level; -//todo write (hikari for this; direct driver for SQLite probably) - /** * A MySQL implementation of the plugin {@link Database} */ @@ -163,10 +162,10 @@ public class MySqlDatabase extends Database { } @Override - public CompletableFuture ensurePlayer(@NotNull Player player) { + public CompletableFuture ensureUser(@NotNull Player player) { return CompletableFuture.runAsync(() -> getUser(player.getUuid()).thenAccept(optionalUser -> optionalUser.ifPresentOrElse(existingUser -> { - if (!existingUser.username().equals(player.getName())) { + if (!existingUser.username.equals(player.getName())) { // Update a player's name if it has changed in the database try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" @@ -175,10 +174,10 @@ public class MySqlDatabase extends Database { WHERE `uuid`=?"""))) { statement.setString(1, player.getName()); - statement.setString(2, existingUser.uuid().toString()); + statement.setString(2, existingUser.uuid.toString()); statement.executeUpdate(); } - getLogger().log(Level.INFO, "Updated " + player.getName() + "'s name in the database (" + existingUser.username() + " -> " + player.getName() + ")"); + getLogger().log(Level.INFO, "Updated " + player.getName() + "'s name in the database (" + existingUser.username + " -> " + player.getName() + ")"); } catch (SQLException e) { getLogger().log(Level.SEVERE, "Failed to update a player's name on the database", e); } @@ -202,7 +201,7 @@ public class MySqlDatabase extends Database { } @Override - public CompletableFuture> getUserByName(@NotNull String name) { + public CompletableFuture> getUserByName(@NotNull String name) { return CompletableFuture.supplyAsync(() -> { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" @@ -213,8 +212,12 @@ public class MySqlDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new User(UUID.fromString(resultSet.getString("uuid")), - resultSet.getString("username"))); + return Optional.of(new UserData( + UUID.fromString(resultSet.getString("uuid")), + resultSet.getString("username"), + resultSet.getInt("home_slots"), + resultSet.getBoolean("ignoring_requests"), + resultSet.getTimestamp("rtp_cooldown").toInstant().getEpochSecond())); } } } catch (SQLException e) { @@ -225,7 +228,7 @@ public class MySqlDatabase extends Database { } @Override - public CompletableFuture> getUser(@NotNull UUID uuid) { + public CompletableFuture> getUser(@NotNull UUID uuid) { return CompletableFuture.supplyAsync(() -> { try (Connection connection = getConnection()) { try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" @@ -237,8 +240,12 @@ public class MySqlDatabase extends Database { final ResultSet resultSet = statement.executeQuery(); if (resultSet.next()) { - return Optional.of(new User(UUID.fromString(resultSet.getString("uuid")), - resultSet.getString("username"))); + return Optional.of(new UserData( + UUID.fromString(resultSet.getString("uuid")), + resultSet.getString("username"), + resultSet.getInt("home_slots"), + resultSet.getBoolean("ignoring_requests"), + resultSet.getTimestamp("rtp_cooldown").toInstant().getEpochSecond())); } } } catch (SQLException e) { @@ -262,7 +269,7 @@ public class MySqlDatabase extends Database { WHERE `owner_uuid`=? ORDER BY `name`;"""))) { - statement.setString(1, user.uuid().toString()); + statement.setString(1, user.uuid.toString()); final ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { @@ -283,7 +290,7 @@ public class MySqlDatabase extends Database { } } } catch (SQLException e) { - getLogger().log(Level.SEVERE, "Failed to query the database for home data for:" + user.username()); + getLogger().log(Level.SEVERE, "Failed to query the database for home data for:" + user.username); } return userHomes; }); @@ -376,7 +383,7 @@ public class MySqlDatabase extends Database { INNER JOIN `%position_metadata_table%` ON `%homes_table%`.`metadata_id`=`%position_metadata_table%`.`id` WHERE `owner_uuid`=? AND `name`=?;"""))) { - statement.setString(1, user.uuid().toString()); + statement.setString(1, user.uuid.toString()); statement.setString(2, homeName); final ResultSet resultSet = statement.executeQuery(); @@ -515,93 +522,362 @@ public class MySqlDatabase extends Database { } @Override - public CompletableFuture> getCurrentTeleport(@NotNull User player) { - return null; + public CompletableFuture> getCurrentTeleport(@NotNull User user) { + return CompletableFuture.supplyAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` + FROM `%teleports_table%` + INNER JOIN `%positions_table%` ON `%teleports_table%`.`destination_id` = `%positions_table%`.`id` + WHERE `player_uuid`=?"""))) { + statement.setString(1, user.uuid.toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(new Teleport(user, + new Position(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + new World(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + new Server(resultSet.getString("server_name"))))); + } + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to query the current teleport of " + user.username, e); + } + return Optional.empty(); + }); } @Override - public CompletableFuture setCurrentTeleport(@NotNull User player, @Nullable Teleport teleport) { - return null; + public CompletableFuture updateUserData(@NotNull UserData userData) { + return CompletableFuture.runAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + UPDATE `%players_table%` + SET `home_slots`=?, `ignoring_requests`=?, `rtp_cooldown`=? + WHERE `uuid`=?"""))) { + + statement.setInt(1, userData.homeSlots); + statement.setBoolean(2, userData.isIgnoringTeleports); + statement.setLong(3, userData.rtpCooldown); + statement.setString(4, userData.uuid.toString()); + statement.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to update user data for " + userData.username + " on the database", e); + } + }); } @Override - public CompletableFuture> getLastPosition(@NotNull User player) { - return null; + public CompletableFuture setCurrentTeleport(@NotNull User user, @Nullable Teleport teleport) { + return CompletableFuture.runAsync(() -> { + if (teleport == null) { + // Clear the user's current teleport + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM `%teleports_table%` + WHERE `player_uuid`=?;"""))) { + statement.setString(1, user.uuid.toString()); + + statement.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to clear the current teleport of " + user.username, e); + } + } else { + // Set the user's teleport into the database + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + INSERT INTO `%teleports_table%` (`player_uuid`, `destination_id`) + VALUES (?,?);"""))) { + statement.setString(1, user.uuid.toString()); + statement.setInt(2, setPosition(teleport.target, connection)); + + statement.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to set the current teleport of " + user.username, e); + } + } + }); } @Override - public CompletableFuture setLastPosition(@NotNull User player, @NotNull Position position) { - return null; + public CompletableFuture> getLastPosition(@NotNull User user) { + return CompletableFuture.supplyAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` + FROM `%players_table%` + INNER JOIN `%positions_table%` ON `%players_table%`.`last_position` = `%positions_table%`.`id` + WHERE `uuid`=?"""))) { + statement.setString(1, user.uuid.toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(new Position(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + new World(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + new Server(resultSet.getString("server_name")))); + } + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to query the last teleport position of " + user.username, e); + } + return Optional.empty(); + }); } @Override - public CompletableFuture> getOfflinePosition(@NotNull User player) { - return null; + public CompletableFuture setLastPosition(@NotNull User user, @NotNull Position position) { + return CompletableFuture.runAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + UPDATE `%players_table%` + SET `last_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 last position of " + user.username, e); + } + }); } @Override - public CompletableFuture setOfflinePosition(@NotNull User player, @NotNull Position position) { - return null; + public CompletableFuture> getOfflinePosition(@NotNull User user) { + return CompletableFuture.supplyAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` + FROM `%players_table%` + INNER JOIN `%positions_table%` ON `%players_table%`.`offline_position` = `%positions_table%`.`id` + WHERE `uuid`=?"""))) { + statement.setString(1, user.uuid.toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(new Position(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + new World(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + new Server(resultSet.getString("server_name")))); + } + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to query the offline position of " + user.username, e); + } + return Optional.empty(); + }); } @Override - public CompletableFuture> getRespawnPosition(@NotNull User player) { - return null; + public CompletableFuture setOfflinePosition(@NotNull User user, @NotNull Position position) { + return CompletableFuture.runAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + UPDATE `%players_table%` + SET `offline_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 offline position of " + user.username, e); + } + }); } @Override - public CompletableFuture setRespawnPosition(@NotNull User player, @Nullable Position position) { - return null; + public CompletableFuture> getRespawnPosition(@NotNull User user) { + return CompletableFuture.supplyAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` + FROM `%players_table%` + INNER JOIN `%positions_table%` ON `%players_table%`.`respawn_position` = `%positions_table%`.`id` + WHERE `uuid`=?"""))) { + statement.setString(1, user.uuid.toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(new Position(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + new World(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + new Server(resultSet.getString("server_name")))); + } + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to query the respawn position of " + user.username, e); + } + return Optional.empty(); + }); + } + + @Override + public CompletableFuture setRespawnPosition(@NotNull User user, @Nullable Position position) { + return CompletableFuture.runAsync(() -> { + if (position == null) { + // Clear the respawn position + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + UPDATE `%players_table%` + SET `respawn_position`=NULL + WHERE `uuid`=?;"""))) { + statement.setString(1, user.uuid.toString()); + + statement.executeUpdate(); + } + } 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); + } + } + }); } @Override public CompletableFuture setHome(@NotNull Home home) { - return null; + 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`=? + 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.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, + "Failed to update a home in the database for " + home.owner.username, e); + } + }, () -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + INSERT INTO `%homes_table%` (`uuid`, `owner_uuid`, `position_id`, `metadata_id`, `public`) + VALUES (?,?,?,?,?);"""))) { + statement.setString(1, home.uuid.toString()); + statement.setString(2, home.owner.uuid.toString()); + statement.setInt(3, setPosition(home, connection)); + statement.setInt(4, setPositionMeta(home.meta, connection)); + statement.setBoolean(5, home.isPublic); + + statement.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, + "Failed to set a home to the database for " + home.owner.username, e); + } + }))); } @Override public CompletableFuture setWarp(@NotNull Warp warp) { - return null; + 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`=? + WHERE `uuid`=?;"""))) { + statement.setInt(1, setPosition(warp, connection)); + statement.setInt(2, setPositionMeta(warp.meta, connection)); + statement.setString(3, warp.uuid.toString()); + + statement.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to update a warp in the database", e); + } + }, () -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + INSERT INTO `%warps_table%` (`uuid`, `position_id`, `metadata_id`) + VALUES (?,?,?);"""))) { + statement.setString(1, warp.uuid.toString()); + statement.setInt(2, setPosition(warp, connection)); + statement.setInt(3, setPositionMeta(warp.meta, connection)); + + statement.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to add a warp to the database", e); + } + }))); } @Override public CompletableFuture deleteHome(@NotNull UUID uuid) { - return null; + return CompletableFuture.runAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM `%homes_table%` + WHERE `uuid`=?;"""))) { + statement.setString(1, uuid.toString()); + + statement.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to delete a home from the database", e); + } + }); } @Override - public CompletableFuture deleteWarp(@NotNull Warp uuid) { - return null; - } + public CompletableFuture deleteWarp(@NotNull UUID uuid) { + return CompletableFuture.runAsync(() -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM `%warps_table%` + WHERE `uuid`=?;"""))) { + statement.setString(1, uuid.toString()); - @Override - public CompletableFuture getRtpCooldown(@NotNull User player) { - return null; - } - - @Override - public CompletableFuture setRtpCooldown(@NotNull User player, long position) { - return null; - } - - @Override - public CompletableFuture getHomeSlots(@NotNull User player) { - return null; - } - - @Override - public CompletableFuture setHomeSlots(@NotNull User player, int homeSlots) { - return null; - } - - @Override - public CompletableFuture getIsIgnoringTeleports(@NotNull User player) { - return null; - } - - @Override - public CompletableFuture setIgnoringTeleports(@NotNull User player, boolean ignoringTeleports) { - return null; + statement.executeUpdate(); + } + } catch (SQLException e) { + getLogger().log(Level.SEVERE, "Failed to delete a warp from the database", e); + } + }); } } \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/listener/EventProcessor.java b/common/src/main/java/net/william278/huskhomes/listener/EventProcessor.java index 477520fd..e5b1135b 100644 --- a/common/src/main/java/net/william278/huskhomes/listener/EventProcessor.java +++ b/common/src/main/java/net/william278/huskhomes/listener/EventProcessor.java @@ -25,10 +25,10 @@ public class EventProcessor { */ public void onPlayerJoin(@NotNull Player player) { // Ensure the player is present on the database first - implementor.getDatabase().ensurePlayer(player).thenRun(() -> { + implementor.getDatabase().ensureUser(player).thenRun(() -> { // If the server is in proxy mode, check if the player is teleporting cross-server if (implementor.getSettings().getBooleanValue(Settings.ConfigOption.ENABLE_PROXY_MODE)) { - implementor.getDatabase().getCurrentTeleport(new User(player.getUuid(), player.getName())) + implementor.getDatabase().getCurrentTeleport(new User(player)) .thenAccept(teleport -> teleport.ifPresent(currentTeleport -> player.teleport(currentTeleport.target).thenAccept(teleportResult -> { if (!teleportResult.successful) { diff --git a/common/src/main/java/net/william278/huskhomes/player/User.java b/common/src/main/java/net/william278/huskhomes/player/User.java index c3557d62..aaf08ebc 100644 --- a/common/src/main/java/net/william278/huskhomes/player/User.java +++ b/common/src/main/java/net/william278/huskhomes/player/User.java @@ -7,10 +7,31 @@ import java.util.UUID; /** * Represents persistently stored {@link User} data about a {@link Player}; uuid and username * A {@link Player} may be retrieved from a user by the platform implementing classes - * - * @param uuid - * @param username {@see {@link Player}} - an online player representation */ -public record User(@NotNull UUID uuid, @NotNull String username) { +public class User { + public final UUID uuid; + + public final String username; + + /** + * Get a user from a {@link UUID} and username + * + * @param uuid Minecraft account {@link UUID} of the player + * @param username Username of the player + */ + public User(@NotNull UUID uuid, @NotNull String username) { + this.uuid = uuid; + this.username = username; + } + + /** + * Get a user from an online {@link Player} + * + * @param player the online {@link Player} + */ + public User(Player player) { + this.uuid = player.getUuid(); + this.username = player.getName(); + } } diff --git a/common/src/main/java/net/william278/huskhomes/player/UserData.java b/common/src/main/java/net/william278/huskhomes/player/UserData.java new file mode 100644 index 00000000..98334a8c --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/player/UserData.java @@ -0,0 +1,44 @@ +package net.william278.huskhomes.player; + +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + +/** + * Extension of a {@link User} that contains persistently-stored additional user data + * (home slots, teleport ignoring status & rtp cooldown) + */ +public class UserData extends User { + + /** + * The number of slots this user has consumed (bought) in total, used for economy checks + */ + public int homeSlots; + + /** + * Whether this user is ignoring incoming /tpa and /tpahere requests + */ + public boolean isIgnoringTeleports; + + /** + * A timestamp indicating when the user can use the /rtp command next + */ + public long rtpCooldown; + + public UserData(@NotNull UUID uuid, @NotNull String username, + int homeSlots, boolean isIgnoringTeleports, long rtpCooldown) { + super(uuid, username); + this.homeSlots = homeSlots; + this.isIgnoringTeleports = isIgnoringTeleports; + this.rtpCooldown = rtpCooldown; + } + + public UserData(@NotNull User user, + int homeSlots, boolean isIgnoringTeleports, long rtpCooldown) { + super(user.uuid, user.username); + this.homeSlots = homeSlots; + this.isIgnoringTeleports = isIgnoringTeleports; + this.rtpCooldown = rtpCooldown; + } + +} diff --git a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java index 87a68308..60c074b6 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java @@ -1,6 +1,7 @@ package net.william278.huskhomes.teleport; import net.william278.huskhomes.player.Player; +import net.william278.huskhomes.player.User; import net.william278.huskhomes.position.Position; import org.jetbrains.annotations.NotNull; @@ -13,7 +14,7 @@ public class Teleport { * The player involved in the teleport */ @NotNull - public Player player; + public User player; /** * The target position for the player @@ -21,7 +22,7 @@ public class Teleport { @NotNull public Position target; - public Teleport(@NotNull Player player, @NotNull Position target) { + public Teleport(@NotNull User player, @NotNull Position target) { this.player = player; this.target = target; } 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 a8e7f3ca..3118fe6b 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportManager.java @@ -2,6 +2,7 @@ package net.william278.huskhomes.teleport; import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.player.Player; +import net.william278.huskhomes.player.User; import net.william278.huskhomes.position.Position; import org.jetbrains.annotations.NotNull; @@ -29,7 +30,7 @@ public class TeleportManager { * @param position the target {@link Position} to teleport to */ public CompletableFuture teleport(Player player, Position position) { - final Teleport teleport = new Teleport(player, position); + final Teleport teleport = new Teleport(new User(player), position); if (position.server.equals(plugin.getServerData())) { return player.teleport(teleport.target); } else {