Improves RTP safety checking system, closing #11

This commit is contained in:
William278 committed 2021-02-06 16:38:04 +00:00
1 parent 4135664a60
commit 430528783a
3 files changed
+57 -20

No files matched your search

@@ -1,6 +1,7 @@
package me.william278.huskhomes2.Objects; package me.william278.huskhomes2.Objects;
import me.william278.huskhomes2.HuskHomes; import me.william278.huskhomes2.HuskHomes;
import me.william278.huskhomes2.messageManager;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
@@ -12,10 +13,13 @@ import java.util.Random;
public class RandomPoint extends TeleportationPoint { public class RandomPoint extends TeleportationPoint {
//List of all the unsafe blocks // List containing all unsafe blocks
final static HashSet<Material> unsafeBlocks = new HashSet<>(); final static HashSet<Material> 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.LAVA);
unsafeBlocks.add(Material.FIRE); unsafeBlocks.add(Material.FIRE);
unsafeBlocks.add(Material.CACTUS); unsafeBlocks.add(Material.CACTUS);
@@ -26,62 +30,93 @@ public class RandomPoint extends TeleportationPoint {
} }
private Location randomLocation(World world) { private Location randomLocation(World world) {
//Generate a random location // Generate a random location
Random random = new Random(); Random random = new Random();
int x; int x;
int y = 64;
int z; int z;
int y;
int negativeX; int negativeX;
int negativeZ; int negativeZ;
x = random.nextInt(HuskHomes.settings.getRtpRange()); // The furthest distance at which a player can be teleported to
z = random.nextInt(HuskHomes.settings.getRtpRange()); 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); negativeX = random.nextInt(2);
negativeZ = random.nextInt(2); negativeZ = random.nextInt(2);
if (negativeX == 1) { if (negativeX == 1) {
x = x * -1; x = x * -1;
} }
if (negativeZ == 1) if (negativeZ == 1) {
z = z * -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); Location randomLocation = new Location(world, x, y, z);
y = randomLocation.getWorld().getHighestBlockYAt(randomLocation); y = world.getHighestBlockYAt(randomLocation) + 1;
randomLocation.setY(y); randomLocation.setY(y);
return randomLocation; return randomLocation;
} }
// Checks to see if the location is safe to teleport to
private boolean isLocationSafe(Location location){ private boolean isLocationSafe(Location location){
int x = location.getBlockX(); int x = location.getBlockX();
int y = location.getBlockY(); int y = location.getBlockY()-1;
int z = location.getBlockZ(); int z = location.getBlockZ();
//Get instances of the blocks around where the player would spawn World world = location.getWorld();
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);
//Check to see if the surroundings are safe or not // Failsafe check in case the world is null
return !(unsafeBlocks.contains(below.getType())) || (block.getType().isSolid()) || (above.getType().isSolid()); 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) { private Location getRandomLocation(World world) {
Location randomLocation = randomLocation(world); Location randomLocation = randomLocation(world);
int attempts = 0;
// Keep looking for a random location that is safe
while (!isLocationSafe(randomLocation)){ while (!isLocationSafe(randomLocation)){
//Keep looking for a safe location // Failsafe timeout in case the plugin can't find a safe location within a number of tries.
randomLocation = randomLocation(world); if (attempts < maxRandomAttempts) {
randomLocation = randomLocation(world);
} else {
return null;
}
attempts = attempts + 1;
} }
return randomLocation; return randomLocation;
} }
public RandomPoint(Player player) { public RandomPoint(Player player) {
super(player.getLocation(), HuskHomes.settings.getServerID()); 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");
}
} }
} }
+1
View File
@@ -72,6 +72,7 @@ rtp_spent_money: '&7Spent %1% to randomly teleport.'
error_command_disabled: '&4Error: &cThat command is disabled!' error_command_disabled: '&4Error: &cThat command is disabled!'
error_console_only: '&4Error: &cThat command can only be run from the server console.' 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_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:' 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' 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.' edit_warp_update_location: '&aRe-located the warp &l%1% &r&ato your current position.'
+1
View File
@@ -59,6 +59,7 @@ tpahere_request_ask: '&a%1% 請求將你傳送至他所在的地方'
error_tp_self: '&4錯誤: &c你不能傳送到自己的位置!' error_tp_self: '&4錯誤: &c你不能傳送到自己的位置!'
error_rtp_invalid_dimension: '&4錯誤: &c你只能在目前在世界裏隨機傳送!' error_rtp_invalid_dimension: '&4錯誤: &c你只能在目前在世界裏隨機傳送!'
error_rtp_cooldown: '&4錯誤: &c你需要在下次隨機傳送前等待 %1% 分鐘.' 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% 以隨機傳送.' rtp_spent_money: '&7使用 %1% 以隨機傳送.'
error_command_disabled: '&4錯誤: &c此指令已被關閉!' error_command_disabled: '&4錯誤: &c此指令已被關閉!'
error_console_only: '&4錯誤: &c該指令只能被伺服器使用.' error_console_only: '&4錯誤: &c該指令只能被伺服器使用.'