Skip to content

Commit

Permalink
Walls are now async
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 16, 2021
1 parent ff487a7 commit baa9338
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 103 deletions.
67 changes: 37 additions & 30 deletions src/main/java/world/bentobox/greenhouses/greenhouse/Roof.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

Expand All @@ -11,6 +13,7 @@
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.greenhouses.Greenhouses;
Expand All @@ -22,7 +25,6 @@
*
*/
public class Roof extends MinMaxXZ {
private static final BentoBox PLUGIN = Greenhouses.getInstance().getPlugin();
private static final List<Material> ROOF_BLOCKS;
static {
List<Material> r = Arrays.stream(Material.values())
Expand All @@ -38,8 +40,8 @@ public class Roof extends MinMaxXZ {
* @param m - material
* @return true if roof material
*/
public static boolean roofBlocks(Material m) {
return ROOF_BLOCKS.contains(m)
public static boolean roofBlocks(@NonNull Material m) {
return ROOF_BLOCKS.contains(Objects.requireNonNull(m))
|| (m.equals(Material.GLOWSTONE) && Greenhouses.getInstance().getSettings().isAllowGlowstone())
|| (m.name().endsWith("GLASS_PANE") && Greenhouses.getInstance().getSettings().isAllowPanes());
}
Expand Down Expand Up @@ -114,29 +116,32 @@ public CompletableFuture<Boolean> findRoof() {
Vector loc = location.toVector();
// This section tries to find a roof block
// Try just going up - this covers every case except if the player is standing under a hole
Bukkit.getScheduler().runTaskAsynchronously(PLUGIN, () -> {
Bukkit.getScheduler().runTaskAsynchronously(BentoBox.getInstance(), () -> {
boolean found = findRoof(loc);
Bukkit.getScheduler().runTask(PLUGIN, () -> r.complete(found));
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> r.complete(found));
});
return r;
}

private boolean findRoof(Vector loc) {
boolean findRoof(Vector loc) {
// This does a ever-growing check around the player to find a wall block. It is possible for the player
// to be outside the greenhouse in this situation, so a check is done later to make sure the player is inside
int roofY = loc.getBlockY();
for (int y = roofY; y < world.getMaxHeight(); y++) {
if (roofBlocks(cache.getBlockType(loc.getBlockX(),y,loc.getBlockZ()))) {
int startY = loc.getBlockY();
for (int y = startY; y < world.getMaxHeight(); y++) {
Vector v = new Vector(loc.getBlockX(),y,loc.getBlockZ());
if (roofBlocks(cache.getBlockType(v))) {
roofFound = true;
loc = new Vector(loc.getBlockX(),y,loc.getBlockZ());
loc = v;
break;
}
}
// If the roof was not found start going around in circles until something is found
// Expand in ever increasing squares around location until a wall block is found
spiralSearch(loc, roofY);
if (!roofFound) {
return false;
loc = spiralSearch(loc, startY);
if (!roofFound) {
return false;
}
}
// Record the height
this.height = loc.getBlockY();
Expand Down Expand Up @@ -183,47 +188,49 @@ public Location getLocation() {
return location;
}

private void spiralSearch(Vector v, int roofY) {
for (int radius = 0; radius < 3 && !roofFound; radius++) {
for (int x = v.getBlockX() - radius; x <= v.getBlockX() + radius && !roofFound; x++) {
for (int z = v.getBlockZ() - radius; z <= v.getBlockZ() + radius && !roofFound; z++) {
private Vector spiralSearch(Vector v, int startY) {
for (int radius = 0; radius < 3; radius++) {
for (int x = v.getBlockX() - radius; x <= v.getBlockX() + radius; x++) {
for (int z = v.getBlockZ() - radius; z <= v.getBlockZ() + radius; z++) {
if (!((x > v.getBlockX() - radius && x < v.getBlockX() + radius) && (z > v.getBlockZ() - radius && z < v.getBlockZ() + radius))) {
checkVertically(v, x, roofY, z);
Optional<Vector> r = checkVertically(x, startY, z);
if (r.isPresent()) {
return r.get();
}
}
}
}
}

return v;
}

/**
* Get highest roof block
* @param v - vector of search block
* @param x - x coord of current search
* @param roofY - roof y coord
* @param startY - starting y coord
* @param z - z coord of current search
*/
private void checkVertically(Vector v, final int x, final int roofY, final int z) {
if (!Walls.wallBlocks(cache.getBlockType(x, roofY, z))) {
private Optional<Vector> checkVertically(final int x, final int startY, final int z) {
if (!Walls.wallBlocks(cache.getBlockType(x, startY, z))) {
// Look up
for (int y = roofY; y < world.getMaxHeight() && !roofFound; y++) {
for (int y = startY; y < world.getMaxHeight() && !roofFound; y++) {
if (roofBlocks(cache.getBlockType(x,y,z))) {

roofFound = true;
// Move roof up because there is a higher block
v = new Vector(x,y,z);
// Roof block found
return Optional.of(new Vector(x,y,z));
}
}
}

return Optional.empty();
}


@Override
public String toString() {
return "Roof [" + (cache != null ? "cache=" + cache + ", " : "") + "height=" + height + ", "
+ (location != null ? "location=" + location + ", " : "") + "roofFound=" + roofFound + ", "
+ (world != null ? "world=" + world + ", " : "") + "minX=" + minX + ", maxX=" + maxX + ", minZ=" + minZ
+ ", maxZ=" + maxZ + "]";
return "Roof [height=" + height + ", roofFound=" + roofFound + ", minX=" + minX + ", maxX=" + maxX + ", minZ="
+ minZ + ", maxZ=" + maxZ + "]";
}


}
93 changes: 48 additions & 45 deletions src/main/java/world/bentobox/greenhouses/greenhouse/Walls.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockFace;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.greenhouses.Greenhouses;
import world.bentobox.greenhouses.world.AsyncWorldCache;

public class Walls extends MinMaxXZ {
private static final List<Material> WALL_BLOCKS;
Expand All @@ -28,7 +29,7 @@ public class Walls extends MinMaxXZ {

private int floor;

private static final List<BlockFace> ORDINALS = Arrays.asList(BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST);
private final AsyncWorldCache cache;

class WallFinder {
int radiusMinX;
Expand All @@ -42,19 +43,34 @@ class WallFinder {
boolean isSearching() {
return !stopMinX || !stopMaxX || !stopMinZ || !stopMaxZ;
}
@Override
public String toString() {
return "WallFinder [radiusMinX=" + radiusMinX + ", radiusMaxX=" + radiusMaxX + ", radiusMinZ=" + radiusMinZ
+ ", radiusMaxZ=" + radiusMaxZ + ", stopMinX=" + stopMinX + ", stopMaxX=" + stopMaxX + ", stopMinZ="
+ stopMinZ + ", stopMaxZ=" + stopMaxZ + "]";
}
}

public Walls(AsyncWorldCache cache) {
this.cache = cache;
}

/**
* Find walls given a roof
* @param roof - the roof
* @return Future walls
*/
public CompletableFuture<Walls> findWalls(Roof roof) {
public CompletableFuture<Walls> findWalls(final Roof roof) {
CompletableFuture<Walls> r = new CompletableFuture<>();
Bukkit.getScheduler().runTaskAsynchronously(BentoBox.getInstance(), () -> findWalls(r, roof));
return r;
}

Walls findWalls(CompletableFuture<Walls> r, Roof roof) {
// The player is under the roof
// Assume the player is inside the greenhouse they are trying to create
Location loc = roof.getLocation();
World world = loc.getWorld();
floor = getFloorY(world, roof.getHeight(), roof.getMinX(), roof.getMaxX(), roof.getMinZ(), roof.getMaxZ());
final Location loc = roof.getLocation();
floor = getFloorY(roof.getHeight(), roof.getMinX(), roof.getMaxX(), roof.getMinZ(), roof.getMaxZ());
// Now start with the player's x and z location
WallFinder wf = new WallFinder();
minX = loc.getBlockX();
Expand All @@ -70,12 +86,14 @@ public CompletableFuture<Walls> findWalls(Roof roof) {
minZ--;
maxZ++;
// Find the floor again, only looking within the walls
floor = getFloorY(world, roof.getHeight(), minX, maxX, minZ,maxZ);
return CompletableFuture.completedFuture(this);
floor = getFloorY(roof.getHeight(), minX, maxX, minZ,maxZ);
// Complete on main thread
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> r.complete(this));
return this;

}

void lookAround(Location loc, WallFinder wf, Roof roof) {
World world = loc.getWorld();
void lookAround(final Location loc, WallFinder wf, final Roof roof) {
// Look around player in an ever expanding cube
minX = loc.getBlockX() - wf.radiusMinX;
maxX = loc.getBlockX() + wf.radiusMaxX;
Expand All @@ -87,7 +105,7 @@ void lookAround(Location loc, WallFinder wf, Roof roof) {
// Only look around outside edge
if (!((x > minX && x < maxX) && (z > minZ && z < maxZ))) {
// Look at block faces
lookAtBlockFaces(wf, world, x, y, z);
lookAtBlockFaces(wf, x, y, z);
}
}
}
Expand Down Expand Up @@ -123,48 +141,33 @@ void analyzeFindings(WallFinder wf, Roof roof) {
}
}

void lookAtBlockFaces(WallFinder wf, World world, int x, int y, int z) {
for (BlockFace bf: ORDINALS) {
switch (bf) {
case EAST:
// positive x
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
wf.stopMaxX = true;
}
break;
case WEST:
// negative x
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
wf.stopMinX = true;
}
break;
case NORTH:
// negative Z
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
wf.stopMinZ = true;
}
break;
case SOUTH:
// positive Z
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
wf.stopMaxZ = true;
}
break;
default:
break;
}
void lookAtBlockFaces(WallFinder wf, int x, int y, int z) {
// positive x
if (WALL_BLOCKS.contains(cache.getBlockType(x + 1, y, z))) {
wf.stopMaxX = true;
}
// negative x
if (WALL_BLOCKS.contains(cache.getBlockType(x - 1, y, z))) {
wf.stopMinX = true;
}
// negative Z
if (WALL_BLOCKS.contains(cache.getBlockType(x, y, z - 1))) {
wf.stopMinZ = true;
}
// positive Z
if (WALL_BLOCKS.contains(cache.getBlockType(x, y, z + 1))) {
wf.stopMaxZ = true;
}

}

int getFloorY(World world, int y, int minX, int maxX, int minZ, int maxZ) {
int getFloorY(int y, int minX, int maxX, int minZ, int maxZ) {
// Find the floor - defined as the last y under the roof where there are no wall blocks
int wallBlockCount;
do {
wallBlockCount = 0;
for (int x = minX; x <= maxX; x++) {
for (int z = minZ; z <= maxZ; z++) {
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getType())) {
if (WALL_BLOCKS.contains(cache.getBlockType(x, y, z))) {
wallBlockCount++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.bukkit.World.Environment;
import org.bukkit.block.Block;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.greenhouses.data.Greenhouse;
import world.bentobox.greenhouses.greenhouse.Roof;
import world.bentobox.greenhouses.greenhouse.Walls;
Expand Down Expand Up @@ -65,9 +64,8 @@ public CompletableFuture<Set<GreenhouseResult>> find(Location location) {
r.complete(result);
return;
}
BentoBox.getInstance().logDebug(roof);
// Find the walls
new Walls().findWalls(roof).thenAccept(walls -> {
new Walls(cache).findWalls(roof).thenAccept(walls -> {
// Make the initial greenhouse
gh = new Greenhouse(location.getWorld(), walls, roof.getHeight());
// Set the original biome
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.bukkit.World;
import org.bukkit.util.Vector;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.util.Pair;
import world.bentobox.bentobox.util.Util;
import world.bentobox.greenhouses.Greenhouses;
Expand Down Expand Up @@ -55,7 +54,7 @@ private CompletableFuture<ChunkSnapshot> getAChunk(int x, int z) {
* @param z - block coord
* @return chunk snapshot
*/
private ChunkSnapshot getSnap(int x, int z) {
private ChunkSnapshot getSnap(final int x, final int z) {
// Convert from block to chunk coords
Pair<Integer, Integer> key = new Pair<>((x >> 4), (z >> 4));
// Get from cache if it is available
Expand Down Expand Up @@ -84,12 +83,13 @@ private ChunkSnapshot getSnap(int x, int z) {
* @param z block coordinate
* @return material type
*/
public Material getBlockType(int x, int y, int z) {
public Material getBlockType(final int x, final int y, final int z) {
// Convert block coords to chunk coords
int xx = x >= 0 ? x % 16 : 15 + (x % 16);
int zz = z >= 0 ? z % 16 : 15 + (z % 16);
// TODO: simplify this - it must be easier than this!
int xx = x >= 0 ? x % 16 : (16 + (x % 16)) % 16;
int zz = z >= 0 ? z % 16 : (16 + (z % 16)) % 16;
Material m = getSnap(x,z).getBlockType(xx, y, zz);
BentoBox.getInstance().logDebug(m);

return m;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;

import org.bukkit.Location;
Expand Down

0 comments on commit baa9338

Please sign in to comment.