Skip to content

Commit

Permalink
this should be better
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Dec 16, 2021
1 parent 6f01310 commit f91ed47
Showing 1 changed file with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
package world.bentobox.bentobox.util.teleport;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.World.Environment;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.Nullable;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Pair;
import world.bentobox.bentobox.util.Util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
* A class that calculates finds a safe spot asynchronously and then teleports the player there.
* @author tastybento
Expand Down Expand Up @@ -231,17 +226,19 @@ private void checkChunks(final List<ChunkSnapshot> chunkSnapshot) {
*/
private boolean scanChunk(ChunkSnapshot chunk) {
int startY = location.getBlockY();
int minY = location.getWorld().getMinHeight() + 1;
int maxY = maxHeight;
int minY = location.getWorld().getMinHeight();
int maxY = 60; // Just a dummy value

// Check the safe spot at the current height
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
if (minY >= startY && startY <= maxY && checkBlock(chunk, x, startY ,z)) {
if (minY >= startY && checkBlock(chunk, x, startY, z)) {
return true;
}
maxY = Math.max(chunk.getHighestBlockYAt(x, z), maxY);
}
}
maxY = Math.min(maxY, maxHeight);

// Expand the height up and down until a safe spot is found
int upperY = startY + 1;
Expand All @@ -251,23 +248,25 @@ private boolean scanChunk(ChunkSnapshot chunk) {
while (checkUpper || checkLower) {
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
if (checkUpper && upperY <= maxY && checkBlock(chunk, x, upperY, z)) {
if (checkUpper && checkBlock(chunk, x, upperY, z)) {
return true;
}
if (checkLower && lowerY >= minY && checkBlock(chunk, x, lowerY, z)) {
if (checkLower && checkBlock(chunk, x, lowerY, z)) {
return true;
}
}
}
if (upperY > maxY) {
checkUpper = false;
} else {
if (checkUpper) {
upperY++;
if (upperY > maxY) {
checkUpper = false;
}
}
if (lowerY < minY) {
checkLower = false;
} else {
if (checkLower) {
lowerY--;
if (lowerY < minY) {
checkLower = false;
}
}
}

Expand Down

0 comments on commit f91ed47

Please sign in to comment.