Let lumberers stand under canopies and clear decaying leaves that block a path.
Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -22,6 +22,7 @@ import org.bukkit.block.data.Levelled;
22
22
import org.bukkit.block.data.Openable;
23
23
import org.bukkit.block.data.type.Door;
24
24
import org.bukkit.block.data.type.Gate;
25
import org.bukkit.block.data.type.Leaves;
25
26
import org.bukkit.entity.CopperGolem;
26
27
import org.bukkit.entity.Entity;
27
28
import org.bukkit.entity.Player;
@@ -297,6 +298,7 @@ final class FarmerManager {
297
298
private Material saplingType;
298
299
private final Set<UUID> unreachableTrees = new HashSet<>();
299
300
private int emptyTreeScans;
301
private boolean clearingLeaves;
300
302
301
303
Brain(CopperGolem golem) {
302
304
this.golem = golem;
@@ -419,21 +421,47 @@ final class FarmerManager {
419
421
activeTree = tree;
420
422
saplingType = tree.species().sapling();
421
423
chopIndex = 0;
424
clearingLeaves = false;
425
if (tryPathToTree(tree) || beginClearingLeavesToTree(tree)) return true;
426
tree.release(golem.getUniqueId());
427
activeTree = null;
428
saplingType = null;
429
return false;
430
}
431
432
private boolean tryPathToTree(DetectedTree tree) {
433
if (tree == null) return false;
422
434
for (BlockRef log : tree.standOrder()) {
423
Block block = FarmerManager.this.block(log);
424
if (block == null || !tree.species().isLog(block.getType())) continue;
425
if (beginPathToBlock(block, State.MOVING_TO_CROP)) {
435
Block logBlock = FarmerManager.this.block(log);
436
if (logBlock == null || !tree.species().isLog(logBlock.getType())) continue;
437
if (beginPathToBlock(logBlock, State.MOVING_TO_CROP)) {
426
438
cropTarget = log;
439
clearingLeaves = false;
440
return true;
441
}
442
}
443
return false;
444
}
445
446
private boolean beginClearingLeavesToTree(DetectedTree tree) {
447
if (tree == null) return false;
448
for (BlockRef leaf : findBlockingLeaves(tree)) {
449
Block leafBlock = FarmerManager.this.block(leaf);
450
if (leafBlock == null || !isDecayingLeaf(leafBlock)) continue;
451
if (beginPathToBlock(leafBlock, State.MOVING_TO_CROP)) {
452
cropTarget = leaf;
453
clearingLeaves = true;
427
454
return true;
428
455
}
429
456
}
430
tree.release(golem.getUniqueId());
431
activeTree = null;
432
saplingType = null;
433
457
return false;
434
458
}
435
459
436
460
private void tickMovingToTree() {
461
if (clearingLeaves) {
462
tickMovingToClearanceLeaf();
463
return;
464
}
437
465
if (activeTree == null) {
438
466
resetCropTask();
439
467
return;
@@ -470,7 +498,32 @@ final class FarmerManager {
470
498
}
471
499
}
472
500
501
private void tickMovingToClearanceLeaf() {
502
Block leaf = block(cropTarget);
503
if (leaf == null || !isDecayingLeaf(leaf)) {
504
finishLeafClear();
505
return;
506
}
507
508
if (reachedNavigationTarget() && near(leaf)) {
509
stopNavigation();
510
beginHarvest(leaf);
511
return;
512
}
513
514
if (!navigationHealthy() && !retryPath()) {
515
if (!beginClearingLeavesToTree(activeTree)) {
516
if (activeTree != null) unreachableTrees.add(activeTree.id());
517
resetCropTask();
518
}
519
}
520
}
521
473
522
private void tickWaitingToChop() {
523
if (clearingLeaves) {
524
tickWaitingToChopLeaf();
525
return;
526
}
474
527
if (activeTree == null) {
475
528
clearHarvestCrack();
476
529
resetCropTask();
@@ -496,6 +549,45 @@ final class FarmerManager {
496
549
chopLog(logBlock);
497
550
}
498
551
552
private void tickWaitingToChopLeaf() {
553
Block leaf = block(cropTarget);
554
if (leaf == null || !isDecayingLeaf(leaf)) {
555
clearHarvestCrack();
556
finishLeafClear();
557
return;
558
}
559
560
lookAt(leaf);
561
playInteractAnimation();
562
float progress = harvestBreakTicks <= 1
563
? 1.0f
564
: Math.min(1.0f, (clock - harvestStartTick + 1f) / harvestBreakTicks);
565
sendHarvestCrack(leaf, progress);
566
567
if (clock < harvestReadyTick) return;
568
clearHarvestCrack();
569
chopLeaf(leaf);
570
finishLeafClear();
571
}
572
573
private void chopLeaf(Block leaf) {
574
playHarvestEffects(leaf);
575
List<ItemStack> calculatedDrops = new ArrayList<>(leaf.getDrops(tool.clone(), golem));
576
leaf.setType(Material.AIR, true);
577
List<ItemStack> overflow = cargo.addAll(calculatedDrops);
578
for (ItemStack item : overflow) {
579
leaf.getWorld().dropItemNaturally(leaf.getLocation().add(0.5, 0.5, 0.5), item);
580
}
581
}
582
583
private void finishLeafClear() {
584
clearingLeaves = false;
585
cropTarget = null;
586
if (tryPathToTree(activeTree) || beginClearingLeavesToTree(activeTree)) return;
587
if (activeTree != null) unreachableTrees.add(activeTree.id());
588
resetCropTask();
589
}
590
499
591
private void chopLog(Block logBlock) {
500
592
playHarvestEffects(logBlock);
501
593
List<ItemStack> calculatedDrops = new ArrayList<>(logBlock.getDrops(tool.clone(), golem));
@@ -1314,10 +1406,9 @@ final class FarmerManager {
1314
1406
return golem.getPathfinder().moveTo(path, moveSpeed);
1315
1407
}
1316
1408
1317
private boolean beginPathToBlock(Block target, State newState) {
1318
if (target == null) return false;
1319
1409
private List<Block> standPositions(Block target) {
1320
1410
List<Block> positions = new ArrayList<>();
1411
if (target == null) return positions;
1321
1412
Set<BlockRef> seen = new HashSet<>();
1322
1413
for (int dy = 0; dy >= -2; dy--) {
1323
1414
for (BlockFace face : HORIZONTAL) {
@@ -1329,6 +1420,13 @@ final class FarmerManager {
1329
1420
if (seen.add(BlockRef.of(below)) && isStandable(below)) positions.add(below);
1330
1421
}
1331
1422
}
1423
return positions;
1424
}
1425
1426
private boolean beginPathToBlock(Block target, State newState) {
1427
if (target == null) return false;
1428
1429
List<Block> positions = standPositions(target);
1332
1430
1333
1431
Pathfinder.PathResult bestPath = null;
1334
1432
Location bestDestination = null;
@@ -1534,11 +1632,88 @@ final class FarmerManager {
1534
1632
}
1535
1633
1536
1634
private boolean isStandable(Block feet) {
1537
if (!feet.isPassable() || !feet.getRelative(BlockFace.UP).isPassable()) return false;
1635
if (feet == null || !feet.isPassable()) return false;
1538
1636
Material below = feet.getRelative(BlockFace.DOWN).getType();
1539
1637
return below.isSolid() || below == Material.FARMLAND || below == Material.SOUL_SAND;
1540
1638
}
1541
1639
1640
private boolean isDecayingLeaf(Block block) {
1641
return block != null && block.getBlockData() instanceof Leaves leaves && !leaves.isPersistent();
1642
}
1643
1644
private boolean isLeafWalkable(Block feet) {
1645
if (feet == null || !feet.getWorld().isChunkLoaded(feet.getX() >> 4, feet.getZ() >> 4)) return false;
1646
if (!feet.isPassable() && !isDecayingLeaf(feet)) return false;
1647
Material below = feet.getRelative(BlockFace.DOWN).getType();
1648
return below.isSolid() || below == Material.FARMLAND || below == Material.SOUL_SAND;
1649
}
1650
1651
private List<Block> leafWalkNeighbors(Block current) {
1652
List<Block> next = new ArrayList<>(12);
1653
for (BlockFace face : HORIZONTAL) {
1654
for (int dy = 1; dy >= -1; dy--) {
1655
Block candidate = current.getRelative(face.getModX(), dy, face.getModZ());
1656
if (isLeafWalkable(candidate)) next.add(candidate);
1657
}
1658
}
1659
return next;
1660
}
1661
1662
private List<BlockRef> findBlockingLeaves(DetectedTree tree) {
1663
Set<BlockRef> goals = new HashSet<>();
1664
for (BlockRef log : tree.standOrder()) {
1665
Block logBlock = FarmerManager.this.block(log);
1666
if (logBlock == null || !tree.species().isLog(logBlock.getType())) continue;
1667
for (Block stand : standPositions(logBlock)) goals.add(BlockRef.of(stand));
1668
}
1669
if (goals.isEmpty()) return List.of();
1670
1671
Block start = golem.getLocation().getBlock();
1672
if (!isLeafWalkable(start)) {
1673
Block down = start.getRelative(BlockFace.DOWN);
1674
if (isLeafWalkable(down)) start = down;
1675
else return List.of();
1676
}
1677
1678
Deque<Block> queue = new ArrayDeque<>();
1679
Map<BlockRef, BlockRef> cameFrom = new HashMap<>();
1680
Set<BlockRef> visited = new HashSet<>();
1681
BlockRef startRef = BlockRef.of(start);
1682
queue.add(start);
1683
visited.add(startRef);
1684
1685
BlockRef goalHit = null;
1686
int visits = 0;
1687
int limit = Math.max(64, cropRadius * cropRadius);
1688
while (!queue.isEmpty() && visits++ < limit) {
1689
Block current = queue.pollFirst();
1690
BlockRef currentRef = BlockRef.of(current);
1691
if (goals.contains(currentRef)) {
1692
goalHit = currentRef;
1693
break;
1694
}
1695
for (Block neighbour : leafWalkNeighbors(current)) {
1696
BlockRef nextRef = BlockRef.of(neighbour);
1697
if (!visited.add(nextRef)) continue;
1698
cameFrom.put(nextRef, currentRef);
1699
queue.addLast(neighbour);
1700
}
1701
}
1702
if (goalHit == null) return List.of();
1703
1704
List<BlockRef> leaves = new ArrayList<>();
1705
BlockRef step = goalHit;
1706
while (step != null) {
1707
Block cell = FarmerManager.this.block(step);
1708
if (isDecayingLeaf(cell)) leaves.add(step);
1709
if (step.equals(startRef)) break;
1710
step = cameFrom.get(step);
1711
}
1712
List<BlockRef> nearestFirst = new ArrayList<>(leaves.size());
1713
for (int i = leaves.size() - 1; i >= 0; i--) nearestFirst.add(leaves.get(i));
1714
return nearestFirst;
1715
}
1716
1542
1717
private boolean near(Block block) {
1543
1718
if (block == null || block.getWorld() != golem.getWorld()) return false;
1544
1719
Location eye = golem.getEyeLocation();
@@ -1600,6 +1775,7 @@ final class FarmerManager {
1600
1775
}
1601
1776
saplingType = null;
1602
1777
chopIndex = 0;
1778
clearingLeaves = false;
1603
1779
}
1604
1780
1605
1781
private void stopNavigation() {
@@ -22,6 +22,7 @@ import org.bukkit.block.data.Levelled;22
import org.bukkit.block.data.Openable;22
import org.bukkit.block.data.Openable;23
import org.bukkit.block.data.type.Door;23
import org.bukkit.block.data.type.Door;24
import org.bukkit.block.data.type.Gate;24
import org.bukkit.block.data.type.Gate;25
import org.bukkit.block.data.type.Leaves;25
import org.bukkit.entity.CopperGolem;26
import org.bukkit.entity.CopperGolem;26
import org.bukkit.entity.Entity;27
import org.bukkit.entity.Entity;27
import org.bukkit.entity.Player;28
import org.bukkit.entity.Player;@@ -297,6 +298,7 @@ final class FarmerManager {297
private Material saplingType;298
private Material saplingType;298
private final Set<UUID> unreachableTrees = new HashSet<>();299
private final Set<UUID> unreachableTrees = new HashSet<>();299
private int emptyTreeScans;300
private int emptyTreeScans;301
private boolean clearingLeaves;300
302
301
Brain(CopperGolem golem) {303
Brain(CopperGolem golem) {302
this.golem = golem;304
this.golem = golem;@@ -419,21 +421,47 @@ final class FarmerManager {419
activeTree = tree;421
activeTree = tree;420
saplingType = tree.species().sapling();422
saplingType = tree.species().sapling();421
chopIndex = 0;423
chopIndex = 0;424
clearingLeaves = false;425
if (tryPathToTree(tree) || beginClearingLeavesToTree(tree)) return true;426
tree.release(golem.getUniqueId());427
activeTree = null;428
saplingType = null;429
return false;430
}431
432
private boolean tryPathToTree(DetectedTree tree) {433
if (tree == null) return false;422
for (BlockRef log : tree.standOrder()) {434
for (BlockRef log : tree.standOrder()) {423
Block block = FarmerManager.this.block(log);435
Block logBlock = FarmerManager.this.block(log);424
if (block == null || !tree.species().isLog(block.getType())) continue;436
if (logBlock == null || !tree.species().isLog(logBlock.getType())) continue;425
if (beginPathToBlock(block, State.MOVING_TO_CROP)) {437
if (beginPathToBlock(logBlock, State.MOVING_TO_CROP)) {426
cropTarget = log;438
cropTarget = log;439
clearingLeaves = false;440
return true;441
}442
}443
return false;444
}445
446
private boolean beginClearingLeavesToTree(DetectedTree tree) {447
if (tree == null) return false;448
for (BlockRef leaf : findBlockingLeaves(tree)) {449
Block leafBlock = FarmerManager.this.block(leaf);450
if (leafBlock == null || !isDecayingLeaf(leafBlock)) continue;451
if (beginPathToBlock(leafBlock, State.MOVING_TO_CROP)) {452
cropTarget = leaf;453
clearingLeaves = true;427
return true;454
return true;428
}455
}429
}456
}430
tree.release(golem.getUniqueId());431
activeTree = null;432
saplingType = null;433
return false;457
return false;434
}458
}435
459
436
private void tickMovingToTree() {460
private void tickMovingToTree() {461
if (clearingLeaves) {462
tickMovingToClearanceLeaf();463
return;464
}437
if (activeTree == null) {465
if (activeTree == null) {438
resetCropTask();466
resetCropTask();439
return;467
return;@@ -470,7 +498,32 @@ final class FarmerManager {470
}498
}471
}499
}472
500
501
private void tickMovingToClearanceLeaf() {502
Block leaf = block(cropTarget);503
if (leaf == null || !isDecayingLeaf(leaf)) {504
finishLeafClear();505
return;506
}507
508
if (reachedNavigationTarget() && near(leaf)) {509
stopNavigation();510
beginHarvest(leaf);511
return;512
}513
514
if (!navigationHealthy() && !retryPath()) {515
if (!beginClearingLeavesToTree(activeTree)) {516
if (activeTree != null) unreachableTrees.add(activeTree.id());517
resetCropTask();518
}519
}520
}521
473
private void tickWaitingToChop() {522
private void tickWaitingToChop() {523
if (clearingLeaves) {524
tickWaitingToChopLeaf();525
return;526
}474
if (activeTree == null) {527
if (activeTree == null) {475
clearHarvestCrack();528
clearHarvestCrack();476
resetCropTask();529
resetCropTask();@@ -496,6 +549,45 @@ final class FarmerManager {496
chopLog(logBlock);549
chopLog(logBlock);497
}550
}498
551
552
private void tickWaitingToChopLeaf() {553
Block leaf = block(cropTarget);554
if (leaf == null || !isDecayingLeaf(leaf)) {555
clearHarvestCrack();556
finishLeafClear();557
return;558
}559
560
lookAt(leaf);561
playInteractAnimation();562
float progress = harvestBreakTicks <= 1563
? 1.0f564
: Math.min(1.0f, (clock - harvestStartTick + 1f) / harvestBreakTicks);565
sendHarvestCrack(leaf, progress);566
567
if (clock < harvestReadyTick) return;568
clearHarvestCrack();569
chopLeaf(leaf);570
finishLeafClear();571
}572
573
private void chopLeaf(Block leaf) {574
playHarvestEffects(leaf);575
List<ItemStack> calculatedDrops = new ArrayList<>(leaf.getDrops(tool.clone(), golem));576
leaf.setType(Material.AIR, true);577
List<ItemStack> overflow = cargo.addAll(calculatedDrops);578
for (ItemStack item : overflow) {579
leaf.getWorld().dropItemNaturally(leaf.getLocation().add(0.5, 0.5, 0.5), item);580
}581
}582
583
private void finishLeafClear() {584
clearingLeaves = false;585
cropTarget = null;586
if (tryPathToTree(activeTree) || beginClearingLeavesToTree(activeTree)) return;587
if (activeTree != null) unreachableTrees.add(activeTree.id());588
resetCropTask();589
}590
499
private void chopLog(Block logBlock) {591
private void chopLog(Block logBlock) {500
playHarvestEffects(logBlock);592
playHarvestEffects(logBlock);501
List<ItemStack> calculatedDrops = new ArrayList<>(logBlock.getDrops(tool.clone(), golem));593
List<ItemStack> calculatedDrops = new ArrayList<>(logBlock.getDrops(tool.clone(), golem));@@ -1314,10 +1406,9 @@ final class FarmerManager {1314
return golem.getPathfinder().moveTo(path, moveSpeed);1406
return golem.getPathfinder().moveTo(path, moveSpeed);1315
}1407
}1316
1408
1317
private boolean beginPathToBlock(Block target, State newState) {1409
private List<Block> standPositions(Block target) {1318
if (target == null) return false;1319
1320
List<Block> positions = new ArrayList<>();1410
List<Block> positions = new ArrayList<>();1411
if (target == null) return positions;1321
Set<BlockRef> seen = new HashSet<>();1412
Set<BlockRef> seen = new HashSet<>();1322
for (int dy = 0; dy >= -2; dy--) {1413
for (int dy = 0; dy >= -2; dy--) {1323
for (BlockFace face : HORIZONTAL) {1414
for (BlockFace face : HORIZONTAL) {@@ -1329,6 +1420,13 @@ final class FarmerManager {1329
if (seen.add(BlockRef.of(below)) && isStandable(below)) positions.add(below);1420
if (seen.add(BlockRef.of(below)) && isStandable(below)) positions.add(below);1330
}1421
}1331
}1422
}1423
return positions;1424
}1425
1426
private boolean beginPathToBlock(Block target, State newState) {1427
if (target == null) return false;1428
1429
List<Block> positions = standPositions(target);1332
1430
1333
Pathfinder.PathResult bestPath = null;1431
Pathfinder.PathResult bestPath = null;1334
Location bestDestination = null;1432
Location bestDestination = null;@@ -1534,11 +1632,88 @@ final class FarmerManager {1534
}1632
}1535
1633
1536
private boolean isStandable(Block feet) {1634
private boolean isStandable(Block feet) {1537
if (!feet.isPassable() || !feet.getRelative(BlockFace.UP).isPassable()) return false;1635
if (feet == null || !feet.isPassable()) return false;1538
Material below = feet.getRelative(BlockFace.DOWN).getType();1636
Material below = feet.getRelative(BlockFace.DOWN).getType();1539
return below.isSolid() || below == Material.FARMLAND || below == Material.SOUL_SAND;1637
return below.isSolid() || below == Material.FARMLAND || below == Material.SOUL_SAND;1540
}1638
}1541
1639
1640
private boolean isDecayingLeaf(Block block) {1641
return block != null && block.getBlockData() instanceof Leaves leaves && !leaves.isPersistent();1642
}1643
1644
private boolean isLeafWalkable(Block feet) {1645
if (feet == null || !feet.getWorld().isChunkLoaded(feet.getX() >> 4, feet.getZ() >> 4)) return false;1646
if (!feet.isPassable() && !isDecayingLeaf(feet)) return false;1647
Material below = feet.getRelative(BlockFace.DOWN).getType();1648
return below.isSolid() || below == Material.FARMLAND || below == Material.SOUL_SAND;1649
}1650
1651
private List<Block> leafWalkNeighbors(Block current) {1652
List<Block> next = new ArrayList<>(12);1653
for (BlockFace face : HORIZONTAL) {1654
for (int dy = 1; dy >= -1; dy--) {1655
Block candidate = current.getRelative(face.getModX(), dy, face.getModZ());1656
if (isLeafWalkable(candidate)) next.add(candidate);1657
}1658
}1659
return next;1660
}1661
1662
private List<BlockRef> findBlockingLeaves(DetectedTree tree) {1663
Set<BlockRef> goals = new HashSet<>();1664
for (BlockRef log : tree.standOrder()) {1665
Block logBlock = FarmerManager.this.block(log);1666
if (logBlock == null || !tree.species().isLog(logBlock.getType())) continue;1667
for (Block stand : standPositions(logBlock)) goals.add(BlockRef.of(stand));1668
}1669
if (goals.isEmpty()) return List.of();1670
1671
Block start = golem.getLocation().getBlock();1672
if (!isLeafWalkable(start)) {1673
Block down = start.getRelative(BlockFace.DOWN);1674
if (isLeafWalkable(down)) start = down;1675
else return List.of();1676
}1677
1678
Deque<Block> queue = new ArrayDeque<>();1679
Map<BlockRef, BlockRef> cameFrom = new HashMap<>();1680
Set<BlockRef> visited = new HashSet<>();1681
BlockRef startRef = BlockRef.of(start);1682
queue.add(start);1683
visited.add(startRef);1684
1685
BlockRef goalHit = null;1686
int visits = 0;1687
int limit = Math.max(64, cropRadius * cropRadius);1688
while (!queue.isEmpty() && visits++ < limit) {1689
Block current = queue.pollFirst();1690
BlockRef currentRef = BlockRef.of(current);1691
if (goals.contains(currentRef)) {1692
goalHit = currentRef;1693
break;1694
}1695
for (Block neighbour : leafWalkNeighbors(current)) {1696
BlockRef nextRef = BlockRef.of(neighbour);1697
if (!visited.add(nextRef)) continue;1698
cameFrom.put(nextRef, currentRef);1699
queue.addLast(neighbour);1700
}1701
}1702
if (goalHit == null) return List.of();1703
1704
List<BlockRef> leaves = new ArrayList<>();1705
BlockRef step = goalHit;1706
while (step != null) {1707
Block cell = FarmerManager.this.block(step);1708
if (isDecayingLeaf(cell)) leaves.add(step);1709
if (step.equals(startRef)) break;1710
step = cameFrom.get(step);1711
}1712
List<BlockRef> nearestFirst = new ArrayList<>(leaves.size());1713
for (int i = leaves.size() - 1; i >= 0; i--) nearestFirst.add(leaves.get(i));1714
return nearestFirst;1715
}1716
1542
private boolean near(Block block) {1717
private boolean near(Block block) {1543
if (block == null || block.getWorld() != golem.getWorld()) return false;1718
if (block == null || block.getWorld() != golem.getWorld()) return false;1544
Location eye = golem.getEyeLocation();1719
Location eye = golem.getEyeLocation();@@ -1600,6 +1775,7 @@ final class FarmerManager {1600
}1775
}1601
saplingType = null;1776
saplingType = null;1602
chopIndex = 0;1777
chopIndex = 0;1778
clearingLeaves = false;1603
}1779
}1604
1780
1605
private void stopNavigation() {1781
private void stopNavigation() {