Skip to content

Commit

Permalink
Added test for SafeSpotTeleport and removed old test
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Dec 20, 2021
2 parents e3b9919 + f049fc6 commit 3ece0d0
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 169 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ dist/
nbdist/
nbactions.xml
nb-configuration.xml
.nb-gradle/
.nb-gradle/
/BentoBox/
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class SafeSpotTeleport {
Util.getChunkAtAsync(location).thenRun(() -> tryToGo(builder.getFailureMessage()));
}

private void tryToGo(String failureMessage) {
void tryToGo(String failureMessage) {
if (plugin.getIslands().isSafeLocation(location)) {
if (portal) {
// If the desired location is safe, then that's where you'll go if there's no portal
Expand All @@ -92,15 +92,16 @@ private void tryToGo(String failureMessage) {
task = Bukkit.getScheduler().runTaskTimer(plugin, () -> gatherChunks(failureMessage), 0L, SPEED);
}

private void gatherChunks(String failureMessage) {
boolean gatherChunks(String failureMessage) {
// Set a flag so this is only run if it's not already in progress
if (checking.get()) {
return;
return false;
}
checking.set(true);
if (checkedChunks > MAX_CHUNKS || !chunksToScanIterator.hasNext()) {
// Nothing left
tidyUp(entity, failureMessage);
return;
return false;
}

// Get the chunk
Expand All @@ -109,22 +110,23 @@ private void gatherChunks(String failureMessage) {
checkedChunks++;
if (checkedChunks >= MAX_CHUNKS) {
checking.set(false);
return;
return false;
}

// Get the chunk snapshot and scan it
Util.getChunkAtAsync(world, chunkPair.x, chunkPair.z)
.thenApply(Chunk::getChunkSnapshot)
.whenCompleteAsync((snapshot, e) -> {
if (snapshot != null && scanChunk(snapshot)) {
task.cancel();
} else {
checking.set(false);
}
});
.thenApply(Chunk::getChunkSnapshot)
.whenCompleteAsync((snapshot, e) -> {
if (snapshot != null && scanChunk(snapshot)) {
task.cancel();
} else {
checking.set(false);
}
});
return true;
}

private void tidyUp(Entity entity, String failureMessage) {
void tidyUp(Entity entity, String failureMessage) {
// Still Async!
// Nothing left to check and still not canceled
task.cancel();
Expand Down Expand Up @@ -165,7 +167,7 @@ private void tidyUp(Entity entity, String failureMessage) {
}
}

private void makeAndTeleport(Material m) {
void makeAndTeleport(Material m) {
location.getBlock().getRelative(BlockFace.DOWN).setType(m, false);
location.getBlock().setType(Material.AIR, false);
location.getBlock().getRelative(BlockFace.UP).setType(Material.AIR, false);
Expand All @@ -181,7 +183,7 @@ private void makeAndTeleport(Material m) {
*
* @return - list of chunk coords to be scanned
*/
private List<Pair<Integer, Integer>> getChunksToScan() {
List<Pair<Integer, Integer>> getChunksToScan() {
List<Pair<Integer, Integer>> chunksToScan = new ArrayList<>();
int maxRadius = plugin.getIslands().getIslandAt(location).map(Island::getProtectionRange).orElseGet(() -> plugin.getIWM().getIslandProtectionRange(world));
maxRadius = Math.min(MAX_RADIUS, maxRadius);
Expand Down Expand Up @@ -210,7 +212,7 @@ private void addChunk(List<Pair<Integer, Integer>> chunksToScan, Pair<Integer, I
* @param chunk - chunk snapshot
* @return true if a safe spot was found
*/
private boolean scanChunk(ChunkSnapshot chunk) {
boolean scanChunk(ChunkSnapshot chunk) {
int startY = location.getBlockY();
int minY = world.getMinHeight();
int maxY = 60; // Just a dummy value
Expand Down Expand Up @@ -265,7 +267,7 @@ private boolean scanChunk(ChunkSnapshot chunk) {
/**
* Teleports entity to the safe spot
*/
private void teleportEntity(final Location loc) {
void teleportEntity(final Location loc) {
task.cancel();
// Return to main thread and teleport the player
Bukkit.getScheduler().runTask(plugin, () -> {
Expand Down Expand Up @@ -303,7 +305,7 @@ boolean checkBlock(ChunkSnapshot chunk, int x, int y, int z) {
return false;
}

private boolean safe(ChunkSnapshot chunk, int x, int y, int z, World world) {
boolean safe(ChunkSnapshot chunk, int x, int y, int z, World world) {
Vector newSpot = new Vector((chunk.getX() << 4) + x + 0.5D, y + 1.0D, (chunk.getZ() << 4) + z + 0.5D);
if (portal) {
if (bestSpot == null) {
Expand Down

This file was deleted.

Loading

0 comments on commit 3ece0d0

Please sign in to comment.