diff --git a/build.gradle b/build.gradle index 013973c1..aebe1d56 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,7 @@ ext { set 'sqlite_driver_version', sqlite_driver_version.toString() set 'mysql_driver_version', mysql_driver_version.toString() set 'mariadb_driver_version', mariadb_driver_version.toString() + set 'postgresql_driver_version', postgresql_driver_version.toString() } publishing { diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index f8e699ff..482ec0c6 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -35,10 +35,7 @@ import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Server; import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.config.Spawn; -import net.william278.huskhomes.database.Database; -import net.william278.huskhomes.database.H2Database; -import net.william278.huskhomes.database.MySqlDatabase; -import net.william278.huskhomes.database.SqLiteDatabase; +import net.william278.huskhomes.database.*; import net.william278.huskhomes.event.BukkitEventDispatcher; import net.william278.huskhomes.hook.Hook; import net.william278.huskhomes.hook.PlaceholderAPIHook; @@ -138,6 +135,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask case MYSQL, MARIADB -> new MySqlDatabase(this); case SQLITE -> new SqLiteDatabase(this); case H2 -> new H2Database(this); + case POSTGRESQL -> new PostgreSqlDatabase(this); }; database.initialize(); diff --git a/common/build.gradle b/common/build.gradle index ee2ead80..430af97b 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -20,6 +20,7 @@ dependencies { compileOnly "com.mysql:mysql-connector-j:${mysql_driver_version}" compileOnly "org.mariadb.jdbc:mariadb-java-client:${mariadb_driver_version}" compileOnly "com.h2database:h2:${h2_driver_version}" + compileOnly "org.postgresql:postgresql:${postgresql_driver_version}" compileOnly 'org.jetbrains:annotations:24.1.0' compileOnly 'com.google.guava:guava:33.1.0-jre' diff --git a/common/src/main/java/net/william278/huskhomes/config/Settings.java b/common/src/main/java/net/william278/huskhomes/config/Settings.java index 0f9da6e5..433f4b4c 100644 --- a/common/src/main/java/net/william278/huskhomes/config/Settings.java +++ b/common/src/main/java/net/william278/huskhomes/config/Settings.java @@ -69,10 +69,10 @@ public final class Settings { @NoArgsConstructor(access = AccessLevel.PRIVATE) public static class DatabaseSettings { - @Comment("Type of database to use (SQLITE, H2, MYSQL, or MARIADB)") + @Comment("Type of database to use (SQLITE, H2, MYSQL, MARIADB, or POSTGRESQL)") private Database.Type type = Database.Type.SQLITE; - @Comment("Specify credentials here if you are using MYSQL or MARIADB") + @Comment("Specify credentials here if you are using MYSQL, MARIADB, or POSTGRESQL") private DatabaseCredentials credentials = new DatabaseCredentials(); @Getter @@ -89,7 +89,7 @@ public final class Settings { "useUnicode=true", "characterEncoding=UTF-8"); } - @Comment({"MYSQL / MARIADB database Hikari connection pool properties", + @Comment({"MYSQL / MARIADB / POSTGRESQL database Hikari connection pool properties", "Don't modify this unless you know what you're doing!"}) private PoolOptions poolOptions = new PoolOptions(); diff --git a/common/src/main/java/net/william278/huskhomes/database/Database.java b/common/src/main/java/net/william278/huskhomes/database/Database.java index 8780be1d..16d4f18a 100644 --- a/common/src/main/java/net/william278/huskhomes/database/Database.java +++ b/common/src/main/java/net/william278/huskhomes/database/Database.java @@ -493,7 +493,8 @@ public abstract class Database { MYSQL("MySQL", "mysql"), MARIADB("MariaDB", "mariadb"), SQLITE("SQLite", "sqlite"), - H2("H2", "h2"); + H2("H2", "h2"), + POSTGRESQL("PostgreSQL", "postgresql"); private final String displayName; private final String protocol; diff --git a/common/src/main/java/net/william278/huskhomes/database/PostgreSqlDatabase.java b/common/src/main/java/net/william278/huskhomes/database/PostgreSqlDatabase.java new file mode 100644 index 00000000..b98090a8 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/database/PostgreSqlDatabase.java @@ -0,0 +1,1276 @@ +/* + * This file is part of HuskHomes, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.huskhomes.database; + +import com.zaxxer.hikari.HikariDataSource; +import net.william278.huskhomes.HuskHomes; +import net.william278.huskhomes.position.*; +import net.william278.huskhomes.teleport.Teleport; +import net.william278.huskhomes.teleport.TeleportationException; +import net.william278.huskhomes.user.OnlineUser; +import net.william278.huskhomes.user.SavedUser; +import net.william278.huskhomes.user.User; +import net.william278.huskhomes.util.TransactionResolver; +import org.jetbrains.annotations.Blocking; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.IOException; +import java.sql.*; +import java.time.Instant; +import java.util.*; +import java.util.logging.Level; + +import static net.william278.huskhomes.config.Settings.DatabaseSettings; + +/** + * A PostgreSQL implementation of the plugin {@link Database}. + */ +@SuppressWarnings("DuplicatedCode") +public class PostgreSqlDatabase extends Database { + + private static final String DATA_POOL_NAME = "HuskHomesHikariPool"; + private final String driverClass; + private HikariDataSource dataSource; + + public PostgreSqlDatabase(@NotNull HuskHomes plugin) { + super(plugin); + + this.driverClass = "org.postgresql.Driver"; + } + + /** + * Fetch the auto-closeable connection from the hikariDataSource. + * + * @return The {@link Connection} to the PostgreSQL database + * @throws SQLException if the connection fails for some reason + */ + @Blocking + @NotNull + private Connection getConnection() throws SQLException { + if (dataSource == null) { + throw new IllegalStateException("The database has not been initialized"); + } + return dataSource.getConnection(); + } + + @Blocking + @Override + public void initialize() throws IllegalStateException { + // Initialize the Hikari pooled connection + final DatabaseSettings.DatabaseCredentials credentials = plugin.getSettings().getDatabase().getCredentials(); + dataSource = new HikariDataSource(); + dataSource.setDriverClassName(driverClass); + dataSource.setJdbcUrl(String.format("jdbc:postgresql://%s:%s/%s%s", + credentials.getHost(), + credentials.getPort(), + credentials.getDatabase(), + credentials.getParameters() + )); + + // Authenticate with the database + dataSource.setUsername(credentials.getUsername()); + dataSource.setPassword(credentials.getPassword()); + + // Set connection pool options + final DatabaseSettings.PoolOptions pool = plugin.getSettings().getDatabase().getPoolOptions(); + dataSource.setMaximumPoolSize(pool.getSize()); + dataSource.setMinimumIdle(pool.getIdle()); + dataSource.setMaxLifetime(pool.getLifetime()); + dataSource.setKeepaliveTime(pool.getKeepAlive()); + dataSource.setConnectionTimeout(pool.getTimeout()); + dataSource.setPoolName(DATA_POOL_NAME); + + // Set additional connection pool properties + final Properties properties = new Properties(); + properties.putAll( + Map.of("cachePrepStmts", "true", + "prepStmtCacheSize", "250", + "prepStmtCacheSqlLimit", "2048", + "useServerPrepStmts", "true", + "useLocalSessionState", "true", + "useLocalTransactionState", "true" + )); + properties.putAll( + Map.of( + "rewriteBatchedStatements", "true", + "cacheResultSetMetadata", "true", + "cacheServerConfiguration", "true", + "elideSetAutoCommits", "true", + "maintainTimeStats", "false") + ); + dataSource.setDataSourceProperties(properties); + + // Prepare database schema; make tables if they don't exist + try (Connection connection = dataSource.getConnection()) { + final String[] databaseSchema = getSchemaStatements("database/postgresql_schema.sql"); + try (Statement statement = connection.createStatement()) { + for (String tableCreationStatement : databaseSchema) { + statement.execute(tableCreationStatement); + } + } catch (SQLException e) { + throw new IllegalStateException("Failed to create database tables. Please ensure that your connecting " + + "user account has privileges to create tables.", e); + } + } catch (SQLException | IOException e) { + throw new IllegalStateException("Failed to establish a connection to the PostgreSQL database. " + + "Please check the supplied database credentials in the config file", e); + } + } + + @Override + protected int setPosition(@NotNull Position position, @NotNull Connection connection) throws SQLException { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + INSERT INTO "%positions_table%" + ("x","y","z","yaw","pitch","world_name","world_uuid","server_name") + VALUES + (?,?,?,?,?,?,?,?);"""), + Statement.RETURN_GENERATED_KEYS)) { + + statement.setDouble(1, position.getX()); + statement.setDouble(2, position.getY()); + statement.setDouble(3, position.getZ()); + statement.setFloat(4, position.getYaw()); + statement.setFloat(5, position.getPitch()); + statement.setString(6, position.getWorld().getName()); + statement.setString(7, position.getWorld().getUuid().toString()); + statement.setString(8, position.getServer()); + statement.executeUpdate(); + + final ResultSet resultSet = statement.getGeneratedKeys(); + if (resultSet.next()) { + return resultSet.getInt(1); + } + throw new SQLException("Failed to insert position into 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.getX()); + statement.setDouble(2, position.getY()); + statement.setDouble(3, position.getZ()); + statement.setFloat(4, position.getYaw()); + statement.setFloat(5, position.getPitch()); + statement.setString(6, position.getWorld().getUuid().toString()); + statement.setString(7, position.getWorld().getName()); + statement.setString(8, position.getServer()); + statement.setDouble(9, positionId); + statement.executeUpdate(); + } + } + + @Override + protected int setSavedPosition(@NotNull SavedPosition position, + @NotNull Connection connection) throws SQLException { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + INSERT INTO "%saved_positions_table%" + ("position_id", "name", "description", "tags", "timestamp") + VALUES + (?,?,?,?,?);"""), + Statement.RETURN_GENERATED_KEYS)) { + + statement.setInt(1, setPosition(position, connection)); + statement.setString(2, position.getName()); + statement.setString(3, position.getMeta().getDescription()); + statement.setString(4, position.getMeta().getSerializedTags()); + statement.setTimestamp(5, Timestamp.from(position.getMeta().getCreationTime())); + statement.executeUpdate(); + + final ResultSet resultSet = statement.getGeneratedKeys(); + if (resultSet.next()) { + return resultSet.getInt(1); + } + throw new SQLException("Failed to insert saved position into database"); + } + } + + @Override + protected void updateSavedPosition(int savedPositionId, @NotNull SavedPosition position, + @NotNull Connection connection) throws SQLException { + try (PreparedStatement selectStatement = connection.prepareStatement(formatStatementTables(""" + SELECT "position_id" + FROM "%saved_positions_table%" + WHERE "id"=?;"""))) { + selectStatement.setInt(1, savedPositionId); + + final ResultSet resultSet = selectStatement.executeQuery(); + if (resultSet.next()) { + final int positionId = resultSet.getInt("position_id"); + updatePosition(positionId, position, connection); + + try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables(""" + UPDATE "%saved_positions_table%" + SET "name"=?, + "description"=?, + "tags"=? + WHERE "id"=?;"""))) { + updateStatement.setString(1, position.getName()); + updateStatement.setString(2, position.getMeta().getDescription()); + updateStatement.setString(3, position.getMeta().getSerializedTags()); + updateStatement.setInt(4, savedPositionId); + updateStatement.executeUpdate(); + } + } + } + } + + @Override + public void ensureUser(@NotNull User onlineUser) { + getUserData(onlineUser.getUuid()).ifPresentOrElse( + existingUserData -> { + if (!existingUserData.getUsername().equals(onlineUser.getUsername())) { + // Update a player's name if it has changed in the database + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + UPDATE "%players_table%" + SET "username"=? + WHERE "uuid"=?"""))) { + + statement.setString(1, onlineUser.getUsername()); + statement.setString(2, existingUserData.getUserUuid().toString()); + statement.executeUpdate(); + } + plugin.log(Level.INFO, "Updated " + onlineUser.getUsername() + + "'s name in the database (" + existingUserData.getUsername() + + " -> " + onlineUser.getUsername() + ")"); + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to update a player's name on the database", e); + } + } + }, + () -> { + // Insert new player data into the database + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + INSERT INTO "%players_table%" ("uuid","username") + VALUES (?,?);"""))) { + + statement.setString(1, onlineUser.getUuid().toString()); + statement.setString(2, onlineUser.getUsername()); + statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to insert a player into the database", e); + } + }); + } + + @Override + public Optional getUserDataByName(@NotNull String name) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "uuid", "username", "home_slots", "ignoring_requests" + FROM "%players_table%" + WHERE "username"=?"""))) { + statement.setString(1, name); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(new SavedUser( + User.of(UUID.fromString(resultSet.getString("uuid")), + resultSet.getString("username")), + resultSet.getInt("home_slots"), + resultSet.getBoolean("ignoring_requests") + )); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to fetch a player by name from the database", e); + } + return Optional.empty(); + } + + @Override + public Optional getUserData(@NotNull UUID uuid) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "uuid", "username", "home_slots", "ignoring_requests" + FROM "%players_table%" + WHERE "uuid"=?"""))) { + + statement.setString(1, uuid.toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(new SavedUser( + User.of(UUID.fromString(resultSet.getString("uuid")), + resultSet.getString("username")), + resultSet.getInt("home_slots"), + resultSet.getBoolean("ignoring_requests") + )); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to fetch a player from uuid from the database", e); + } + return Optional.empty(); + } + + @Override + public void deleteUserData(@NotNull UUID uuid) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM "%positions_table%" + WHERE "id" + IN ((SELECT "last_position" FROM "%players_table%" WHERE "uuid" = ?), + (SELECT "offline_position" FROM "%players_table%" WHERE "uuid" = ?), + (SELECT "respawn_position" FROM "%players_table%" WHERE "uuid" = ?));"""))) { + statement.setString(1, uuid.toString()); + statement.setString(2, uuid.toString()); + statement.setString(3, uuid.toString()); + statement.executeUpdate(); + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to delete player positions from the database", e); + return; + } + + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM "%players_table%" + WHERE "uuid"=?;"""))) { + statement.setString(1, uuid.toString()); + statement.executeUpdate(); + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to delete a player from the database", e); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to delete user data", e); + } + } + + @Override + public Optional getCooldown(@NotNull TransactionResolver.Action action, @NotNull User user) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "type", "start_timestamp", "end_timestamp" + FROM "%cooldowns_table%" + WHERE "player_uuid"=? AND "type"=? + ORDER BY "start_timestamp" DESC + LIMIT 1;"""))) { + statement.setString(1, user.getUuid().toString()); + statement.setString(2, action.name().toLowerCase(Locale.ENGLISH)); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(resultSet.getTimestamp("end_timestamp").toInstant()); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to fetch a player's cooldown from the database", e); + } + return Optional.empty(); + } + + @Override + public void setCooldown(@NotNull TransactionResolver.Action action, @NotNull User user, + @NotNull Instant cooldownExpiry) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + INSERT INTO "%cooldowns_table%" ("player_uuid", "type", "start_timestamp", "end_timestamp") + VALUES (?,?,?,?);"""))) { + statement.setString(1, user.getUuid().toString()); + statement.setString(2, action.name().toLowerCase(Locale.ENGLISH)); + statement.setTimestamp(3, Timestamp.from(Instant.now())); + statement.setTimestamp(4, Timestamp.from(cooldownExpiry)); + statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to set a player's cooldown in the database", e); + } + } + + @Override + public void removeCooldown(@NotNull TransactionResolver.Action action, @NotNull User user) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM "%cooldowns_table%" + WHERE "player_uuid"=? AND "type"=?;"""))) { + + statement.setString(1, user.getUuid().toString()); + statement.setString(2, action.name().toLowerCase(Locale.ENGLISH)); + statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to remove a player's cooldown from the database", e); + } + } + + @Override + public List getHomes(@NotNull User user) { + final List userHomes = new ArrayList<>(); + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "%homes_table%"."uuid" AS "home_uuid", "owner_uuid", "name", "description", "tags", + "timestamp", "x", "y", "z", "yaw", "pitch", "world_name", "world_uuid", "server_name", "public" + FROM "%homes_table%" + INNER JOIN "%saved_positions_table%" + ON "%homes_table%"."saved_position_id"="%saved_positions_table%"."id" + INNER JOIN "%positions_table%" + ON "%saved_positions_table%"."position_id"="%positions_table%"."id" + INNER JOIN "%players_table%" + ON "%homes_table%"."owner_uuid"="%players_table%"."uuid" + WHERE "owner_uuid"=? + ORDER BY "name";"""))) { + + statement.setString(1, user.getUuid().toString()); + + final ResultSet resultSet = statement.executeQuery(); + while (resultSet.next()) { + userHomes.add(Home.from(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + World.from(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + resultSet.getString("server_name"), + PositionMeta.from(resultSet.getString("name"), + resultSet.getString("description"), + resultSet.getTimestamp("timestamp").toInstant(), + resultSet.getString("tags")), + UUID.fromString(resultSet.getString("home_uuid")), + user, + resultSet.getBoolean("public"))); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to query the database for home data for:" + user.getUsername()); + } + return userHomes; + } + + @Override + public List getWarps() { + final List warps = new ArrayList<>(); + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "%warps_table%"."uuid" AS "warp_uuid", "name", "description", "tags", "timestamp", + "x", "y", "z", "yaw", "pitch", "world_name", "world_uuid", "server_name" + FROM "%warps_table%" + INNER JOIN "%saved_positions_table%" + ON "%warps_table%"."saved_position_id"="%saved_positions_table%"."id" + INNER JOIN "%positions_table%" + ON "%saved_positions_table%"."position_id"="%positions_table%"."id" + ORDER BY "name";"""))) { + + final ResultSet resultSet = statement.executeQuery(); + while (resultSet.next()) { + warps.add(Warp.from(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + World.from(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + resultSet.getString("server_name"), + PositionMeta.from(resultSet.getString("name"), + resultSet.getString("description"), + resultSet.getTimestamp("timestamp").toInstant(), + resultSet.getString("tags")), + UUID.fromString(resultSet.getString("warp_uuid")))); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to query the database for warp data."); + } + return warps; + } + + @Override + public List getPublicHomes() { + final List userHomes = new ArrayList<>(); + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "%homes_table%"."uuid" AS "home_uuid", "owner_uuid", "username" AS "owner_username", "name", + "description", "tags", "timestamp", "x", "y", "z", "yaw", "pitch", "world_name", "world_uuid", + "server_name", "public" + FROM "%homes_table%" + INNER JOIN "%saved_positions_table%" + ON "%homes_table%"."saved_position_id"="%saved_positions_table%"."id" + INNER JOIN "%positions_table%" + ON "%saved_positions_table%"."position_id"="%positions_table%"."id" + INNER JOIN "%players_table%" + ON "%homes_table%"."owner_uuid"="%players_table%"."uuid" + WHERE "public"=true + ORDER BY "name";"""))) { + final ResultSet resultSet = statement.executeQuery(); + while (resultSet.next()) { + userHomes.add(Home.from(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + World.from(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + resultSet.getString("server_name"), + PositionMeta.from(resultSet.getString("name"), + resultSet.getString("description"), + resultSet.getTimestamp("timestamp").toInstant(), + resultSet.getString("tags")), + UUID.fromString(resultSet.getString("home_uuid")), + User.of(UUID.fromString(resultSet.getString("owner_uuid")), + resultSet.getString("owner_username")), + resultSet.getBoolean("public"))); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to query the database for public home data"); + } + return userHomes; + } + + @Override + public List getPublicHomes(@NotNull String name, boolean caseInsensitive) { + final List userHomes = new ArrayList<>(); + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "%homes_table%"."uuid" AS "home_uuid", "owner_uuid", "username" AS "owner_username", "name", + "description", "tags", "timestamp", "x", "y", "z", "yaw", "pitch", "world_name", "world_uuid", + "server_name", "public" + FROM "%homes_table%" + INNER JOIN "%saved_positions_table%" + ON "%homes_table%"."saved_position_id"="%saved_positions_table%"."id" + INNER JOIN "%positions_table%" + ON "%saved_positions_table%"."position_id"="%positions_table%"."id" + INNER JOIN "%players_table%" + ON "%homes_table%"."owner_uuid"="%players_table%"."uuid" + WHERE "public"=true + AND ((? AND UPPER("name") LIKE UPPER(?)) OR ("name"=?)) + ORDER BY "name";"""))) { + statement.setBoolean(1, caseInsensitive); + statement.setString(2, name); + statement.setString(3, name); + + final ResultSet resultSet = statement.executeQuery(); + while (resultSet.next()) { + userHomes.add(Home.from(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + World.from(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + resultSet.getString("server_name"), + PositionMeta.from(resultSet.getString("name"), + resultSet.getString("description"), + resultSet.getTimestamp("timestamp").toInstant(), + resultSet.getString("tags")), + UUID.fromString(resultSet.getString("home_uuid")), + User.of(UUID.fromString(resultSet.getString("owner_uuid")), + resultSet.getString("owner_username")), + resultSet.getBoolean("public"))); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to query the database for public home data"); + } + return userHomes; + } + + @Override + public Optional getHome(@NotNull User user, @NotNull String homeName, boolean caseInsensitive) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "%homes_table%"."uuid" AS "home_uuid", "owner_uuid", "username" AS "owner_username", + "name", "description", "tags", "timestamp", "x", "y", "z", "yaw", "pitch", "world_name", + "world_uuid", "server_name", "public" + FROM "%homes_table%" + INNER JOIN "%saved_positions_table%" + ON "%homes_table%"."saved_position_id"="%saved_positions_table%"."id" + INNER JOIN "%positions_table%" + ON "%saved_positions_table%"."position_id"="%positions_table%"."id" + INNER JOIN "%players_table%" + ON "%homes_table%"."owner_uuid"="%players_table%"."uuid" + WHERE "owner_uuid"=? + AND ((? AND UPPER("name") LIKE UPPER(?)) OR ("name"=?))"""))) { + statement.setString(1, user.getUuid().toString()); + statement.setBoolean(2, caseInsensitive); + statement.setString(3, homeName); + statement.setString(4, homeName); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(Home.from(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + World.from(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + resultSet.getString("server_name"), + PositionMeta.from(resultSet.getString("name"), + resultSet.getString("description"), + resultSet.getTimestamp("timestamp").toInstant(), + resultSet.getString("tags")), + UUID.fromString(resultSet.getString("home_uuid")), + user, + resultSet.getBoolean("public"))); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to query a player's home", e); + } + return Optional.empty(); + } + + @Override + public Optional getHome(@NotNull UUID uuid) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "%homes_table%"."uuid" AS "home_uuid", "owner_uuid", "username" AS "owner_username", + "name", "description", "tags", "timestamp", "x", "y", "z", "yaw", "pitch", "world_name", + "world_uuid", "server_name", "public" + FROM "%homes_table%" + INNER JOIN "%saved_positions_table%" + ON "%homes_table%"."saved_position_id"="%saved_positions_table%"."id" + INNER JOIN "%positions_table%" + ON "%saved_positions_table%"."position_id"="%positions_table%"."id" + INNER JOIN "%players_table%" + ON "%homes_table%"."owner_uuid"="%players_table%"."uuid" + WHERE "%homes_table%"."uuid"=?;"""))) { + statement.setString(1, uuid.toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(Home.from(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + World.from(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + resultSet.getString("server_name"), + PositionMeta.from(resultSet.getString("name"), + resultSet.getString("description"), + resultSet.getTimestamp("timestamp").toInstant(), + resultSet.getString("tags")), + UUID.fromString(resultSet.getString("home_uuid")), + User.of(UUID.fromString(resultSet.getString("owner_uuid")), + resultSet.getString("owner_username")), + resultSet.getBoolean("public"))); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to query a player's home by uuid", e); + } + return Optional.empty(); + } + + @Override + public Optional getWarp(@NotNull String warpName, boolean caseInsensitive) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "%warps_table%"."uuid" AS "warp_uuid", "name", "description", "tags", "timestamp", + "x", "y", "z", "yaw", "pitch", "world_name", "world_uuid", "server_name" + FROM "%warps_table%" + INNER JOIN "%saved_positions_table%" + ON "%warps_table%"."saved_position_id"="%saved_positions_table%"."id" + INNER JOIN "%positions_table%" + ON "%saved_positions_table%"."position_id"="%positions_table%"."id" + AND ((? AND UPPER("name") LIKE UPPER(?)) OR ("name"=?))"""))) { + statement.setBoolean(1, caseInsensitive); + statement.setString(2, warpName); + statement.setString(3, warpName); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(Warp.from(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + World.from(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + resultSet.getString("server_name"), + PositionMeta.from(resultSet.getString("name"), + resultSet.getString("description"), + resultSet.getTimestamp("timestamp").toInstant(), + resultSet.getString("tags")), + UUID.fromString(resultSet.getString("warp_uuid")))); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to query a server warp", e); + } + return Optional.empty(); + } + + @Override + public Optional getWarp(@NotNull UUID uuid) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "%warps_table%"."uuid" AS "warp_uuid", "name", "description", "tags", "timestamp", + "x", "y", "z", "yaw", "pitch", "world_name", "world_uuid", "server_name" + FROM "%warps_table%" + INNER JOIN "%saved_positions_table%" + ON "%warps_table%"."saved_position_id"="%saved_positions_table%"."id" + INNER JOIN "%positions_table%" + ON "%saved_positions_table%"."position_id"="%positions_table%"."id" + WHERE "%warps_table%".uuid=?;"""))) { + statement.setString(1, uuid.toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(Warp.from(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + World.from(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + resultSet.getString("server_name"), + PositionMeta.from(resultSet.getString("name"), + resultSet.getString("description"), + resultSet.getTimestamp("timestamp").toInstant(), + resultSet.getString("tags")), + UUID.fromString(resultSet.getString("warp_uuid")))); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to query a server warp", e); + } + return Optional.empty(); + } + + @Override + public Optional getCurrentTeleport(@NotNull OnlineUser onlineUser) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "x", "y", "z", "yaw", "pitch", "world_name", "world_uuid", "server_name", "type" + FROM "%teleports_table%" + INNER JOIN "%positions_table%" ON "%teleports_table%"."destination_id" = "%positions_table%"."id" + WHERE "player_uuid"=?"""))) { + statement.setString(1, onlineUser.getUuid().toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(Teleport.builder(plugin) + .teleporter(onlineUser) + .target(Position.at(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + World.from(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + resultSet.getString("server_name"))) + .type(Teleport.Type.getTeleportType(resultSet.getInt("type")) + .orElse(Teleport.Type.TELEPORT)) + .updateLastPosition(false) + .toTeleport()); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to query the current teleport of " + onlineUser.getUsername(), e); + } catch (TeleportationException e) { + e.displayMessage(onlineUser); + } + return Optional.empty(); + } + + @Override + public void updateUserData(@NotNull SavedUser savedUser) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + UPDATE "%players_table%" + SET "home_slots"=?, "ignoring_requests"=? + WHERE "uuid"=?"""))) { + + statement.setInt(1, savedUser.getHomeSlots()); + statement.setBoolean(2, savedUser.isIgnoringTeleports()); + statement.setString(3, savedUser.getUserUuid().toString()); + statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to update user data for " + savedUser.getUsername(), e); + } + } + + @Override + public void setCurrentTeleport(@NotNull User user, @Nullable Teleport teleport) { + try (Connection connection = getConnection()) { + // Clear the user's current teleport + try (PreparedStatement deleteStatement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM "%positions_table%" + WHERE "id"=( + SELECT "destination_id" + FROM "%teleports_table%" + WHERE "%teleports_table%"."player_uuid"=? + );"""))) { + deleteStatement.setString(1, user.getUuid().toString()); + deleteStatement.executeUpdate(); + } + + // Set the user's teleport into the database (if it's not null) + if (teleport != null) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + INSERT INTO "%teleports_table%" ("player_uuid", "destination_id", "type") + VALUES (?,?,?);"""))) { + statement.setString(1, user.getUuid().toString()); + statement.setInt(2, setPosition((Position) teleport.getTarget(), connection)); + statement.setInt(3, teleport.getType().getTypeId()); + + statement.executeUpdate(); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to clear the current teleport of " + user.getUsername(), e); + } + } + + @Override + public Optional getLastPosition(@NotNull User user) { + 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.getUuid().toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(Position.at(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + World.from(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + resultSet.getString("server_name"))); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to query the last teleport position of " + user.getUsername(), e); + } + return Optional.empty(); + } + + @Override + public void setLastPosition(@NotNull User user, @NotNull Position position) { + try (Connection connection = getConnection()) { + try (PreparedStatement queryStatement = connection.prepareStatement(formatStatementTables(""" + SELECT "last_position" + FROM "%players_table%" + INNER JOIN "%positions_table%" ON "%players_table%".last_position = "%positions_table%"."id" + WHERE "uuid"=?;"""))) { + queryStatement.setString(1, user.getUuid().toString()); + + final ResultSet resultSet = queryStatement.executeQuery(); + if (resultSet.next()) { + // Update the last position + updatePosition(resultSet.getInt("last_position"), position, connection); + } else { + // Set the last position + try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables(""" + UPDATE "%players_table%" + SET "last_position"=? + WHERE "uuid"=?;"""))) { + updateStatement.setInt(1, setPosition(position, connection)); + updateStatement.setString(2, user.getUuid().toString()); + updateStatement.executeUpdate(); + } + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to set the last position of " + user.getUsername(), e); + } + } + + @Override + public Optional getOfflinePosition(@NotNull User user) { + 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.getUuid().toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(Position.at(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + World.from(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + resultSet.getString("server_name"))); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to query the offline position of " + user.getUsername(), e); + } + return Optional.empty(); + } + + @Override + public void setOfflinePosition(@NotNull User user, @NotNull Position position) { + try (Connection connection = getConnection()) { + try (PreparedStatement queryStatement = connection.prepareStatement(formatStatementTables(""" + SELECT "offline_position" FROM "%players_table%" + INNER JOIN "%positions_table%" ON "%players_table%"."offline_position" = "%positions_table%"."id" + WHERE "uuid"=?;"""))) { + queryStatement.setString(1, user.getUuid().toString()); + + final ResultSet resultSet = queryStatement.executeQuery(); + if (resultSet.next()) { + // Update the offline position + updatePosition(resultSet.getInt("offline_position"), position, connection); + } else { + // Set the offline position + try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables(""" + UPDATE "%players_table%" + SET "offline_position"=? + WHERE "uuid"=?;"""))) { + updateStatement.setInt(1, setPosition(position, connection)); + updateStatement.setString(2, user.getUuid().toString()); + updateStatement.executeUpdate(); + } + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to set the offline position of " + user.getUsername(), e); + } + } + + @Override + public Optional getRespawnPosition(@NotNull User user) { + 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.getUuid().toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return Optional.of(Position.at(resultSet.getDouble("x"), + resultSet.getDouble("y"), + resultSet.getDouble("z"), + resultSet.getFloat("yaw"), + resultSet.getFloat("pitch"), + World.from(resultSet.getString("world_name"), + UUID.fromString(resultSet.getString("world_uuid"))), + resultSet.getString("server_name"))); + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to query the respawn position of " + user.getUsername(), e); + } + return Optional.empty(); + } + + @Override + public void setRespawnPosition(@NotNull User user, @Nullable Position position) { + try (Connection connection = getConnection()) { + try (PreparedStatement queryStatement = connection.prepareStatement(formatStatementTables(""" + SELECT "respawn_position" FROM "%players_table%" + INNER JOIN "%positions_table%" ON "%players_table%".respawn_position = "%positions_table%"."id" + WHERE "uuid"=?;"""))) { + queryStatement.setString(1, user.getUuid().toString()); + + final ResultSet resultSet = queryStatement.executeQuery(); + if (resultSet.next()) { + if (position == null) { + // Delete a respawn position + try (PreparedStatement deleteStatement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM "%positions_table%" + WHERE "id"=( + SELECT "respawn_position" + FROM "%players_table%" + WHERE "%players_table%"."uuid"=? + );"""))) { + deleteStatement.setString(1, user.getUuid().toString()); + deleteStatement.executeUpdate(); + } + } else { + // Update the respawn position + updatePosition(resultSet.getInt("respawn_position"), position, connection); + } + } else { + if (position != null) { + // Set a respawn position + try (PreparedStatement updateStatement = connection.prepareStatement(formatStatementTables(""" + UPDATE "%players_table%" + SET "respawn_position"=? + WHERE "uuid"=?;"""))) { + updateStatement.setInt(1, setPosition(position, connection)); + updateStatement.setString(2, user.getUuid().toString()); + updateStatement.executeUpdate(); + } + } + } + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to set the respawn position of " + user.getUsername(), e); + } + } + + @Override + public void saveHome(@NotNull Home home) { + getHome(home.getUuid()).ifPresentOrElse(presentHome -> { + try (Connection connection = getConnection()) { + // Update the home's saved position, including metadata + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "saved_position_id" FROM "%homes_table%" + WHERE "uuid"=?;"""))) { + statement.setString(1, home.getUuid().toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + updateSavedPosition(resultSet.getInt("saved_position_id"), home, connection); + } + } + + // Update the home privacy + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + UPDATE "%homes_table%" + SET "public"=? + WHERE "uuid"=?;"""))) { + statement.setBoolean(1, home.isPublic()); + statement.setString(2, home.getUuid().toString()); + statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, + "Failed to update a home in the database for " + home.getOwner().getUsername(), e); + } + }, () -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + INSERT INTO "%homes_table%" ("uuid", "saved_position_id", "owner_uuid", "public") + VALUES (?,?,?,?);"""))) { + statement.setString(1, home.getUuid().toString()); + statement.setInt(2, setSavedPosition(home, connection)); + statement.setString(3, home.getOwner().getUuid().toString()); + statement.setBoolean(4, home.isPublic()); + + statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, + "Failed to set a home to the database for " + home.getOwner().getUsername(), e); + } + }); + } + + @Override + public void saveWarp(@NotNull Warp warp) { + getWarp(warp.getUuid()).ifPresentOrElse(presentWarp -> { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + SELECT "saved_position_id" FROM "%warps_table%" + WHERE "uuid"=?;"""))) { + statement.setString(1, warp.getUuid().toString()); + + final ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + updateSavedPosition(resultSet.getInt("saved_position_id"), warp, connection); + } + } + } catch (SQLException e) { + plugin.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", "saved_position_id") + VALUES (?,?);"""))) { + statement.setString(1, warp.getUuid().toString()); + statement.setInt(2, setSavedPosition(warp, connection)); + + statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to add a warp to the database", e); + } + }); + } + + @Override + public void deleteHome(@NotNull UUID uuid) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM "%positions_table%" + WHERE "%positions_table%"."id"=( + SELECT "position_id" + FROM "%saved_positions_table%" + WHERE "%saved_positions_table%"."id"=( + SELECT "saved_position_id" + FROM "%homes_table%" + WHERE "uuid"=? + ) + );"""))) { + statement.setString(1, uuid.toString()); + + statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to delete a home from the database", e); + } + } + + @Override + public int deleteAllHomes(@NotNull User user) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM "%positions_table%" + WHERE "%positions_table%"."id" IN ( + SELECT "position_id" + FROM "%saved_positions_table%" + WHERE "%saved_positions_table%"."id" IN ( + SELECT "saved_position_id" + FROM "%homes_table%" + WHERE "owner_uuid"=? + ) + );"""))) { + + statement.setString(1, user.getUuid().toString()); + return statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to delete all homes for " + user.getUsername() + " from the database", e); + } + return 0; + } + + @Override + public int deleteAllHomes(@NotNull String worldName, @NotNull String serverName) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM "%positions_table%" + WHERE "%positions_table%"."id" IN ( + SELECT "position_id" + FROM "%saved_positions_table%" + WHERE "%saved_positions_table%"."id" IN ( + SELECT "saved_position_id" + FROM "%homes_table%" + WHERE "world_name"=? + AND "server_name"=? + ) + );"""))) { + statement.setString(1, worldName); + statement.setString(2, serverName); + + return statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to delete homes in the world " + worldName + " on the server " + + serverName + " from the database", e); + } + return 0; + } + + @Override + public void deleteWarp(@NotNull UUID uuid) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM "%positions_table%" + WHERE "%positions_table%"."id"=( + SELECT "position_id" + FROM "%saved_positions_table%" + WHERE "%saved_positions_table%"."id"=( + SELECT "saved_position_id" + FROM "%warps_table%" + WHERE "uuid"=? + ) + );"""))) { + statement.setString(1, uuid.toString()); + + statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to delete a warp from the database", e); + } + } + + @Override + public int deleteAllWarps() { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM "%positions_table%" + WHERE "%positions_table%"."id" IN ( + SELECT "position_id" + FROM "%saved_positions_table%" + WHERE "%saved_positions_table%"."id" IN ( + SELECT "saved_position_id" + FROM "%warps_table%" + ) + );"""))) { + return statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to delete all warps from the database", e); + } + return 0; + } + + @Override + public int deleteAllWarps(@NotNull String worldName, @NotNull String serverName) { + try (Connection connection = getConnection()) { + try (PreparedStatement statement = connection.prepareStatement(formatStatementTables(""" + DELETE FROM "%positions_table%" + WHERE "%positions_table%"."id" IN ( + SELECT "position_id" + FROM "%saved_positions_table%" + WHERE "%saved_positions_table%"."id" IN ( + SELECT "saved_position_id" + FROM "%warps_table%" + WHERE "world_name"=? + AND "server_name"=? + ) + );"""))) { + statement.setString(1, worldName); + statement.setString(2, serverName); + + return statement.executeUpdate(); + } + } catch (SQLException e) { + plugin.log(Level.SEVERE, "Failed to delete warps in the world " + worldName + " on the server " + + serverName + " from the database", e); + } + return 0; + } + + @Override + public void terminate() { + if (dataSource != null) { + if (!dataSource.isClosed()) { + dataSource.close(); + } + } + } + +} diff --git a/common/src/main/resources/database/postgresql_schema.sql b/common/src/main/resources/database/postgresql_schema.sql new file mode 100644 index 00000000..ee62e140 --- /dev/null +++ b/common/src/main/resources/database/postgresql_schema.sql @@ -0,0 +1,94 @@ +/* Create the positions table if it does not exist */ +CREATE TABLE IF NOT EXISTS "%positions_table%" +( + "id" SERIAL, + "x" double precision NOT NULL, + "y" double precision NOT NULL, + "z" double precision NOT NULL, + "yaw" real NOT NULL, + "pitch" real NOT NULL, + "world_name" varchar(255) NOT NULL, + "world_uuid" char(36) NOT NULL, + "server_name" varchar(255) NOT NULL, + + PRIMARY KEY ("id") +); + +/* Create the players table if it does not exist */ +CREATE TABLE IF NOT EXISTS "%players_table%" +( + "uuid" char(36) NOT NULL UNIQUE, + "username" varchar(16) NOT NULL, + "last_position" integer DEFAULT NULL, + "offline_position" integer DEFAULT NULL, + "respawn_position" integer DEFAULT NULL, + "home_slots" integer NOT NULL DEFAULT 0, + "ignoring_requests" boolean NOT NULL DEFAULT FALSE, + + PRIMARY KEY ("uuid"), + FOREIGN KEY ("last_position") REFERENCES "%positions_table%" ("id") ON DELETE SET NULL ON UPDATE NO ACTION, + FOREIGN KEY ("offline_position") REFERENCES "%positions_table%" ("id") ON DELETE SET NULL ON UPDATE NO ACTION, + FOREIGN KEY ("respawn_position") REFERENCES "%positions_table%" ("id") ON DELETE SET NULL ON UPDATE NO ACTION +); + +/* Create the cooldowns table if it does not exist */ +CREATE TABLE IF NOT EXISTS "%cooldowns_table%" +( + "id" SERIAL, + "player_uuid" char(36) NOT NULL, + "type" varchar(255) NOT NULL, + "start_timestamp" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + "end_timestamp" timestamp NOT NULL, + + PRIMARY KEY ("id"), + FOREIGN KEY ("player_uuid") REFERENCES "%players_table%" ("uuid") ON DELETE CASCADE ON UPDATE CASCADE +); + +/* Create the current cross-server teleports table if it does not exist */ +CREATE TABLE IF NOT EXISTS "%teleports_table%" +( + "player_uuid" char(36) NOT NULL UNIQUE, + "destination_id" integer NOT NULL, + "type" integer NOT NULL DEFAULT 0, + + PRIMARY KEY ("player_uuid"), + FOREIGN KEY ("player_uuid") REFERENCES "%players_table%" ("uuid") ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY ("destination_id") REFERENCES "%positions_table%" ("id") ON DELETE CASCADE ON UPDATE NO ACTION +); + +/* Create the saved positions table if it does not exist */ +CREATE TABLE IF NOT EXISTS "%saved_positions_table%" +( + "id" SERIAL, + "position_id" integer NOT NULL, + "name" varchar(64) NOT NULL, + "description" varchar(255) NOT NULL, + "tags" text DEFAULT NULL, + "timestamp" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + + PRIMARY KEY ("id"), + FOREIGN KEY ("position_id") REFERENCES "%positions_table%" ("id") ON DELETE CASCADE ON UPDATE NO ACTION +); + +/* Create the homes table if it does not exist */ +CREATE TABLE IF NOT EXISTS "%homes_table%" +( + "uuid" char(36) NOT NULL UNIQUE, + "saved_position_id" integer NOT NULL, + "owner_uuid" char(36) NOT NULL, + "public" boolean NOT NULL DEFAULT FALSE, + + PRIMARY KEY ("uuid"), + FOREIGN KEY ("owner_uuid") REFERENCES "%players_table%" ("uuid") ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY ("saved_position_id") REFERENCES "%saved_positions_table%" ("id") ON DELETE CASCADE ON UPDATE NO ACTION +); + +/* Create the warps table if it does not exist */ +CREATE TABLE IF NOT EXISTS "%warps_table%" +( + "uuid" char(36) NOT NULL UNIQUE, + "saved_position_id" integer NOT NULL, + + PRIMARY KEY ("uuid"), + FOREIGN KEY ("saved_position_id") REFERENCES "%saved_positions_table%" ("id") ON DELETE CASCADE ON UPDATE NO ACTION +); \ No newline at end of file diff --git a/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java b/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java index 77f83267..7c9675dc 100644 --- a/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java +++ b/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java @@ -46,10 +46,7 @@ import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Server; import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.config.Spawn; -import net.william278.huskhomes.database.Database; -import net.william278.huskhomes.database.H2Database; -import net.william278.huskhomes.database.MySqlDatabase; -import net.william278.huskhomes.database.SqLiteDatabase; +import net.william278.huskhomes.database.*; import net.william278.huskhomes.event.FabricEventDispatcher; import net.william278.huskhomes.hook.FabricImpactorEconomyHook; import net.william278.huskhomes.hook.FabricPlaceholderAPIHook; @@ -147,6 +144,7 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes case MYSQL, MARIADB -> new MySqlDatabase(this); case SQLITE -> new SqLiteDatabase(this); case H2 -> new H2Database(this); + case POSTGRESQL -> new PostgreSqlDatabase(this); }; database.initialize(); diff --git a/gradle.properties b/gradle.properties index c09e4dbe..041f495c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,6 +14,7 @@ mysql_driver_version=8.3.0 mariadb_driver_version=3.3.3 sqlite_driver_version=3.45.2.0 h2_driver_version=2.2.220 +postgresql_driver_version=42.7.3 commons_pool_version=2.12.0 # Fabric settings diff --git a/paper/src/main/resources/paper-libraries.yml b/paper/src/main/resources/paper-libraries.yml index 8e96fb14..8d461d1f 100644 --- a/paper/src/main/resources/paper-libraries.yml +++ b/paper/src/main/resources/paper-libraries.yml @@ -4,4 +4,5 @@ libraries: - 'com.mysql:mysql-connector-j:${mysql_driver_version}' - 'org.mariadb.jdbc:mariadb-java-client:${mariadb_driver_version}' - 'org.xerial:sqlite-jdbc:${sqlite_driver_version}' - - 'com.h2database:h2:${h2_driver_version}' \ No newline at end of file + - 'com.h2database:h2:${h2_driver_version}' + - 'org.postgresql:postgresql:${postgresql_driver_version}' \ No newline at end of file diff --git a/sponge/src/main/java/net/william278/huskhomes/SpongeHuskHomes.java b/sponge/src/main/java/net/william278/huskhomes/SpongeHuskHomes.java index b1efd5f1..a228348f 100644 --- a/sponge/src/main/java/net/william278/huskhomes/SpongeHuskHomes.java +++ b/sponge/src/main/java/net/william278/huskhomes/SpongeHuskHomes.java @@ -33,10 +33,7 @@ import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Server; import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.config.Spawn; -import net.william278.huskhomes.database.Database; -import net.william278.huskhomes.database.H2Database; -import net.william278.huskhomes.database.MySqlDatabase; -import net.william278.huskhomes.database.SqLiteDatabase; +import net.william278.huskhomes.database.*; import net.william278.huskhomes.event.SpongeEventDispatcher; import net.william278.huskhomes.hook.Hook; import net.william278.huskhomes.hook.SpongeEconomyHook; @@ -141,6 +138,7 @@ public class SpongeHuskHomes implements HuskHomes, SpongeTask.Supplier, SpongeSa case MYSQL, MARIADB -> new MySqlDatabase(this); case SQLITE -> new SqLiteDatabase(this); case H2 -> new H2Database(this); + case POSTGRESQL -> new PostgreSqlDatabase(this); }; database.initialize();