More bukkit adapter stuff, load SQL schema from file, start work on MySQL implementation, start event processing

This commit is contained in:
William committed 2022-05-22 03:03:23 +01:00
1 parent 8e7d5e74a0
commit bd52c99d4d
18 files changed
+973 -101

No files matched your search

@@ -2,25 +2,35 @@ package net.william278.huskhomes.data;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.player.Player;
import net.william278.huskhomes.position.*;
import net.william278.huskhomes.player.User;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.position.PositionMeta;
import net.william278.huskhomes.position.Warp;
import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.util.Logger;
import net.william278.huskhomes.util.ResourceReader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
/**
* An abstract representation of the plugin database, storing home, warp & player data.
* An abstract representation of the plugin database, storing home, warp and player data.
* <p>
* Implemented by different database platforms - MySQL, SQLite, etc. - as configured by the administrator.
*/
public abstract class Database {
/**
* Stores data about {@link Player}s
* Stores data about {@link User}s
* <ol>
* <li>uuid - Primary key, unique; the user's minecraft account unique ID</li>
* <li>username - The user's username. Checked to be updated on login</li>
@@ -84,7 +94,7 @@ public abstract class Database {
protected final String warpsTableName;
/**
* Stores data about current cross-server teleports being executed by {@link Player}s
* Stores data about current cross-server teleports being executed by {@link User}s
* <ol>
* <li>player_uuid - Primary key, unique, references player table; represents the unique id of a teleporting player</li>
* <li>destination_id - References positions table; the destination of the teleporting player</li>
@@ -92,12 +102,58 @@ public abstract class Database {
*/
protected final String teleportsTableName;
/**
* Logger instance used for database error logging
*/
private final Logger logger;
/**
* Returns the {@link Logger} used to log database errors
*
* @return the {@link Logger} instance
*/
protected Logger getLogger() {
return logger;
}
private final ResourceReader resourceReader;
/**
* Loads SQL table creation schema statements from a resource file as a string array
*
* @param schemaFileName database script resource file to load from
* @return Array of string-formatted table creation schema statements
* @throws IOException if the resource could not be read
*/
protected final String[] getSchemaStatements(@NotNull String schemaFileName) throws IOException {
return formatStatementTables(
new String(resourceReader.getResource(schemaFileName)
.readAllBytes(), StandardCharsets.UTF_8))
.split(";");
}
/**
* Format all table name placeholder strings in a SQL statement
*
* @param sql the SQL statement with unformatted table name placeholders
* @return the formatted statement, with table placeholders replaced with the correct names
*/
protected final String formatStatementTables(@NotNull String sql) {
return sql
.replaceAll("%positions_table%", positionsTableName)
.replaceAll("%players_table%", playerTableName)
.replaceAll("%teleports_table%", teleportsTableName)
.replaceAll("%position_metadata_table%", positionMetadataTableName)
.replaceAll("%homes_table%", homesTableName)
.replaceAll("%warps_table%", warpsTableName);
}
/**
* Create a database instance with specified table data {@link Settings}
*
* @param settings {@link Settings} to fetch database configuration data from
*/
protected Database(@NotNull Settings settings) {
protected Database(@NotNull Settings settings, @NotNull Logger logger, @NotNull ResourceReader resourceReader) {
this.playerTableName = settings.
getStringValue(Settings.ConfigOption.DATABASE_PLAYER_TABLE_NAME);
this.positionsTableName = settings.
@@ -110,6 +166,8 @@ public abstract class Database {
getStringValue(Settings.ConfigOption.DATABASE_WARPS_TABLE_NAME);
this.teleportsTableName = settings.
getStringValue(Settings.ConfigOption.DATABASE_TELEPORTS_TABLE_NAME);
this.logger = logger;
this.resourceReader = resourceReader;
}
/**
@@ -123,20 +181,20 @@ public abstract class Database {
* <b>(Internal use only)</b> - Sets a position to the position table in the database
*
* @param position The {@link Position} to set
* @return A future returning the inserted row ID when the operation has completed.
* @return The newly inserted row ID
*/
protected abstract CompletableFuture<Integer> setPosition(@NotNull Position position);
protected abstract int setPosition(@NotNull Position position, @NotNull Connection connection) throws SQLException;
/**
* <b>(Internal use only)</b> - Sets position meta to the position metadata table in the database
*
* @param meta The {@link PositionMeta} to set
* @return A future returning the inserted row ID when the operation has completed.
* @return The newly inserted row ID
*/
protected abstract CompletableFuture<Integer> setPositionMeta(@NotNull PositionMeta meta);
protected abstract int setPositionMeta(@NotNull PositionMeta meta, @NotNull Connection connection) throws SQLException;
/**
* Ensure a {@link Player} has an entry in the database and that their username is up-to-date
* Ensure a {@link Player} has a {@link User} 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
@@ -146,18 +204,27 @@ public abstract class Database {
/**
* Get a player by their username (<i>case-insensitive</i>)
*
* @param name Username of the {@link Player} to get (<i>case-insensitive</i>)
* @return A future returning an optional with the {@link Player} present if they exist
* @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
*/
public abstract CompletableFuture<Optional<Player>> getPlayerByName(@NotNull String name);
public abstract CompletableFuture<Optional<User>> getUserByName(@NotNull String name);
/**
* Get a list of {@link Home}s set by a {@link Player}
* Get a player by their Minecraft account {@link UUID}
*
* @param player {@link Player} to get the homes of
* @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
*/
public abstract CompletableFuture<Optional<User>> 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
* @return A future returning void when complete
*/
public abstract CompletableFuture<List<Home>> getHomes(@NotNull Player player);
public abstract CompletableFuture<List<Home>> getHomes(@NotNull User player);
/**
* Get a list of all {@link Warp}s that have been set
@@ -174,13 +241,13 @@ public abstract class Database {
public abstract CompletableFuture<List<Home>> getPublicHomes();
/**
* Get a {@link Home} set by a {@link Player}
* Get a {@link Home} set by a {@link User}
*
* @param player The {@link Player} who set the home
* @param player 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 Player player, @NotNull String homeName);
public abstract CompletableFuture<Optional<Home>> getHome(@NotNull User player, @NotNull String homeName);
/**
* Get a {@link Home} by its unique id
@@ -207,74 +274,74 @@ public abstract class Database {
public abstract CompletableFuture<Optional<Warp>> getWarp(@NotNull UUID uuid);
/**
* Get the current {@link Teleport} being executed by the specified {@link Player}
* Get the current {@link Teleport} being executed by the specified {@link User}
*
* @param player The {@link Player} to check
* @param player 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 Player player);
public abstract CompletableFuture<Optional<Teleport>> getCurrentTeleport(@NotNull User player);
/**
* Sets or clears the current {@link Teleport} being executed by a {@link Player}
* Sets or clears the current {@link Teleport} being executed by a {@link User}
*
* @param player The {@link Player} to set the current teleport of.
* @param player 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 Player player, @Nullable Teleport teleport);
public abstract CompletableFuture<Void> setCurrentTeleport(@NotNull User player, @Nullable Teleport teleport);
/**
* Get the last teleport {@link Position} of a specified {@link Player}
* Get the last teleport {@link Position} of a specified {@link User}
*
* @param player The {@link Player} to check
* @param player 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 Player player);
public abstract CompletableFuture<Optional<Position>> getLastPosition(@NotNull User player);
/**
* Sets the last teleport {@link Position} of a {@link Player}
* Sets the last teleport {@link Position} of a {@link User}
*
* @param player The {@link Player} to set the last position of
* @param player 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 Player player, @NotNull Position position);
public abstract CompletableFuture<Void> setLastPosition(@NotNull User player, @NotNull Position position);
/**
* Get the offline {@link Position} of a specified {@link Player}
* Get the offline {@link Position} of a specified {@link User}
*
* @param player The {@link Player} to check
* @param player 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 Player player);
public abstract CompletableFuture<Optional<Position>> getOfflinePosition(@NotNull User player);
/**
* Sets the offline {@link Position} of a {@link Player}
* Sets the offline {@link Position} of a {@link User}
*
* @param player The {@link Player} to set the offline position of
* @param player 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 Player player, @NotNull Position position);
public abstract CompletableFuture<Void> setOfflinePosition(@NotNull User player, @NotNull Position position);
/**
* Get the respawn {@link Position} of a specified {@link Player}
* Get the respawn {@link Position} of a specified {@link User}
*
* @param player The {@link Player} to check
* @param player 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 Player player);
public abstract CompletableFuture<Optional<Position>> getRespawnPosition(@NotNull User player);
/**
* Sets or clears the respawn {@link Position} of a {@link Player}
* Sets or clears the respawn {@link Position} of a {@link User}
*
* @param player The {@link Player} to set the respawn position of
* @param player 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 Player player, @Nullable Position position);
public abstract CompletableFuture<Void> setRespawnPosition(@NotNull User player, @Nullable Position position);
/**
* Sets or updates a {@link Home} into the home data table on the database.
@@ -308,4 +375,55 @@ public abstract class Database {
*/
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);
}