Improve safe location checking

This commit is contained in:
Fabrizio La Rosa 2020-06-13 01:18:15 +02:00
parent 51daa0498d
commit 336d5bf067
3 changed files with 53 additions and 31 deletions

View File

@ -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) {

View File

@ -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();

View File

@ -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();
}