forked from jzitnik/twodcraft
fix: Leaves falling when base out of loaded radius
This commit is contained in:
parent
0bd9dfb412
commit
c1674d7866
@ -10,6 +10,7 @@ import java.util.*;
|
|||||||
@CustomLogic
|
@CustomLogic
|
||||||
public class LeavesFallingLogic implements CustomLogicInterface {
|
public class LeavesFallingLogic implements CustomLogicInterface {
|
||||||
private static final int RADIUS = 30;
|
private static final int RADIUS = 30;
|
||||||
|
private static final int SAFE_MARGIN = 5;
|
||||||
private final Random random = new Random();
|
private final Random random = new Random();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -33,20 +34,28 @@ public class LeavesFallingLogic implements CustomLogicInterface {
|
|||||||
if (blocks.stream().anyMatch(block -> block.getBlockId().equals("oak_leaves"))) {
|
if (blocks.stream().anyMatch(block -> block.getBlockId().equals("oak_leaves"))) {
|
||||||
String key = x + "," + y;
|
String key = x + "," + y;
|
||||||
if (!supportedLeaves.contains(key)) {
|
if (!supportedLeaves.contains(key)) {
|
||||||
fallingLeaves.add(new int[]{x, y});
|
fallingLeaves.add(new int[] { x, y });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fallingLeaves.isEmpty() && random.nextInt(4) < 1) {
|
if (!fallingLeaves.isEmpty() && random.nextInt(4) < 1) {
|
||||||
int[] leafToRemove = fallingLeaves.get(random.nextInt(fallingLeaves.size()));
|
List<int[]> safeFallingLeaves = fallingLeaves.stream()
|
||||||
|
.filter(leaf -> leaf[0] > startX + SAFE_MARGIN &&
|
||||||
|
leaf[0] < endX - SAFE_MARGIN &&
|
||||||
|
leaf[1] > startY + SAFE_MARGIN &&
|
||||||
|
leaf[1] < endY - SAFE_MARGIN)
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
if (!safeFallingLeaves.isEmpty()) {
|
||||||
|
int[] leafToRemove = safeFallingLeaves.get(random.nextInt(safeFallingLeaves.size()));
|
||||||
int leafX = leafToRemove[0];
|
int leafX = leafToRemove[0];
|
||||||
int leafY = leafToRemove[1];
|
int leafY = leafToRemove[1];
|
||||||
|
|
||||||
world[leafY][leafX].removeIf(block -> block.getBlockId().equals("oak_leaves"));
|
world[leafY][leafX].removeIf(block -> block.getBlockId().equals("oak_leaves"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Set<String> findSupportedLeaves(List<Block>[][] world, int startX, int startY, int endX, int endY) {
|
private Set<String> findSupportedLeaves(List<Block>[][] world, int startX, int startY, int endX, int endY) {
|
||||||
Set<String> supportedLeaves = new HashSet<>();
|
Set<String> supportedLeaves = new HashSet<>();
|
||||||
@ -56,12 +65,12 @@ public class LeavesFallingLogic implements CustomLogicInterface {
|
|||||||
for (int y = startY; y <= endY; y++) {
|
for (int y = startY; y <= endY; y++) {
|
||||||
List<Block> blocks = world[y][x];
|
List<Block> blocks = world[y][x];
|
||||||
if (blocks.stream().anyMatch(block -> block.getBlockId().equals("oak_log"))) {
|
if (blocks.stream().anyMatch(block -> block.getBlockId().equals("oak_log"))) {
|
||||||
queue.add(new int[]{x, y});
|
queue.add(new int[] { x, y });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int[][] directions = {{0,1}, {1,0}, {0,-1}, {-1,0}, {-1,1}, {1,1}, {-1,-1}, {1,-1}};
|
int[][] directions = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 }, { -1, 1 }, { 1, 1 }, { -1, -1 }, { 1, -1 } };
|
||||||
|
|
||||||
while (!queue.isEmpty()) {
|
while (!queue.isEmpty()) {
|
||||||
int[] pos = queue.poll();
|
int[] pos = queue.poll();
|
||||||
@ -78,7 +87,7 @@ public class LeavesFallingLogic implements CustomLogicInterface {
|
|||||||
String key = nx + "," + ny;
|
String key = nx + "," + ny;
|
||||||
if (!supportedLeaves.contains(key)) {
|
if (!supportedLeaves.contains(key)) {
|
||||||
supportedLeaves.add(key);
|
supportedLeaves.add(key);
|
||||||
queue.add(new int[]{nx, ny});
|
queue.add(new int[] { nx, ny });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user