BlueGit

timfa copperfarmer

Keep bound chest memory when its chunk is unloaded.

Do not clear chest bindings for unloaded chunks; path toward remembered coordinates without force-loading.

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

timfa Tim Falken · pushed by timfa

Authored 2026-08-15 · 3 months ago

Hash
76d1052dab2eedc5128c7dfabb1e5bf9329cb858
Parent
002758a5
6 files changed +76 −19
README.md +1 −1
@@ -1,4 +1,4 @@
1 # CopperFarmer 0.1.12
1 # CopperFarmer 0.1.13
2 2
3 3 First-version Paper 26.2 / Java 25 plugin for autonomous Copper Farmer Golems.
4 4
@@ -1,4 +1,4 @@
1# CopperFarmer 0.1.12
1# CopperFarmer 0.1.13
2
2
3First-version Paper 26.2 / Java 25 plugin for autonomous Copper Farmer Golems.
3First-version Paper 26.2 / Java 25 plugin for autonomous Copper Farmer Golems.
4
4
TESTING.md +2 −1
@@ -1,4 +1,4 @@
1 # CopperFarmer 0.1.12 smoke test
1 # CopperFarmer 0.1.13 smoke test
2 2
3 3 Use this checklist on a Paper 26.2 server before treating 0.1.0 as production-ready.
4 4
@@ -24,6 +24,7 @@ Use this checklist on a Paper 26.2 server before treating 0.1.0 as production-re
24 24 - [ ] Put a closer chest in an unloaded chunk and verify CopperFarmer does not force-load that chunk.
25 25 - [ ] Break the bound chest while the farmer is carrying items and verify it searches for another copper chest.
26 26 - [ ] Make the bound chest unreachable and verify the farmer eventually abandons it after the configured path retries/cooldown.
27 - [ ] With the farmer loaded and its bound chest chunk unloaded, verify the chest binding is kept (not cleared from PDC).
27 28 - [ ] Fill the chest completely and verify the farmer keeps its cargo instead of dropping/deleting it.
28 29
29 30 ## Crops
@@ -1,4 +1,4 @@
1# CopperFarmer 0.1.12 smoke test
1# CopperFarmer 0.1.13 smoke test
2
2
3Use this checklist on a Paper 26.2 server before treating 0.1.0 as production-ready.
3Use this checklist on a Paper 26.2 server before treating 0.1.0 as production-ready.
4
4
@@ -24,6 +24,7 @@ Use this checklist on a Paper 26.2 server before treating 0.1.0 as production-re
24- [ ] Put a closer chest in an unloaded chunk and verify CopperFarmer does not force-load that chunk.
24- [ ] Put a closer chest in an unloaded chunk and verify CopperFarmer does not force-load that chunk.
25- [ ] Break the bound chest while the farmer is carrying items and verify it searches for another copper chest.
25- [ ] Break the bound chest while the farmer is carrying items and verify it searches for another copper chest.
26- [ ] Make the bound chest unreachable and verify the farmer eventually abandons it after the configured path retries/cooldown.
26- [ ] Make the bound chest unreachable and verify the farmer eventually abandons it after the configured path retries/cooldown.
27- [ ] With the farmer loaded and its bound chest chunk unloaded, verify the chest binding is kept (not cleared from PDC).
27- [ ] Fill the chest completely and verify the farmer keeps its cargo instead of dropping/deleting it.
28- [ ] Fill the chest completely and verify the farmer keeps its cargo instead of dropping/deleting it.
28
29
29## Crops
30## Crops
build.gradle.kts +1 −1
@@ -3,7 +3,7 @@ plugins {
3 3 }
4 4
5 5 group = "nl.timfalken"
6 version = "0.1.12"
6 version = "0.1.13"
7 7
8 8 repositories {
9 9 mavenCentral()
@@ -3,7 +3,7 @@ plugins {
3}
3}
4
4
5group = "nl.timfalken"
5group = "nl.timfalken"
6version = "0.1.12"
6version = "0.1.13"
7
7
8repositories {
8repositories {
9 mavenCentral()
9 mavenCentral()
src/main/java/nl/timfalken/copperfarmer/CopperFarmerPlugin.java +1 −1
@@ -49,7 +49,7 @@ public final class CopperFarmerPlugin extends JavaPlugin {
49 49 long interval = Math.max(1L, getConfig().getLong("brain-tick-interval", 5L));
50 50 brainTask = getServer().getScheduler().runTaskTimer(this, () -> manager.tick(interval), interval, interval);
51 51
52 getLogger().info("CopperFarmer 0.1.12 enabled for Paper 26.2.");
52 getLogger().info("CopperFarmer 0.1.13 enabled for Paper 26.2.");
53 53 }
54 54
55 55 @Override
@@ -49,7 +49,7 @@ public final class CopperFarmerPlugin extends JavaPlugin {
49 long interval = Math.max(1L, getConfig().getLong("brain-tick-interval", 5L));
49 long interval = Math.max(1L, getConfig().getLong("brain-tick-interval", 5L));
50 brainTask = getServer().getScheduler().runTaskTimer(this, () -> manager.tick(interval), interval, interval);
50 brainTask = getServer().getScheduler().runTaskTimer(this, () -> manager.tick(interval), interval, interval);
51
51
52 getLogger().info("CopperFarmer 0.1.12 enabled for Paper 26.2.");
52 getLogger().info("CopperFarmer 0.1.13 enabled for Paper 26.2.");
53 }
53 }
54
54
55 @Override
55 @Override
src/main/java/nl/timfalken/copperfarmer/FarmerManager.java +70 −14
@@ -272,7 +272,9 @@ final class FarmerManager {
272 272 cleanupCooldowns();
273 273 tickDoors();
274 274
275 if (!isChestValid(chest)) {
275 // Only forget the binding when the chest chunk is loaded and the block
276 // is no longer a copper chest. An unloaded chest chunk must not wipe memory.
277 if (chest != null && isChestDefinitelyGone(chest)) {
276 278 clearChest();
277 279 }
278 280
@@ -442,6 +444,15 @@ final class FarmerManager {
442 444 }
443 445
444 446 Block chestBlock = block(chest);
447 if (chestBlock == null) {
448 // Bound chest chunk is unloaded: keep walking toward remembered coords
449 // (or wait/retry). Never clear the binding just because the chunk is gone.
450 Location approx = rememberedChestLocation(chest);
451 if (approx != null && !navigationHealthy()) {
452 beginPathAllowPartial(approx, State.MOVING_TO_CHEST);
453 }
454 return;
455 }
445 456 if (!isCopperChest(chestBlock)) {
446 457 clearChest();
447 458 state = State.IDLE;
@@ -456,10 +467,12 @@ final class FarmerManager {
456 467 }
457 468
458 469 if (!navigationHealthy() && !retryPath()) {
459 markChestUnreachable(chest);
460 clearChest();
461 state = State.IDLE;
462 nextScanTick = clock;
470 if (!beginPathToBlock(chestBlock, State.MOVING_TO_CHEST)) {
471 markChestUnreachable(chest);
472 clearChest();
473 state = State.IDLE;
474 nextScanTick = clock;
475 }
463 476 }
464 477 }
465 478
@@ -496,7 +509,7 @@ final class FarmerManager {
496 509 nextScanTick = clock + scanInterval;
497 510 return;
498 511 }
499 if (!isChestValid(chest)) {
512 if (isChestDefinitelyGone(chest)) {
500 513 clearChest();
501 514 state = State.IDLE;
502 515 nextScanTick = clock;
@@ -680,11 +693,20 @@ final class FarmerManager {
680 693 }
681 694
682 695 Block chestBlock = block(chest);
683 if (chestBlock == null || !beginPathToBlock(chestBlock, State.MOVING_TO_CHEST)) {
684 markChestUnreachable(chest);
685 clearChest();
686 state = State.IDLE;
687 nextScanTick = clock + scanInterval;
696 if (chestBlock != null) {
697 if (!beginPathToBlock(chestBlock, State.MOVING_TO_CHEST)) {
698 markChestUnreachable(chest);
699 clearChest();
700 state = State.IDLE;
701 nextScanTick = clock + scanInterval;
702 }
703 return;
704 }
705
706 // Chest chunk unloaded: path toward remembered coordinates without force-loading.
707 Location approx = rememberedChestLocation(chest);
708 if (approx == null || !beginPathAllowPartial(approx, State.MOVING_TO_CHEST)) {
709 state = State.MOVING_TO_CHEST;
688 710 }
689 711 }
690 712
@@ -1092,6 +1114,36 @@ final class FarmerManager {
1092 1114 return golem.getPathfinder().moveTo(bestPath, moveSpeed);
1093 1115 }
1094 1116
1117 /**
1118 * Walk toward a remembered location even if the destination chunk is unloaded
1119 * (partial paths are OK). Used so farmers do not forget a bound chest.
1120 */
1121 private boolean beginPathAllowPartial(Location destination, State newState) {
1122 if (destination == null || destination.getWorld() != golem.getWorld()) return false;
1123
1124 navigationTarget = destination.clone();
1125 pathRetries = 0;
1126 lastProgressLocation = golem.getLocation().clone();
1127 lastProgressTick = clock;
1128 state = newState;
1129
1130 if (reachedNavigationTarget()) {
1131 golem.getPathfinder().stopPathfinding();
1132 return true;
1133 }
1134
1135 Pathfinder.PathResult path = golem.getPathfinder().findPath(destination);
1136 if (path != null) return golem.getPathfinder().moveTo(path, moveSpeed);
1137 return golem.getPathfinder().moveTo(destination, moveSpeed);
1138 }
1139
1140 private Location rememberedChestLocation(BlockRef ref) {
1141 if (ref == null) return null;
1142 World world = Bukkit.getWorld(ref.worldId());
1143 if (world == null) return null;
1144 return new Location(world, ref.x() + 0.5, ref.y(), ref.z() + 0.5);
1145 }
1146
1095 1147 private Pathfinder.PathResult findCompletePath(Location destination) {
1096 1148 Pathfinder.PathResult path = golem.getPathfinder().findPath(destination);
1097 1149 if (path == null || !path.canReachFinalPoint()) return null;
@@ -1307,9 +1359,13 @@ final class FarmerManager {
1307 1359 pdc.remove(keys.chestZ);
1308 1360 }
1309 1361
1310 private boolean isChestValid(BlockRef ref) {
1311 Block block = block(ref);
1312 return isCopperChest(block);
1362 /** True only when the chest chunk is loaded and the block is not a copper chest. */
1363 private boolean isChestDefinitelyGone(BlockRef ref) {
1364 if (ref == null) return true;
1365 World world = Bukkit.getWorld(ref.worldId());
1366 if (world == null) return false;
1367 if (!world.isChunkLoaded(ref.x() >> 4, ref.z() >> 4)) return false;
1368 return !isCopperChest(world.getBlockAt(ref.x(), ref.y(), ref.z()));
1313 1369 }
1314 1370
1315 1371 private void tickDoors() {
@@ -272,7 +272,9 @@ final class FarmerManager {
272 cleanupCooldowns();
272 cleanupCooldowns();
273 tickDoors();
273 tickDoors();
274
274
275 if (!isChestValid(chest)) {
275 // Only forget the binding when the chest chunk is loaded and the block
276 // is no longer a copper chest. An unloaded chest chunk must not wipe memory.
277 if (chest != null && isChestDefinitelyGone(chest)) {
276 clearChest();
278 clearChest();
277 }
279 }
278
280
@@ -442,6 +444,15 @@ final class FarmerManager {
442 }
444 }
443
445
444 Block chestBlock = block(chest);
446 Block chestBlock = block(chest);
447 if (chestBlock == null) {
448 // Bound chest chunk is unloaded: keep walking toward remembered coords
449 // (or wait/retry). Never clear the binding just because the chunk is gone.
450 Location approx = rememberedChestLocation(chest);
451 if (approx != null && !navigationHealthy()) {
452 beginPathAllowPartial(approx, State.MOVING_TO_CHEST);
453 }
454 return;
455 }
445 if (!isCopperChest(chestBlock)) {
456 if (!isCopperChest(chestBlock)) {
446 clearChest();
457 clearChest();
447 state = State.IDLE;
458 state = State.IDLE;
@@ -456,10 +467,12 @@ final class FarmerManager {
456 }
467 }
457
468
458 if (!navigationHealthy() && !retryPath()) {
469 if (!navigationHealthy() && !retryPath()) {
459 markChestUnreachable(chest);
470 if (!beginPathToBlock(chestBlock, State.MOVING_TO_CHEST)) {
460 clearChest();
471 markChestUnreachable(chest);
461 state = State.IDLE;
472 clearChest();
462 nextScanTick = clock;
473 state = State.IDLE;
474 nextScanTick = clock;
475 }
463 }
476 }
464 }
477 }
465
478
@@ -496,7 +509,7 @@ final class FarmerManager {
496 nextScanTick = clock + scanInterval;
509 nextScanTick = clock + scanInterval;
497 return;
510 return;
498 }
511 }
499 if (!isChestValid(chest)) {
512 if (isChestDefinitelyGone(chest)) {
500 clearChest();
513 clearChest();
501 state = State.IDLE;
514 state = State.IDLE;
502 nextScanTick = clock;
515 nextScanTick = clock;
@@ -680,11 +693,20 @@ final class FarmerManager {
680 }
693 }
681
694
682 Block chestBlock = block(chest);
695 Block chestBlock = block(chest);
683 if (chestBlock == null || !beginPathToBlock(chestBlock, State.MOVING_TO_CHEST)) {
696 if (chestBlock != null) {
684 markChestUnreachable(chest);
697 if (!beginPathToBlock(chestBlock, State.MOVING_TO_CHEST)) {
685 clearChest();
698 markChestUnreachable(chest);
686 state = State.IDLE;
699 clearChest();
687 nextScanTick = clock + scanInterval;
700 state = State.IDLE;
701 nextScanTick = clock + scanInterval;
702 }
703 return;
704 }
705
706 // Chest chunk unloaded: path toward remembered coordinates without force-loading.
707 Location approx = rememberedChestLocation(chest);
708 if (approx == null || !beginPathAllowPartial(approx, State.MOVING_TO_CHEST)) {
709 state = State.MOVING_TO_CHEST;
688 }
710 }
689 }
711 }
690
712
@@ -1092,6 +1114,36 @@ final class FarmerManager {
1092 return golem.getPathfinder().moveTo(bestPath, moveSpeed);
1114 return golem.getPathfinder().moveTo(bestPath, moveSpeed);
1093 }
1115 }
1094
1116
1117 /**
1118 * Walk toward a remembered location even if the destination chunk is unloaded
1119 * (partial paths are OK). Used so farmers do not forget a bound chest.
1120 */
1121 private boolean beginPathAllowPartial(Location destination, State newState) {
1122 if (destination == null || destination.getWorld() != golem.getWorld()) return false;
1123
1124 navigationTarget = destination.clone();
1125 pathRetries = 0;
1126 lastProgressLocation = golem.getLocation().clone();
1127 lastProgressTick = clock;
1128 state = newState;
1129
1130 if (reachedNavigationTarget()) {
1131 golem.getPathfinder().stopPathfinding();
1132 return true;
1133 }
1134
1135 Pathfinder.PathResult path = golem.getPathfinder().findPath(destination);
1136 if (path != null) return golem.getPathfinder().moveTo(path, moveSpeed);
1137 return golem.getPathfinder().moveTo(destination, moveSpeed);
1138 }
1139
1140 private Location rememberedChestLocation(BlockRef ref) {
1141 if (ref == null) return null;
1142 World world = Bukkit.getWorld(ref.worldId());
1143 if (world == null) return null;
1144 return new Location(world, ref.x() + 0.5, ref.y(), ref.z() + 0.5);
1145 }
1146
1095 private Pathfinder.PathResult findCompletePath(Location destination) {
1147 private Pathfinder.PathResult findCompletePath(Location destination) {
1096 Pathfinder.PathResult path = golem.getPathfinder().findPath(destination);
1148 Pathfinder.PathResult path = golem.getPathfinder().findPath(destination);
1097 if (path == null || !path.canReachFinalPoint()) return null;
1149 if (path == null || !path.canReachFinalPoint()) return null;
@@ -1307,9 +1359,13 @@ final class FarmerManager {
1307 pdc.remove(keys.chestZ);
1359 pdc.remove(keys.chestZ);
1308 }
1360 }
1309
1361
1310 private boolean isChestValid(BlockRef ref) {
1362 /** True only when the chest chunk is loaded and the block is not a copper chest. */
1311 Block block = block(ref);
1363 private boolean isChestDefinitelyGone(BlockRef ref) {
1312 return isCopperChest(block);
1364 if (ref == null) return true;
1365 World world = Bukkit.getWorld(ref.worldId());
1366 if (world == null) return false;
1367 if (!world.isChunkLoaded(ref.x() >> 4, ref.z() >> 4)) return false;
1368 return !isCopperChest(world.getBlockAt(ref.x(), ref.y(), ref.z()));
1313 }
1369 }
1314
1370
1315 private void tickDoors() {
1371 private void tickDoors() {
src/main/resources/plugin.yml +1 −1
@@ -1,5 +1,5 @@
1 1 name: CopperFarmer
2 version: 0.1.12
2 version: 0.1.13
3 3 main: nl.timfalken.copperfarmer.CopperFarmerPlugin
4 4 api-version: '26.2'
5 5 author: Tim Falken
@@ -1,5 +1,5 @@
1name: CopperFarmer
1name: CopperFarmer
2version: 0.1.12
2version: 0.1.13
3main: nl.timfalken.copperfarmer.CopperFarmerPlugin
3main: nl.timfalken.copperfarmer.CopperFarmerPlugin
4api-version: '26.2'
4api-version: '26.2'
5author: Tim Falken
5author: Tim Falken