From 336d5bf0674ca20b4a39c393dfba37bdc351a516 Mon Sep 17 00:00:00 2001 From: Fabrizio La Rosa Date: Sat, 13 Jun 2020 01:18:15 +0200 Subject: [PATCH] Improve safe location checking --- .../skyblock/island/IslandManager.java | 25 +++------------- .../com/songoda/skyblock/listeners/Move.java | 29 +++++++++++------- .../skyblock/utils/world/LocationUtil.java | 30 +++++++++++++++++++ 3 files changed, 53 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/songoda/skyblock/island/IslandManager.java b/src/main/java/com/songoda/skyblock/island/IslandManager.java index e184ee00..7e064d61 100644 --- a/src/main/java/com/songoda/skyblock/island/IslandManager.java +++ b/src/main/java/com/songoda/skyblock/island/IslandManager.java @@ -1035,31 +1035,14 @@ public class IslandManager { FileConfiguration configLoad = languageConfig.getFileConfiguration(); if (island.hasRole(IslandRole.Member, player.getUniqueId()) || island.hasRole(IslandRole.Operator, player.getUniqueId()) || island.hasRole(IslandRole.Owner, player.getUniqueId())) { - boolean found = false; - Location loc = island.getLocation(IslandWorld.Normal, IslandEnvironment.Visitor); - Location locChecked = loc.clone(); - loc.getWorld().loadChunk(loc.getWorld().getChunkAt(loc)); - for(int i=loc.getBlockY(); i>=0 && !found; i--){ - locChecked.subtract(0, 1, 0); - if(!locChecked.getBlock().isEmpty() && !locChecked.getBlock().isLiquid()){ - found = true; - } - } - if(!found){ - for(int i=loc.getBlockY(); i<256 && !found; i++){ - locChecked.add(0, 1, 0); - if(!locChecked.getBlock().isEmpty() && !locChecked.getBlock().isLiquid()){ - found = true; - } - } - } - if(found){ - player.teleport(locChecked); + Location loc = LocationUtil.getSafeLocation(island.getLocation(IslandWorld.Normal, IslandEnvironment.Visitor)); + if(loc != null){ + player.teleport(loc.add(0d,1d,0d)); if(!configLoad.getBoolean("Island.Teleport.FallDamage")){ player.setFallDistance(0.0F); } } else { - player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cNessuna posizione sicura trovata!")); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cNessuna posizione sicura trovata!")); // TODO: Use language.yml } } else { if (scoreboardManager != null) { diff --git a/src/main/java/com/songoda/skyblock/listeners/Move.java b/src/main/java/com/songoda/skyblock/listeners/Move.java index 3ea19408..5ab64100 100644 --- a/src/main/java/com/songoda/skyblock/listeners/Move.java +++ b/src/main/java/com/songoda/skyblock/listeners/Move.java @@ -14,6 +14,7 @@ import com.songoda.skyblock.utils.version.NMSUtil; import com.songoda.skyblock.utils.world.LocationUtil; import com.songoda.skyblock.world.WorldManager; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.attribute.Attribute; import org.bukkit.configuration.file.FileConfiguration; @@ -192,25 +193,33 @@ public class Move implements Listener { } private void teleportPlayerToIslandSpawn(Player player, IslandWorld world, Island island) { - if (!island.getVisit().isVisitor(player.getUniqueId())) { - player.teleport(island.getLocation(world, IslandEnvironment.Main)); + if (island.hasRole(IslandRole.Member, player.getUniqueId()) || island.hasRole(IslandRole.Operator, player.getUniqueId()) + || island.hasRole(IslandRole.Owner, player.getUniqueId())) { + Location loc = LocationUtil.getSafeLocation(island.getLocation(world, IslandEnvironment.Main)); + if(loc != null){ + player.teleport(loc); + } else { + LocationUtil.teleportPlayerToSpawn(player); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cNessuna posizione sicura trovata!")); // TODO: Use language.yml + } } else { - player.teleport(island.getLocation(world, IslandEnvironment.Visitor)); + Location loc = LocationUtil.getSafeLocation(island.getLocation(world, IslandEnvironment.Visitor)); + if(loc != null){ + player.teleport(loc); + } else { + LocationUtil.teleportPlayerToSpawn(player); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cNessuna posizione sicura trovata!")); // TODO: Use language.yml + } } } private void teleportPlayerToIslandSpawn(Player player, Island island) { - if (island.hasRole(IslandRole.Member, player.getUniqueId()) || island.hasRole(IslandRole.Operator, player.getUniqueId()) - || island.hasRole(IslandRole.Owner, player.getUniqueId())) { - player.teleport(island.getLocation(IslandWorld.Normal, IslandEnvironment.Main)); - } else { - player.teleport(island.getLocation(IslandWorld.Normal, IslandEnvironment.Visitor)); - } + this.teleportPlayerToIslandSpawn(player, IslandWorld.Normal, island); } private void teleportPlayerToIslandSpawn(Player player, SoundManager soundManager, Island island) { teleportPlayerToIslandSpawn(player, island); - + FileManager fileManager = skyblock.getFileManager(); Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "config.yml")); FileConfiguration configLoad = config.getFileConfiguration(); diff --git a/src/main/java/com/songoda/skyblock/utils/world/LocationUtil.java b/src/main/java/com/songoda/skyblock/utils/world/LocationUtil.java index bbabc85c..4cc4063f 100644 --- a/src/main/java/com/songoda/skyblock/utils/world/LocationUtil.java +++ b/src/main/java/com/songoda/skyblock/utils/world/LocationUtil.java @@ -25,6 +25,36 @@ import java.util.logging.Level; public final class LocationUtil { + public static Location getSafeLocation(Location loc){ + boolean found = false; + Location locChecked = loc.clone(); + loc.getWorld().loadChunk(loc.getWorld().getChunkAt(loc)); + for(int i=loc.getBlockY(); i>=0 && !found; i--){ + locChecked = locChecked.subtract(0d, 1d, 0d); + if(!locChecked.getBlock().isEmpty() && + !locChecked.getBlock().isLiquid() && + locChecked.add(0d,1d,0d).getBlock().isEmpty() && + locChecked.add(0d,2d,0d).getBlock().isEmpty()){ + found = true; + } + } + if(!found){ + for(int i=loc.getBlockY(); i<256 && !found; i++){ + locChecked = locChecked.add(0d, 1d, 0d); + if(!locChecked.getBlock().isEmpty() && + !locChecked.getBlock().isLiquid() && + locChecked.add(0d,1d,0d).getBlock().isEmpty() && + locChecked.add(0d,2d,0d).getBlock().isEmpty()){ + found = true; + } + } + } + if(!found){ + locChecked = null; + } + return locChecked; + } + public static boolean isLocationLocation(Location location1, Location location2) { return location1.getBlockX() == location2.getBlockX() && location1.getBlockY() == location2.getBlockY() && location1.getBlockZ() == location2.getBlockZ(); }