From 430528783af5ee852df4ce5ed46b9abf5d56e887 Mon Sep 17 00:00:00 2001 From: William278 Date: Sat, 6 Feb 2021 16:38:04 +0000 Subject: [PATCH] Improves RTP safety checking system, closing #11 --- .../huskhomes2/Objects/RandomPoint.java | 75 ++++++++++++++----- src/main/resources/Languages/en-gb.yml | 1 + src/main/resources/Languages/zh-tw.yml | 1 + 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/main/java/me/william278/huskhomes2/Objects/RandomPoint.java b/src/main/java/me/william278/huskhomes2/Objects/RandomPoint.java index 6d928ce7..7e8322bd 100644 --- a/src/main/java/me/william278/huskhomes2/Objects/RandomPoint.java +++ b/src/main/java/me/william278/huskhomes2/Objects/RandomPoint.java @@ -1,6 +1,7 @@ package me.william278.huskhomes2.Objects; import me.william278.huskhomes2.HuskHomes; +import me.william278.huskhomes2.messageManager; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -12,10 +13,13 @@ import java.util.Random; public class RandomPoint extends TeleportationPoint { - //List of all the unsafe blocks + // List containing all unsafe blocks final static HashSet unsafeBlocks = new HashSet<>(); - static{ + // Maximum number of attempts to find a random location + final static int maxRandomAttempts = 8; + + static { unsafeBlocks.add(Material.LAVA); unsafeBlocks.add(Material.FIRE); unsafeBlocks.add(Material.CACTUS); @@ -26,62 +30,93 @@ public class RandomPoint extends TeleportationPoint { } private Location randomLocation(World world) { - //Generate a random location + // Generate a random location Random random = new Random(); int x; + int y = 64; int z; - int y; int negativeX; int negativeZ; - x = random.nextInt(HuskHomes.settings.getRtpRange()); - z = random.nextInt(HuskHomes.settings.getRtpRange()); + // The furthest distance at which a player can be teleported to + int rtpRange = HuskHomes.settings.getRtpRange(); + + // Calculate random X and Z coords + x = random.nextInt(rtpRange); + z = random.nextInt(rtpRange); + + // Determine if the plugin should teleport to positive or negative Z negativeX = random.nextInt(2); negativeZ = random.nextInt(2); - if (negativeX == 1) { x = x * -1; } - if (negativeZ == 1) + if (negativeZ == 1) { z = z * -1; - y = 150; + } + // Put together random location, get the highest block plus one to determine Y Location randomLocation = new Location(world, x, y, z); - y = randomLocation.getWorld().getHighestBlockYAt(randomLocation); + y = world.getHighestBlockYAt(randomLocation) + 1; randomLocation.setY(y); return randomLocation; } + // Checks to see if the location is safe to teleport to private boolean isLocationSafe(Location location){ int x = location.getBlockX(); - int y = location.getBlockY(); + int y = location.getBlockY()-1; int z = location.getBlockZ(); - //Get instances of the blocks around where the player would spawn - Block block = location.getWorld().getBlockAt(x, y, z); - Block below = location.getWorld().getBlockAt(x, y - 1, z); - Block above = location.getWorld().getBlockAt(x, y + 1, z); + World world = location.getWorld(); - //Check to see if the surroundings are safe or not - return !(unsafeBlocks.contains(below.getType())) || (block.getType().isSolid()) || (above.getType().isSolid()); + // Failsafe check in case the world is null + if (world != null) { + + // Get instances of the blocks around where the player would spawn + Block block = world.getBlockAt(x, y, z); + Block below = world.getBlockAt(x, y - 1, z); + Block above = world.getBlockAt(x, y + 1, z); + + // Check to see if those blocks are safe to teleport to + if (!unsafeBlocks.contains(block.getType()) && block.getType().isSolid()) { + if (!unsafeBlocks.contains(above.getType())) { + return !unsafeBlocks.contains(below.getType()); + } + } + } + return false; } private Location getRandomLocation(World world) { Location randomLocation = randomLocation(world); + int attempts = 0; + + // Keep looking for a random location that is safe while (!isLocationSafe(randomLocation)){ - //Keep looking for a safe location - randomLocation = randomLocation(world); + // Failsafe timeout in case the plugin can't find a safe location within a number of tries. + if (attempts < maxRandomAttempts) { + randomLocation = randomLocation(world); + } else { + return null; + } + attempts = attempts + 1; } return randomLocation; } public RandomPoint(Player player) { super(player.getLocation(), HuskHomes.settings.getServerID()); - setLocation(getRandomLocation(player.getWorld()), HuskHomes.settings.getServerID()); + Location randomLocation = getRandomLocation(player.getWorld()); + if (randomLocation != null) { + setLocation(randomLocation, HuskHomes.settings.getServerID()); + } else { + messageManager.sendMessage(player, "error_rtp_randomization_timeout"); + } } } diff --git a/src/main/resources/Languages/en-gb.yml b/src/main/resources/Languages/en-gb.yml index f8b2c328..2a16e1d7 100644 --- a/src/main/resources/Languages/en-gb.yml +++ b/src/main/resources/Languages/en-gb.yml @@ -72,6 +72,7 @@ rtp_spent_money: '&7Spent %1% to randomly teleport.' error_command_disabled: '&4Error: &cThat command is disabled!' error_console_only: '&4Error: &cThat command can only be run from the server console.' error_tp_invalid_world: '&4Error: &cPlease specify a valid server world.' +error_rtp_randomization_timeout: '&4Error: &cFailed to find a safe random location to teleport you.' edit_warp_title: '&aEdit details for the warp &l%1%&r&a:' error_edit_warp_invalid_description: '&4Error: &cWarp descriptions may only contain alphanumeric (A-Z, 0-9) characters, hyphens (-) and underscores (_) and must be less than 255 characters in length' edit_warp_update_location: '&aRe-located the warp &l%1% &r&ato your current position.' diff --git a/src/main/resources/Languages/zh-tw.yml b/src/main/resources/Languages/zh-tw.yml index 0e614a6d..06432ac9 100644 --- a/src/main/resources/Languages/zh-tw.yml +++ b/src/main/resources/Languages/zh-tw.yml @@ -59,6 +59,7 @@ tpahere_request_ask: '&a%1% 請求將你傳送至他所在的地方' error_tp_self: '&4錯誤: &c你不能傳送到自己的位置!' error_rtp_invalid_dimension: '&4錯誤: &c你只能在目前在世界裏隨機傳送!' error_rtp_cooldown: '&4錯誤: &c你需要在下次隨機傳送前等待 %1% 分鐘.' +error_rtp_randomization_timeout: '&4Error: &cFailed to find a safe random location to teleport you.' rtp_spent_money: '&7使用 %1% 以隨機傳送.' error_command_disabled: '&4錯誤: &c此指令已被關閉!' error_console_only: '&4錯誤: &c該指令只能被伺服器使用.'