Skip to content

Commit 5f8e8fa

Browse files
committed
Refactored Walls and added test class
1 parent 50e3bf2 commit 5f8e8fa

File tree

3 files changed

+336
-84
lines changed

3 files changed

+336
-84
lines changed

src/main/java/world/bentobox/greenhouses/greenhouse/Walls.java

Lines changed: 104 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -29,108 +29,129 @@ public class Walls extends MinMaxXZ {
2929

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

32-
public Walls(Roof roof) {
32+
class WallFinder {
33+
int radiusMinX;
34+
int radiusMaxX;
35+
int radiusMinZ;
36+
int radiusMaxZ;
37+
boolean stopMinX;
38+
boolean stopMaxX;
39+
boolean stopMinZ;
40+
boolean stopMaxZ;
41+
boolean isSearching() {
42+
return !stopMinX || !stopMaxX || !stopMinZ || !stopMaxZ;
43+
}
44+
}
45+
46+
public Walls findWalls(Roof roof) {
3347
// The player is under the roof
3448
// Assume the player is inside the greenhouse they are trying to create
3549
Location loc = roof.getLocation();
36-
World world = roof.getLocation().getWorld();
50+
World world = loc.getWorld();
3751
floor = getFloorY(world, roof.getHeight(), roof.getMinX(), roof.getMaxX(), roof.getMinZ(), roof.getMaxZ());
3852
// Now start with the player's x and z location
39-
int radiusMinX = 0;
40-
int radiusMaxX = 0;
41-
int radiusMinZ = 0;
42-
int radiusMaxZ = 0;
43-
boolean stopMinX = false;
44-
boolean stopMaxX = false;
45-
boolean stopMinZ = false;
46-
boolean stopMaxZ = false;
53+
WallFinder wf = new WallFinder();
4754
minX = loc.getBlockX();
4855
maxX = loc.getBlockX();
4956
minZ = loc.getBlockZ();
5057
maxZ = loc.getBlockZ();
5158
do {
52-
// Look around player in an ever expanding cube
53-
minX = loc.getBlockX() - radiusMinX;
54-
maxX = loc.getBlockX() + radiusMaxX;
55-
minZ = loc.getBlockZ() - radiusMinZ;
56-
maxZ = loc.getBlockZ() + radiusMaxZ;
57-
int y;
58-
for (y = roof.getHeight() - 1; y > floor; y--) {
59-
for (int x = minX; x <= maxX; x++) {
60-
for (int z = minZ; z <= maxZ; z++) {
61-
// Only look around outside edge
62-
if (!((x > minX && x < maxX) && (z > minZ && z < maxZ))) {
63-
// Look at block faces
64-
for (BlockFace bf: ORDINALS) {
65-
switch (bf) {
66-
case EAST:
67-
// positive x
68-
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
69-
stopMaxX = true;
70-
}
71-
break;
72-
case WEST:
73-
// negative x
74-
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
75-
stopMinX = true;
76-
}
77-
break;
78-
case NORTH:
79-
// negative Z
80-
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
81-
stopMinZ = true;
82-
}
83-
break;
84-
case SOUTH:
85-
// positive Z
86-
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
87-
stopMaxZ = true;
88-
}
89-
break;
90-
default:
91-
break;
92-
}
93-
}
94-
}
95-
}
96-
}
97-
}
98-
if (minX < roof.getMinX()) {
99-
stopMinX = true;
100-
}
101-
if (maxX > roof.getMaxX()) {
102-
stopMaxX = true;
103-
}
104-
if (minZ < roof.getMinZ()) {
105-
stopMinZ = true;
106-
}
107-
if (maxZ > roof.getMaxZ()) {
108-
stopMaxZ = true;
109-
}
110-
// Expand the edges
111-
if (!stopMinX) {
112-
radiusMinX++;
113-
}
114-
if (!stopMaxX) {
115-
radiusMaxX++;
116-
}
117-
if (!stopMinZ) {
118-
radiusMinZ++;
119-
}
120-
if (!stopMaxZ) {
121-
radiusMaxZ++;
122-
}
123-
} while (!stopMinX || !stopMaxX || !stopMinZ || !stopMaxZ);
59+
lookAround(loc, wf, roof);
60+
} while (wf.isSearching());
12461
// We should have the largest cube we can make now
12562
minX--;
12663
maxX++;
12764
minZ--;
12865
maxZ++;
12966
// Find the floor again, only looking within the walls
13067
floor = getFloorY(world, roof.getHeight(), minX, maxX, minZ,maxZ);
68+
return this;
69+
}
70+
71+
void lookAround(Location loc, WallFinder wf, Roof roof) {
72+
World world = loc.getWorld();
73+
// Look around player in an ever expanding cube
74+
minX = loc.getBlockX() - wf.radiusMinX;
75+
maxX = loc.getBlockX() + wf.radiusMaxX;
76+
minZ = loc.getBlockZ() - wf.radiusMinZ;
77+
maxZ = loc.getBlockZ() + wf.radiusMaxZ;
78+
for (int y = roof.getHeight() - 1; y > floor; y--) {
79+
for (int x = minX; x <= maxX; x++) {
80+
for (int z = minZ; z <= maxZ; z++) {
81+
// Only look around outside edge
82+
if (!((x > minX && x < maxX) && (z > minZ && z < maxZ))) {
83+
// Look at block faces
84+
lookAtBlockFaces(wf, world, x, y, z);
85+
}
86+
}
87+
}
88+
}
89+
analyzeFindings(wf, roof);
90+
}
91+
92+
void analyzeFindings(WallFinder wf, Roof roof) {
93+
if (minX < roof.getMinX()) {
94+
wf.stopMinX = true;
95+
}
96+
if (maxX > roof.getMaxX()) {
97+
wf.stopMaxX = true;
98+
}
99+
if (minZ < roof.getMinZ()) {
100+
wf.stopMinZ = true;
101+
}
102+
if (maxZ > roof.getMaxZ()) {
103+
wf.stopMaxZ = true;
104+
}
105+
// Expand the edges
106+
if (!wf.stopMinX) {
107+
wf.radiusMinX++;
108+
}
109+
if (!wf.stopMaxX) {
110+
wf.radiusMaxX++;
111+
}
112+
if (!wf.stopMinZ) {
113+
wf.radiusMinZ++;
114+
}
115+
if (!wf.stopMaxZ) {
116+
wf.radiusMaxZ++;
117+
}
118+
}
119+
120+
void lookAtBlockFaces(WallFinder wf, World world, int x, int y, int z) {
121+
for (BlockFace bf: ORDINALS) {
122+
switch (bf) {
123+
case EAST:
124+
// positive x
125+
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
126+
wf.stopMaxX = true;
127+
}
128+
break;
129+
case WEST:
130+
// negative x
131+
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
132+
wf.stopMinX = true;
133+
}
134+
break;
135+
case NORTH:
136+
// negative Z
137+
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
138+
wf.stopMinZ = true;
139+
}
140+
break;
141+
case SOUTH:
142+
// positive Z
143+
if (WALL_BLOCKS.contains(world.getBlockAt(x, y, z).getRelative(bf).getType())) {
144+
wf.stopMaxZ = true;
145+
}
146+
break;
147+
default:
148+
break;
149+
}
150+
}
151+
131152
}
132153

133-
private int getFloorY(World world, int y, int minX, int maxX, int minZ, int maxZ) {
154+
int getFloorY(World world, int y, int minX, int maxX, int minZ, int maxZ) {
134155
// Find the floor - defined as the last y under the roof where there are no wall blocks
135156
int wallBlockCount;
136157
do {

src/main/java/world/bentobox/greenhouses/managers/GreenhouseFinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Set<GreenhouseResult> find(Location location) {
5858
return result;
5959
}
6060
// Find the walls
61-
Walls walls = new Walls(roof);
61+
Walls walls = new Walls().findWalls(roof);
6262
// Make the initial greenhouse
6363
gh = new Greenhouse(location.getWorld(), walls, roof.getHeight());
6464
// Set the original biome

0 commit comments

Comments
 (0)