mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
Finish MySQL implementation
This commit is contained in:
1 parent
bd52c99d4d
commit
28a54b0b4a
8 files changed
+457
-156
No files matched your search
@@ -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<Void> ensurePlayer(@NotNull Player player);
|
||||
public abstract CompletableFuture<Void> ensureUser(@NotNull Player player);
|
||||
|
||||
/**
|
||||
* Get a player by their username (<i>case-insensitive</i>)
|
||||
* Get a user by their username (<i>case-insensitive</i>)
|
||||
*
|
||||
* @param name Username of the {@link User} to get (<i>case-insensitive</i>)
|
||||
* @return A future returning an optional with the {@link User} present if they exist
|
||||
* @param name Username of the {@link UserData} to get (<i>case-insensitive</i>)
|
||||
* @return A future returning an optional with the {@link UserData} present if they exist
|
||||
*/
|
||||
public abstract CompletableFuture<Optional<User>> getUserByName(@NotNull String name);
|
||||
public abstract CompletableFuture<Optional<UserData>> 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<Optional<User>> getUser(@NotNull UUID uuid);
|
||||
public abstract CompletableFuture<Optional<UserData>> 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<List<Home>> getHomes(@NotNull User player);
|
||||
public abstract CompletableFuture<List<Home>> 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 <i>case-insensitive</i> name of the home to get
|
||||
* @return A future returning an optional with the {@link Home} present if it exists
|
||||
*/
|
||||
public abstract CompletableFuture<Optional<Home>> getHome(@NotNull User player, @NotNull String homeName);
|
||||
public abstract CompletableFuture<Optional<Home>> 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<Optional<Teleport>> getCurrentTeleport(@NotNull User player);
|
||||
public abstract CompletableFuture<Optional<Teleport>> 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<Void> 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.<p>
|
||||
* @param teleport The {@link Teleport} to set as their current cross-server teleport
|
||||
* @return A future returning void when complete
|
||||
*/
|
||||
public abstract CompletableFuture<Void> setCurrentTeleport(@NotNull User player, @Nullable Teleport teleport);
|
||||
public abstract CompletableFuture<Void> 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<Optional<Position>> getLastPosition(@NotNull User player);
|
||||
public abstract CompletableFuture<Optional<Position>> 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<Void> setLastPosition(@NotNull User player, @NotNull Position position);
|
||||
public abstract CompletableFuture<Void> 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<Optional<Position>> getOfflinePosition(@NotNull User player);
|
||||
public abstract CompletableFuture<Optional<Position>> 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<Void> setOfflinePosition(@NotNull User player, @NotNull Position position);
|
||||
public abstract CompletableFuture<Void> 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<Optional<Position>> getRespawnPosition(@NotNull User player);
|
||||
public abstract CompletableFuture<Optional<Position>> 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.<p>
|
||||
* @return A future returning void when complete
|
||||
*/
|
||||
public abstract CompletableFuture<Void> setRespawnPosition(@NotNull User player, @Nullable Position position);
|
||||
public abstract CompletableFuture<Void> 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<Void> 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<Long> 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<Void> 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<Integer> 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<Void> 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<Boolean> 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<Void> setIgnoringTeleports(@NotNull User player, boolean ignoringTeleports);
|
||||
public abstract CompletableFuture<Void> deleteWarp(@NotNull UUID uuid);
|
||||
|
||||
}
|
||||
@@ -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<Void> ensurePlayer(@NotNull Player player) {
|
||||
public CompletableFuture<Void> 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<Optional<User>> getUserByName(@NotNull String name) {
|
||||
public CompletableFuture<Optional<UserData>> 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<Optional<User>> getUser(@NotNull UUID uuid) {
|
||||
public CompletableFuture<Optional<UserData>> 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<Optional<Teleport>> getCurrentTeleport(@NotNull User player) {
|
||||
return null;
|
||||
public CompletableFuture<Optional<Teleport>> 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<Void> setCurrentTeleport(@NotNull User player, @Nullable Teleport teleport) {
|
||||
return null;
|
||||
public CompletableFuture<Void> 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<Optional<Position>> getLastPosition(@NotNull User player) {
|
||||
return null;
|
||||
public CompletableFuture<Void> 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<Void> setLastPosition(@NotNull User player, @NotNull Position position) {
|
||||
return null;
|
||||
public CompletableFuture<Optional<Position>> 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<Optional<Position>> getOfflinePosition(@NotNull User player) {
|
||||
return null;
|
||||
public CompletableFuture<Void> 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<Void> setOfflinePosition(@NotNull User player, @NotNull Position position) {
|
||||
return null;
|
||||
public CompletableFuture<Optional<Position>> 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<Optional<Position>> getRespawnPosition(@NotNull User player) {
|
||||
return null;
|
||||
public CompletableFuture<Void> 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<Void> setRespawnPosition(@NotNull User player, @Nullable Position position) {
|
||||
return null;
|
||||
public CompletableFuture<Optional<Position>> 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<Void> 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<Void> 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<Void> 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<Void> 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<Void> deleteWarp(@NotNull Warp uuid) {
|
||||
return null;
|
||||
}
|
||||
public CompletableFuture<Void> 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<Long> getRtpCooldown(@NotNull User player) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> setRtpCooldown(@NotNull User player, long position) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Integer> getHomeSlots(@NotNull User player) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> setHomeSlots(@NotNull User player, int homeSlots) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Boolean> getIsIgnoringTeleports(@NotNull User player) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<TeleportResult> 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 {
|
||||
|
||||
Reference in new issue
Block a user