Improves calculations for finding greenhouse.

This commit is contained in:
tastybento 2019-11-22 20:58:32 -08:00
parent c8008e34e2
commit d41e49597d
3 changed files with 30 additions and 35 deletions

View File

@ -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;
@ -16,15 +17,9 @@ import org.bukkit.util.Vector;
*
*/
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
@ -32,7 +27,11 @@ public class Roof extends MinMaxXZ {
|| 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
@ -60,10 +59,10 @@ public class Roof extends MinMaxXZ {
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;
@ -125,7 +124,7 @@ public class Roof extends MinMaxXZ {
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));
@ -134,7 +133,7 @@ public class Roof extends MinMaxXZ {
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));
}
@ -142,7 +141,7 @@ public class Roof extends MinMaxXZ {
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));
}
@ -150,7 +149,7 @@ public class Roof extends MinMaxXZ {
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));
}

View File

@ -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;
@ -10,15 +11,9 @@ import org.bukkit.World;
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
@ -26,8 +21,13 @@ public class Walls extends MinMaxXZ {
|| 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
@ -64,25 +64,25 @@ public class Walls extends MinMaxXZ {
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;
@ -136,7 +136,7 @@ public class Walls extends MinMaxXZ {
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++;
}
}
@ -154,10 +154,6 @@ public class Walls extends MinMaxXZ {
return floor;
}
public static boolean isWallBlock(Material blockType) {
return getWallBlocks().contains(blockType);
}
/**
* @return width of the space
*/

View File

@ -80,8 +80,8 @@ public class GreenhouseFinder {
} 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;