Skip to content

Commit

Permalink
Improves calculations for finding greenhouse.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Nov 23, 2019
1 parent c8008e3 commit d41e495
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
29 changes: 14 additions & 15 deletions src/main/java/world/bentobox/greenhouses/greenhouse/Roof.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package world.bentobox.greenhouses.greenhouse;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -16,23 +17,21 @@
*
*/
public class Roof extends MinMaxXZ {
private final Location location;
private int height;
private boolean roofFound;

/**
* @return a list of valid roof blocks
*/
public static List<Material> getRoofBlocks() {
return Arrays.stream(Material.values())
public static final List<Material> ROOF_BLOCKS;
static {
List<Material> r = Arrays.stream(Material.values())
.filter(Material::isBlock) // Blocks only, no items
.filter(m -> !m.name().contains("DOOR")) // No doors
.filter(m -> m.name().contains("TRAPDOOR") // All trapdoors
|| m.name().contains("GLASS") // All glass blocks
|| m.equals(Material.HOPPER) // Hoppers
|| m.equals(Material.GLOWSTONE)) // Glowstone
.collect(Collectors.toList());
ROOF_BLOCKS = Collections.unmodifiableList(r);
}
private final Location location;
private int height;
private boolean roofFound;

/**
* Finds a roof from a starting location under the roof and characterizes it
Expand Down Expand Up @@ -60,10 +59,10 @@ private boolean findRoof(Location loc) {
if (!((x > loc.getBlockX() - radius && x < loc.getBlockX() + radius)
&& (z > loc.getBlockZ() - radius && z < loc.getBlockZ() + radius))) {
Block b = world.getBlockAt(x, roofY, z);
if (!Walls.isWallBlock(b.getType())) {
if (!Walls.WALL_BLOCKS.contains(b.getType())) {
// Look up
for (int y = roofY; y < world.getMaxHeight(); y++) {
if (getRoofBlocks().contains(world.getBlockAt(x,y,z).getType())) {
if (ROOF_BLOCKS.contains(world.getBlockAt(x,y,z).getType())) {
roofFound = true;
loc = new Location(world,x,y,z);
break;
Expand Down Expand Up @@ -125,7 +124,7 @@ private void expandCoords(World world, Vector height) {
Location maxz = height.toLocation(world);
Location minz = height.toLocation(world);
int limit = 0;
while (getRoofBlocks()
while (ROOF_BLOCKS
.contains(world.getBlockAt(maxx).getType()) && limit < 100) {
limit++;
maxx.add(new Vector(1,0,0));
Expand All @@ -134,23 +133,23 @@ private void expandCoords(World world, Vector height) {
maxX = maxx.getBlockX()-1;
}

while (getRoofBlocks().contains(world.getBlockAt(minx).getType()) && limit < 200) {
while (ROOF_BLOCKS.contains(world.getBlockAt(minx).getType()) && limit < 200) {
limit++;
minx.subtract(new Vector(1,0,0));
}
if (minx.getBlockX() + 1 < minX) {
minX = minx.getBlockX() + 1;
}

while (getRoofBlocks().contains(world.getBlockAt(maxz).getType()) && limit < 300) {
while (ROOF_BLOCKS.contains(world.getBlockAt(maxz).getType()) && limit < 300) {
limit++;
maxz.add(new Vector(0,0,1));
}
if (maxz.getBlockZ() - 1 > maxZ) {
maxZ = maxz.getBlockZ() - 1;
}

while (getRoofBlocks().contains(world.getBlockAt(minz).getType()) && limit < 400) {
while (ROOF_BLOCKS.contains(world.getBlockAt(minz).getType()) && limit < 400) {
limit++;
minz.subtract(new Vector(0,0,1));
}
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/world/bentobox/greenhouses/greenhouse/Walls.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package world.bentobox.greenhouses.greenhouse;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -10,24 +11,23 @@
import org.bukkit.block.BlockFace;

public class Walls extends MinMaxXZ {
private int floor;

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

/**
* @return list of valid wall blocks
*/
public static List<Material> getWallBlocks() {
return Arrays.stream(Material.values())
public static final List<Material> WALL_BLOCKS;
static {
List<Material> w = Arrays.stream(Material.values())
.filter(Material::isBlock) // Blocks only, no items
.filter(m -> !m.name().contains("TRAPDOOR")) // No trap doors
.filter(m -> m.name().contains("DOOR") // All doors
|| m.name().contains("GLASS") // All glass blocks
|| m.equals(Material.HOPPER) // Hoppers
|| m.equals(Material.GLOWSTONE)) // Glowstone
.collect(Collectors.toList());
WALL_BLOCKS = Collections.unmodifiableList(w);
}

private int floor;

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

public Walls(Roof roof) {
// The player is under the roof
// Assume the player is inside the greenhouse they are trying to create
Expand Down Expand Up @@ -64,25 +64,25 @@ public Walls(Roof roof) {
switch (bf) {
case EAST:
// positive x
if (getWallBlocks().contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
stopMaxX = true;
}
break;
case WEST:
// negative x
if (getWallBlocks().contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
stopMinX = true;
}
break;
case NORTH:
// negative Z
if (getWallBlocks().contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
stopMinZ = true;
}
break;
case SOUTH:
// positive Z
if (getWallBlocks().contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
stopMaxZ = true;
}
break;
Expand Down Expand Up @@ -136,7 +136,7 @@ private int getFloorY(World world, int y, int minX, int maxX, int minZ, int maxZ
wallBlockCount = 0;
for (int x = minX; x <= maxX; x++) {
for (int z = minZ; z <= maxZ; z++) {
if (getWallBlocks().contains(world.getBlockAt(x, y, z).getType())) {
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getType())) {
wallBlockCount++;
}
}
Expand All @@ -154,10 +154,6 @@ public int getFloor() {
return floor;
}

public static boolean isWallBlock(Material blockType) {
return getWallBlocks().contains(blockType);
}

/**
* @return width of the space
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public Set<GreenhouseResult> find(Location location) {
} else {
// Check just the walls
if (y == roof.getHeight() || x == minX || x == maxX || z == minZ || z== maxZ) {
if ((y != roof.getHeight() && !Walls.getWallBlocks().contains(blockType))
|| (y == roof.getHeight() && !Roof.getRoofBlocks().contains(blockType))) {
if ((y != roof.getHeight() && !Walls.WALL_BLOCKS.contains(blockType))
|| (y == roof.getHeight() && !Roof.ROOF_BLOCKS.contains(blockType))) {
//logger(2,"DEBUG: bad block found at " + x + "," + y+ "," + z + " " + blockType);
if (blockType == Material.AIR) {
airHole = true;
Expand Down

0 comments on commit d41e495

Please sign in to comment.