Simplify RandomPoint and fail-safe it

This commit is contained in:
Daniil Z committed 2021-03-01 02:04:29 +03:00
1 parent 1dbf932230
commit 98ba010298
1 file changed
+21 -40
@@ -16,24 +16,24 @@ import java.util.concurrent.ThreadLocalRandom;
public class RandomPoint extends TeleportationPoint { public class RandomPoint extends TeleportationPoint {
// List containing all unsafe blocks // List containing all unsafe blocks
private final static Set<Material> unsafeBlocks = EnumSet.noneOf(Material.class); private static final Set<Material> unsafeBlocks = EnumSet.of(
Material.LAVA, Material.FIRE, Material.MAGMA_BLOCK, Material.CACTUS, Material.WATER, Material.OBSIDIAN,
Material.JUNGLE_LEAVES, Material.SPRUCE_LEAVES, Material.OAK_LEAVES,
Material.BIRCH_LEAVES, Material.ACACIA_LEAVES, Material.DARK_OAK_LEAVES
);
// Maximum number of attempts to find a random location // Maximum number of attempts to find a random location
private final static int maxRandomAttempts = 8; private static final int MAX_RANDOM_ATTEMPTS = 8;
static { public RandomPoint(Player player) {
unsafeBlocks.add(Material.LAVA); super(player.getLocation(), HuskHomes.settings.getServerID());
unsafeBlocks.add(Material.FIRE); Location randomLocation = getRandomLocation(player.getWorld());
unsafeBlocks.add(Material.CACTUS); if (randomLocation != null) {
unsafeBlocks.add(Material.WATER); setLocation(randomLocation, HuskHomes.settings.getServerID());
unsafeBlocks.add(Material.MAGMA_BLOCK); } else {
unsafeBlocks.add(Material.JUNGLE_LEAVES); MessageManager.sendMessage(player, "error_rtp_randomization_timeout");
unsafeBlocks.add(Material.SPRUCE_LEAVES); setLocation(player.getLocation(), HuskHomes.settings.getServerID());
unsafeBlocks.add(Material.OAK_LEAVES); }
unsafeBlocks.add(Material.BIRCH_LEAVES);
unsafeBlocks.add(Material.ACACIA_LEAVES);
unsafeBlocks.add(Material.DARK_OAK_LEAVES);
unsafeBlocks.add(Material.OBSIDIAN);
} }
private Location randomLocation(World world) { private Location randomLocation(World world) {
@@ -82,7 +82,7 @@ public class RandomPoint extends TeleportationPoint {
// Failsafe check in case the world is null // Failsafe check in case the world is null
if (world != null) { if (world != null) {
// Get instances of the blocks around where the player would spawn // Get instances of the blocks around where the player would teleport
Block block = world.getBlockAt(x, y, z); Block block = world.getBlockAt(x, y, z);
Block below = world.getBlockAt(x, y - 1, z); Block below = world.getBlockAt(x, y - 1, z);
Block above = world.getBlockAt(x, y + 1, z); Block above = world.getBlockAt(x, y + 1, z);
@@ -98,32 +98,13 @@ public class RandomPoint extends TeleportationPoint {
} }
private Location getRandomLocation(World world) { private Location getRandomLocation(World world) {
Location randomLocation = randomLocation(world); for (int i = 0; i < MAX_RANDOM_ATTEMPTS; i++) {
Location randomLocation = randomLocation(world);
int attempts = 0; if (isLocationSafe(randomLocation)) {
return randomLocation;
// Keep looking for a random location that is safe
while (!isLocationSafe(randomLocation)){
// 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) { return null;
super(player.getLocation(), HuskHomes.settings.getServerID());
Location randomLocation = getRandomLocation(player.getWorld());
if (randomLocation != null) {
setLocation(randomLocation, HuskHomes.settings.getServerID());
} else {
MessageManager.sendMessage(player, "error_rtp_randomization_timeout");
}
} }
} }