diff --git a/bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java b/bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java index b7ac17f9..f6dd6678 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java +++ b/bukkit/src/main/java/net/william278/huskhomes/user/BukkitUser.java @@ -175,4 +175,13 @@ public class BukkitUser extends OnlineUser { markedAsInvulnerable = false; } + /** + * Check if the teleporter can teleport. + * + * @return true if the teleport may complete. + */ + @Override + public boolean isValid() { + return getHealth() > 0; + } } diff --git a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java index a257debb..167510f5 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java @@ -82,6 +82,10 @@ public class Teleport implements Completable { * @throws TeleportationException if the teleport fails for some reason. */ public void execute() throws TeleportationException { + if (!teleporter.isValid()) { + throw new TeleportationException(TeleportationException.Type.TELEPORTER_INVALID, plugin); + } + validateTransactions(); resolveLocalTeleporter().ifPresentOrElse(this::executeLocal, this::executeRemote); } diff --git a/common/src/main/java/net/william278/huskhomes/teleport/Teleportable.java b/common/src/main/java/net/william278/huskhomes/teleport/Teleportable.java index af8d33fd..86dab157 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/Teleportable.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/Teleportable.java @@ -49,6 +49,13 @@ public interface Teleportable { @NotNull String getName(); + /** + * Check if the teleporter can teleport. + * + * @return true if the teleport may complete. + */ + boolean isValid(); + @NotNull @Deprecated(since = "4.8") default String getUsername() { diff --git a/common/src/main/java/net/william278/huskhomes/teleport/TeleportationException.java b/common/src/main/java/net/william278/huskhomes/teleport/TeleportationException.java index 7f727974..1ff5116d 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/TeleportationException.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/TeleportationException.java @@ -80,7 +80,8 @@ public class TeleportationException extends IllegalStateException { WARMUP_ALREADY_MOVING, WORLD_NOT_FOUND, ILLEGAL_TARGET_COORDINATES, - CANNOT_TELEPORT_TO_SELF + CANNOT_TELEPORT_TO_SELF, + TELEPORTER_INVALID } } diff --git a/common/src/main/java/net/william278/huskhomes/teleport/Username.java b/common/src/main/java/net/william278/huskhomes/teleport/Username.java index 0341b4ee..7abfca25 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/Username.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/Username.java @@ -65,4 +65,13 @@ public record Username(@NotNull String name) implements Teleportable, Target { return name; } + /** + * Check if the teleporter can teleport. + * + * @return true if the teleport may complete. + */ + @Override + public boolean isValid() { + return false; + } } diff --git a/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java b/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java index d224027b..14bcc3a9 100644 --- a/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java +++ b/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java @@ -196,4 +196,13 @@ public class FabricUser extends OnlineUser { return player; } + /** + * Check if the teleporter can teleport. + * + * @return true if the teleport may complete. + */ + @Override + public boolean isValid() { + return player.isAlive(); + } }