diff --git a/src/main/java/cz/jzitnik/game/logic/services/leaves/LeavesFallingLogic.java b/src/main/java/cz/jzitnik/game/logic/services/leaves/LeavesFallingLogic.java index 07beb05..d6639ae 100644 --- a/src/main/java/cz/jzitnik/game/logic/services/leaves/LeavesFallingLogic.java +++ b/src/main/java/cz/jzitnik/game/logic/services/leaves/LeavesFallingLogic.java @@ -10,6 +10,7 @@ import java.util.*; @CustomLogic public class LeavesFallingLogic implements CustomLogicInterface { private static final int RADIUS = 30; + private static final int SAFE_MARGIN = 5; private final Random random = new Random(); @Override @@ -33,18 +34,26 @@ public class LeavesFallingLogic implements CustomLogicInterface { if (blocks.stream().anyMatch(block -> block.getBlockId().equals("oak_leaves"))) { String key = x + "," + y; if (!supportedLeaves.contains(key)) { - fallingLeaves.add(new int[]{x, y}); + fallingLeaves.add(new int[] { x, y }); } } } } if (!fallingLeaves.isEmpty() && random.nextInt(4) < 1) { - int[] leafToRemove = fallingLeaves.get(random.nextInt(fallingLeaves.size())); - int leafX = leafToRemove[0]; - int leafY = leafToRemove[1]; + List 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(); - world[leafY][leafX].removeIf(block -> block.getBlockId().equals("oak_leaves")); + if (!safeFallingLeaves.isEmpty()) { + int[] leafToRemove = safeFallingLeaves.get(random.nextInt(safeFallingLeaves.size())); + int leafX = leafToRemove[0]; + int leafY = leafToRemove[1]; + world[leafY][leafX].removeIf(block -> block.getBlockId().equals("oak_leaves")); + } } } @@ -56,12 +65,12 @@ public class LeavesFallingLogic implements CustomLogicInterface { for (int y = startY; y <= endY; y++) { List blocks = world[y][x]; 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()) { int[] pos = queue.poll(); @@ -78,7 +87,7 @@ public class LeavesFallingLogic implements CustomLogicInterface { String key = nx + "," + ny; if (!supportedLeaves.contains(key)) { supportedLeaves.add(key); - queue.add(new int[]{nx, ny}); + queue.add(new int[] { nx, ny }); } } }