BlueGit

timfa copperfarmer

Refresh the lumberer tree cache after five idle minutes without a target.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-08-22 · 2 months ago

Hash
2b740371a15de4e92e772b15d8c1ff42ff25d239
Parent
73214548
2 files changed +36 −0
src/main/java/nl/timfalken/copperfarmer/FarmerManager.java +10 −0
@@ -78,6 +78,7 @@ final class FarmerManager {
78 78 private final long afterDepositScanTicks;
79 79 private final int cargoSlots;
80 80 private final boolean preventOxidation;
81 private final int emptyTreeScansBeforeInvalidate;
81 82
82 83 private long clock;
83 84
@@ -107,6 +108,7 @@ final class FarmerManager {
107 108 this.afterDepositScanTicks = Math.max(1L, plugin.getConfig().getLong("after-deposit-scan-ticks", 40L));
108 109 this.cargoSlots = Math.max(1, plugin.getConfig().getInt("cargo-slots", 9));
109 110 this.preventOxidation = plugin.getConfig().getBoolean("prevent-oxidation", false);
111 this.emptyTreeScansBeforeInvalidate = Math.max(1, (int) ((5L * 60L * 20L) / this.scanInterval));
110 112 }
111 113
112 114 void tick(long elapsedTicks) {
@@ -294,6 +296,7 @@ final class FarmerManager {
294 296 private int chopIndex;
295 297 private Material saplingType;
296 298 private final Set<UUID> unreachableTrees = new HashSet<>();
299 private int emptyTreeScans;
297 300
298 301 Brain(CopperGolem golem) {
299 302 this.golem = golem;
@@ -394,10 +397,17 @@ final class FarmerManager {
394 397 return;
395 398 }
396 399 unreachableTrees.clear();
400 emptyTreeScans++;
401 if (emptyTreeScans >= emptyTreeScansBeforeInvalidate) {
402 emptyTreeScans = 0;
403 treeIndex.invalidateAround(chestBlock, cropRadius);
404 treeIndex.requestAround(chestBlock, cropRadius);
405 }
397 406 nextScanTick = clock + scanInterval;
398 407 return;
399 408 }
400 409
410 emptyTreeScans = 0;
401 411 if (!beginChopTree(tree)) {
402 412 unreachableTrees.add(tree.id());
403 413 nextScanTick = clock + 20;
@@ -78,6 +78,7 @@ final class FarmerManager {
78 private final long afterDepositScanTicks;
78 private final long afterDepositScanTicks;
79 private final int cargoSlots;
79 private final int cargoSlots;
80 private final boolean preventOxidation;
80 private final boolean preventOxidation;
81 private final int emptyTreeScansBeforeInvalidate;
81
82
82 private long clock;
83 private long clock;
83
84
@@ -107,6 +108,7 @@ final class FarmerManager {
107 this.afterDepositScanTicks = Math.max(1L, plugin.getConfig().getLong("after-deposit-scan-ticks", 40L));
108 this.afterDepositScanTicks = Math.max(1L, plugin.getConfig().getLong("after-deposit-scan-ticks", 40L));
108 this.cargoSlots = Math.max(1, plugin.getConfig().getInt("cargo-slots", 9));
109 this.cargoSlots = Math.max(1, plugin.getConfig().getInt("cargo-slots", 9));
109 this.preventOxidation = plugin.getConfig().getBoolean("prevent-oxidation", false);
110 this.preventOxidation = plugin.getConfig().getBoolean("prevent-oxidation", false);
111 this.emptyTreeScansBeforeInvalidate = Math.max(1, (int) ((5L * 60L * 20L) / this.scanInterval));
110 }
112 }
111
113
112 void tick(long elapsedTicks) {
114 void tick(long elapsedTicks) {
@@ -294,6 +296,7 @@ final class FarmerManager {
294 private int chopIndex;
296 private int chopIndex;
295 private Material saplingType;
297 private Material saplingType;
296 private final Set<UUID> unreachableTrees = new HashSet<>();
298 private final Set<UUID> unreachableTrees = new HashSet<>();
299 private int emptyTreeScans;
297
300
298 Brain(CopperGolem golem) {
301 Brain(CopperGolem golem) {
299 this.golem = golem;
302 this.golem = golem;
@@ -394,10 +397,17 @@ final class FarmerManager {
394 return;
397 return;
395 }
398 }
396 unreachableTrees.clear();
399 unreachableTrees.clear();
400 emptyTreeScans++;
401 if (emptyTreeScans >= emptyTreeScansBeforeInvalidate) {
402 emptyTreeScans = 0;
403 treeIndex.invalidateAround(chestBlock, cropRadius);
404 treeIndex.requestAround(chestBlock, cropRadius);
405 }
397 nextScanTick = clock + scanInterval;
406 nextScanTick = clock + scanInterval;
398 return;
407 return;
399 }
408 }
400
409
410 emptyTreeScans = 0;
401 if (!beginChopTree(tree)) {
411 if (!beginChopTree(tree)) {
402 unreachableTrees.add(tree.id());
412 unreachableTrees.add(tree.id());
403 nextScanTick = clock + 20;
413 nextScanTick = clock + 20;
src/main/java/nl/timfalken/copperfarmer/TreeIndex.java +26 −0
@@ -105,6 +105,20 @@ final class TreeIndex {
105 105 }
106 106 }
107 107
108 void invalidateAround(Block origin, int radius) {
109 if (origin == null) return;
110 World world = origin.getWorld();
111 int chunkRadius = (radius >> 4) + 2;
112 int centerX = origin.getChunk().getX();
113 int centerZ = origin.getChunk().getZ();
114 UUID worldId = world.getUID();
115 for (int cx = centerX - chunkRadius; cx <= centerX + chunkRadius; cx++) {
116 for (int cz = centerZ - chunkRadius; cz <= centerZ + chunkRadius; cz++) {
117 forceInvalidateChunk(worldId, cx, cz);
118 }
119 }
120 }
121
108 122 boolean isScanningAround(Block origin, int radius) {
109 123 if (origin == null) return false;
110 124 int chunkRadius = (radius >> 4) + 2;
@@ -437,6 +451,18 @@ final class TreeIndex {
437 451 }
438 452 }
439 453
454 private void forceInvalidateChunk(UUID worldId, int chunkX, int chunkZ) {
455 ChunkKey key = new ChunkKey(worldId, chunkX, chunkZ);
456 removeTreesTouching(key);
457 ChunkScan scan = chunks.get(key);
458 if (scan == null) {
459 scan = new ChunkScan(key);
460 chunks.put(key, scan);
461 }
462 scan.status = Status.DIRTY;
463 scan.nextColumn = 0;
464 }
465
440 466 private void removeTreesTouching(ChunkKey key) {
441 467 List<UUID> remove = new ArrayList<>();
442 468 for (DetectedTree tree : trees.values()) {
@@ -105,6 +105,20 @@ final class TreeIndex {
105 }
105 }
106 }
106 }
107
107
108 void invalidateAround(Block origin, int radius) {
109 if (origin == null) return;
110 World world = origin.getWorld();
111 int chunkRadius = (radius >> 4) + 2;
112 int centerX = origin.getChunk().getX();
113 int centerZ = origin.getChunk().getZ();
114 UUID worldId = world.getUID();
115 for (int cx = centerX - chunkRadius; cx <= centerX + chunkRadius; cx++) {
116 for (int cz = centerZ - chunkRadius; cz <= centerZ + chunkRadius; cz++) {
117 forceInvalidateChunk(worldId, cx, cz);
118 }
119 }
120 }
121
108 boolean isScanningAround(Block origin, int radius) {
122 boolean isScanningAround(Block origin, int radius) {
109 if (origin == null) return false;
123 if (origin == null) return false;
110 int chunkRadius = (radius >> 4) + 2;
124 int chunkRadius = (radius >> 4) + 2;
@@ -437,6 +451,18 @@ final class TreeIndex {
437 }
451 }
438 }
452 }
439
453
454 private void forceInvalidateChunk(UUID worldId, int chunkX, int chunkZ) {
455 ChunkKey key = new ChunkKey(worldId, chunkX, chunkZ);
456 removeTreesTouching(key);
457 ChunkScan scan = chunks.get(key);
458 if (scan == null) {
459 scan = new ChunkScan(key);
460 chunks.put(key, scan);
461 }
462 scan.status = Status.DIRTY;
463 scan.nextColumn = 0;
464 }
465
440 private void removeTreesTouching(ChunkKey key) {
466 private void removeTreesTouching(ChunkKey key) {
441 List<UUID> remove = new ArrayList<>();
467 List<UUID> remove = new ArrayList<>();
442 for (DetectedTree tree : trees.values()) {
468 for (DetectedTree tree : trees.values()) {