From 010040cf51d6d62f4ea668a2771fa02c8cc70ea2 Mon Sep 17 00:00:00 2001 From: William Date: Sat, 29 Jan 2022 03:41:42 +0000 Subject: [PATCH] Add additional safety checks to prevent players teleporting out of bounds, fix wrongly caught IllegalArgumentException preventing the invalid on arrival error from showing, Fix #133 --- .../huskhomes2/commands/TpCommand.java | 1 + .../huskhomes2/teleport/TeleportManager.java | 18 +++++++++++++----- .../teleport/points/TeleportationPoint.java | 2 +- src/main/resources/languages/en-gb.yml | 1 + src/main/resources/languages/it-it.yml | 1 + src/main/resources/languages/pl.yml | 1 + src/main/resources/languages/zh-cn.yml | 1 + src/main/resources/languages/zh-tw.yml | 1 + 8 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/java/me/william278/huskhomes2/commands/TpCommand.java b/src/main/java/me/william278/huskhomes2/commands/TpCommand.java index e427b934..d3b20d03 100644 --- a/src/main/java/me/william278/huskhomes2/commands/TpCommand.java +++ b/src/main/java/me/william278/huskhomes2/commands/TpCommand.java @@ -233,6 +233,7 @@ public class TpCommand extends CommandBase { (yaw != null ? yaw : player.getLocation().getYaw()), (pitch != null ? pitch : player.getLocation().getPitch()), serverId != null || HuskHomes.getSettings().doBungee() ? serverId : HuskHomes.getSettings().getServerID()); + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { TeleportManager.teleportPlayer(player, teleportationPoint); if (sender instanceof Player sendingPlayer) { diff --git a/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java b/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java index 1d86d32d..a9f2a7d0 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java +++ b/src/main/java/me/william278/huskhomes2/teleport/TeleportManager.java @@ -46,12 +46,20 @@ public class TeleportManager { } // Teleport a player to a location on the server or proxy server network - public static void teleportPlayer(Player player, TeleportationPoint point) { - final TeleportationPoint destination = new TeleportationPoint(player.getLocation(), + public static void teleportPlayer(Player player, TeleportationPoint targetPoint) { + final TeleportationPoint currentPosition = new TeleportationPoint(player.getLocation(), HuskHomes.getSettings().getServerID()); + + // Safety checks + if (Math.abs(targetPoint.getX()) > 30000000 || Math.abs(targetPoint.getZ()) > 30000000 || Math.abs(targetPoint.getY()) > 30000000) { + MessageManager.sendMessage(player, "error_invalid_coordinates"); + return; + } + + // Execute teleport try (Connection connection = HuskHomes.getConnection()) { - DataManager.setPlayerLastPosition(player, destination, connection); - DataManager.setPlayerDestinationLocation(player, point, connection); + DataManager.setPlayerLastPosition(player, currentPosition, connection); + DataManager.setPlayerDestinationLocation(player, targetPoint, connection); teleportPlayer(player); } catch (SQLException e) { plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred teleporting a player.", e); @@ -108,7 +116,7 @@ public class TeleportManager { } } catch (SQLException sqlException) { plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred teleporting a player", sqlException); - } catch (IllegalArgumentException illegalArgumentException) { + } catch (IllegalStateException illegalStateException) { MessageManager.sendMessage(player, "error_invalid_on_arrival"); } }); diff --git a/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java b/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java index 360cb61a..346d66c0 100644 --- a/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java +++ b/src/main/java/me/william278/huskhomes2/teleport/points/TeleportationPoint.java @@ -82,7 +82,7 @@ public class TeleportationPoint { if (world != null) { return new Location(world, x, y, z, yaw, pitch); } else { - throw new IllegalStateException("The world \"" + worldName + "\" could not be found on the server; could not return location"); + throw new IllegalStateException(worldName + " is not a valid world on the server; failed to return location"); } } diff --git a/src/main/resources/languages/en-gb.yml b/src/main/resources/languages/en-gb.yml index aecc3cbc..b5962741 100644 --- a/src/main/resources/languages/en-gb.yml +++ b/src/main/resources/languages/en-gb.yml @@ -123,6 +123,7 @@ error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a ma tpaall_request_sent: '[You sent a teleport request asking everyone to teleport to you!](#00fb9a)' error_no_other_online_players: '[Error:](#ff3300) [There are no other online players who could teleport to you.](#ff7e5e)' teleporting_all_players: '[Teleporting all players to your position...](#00fb9a)' +error_illegal_target_coordinates: '[Error:](#ff3300) [Aborted teleport as it exceeds the possible game world limits.](#ff7e5e)' home_command_description: 'Teleport to one of your homes' sethome_command_description: 'Set a new home with given name' delhome_command_description: 'Delete a home you previously set' diff --git a/src/main/resources/languages/it-it.yml b/src/main/resources/languages/it-it.yml index aaccca42..43c035a8 100644 --- a/src/main/resources/languages/it-it.yml +++ b/src/main/resources/languages/it-it.yml @@ -123,6 +123,7 @@ error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a ma tpaall_request_sent: '[You sent a teleport request asking everyone to teleport to you!](#00fb9a)' error_no_other_online_players: '[Error:](#ff3300) [There are no other online players who could teleport to you.](#ff7e5e)' teleporting_all_players: '[Teleporting all players to your position...](#00fb9a)' +error_illegal_target_coordinates: '[Error:](#ff3300) [Aborted teleport as it exceeds the possible game world limits.](#ff7e5e)' home_command_description: 'Teleport to one of your homes' sethome_command_description: 'Set a new home with given name' delhome_command_description: 'Delete a home you previously set' diff --git a/src/main/resources/languages/pl.yml b/src/main/resources/languages/pl.yml index c9a8c301..e15d14d4 100644 --- a/src/main/resources/languages/pl.yml +++ b/src/main/resources/languages/pl.yml @@ -123,6 +123,7 @@ error_edit_home_maximum_public_homes: '[Błąd:](#ff3300) [Możesz maksymalnie u tpaall_request_sent: '[You sent a teleport request asking everyone to teleport to you!](#00fb9a)' error_no_other_online_players: '[Error:](#ff3300) [There are no other online players who could teleport to you.](#ff7e5e)' teleporting_all_players: '[Teleporting all players to your position...](#00fb9a)' +error_illegal_target_coordinates: '[Error:](#ff3300) [Aborted teleport as it exceeds the possible game world limits.](#ff7e5e)' home_command_description: 'Teleport to one of your homes' sethome_command_description: 'Set a new home with given name' delhome_command_description: 'Delete a home you previously set' diff --git a/src/main/resources/languages/zh-cn.yml b/src/main/resources/languages/zh-cn.yml index a2ccac5d..90447f72 100644 --- a/src/main/resources/languages/zh-cn.yml +++ b/src/main/resources/languages/zh-cn.yml @@ -123,6 +123,7 @@ error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a ma tpaall_request_sent: '[You sent a teleport request asking everyone to teleport to you!](#00fb9a)' error_no_other_online_players: '[Error:](#ff3300) [There are no other online players who could teleport to you.](#ff7e5e)' teleporting_all_players: '[Teleporting all players to your position...](#00fb9a)' +error_illegal_target_coordinates: '[Error:](#ff3300) [Aborted teleport as it exceeds the possible game world limits.](#ff7e5e)' home_command_description: 'Teleport to one of your homes' sethome_command_description: 'Set a new home with given name' delhome_command_description: 'Delete a home you previously set' diff --git a/src/main/resources/languages/zh-tw.yml b/src/main/resources/languages/zh-tw.yml index 381b183b..0ffa3e67 100644 --- a/src/main/resources/languages/zh-tw.yml +++ b/src/main/resources/languages/zh-tw.yml @@ -123,6 +123,7 @@ error_edit_home_maximum_public_homes: '[Error:](#ff3300) [You can only make a ma tpaall_request_sent: '[You sent a teleport request asking everyone to teleport to you!](#00fb9a)' error_no_other_online_players: '[Error:](#ff3300) [There are no other online players who could teleport to you.](#ff7e5e)' teleporting_all_players: '[Teleporting all players to your position...](#00fb9a)' +error_illegal_target_coordinates: '[Error:](#ff3300) [Aborted teleport as it exceeds the possible game world limits.](#ff7e5e)' home_command_description: 'Teleport to one of your homes' sethome_command_description: 'Set a new home with given name' delhome_command_description: 'Delete a home you previously set'