Add handling for TeleportExceptions thrown when completing TimedTeleports

This commit is contained in:
William committed 2023-05-05 23:48:59 +01:00
1 parent daa72182f8
commit 28ab6d4a87
5 files changed
+31 -20

No files matched your search

@@ -23,6 +23,7 @@ import com.zaxxer.hikari.HikariDataSource;
import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.position.*; import net.william278.huskhomes.position.*;
import net.william278.huskhomes.teleport.Teleport; import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.teleport.TeleportationException;
import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.SavedUser; import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.user.User; import net.william278.huskhomes.user.User;
@@ -592,6 +593,8 @@ public class MySqlDatabase extends Database {
} }
} catch (SQLException e) { } catch (SQLException e) {
plugin.log(Level.SEVERE, "Failed to query the current teleport of " + onlineUser.getUsername(), e); plugin.log(Level.SEVERE, "Failed to query the current teleport of " + onlineUser.getUsername(), e);
} catch (TeleportationException e) {
e.displayMessage(onlineUser, plugin);
} }
return Optional.empty(); return Optional.empty();
} }
@@ -22,6 +22,7 @@ package net.william278.huskhomes.database;
import net.william278.huskhomes.HuskHomes; import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.position.*; import net.william278.huskhomes.position.*;
import net.william278.huskhomes.teleport.Teleport; import net.william278.huskhomes.teleport.Teleport;
import net.william278.huskhomes.teleport.TeleportationException;
import net.william278.huskhomes.user.OnlineUser; import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.SavedUser; import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.user.User; import net.william278.huskhomes.user.User;
@@ -108,7 +109,7 @@ public class SqLiteDatabase extends Database {
// Prepare database schema; make tables if they don't exist // Prepare database schema; make tables if they don't exist
try { try {
// Load database schema CREATE statements from schema file // Load the database schema CREATE statements from schema file
final String[] databaseSchema = getSchemaStatements("database/sqlite_schema.sql"); final String[] databaseSchema = getSchemaStatements("database/sqlite_schema.sql");
try (Statement statement = getConnection().createStatement()) { try (Statement statement = getConnection().createStatement()) {
for (String tableCreationStatement : databaseSchema) { for (String tableCreationStatement : databaseSchema) {
@@ -432,13 +433,13 @@ public class SqLiteDatabase extends Database {
public Optional<Home> getHome(@NotNull User user, @NotNull String homeName, boolean caseInsensitive) { public Optional<Home> getHome(@NotNull User user, @NotNull String homeName, boolean caseInsensitive) {
try { try {
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().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` 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%` FROM `%homes_table%`
INNER JOIN `%saved_positions_table%` ON `%homes_table%`.`saved_position_id`=`%saved_positions_table%`.`id` 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 `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id`
INNER JOIN `%players_table%` ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid` INNER JOIN `%players_table%` ON `%homes_table%`.`owner_uuid`=`%players_table%`.`uuid`
WHERE `owner_uuid`=? WHERE `owner_uuid`=?
""" + (caseInsensitive ? "AND UPPER(`name`) LIKE UPPER(?);" : "AND `name`=?;")))) { """ + (caseInsensitive ? "AND UPPER(`name`) LIKE UPPER(?);" : "AND `name`=?;")))) {
statement.setString(1, user.getUuid().toString()); statement.setString(1, user.getUuid().toString());
statement.setString(2, homeName); statement.setString(2, homeName);
@@ -509,11 +510,11 @@ public class SqLiteDatabase extends Database {
public Optional<Warp> getWarp(@NotNull String warpName, boolean caseInsensitive) { public Optional<Warp> getWarp(@NotNull String warpName, boolean caseInsensitive) {
try { try {
try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables(""" try (PreparedStatement statement = getConnection().prepareStatement(formatStatementTables("""
SELECT `%warps_table%`.`uuid` AS `warp_uuid`, `name`, `description`, `tags`, `timestamp`, `x`, `y`, `z`, `yaw`, `pitch`, `world_name`, `world_uuid`, `server_name` 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%` FROM `%warps_table%`
INNER JOIN `%saved_positions_table%` ON `%warps_table%`.`saved_position_id`=`%saved_positions_table%`.`id` 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` INNER JOIN `%positions_table%` ON `%saved_positions_table%`.`position_id`=`%positions_table%`.`id`
""" + (caseInsensitive ? "WHERE UPPER(`name`) LIKE UPPER(?);" : "WHERE `name`=?;")))) { """ + (caseInsensitive ? "WHERE UPPER(`name`) LIKE UPPER(?);" : "WHERE `name`=?;")))) {
statement.setString(1, warpName); statement.setString(1, warpName);
final ResultSet resultSet = statement.executeQuery(); final ResultSet resultSet = statement.executeQuery();
@@ -603,6 +604,8 @@ public class SqLiteDatabase extends Database {
} catch (SQLException e) { } catch (SQLException e) {
plugin.log(Level.SEVERE, "Failed to query the current teleport of " + onlineUser.getUsername(), e); plugin.log(Level.SEVERE, "Failed to query the current teleport of " + onlineUser.getUsername(), e);
} catch (TeleportationException e) {
e.displayMessage(onlineUser, plugin);
} }
return Optional.empty(); return Optional.empty();
} }
@@ -43,8 +43,8 @@ public class TeleportBuilder {
} }
@NotNull @NotNull
public Teleport toTeleport() throws IllegalStateException { public Teleport toTeleport() throws TeleportationException {
validateTeleport(); this.validateTeleport();
return new Teleport(executor, teleporter, target, type, updateLastPosition, economyActions, plugin); return new Teleport(executor, teleporter, target, type, updateLastPosition, economyActions, plugin);
} }
@@ -23,7 +23,7 @@ import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.user.CommandUser; import net.william278.huskhomes.user.CommandUser;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class TeleportationException extends IllegalArgumentException { public class TeleportationException extends IllegalStateException {
public TeleportationException(@NotNull Type error) { public TeleportationException(@NotNull Type error) {
super("Error during teleport operation: " + error.name()); super("Error during teleport operation: " + error.name());
@@ -40,7 +40,7 @@ public class TeleportationException extends IllegalArgumentException {
CANNOT_TELEPORT_TO_SELF CANNOT_TELEPORT_TO_SELF
} }
public void displayMessage(@NotNull CommandUser user, @NotNull HuskHomes plugin, @NotNull String[] args) { public void displayMessage(@NotNull CommandUser user, @NotNull HuskHomes plugin, @NotNull String... args) {
switch (Type.valueOf(getMessage().split(": ")[1])) { switch (Type.valueOf(getMessage().split(": ")[1])) {
case TELEPORTER_NOT_FOUND, TARGET_NOT_FOUND -> plugin.getLocales() case TELEPORTER_NOT_FOUND, TARGET_NOT_FOUND -> plugin.getLocales()
.getLocale("error_player_not_found", args) .getLocale("error_player_not_found", args)
@@ -33,6 +33,7 @@ import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Represents a {@link Teleport} that has an associated warmup time; the teleport will not be performed until the * Represents a {@link Teleport} that has an associated warmup time; the teleport will not be performed until the
* warmup time has elapsed, during which the user must not move or take damage. * warmup time has elapsed, during which the user must not move or take damage.
*
* @see Teleport#builder(HuskHomes) * @see Teleport#builder(HuskHomes)
*/ */
public class TimedTeleport extends Teleport { public class TimedTeleport extends Teleport {
@@ -88,7 +89,7 @@ public class TimedTeleport extends Teleport {
// Run the warmup // Run the warmup
final AtomicInteger warmupTaskId = new AtomicInteger(); final AtomicInteger warmupTaskId = new AtomicInteger();
final Runnable countdownRunnable = (() -> { final Runnable countdownRunnable = (() -> {
// Display countdown action bar message // Display a countdown action bar message
if (timeLeft > 0) { if (timeLeft > 0) {
plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_WARMUP) plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_WARMUP)
.ifPresent(teleporter::playSound); .ifPresent(teleporter::playSound);
@@ -97,7 +98,11 @@ public class TimedTeleport extends Teleport {
} else { } else {
plugin.getLocales().getLocale("teleporting_action_bar_processing") plugin.getLocales().getLocale("teleporting_action_bar_processing")
.ifPresent(this::sendStatusMessage); .ifPresent(this::sendStatusMessage);
super.execute(); try {
super.execute();
} catch (TeleportationException e) {
e.displayMessage(teleporter, plugin);
}
} }
// Tick (decrement) the timed teleport timer and end it if done // Tick (decrement) the timed teleport timer and end it if done
@@ -113,7 +118,7 @@ public class TimedTeleport extends Teleport {
/** /**
* Ticks a timed teleport, decrementing the time left until the teleport is complete * Ticks a timed teleport, decrementing the time left until the teleport is complete
* <p> * <p>
* A timed teleport will be cancelled if certain criteria are met: * A timed teleport will be canceled if certain criteria are met:
* <ul> * <ul>
* <li>The player has left the server</li> * <li>The player has left the server</li>
* <li>The plugin is disabling</li> * <li>The plugin is disabling</li>