Fixed errors when getting safe location

This commit is contained in:
Fabrizio La Rosa 2020-06-19 00:12:06 +02:00
parent 9fbbca4a13
commit 2fef576a48

View File

@ -18,6 +18,8 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -38,29 +40,32 @@ public final class LocationUtil {
} }
} }
public static Location getSafeLocation(Location loc){ public static @Nullable Location getSafeLocation(Location loc){
boolean found = false; boolean found = false;
Location locChecked = loc.clone(); Location locChecked = null;
loc.getWorld().loadChunk(loc.getWorld().getChunkAt(loc)); if(loc != null && loc.getWorld() != null){
for(int i=loc.getBlockY(); i>=0 && !found; i--){ locChecked = loc.clone();
locChecked = locChecked.subtract(0d, 1d, 0d); loc.getWorld().loadChunk(loc.getWorld().getChunkAt(loc));
found = checkBlock(locChecked); for(int i=loc.getBlockY(); i>=0 && !found; i--){
} locChecked = locChecked.subtract(0d, 1d, 0d);
if(!found){
for(int i=loc.getBlockY(); i<256 && !found; i++){
locChecked = locChecked.add(0d, 1d, 0d);
found = checkBlock(locChecked); found = checkBlock(locChecked);
} }
} if(!found){
if (found) { for(int i=loc.getBlockY(); i<256 && !found; i++){
locChecked = locChecked.add(0d,1d,0d); locChecked = locChecked.add(0d, 1d, 0d);
} else { found = checkBlock(locChecked);
locChecked = null; }
}
if (found) {
locChecked = locChecked.add(0d,1d,0d);
} else {
locChecked = null;
}
} }
return locChecked; return locChecked;
} }
public static Location getDefinitiveLocation(Location loc){ public static @Nonnull Location getDefinitiveLocation(Location loc){
Location locWorking = loc.clone(); Location locWorking = loc.clone();
for(int i=locWorking.getBlockY(); i>=0; i--){ for(int i=locWorking.getBlockY(); i>=0; i--){
if(!locWorking.getBlock().isEmpty()){ if(!locWorking.getBlock().isEmpty()){