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
+18 -7

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) {
@@ -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);
try {
super.execute(); 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>