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; package world.bentobox.greenhouses.greenhouse;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -16,15 +17,9 @@ import org.bukkit.util.Vector;
* *
*/ */
public class Roof extends MinMaxXZ { public class Roof extends MinMaxXZ {
private final Location location; public static final List<Material> ROOF_BLOCKS;
private int height; static {
private boolean roofFound; List<Material> r = Arrays.stream(Material.values())
/**
* @return a list of valid roof blocks
*/
public static List<Material> getRoofBlocks() {
return Arrays.stream(Material.values())
.filter(Material::isBlock) // Blocks only, no items .filter(Material::isBlock) // Blocks only, no items
.filter(m -> !m.name().contains("DOOR")) // No doors .filter(m -> !m.name().contains("DOOR")) // No doors
.filter(m -> m.name().contains("TRAPDOOR") // All trapdoors .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.HOPPER) // Hoppers
|| m.equals(Material.GLOWSTONE)) // Glowstone || m.equals(Material.GLOWSTONE)) // Glowstone
.collect(Collectors.toList()); .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 * 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) if (!((x > loc.getBlockX() - radius && x < loc.getBlockX() + radius)
&& (z > loc.getBlockZ() - radius && z < loc.getBlockZ() + radius))) { && (z > loc.getBlockZ() - radius && z < loc.getBlockZ() + radius))) {
Block b = world.getBlockAt(x, roofY, z); Block b = world.getBlockAt(x, roofY, z);
if (!Walls.isWallBlock(b.getType())) { if (!Walls.WALL_BLOCKS.contains(b.getType())) {
// Look up // Look up
for (int y = roofY; y < world.getMaxHeight(); y++) { 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; roofFound = true;
loc = new Location(world,x,y,z); loc = new Location(world,x,y,z);
break; break;
@ -125,7 +124,7 @@ public class Roof extends MinMaxXZ {
Location maxz = height.toLocation(world); Location maxz = height.toLocation(world);
Location minz = height.toLocation(world); Location minz = height.toLocation(world);
int limit = 0; int limit = 0;
while (getRoofBlocks() while (ROOF_BLOCKS
.contains(world.getBlockAt(maxx).getType()) && limit < 100) { .contains(world.getBlockAt(maxx).getType()) && limit < 100) {
limit++; limit++;
maxx.add(new Vector(1,0,0)); maxx.add(new Vector(1,0,0));
@ -134,7 +133,7 @@ public class Roof extends MinMaxXZ {
maxX = maxx.getBlockX()-1; maxX = maxx.getBlockX()-1;
} }
while (getRoofBlocks().contains(world.getBlockAt(minx).getType()) && limit < 200) { while (ROOF_BLOCKS.contains(world.getBlockAt(minx).getType()) && limit < 200) {
limit++; limit++;
minx.subtract(new Vector(1,0,0)); minx.subtract(new Vector(1,0,0));
} }
@ -142,7 +141,7 @@ public class Roof extends MinMaxXZ {
minX = minx.getBlockX() + 1; minX = minx.getBlockX() + 1;
} }
while (getRoofBlocks().contains(world.getBlockAt(maxz).getType()) && limit < 300) { while (ROOF_BLOCKS.contains(world.getBlockAt(maxz).getType()) && limit < 300) {
limit++; limit++;
maxz.add(new Vector(0,0,1)); maxz.add(new Vector(0,0,1));
} }
@ -150,7 +149,7 @@ public class Roof extends MinMaxXZ {
maxZ = maxz.getBlockZ() - 1; maxZ = maxz.getBlockZ() - 1;
} }
while (getRoofBlocks().contains(world.getBlockAt(minz).getType()) && limit < 400) { while (ROOF_BLOCKS.contains(world.getBlockAt(minz).getType()) && limit < 400) {
limit++; limit++;
minz.subtract(new Vector(0,0,1)); minz.subtract(new Vector(0,0,1));
} }

View File

@ -1,6 +1,7 @@
package world.bentobox.greenhouses.greenhouse; package world.bentobox.greenhouses.greenhouse;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -10,15 +11,9 @@ import org.bukkit.World;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
public class Walls extends MinMaxXZ { public class Walls extends MinMaxXZ {
private int floor; public static final List<Material> WALL_BLOCKS;
static {
private static final List<BlockFace> ORDINALS = Arrays.asList(BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST); List<Material> w = Arrays.stream(Material.values())
/**
* @return list of valid wall blocks
*/
public static List<Material> getWallBlocks() {
return Arrays.stream(Material.values())
.filter(Material::isBlock) // Blocks only, no items .filter(Material::isBlock) // Blocks only, no items
.filter(m -> !m.name().contains("TRAPDOOR")) // No trap doors .filter(m -> !m.name().contains("TRAPDOOR")) // No trap doors
.filter(m -> m.name().contains("DOOR") // All 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.HOPPER) // Hoppers
|| m.equals(Material.GLOWSTONE)) // Glowstone || m.equals(Material.GLOWSTONE)) // Glowstone
.collect(Collectors.toList()); .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) { public Walls(Roof roof) {
// The player is under the roof // The player is under the roof
// Assume the player is inside the greenhouse they are trying to create // Assume the player is inside the greenhouse they are trying to create
@ -64,25 +64,25 @@ public class Walls extends MinMaxXZ {
switch (bf) { switch (bf) {
case EAST: case EAST:
// positive x // 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; stopMaxX = true;
} }
break; break;
case WEST: case WEST:
// negative x // 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; stopMinX = true;
} }
break; break;
case NORTH: case NORTH:
// negative Z // 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; stopMinZ = true;
} }
break; break;
case SOUTH: case SOUTH:
// positive Z // 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; stopMaxZ = true;
} }
break; break;
@ -136,7 +136,7 @@ public class Walls extends MinMaxXZ {
wallBlockCount = 0; wallBlockCount = 0;
for (int x = minX; x <= maxX; x++) { for (int x = minX; x <= maxX; x++) {
for (int z = minZ; z <= maxZ; z++) { 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++; wallBlockCount++;
} }
} }
@ -154,10 +154,6 @@ public class Walls extends MinMaxXZ {
return floor; return floor;
} }
public static boolean isWallBlock(Material blockType) {
return getWallBlocks().contains(blockType);
}
/** /**
* @return width of the space * @return width of the space
*/ */

View File

@ -80,8 +80,8 @@ public class GreenhouseFinder {
} else { } else {
// Check just the walls // Check just the walls
if (y == roof.getHeight() || x == minX || x == maxX || z == minZ || z== maxZ) { if (y == roof.getHeight() || x == minX || x == maxX || z == minZ || z== maxZ) {
if ((y != roof.getHeight() && !Walls.getWallBlocks().contains(blockType)) if ((y != roof.getHeight() && !Walls.WALL_BLOCKS.contains(blockType))
|| (y == roof.getHeight() && !Roof.getRoofBlocks().contains(blockType))) { || (y == roof.getHeight() && !Roof.ROOF_BLOCKS.contains(blockType))) {
//logger(2,"DEBUG: bad block found at " + x + "," + y+ "," + z + " " + blockType); //logger(2,"DEBUG: bad block found at " + x + "," + y+ "," + z + " " + blockType);
if (blockType == Material.AIR) { if (blockType == Material.AIR) {
airHole = true; airHole = true;