BlueGit

timfa copperfarmer

Improve farmer navigation with LOS checks and complete paths.

Reject partial wall paths, prefer the shortest reachable stand route, raise follow range, and require line-of-sight with tighter action distances.

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

timfa Tim Falken · pushed by timfa

Authored 2026-08-14 · 3 months ago

Hash
2e2d8f88ec51f1748c7129fe77e4d73ca3412c80
Parent
320de380
7 files changed +131 −39
README.md +1 −1
@@ -1,4 +1,4 @@
1 # CopperFarmer 0.1.8
1 # CopperFarmer 0.1.9
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.8
1# CopperFarmer 0.1.9
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 +3 −1
@@ -1,4 +1,4 @@
1 # CopperFarmer 0.1.8 smoke test
1 # CopperFarmer 0.1.9 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
@@ -46,8 +46,10 @@ Also verify:
46 46 - [ ] Immature crops are ignored.
47 47 - [ ] A mature but completely unreachable crop is not selected endlessly.
48 48 - [ ] The farmer tries alternative standable sides of a target before declaring it unreachable.
49 - [ ] The farmer only accepts complete native paths (no partial path into a wall) and prefers the shortest complete stand-position route.
49 50 - [ ] Once a path is accepted, the farmer follows vanilla navigation rather than recalculating every brain tick.
50 51 - [ ] A changed obstruction causes a retry instead of permanently freezing the farmer.
52 - [ ] Harvest/plant/deposit require line-of-sight and the configured horizontal/vertical action distances (no acting through walls).
51 53
52 54 ## Seeds and expansion
53 55
@@ -1,4 +1,4 @@
1# CopperFarmer 0.1.8 smoke test
1# CopperFarmer 0.1.9 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
@@ -46,8 +46,10 @@ Also verify:
46- [ ] Immature crops are ignored.
46- [ ] Immature crops are ignored.
47- [ ] A mature but completely unreachable crop is not selected endlessly.
47- [ ] A mature but completely unreachable crop is not selected endlessly.
48- [ ] The farmer tries alternative standable sides of a target before declaring it unreachable.
48- [ ] The farmer tries alternative standable sides of a target before declaring it unreachable.
49- [ ] The farmer only accepts complete native paths (no partial path into a wall) and prefers the shortest complete stand-position route.
49- [ ] Once a path is accepted, the farmer follows vanilla navigation rather than recalculating every brain tick.
50- [ ] Once a path is accepted, the farmer follows vanilla navigation rather than recalculating every brain tick.
50- [ ] A changed obstruction causes a retry instead of permanently freezing the farmer.
51- [ ] A changed obstruction causes a retry instead of permanently freezing the farmer.
52- [ ] Harvest/plant/deposit require line-of-sight and the configured horizontal/vertical action distances (no acting through walls).
51
53
52## Seeds and expansion
54## Seeds and expansion
53
55
build.gradle.kts +1 −1
@@ -3,7 +3,7 @@ plugins {
3 3 }
4 4
5 5 group = "nl.timfalken"
6 version = "0.1.8"
6 version = "0.1.9"
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.8"
6version = "0.1.9"
7
7
8repositories {
8repositories {
9 mavenCentral()
9 mavenCentral()
src/main/java/nl/timfalken/copperfarmer/CopperFarmerPlugin.java +1 −1
@@ -48,7 +48,7 @@ public final class CopperFarmerPlugin extends JavaPlugin {
48 48 long interval = Math.max(1L, getConfig().getLong("brain-tick-interval", 5L));
49 49 brainTask = getServer().getScheduler().runTaskTimer(this, () -> manager.tick(interval), interval, interval);
50 50
51 getLogger().info("CopperFarmer 0.1.8 enabled for Paper 26.2.");
51 getLogger().info("CopperFarmer 0.1.9 enabled for Paper 26.2.");
52 52 }
53 53
54 54 @Override
@@ -48,7 +48,7 @@ public final class CopperFarmerPlugin extends JavaPlugin {
48 long interval = Math.max(1L, getConfig().getLong("brain-tick-interval", 5L));
48 long interval = Math.max(1L, getConfig().getLong("brain-tick-interval", 5L));
49 brainTask = getServer().getScheduler().runTaskTimer(this, () -> manager.tick(interval), interval, interval);
49 brainTask = getServer().getScheduler().runTaskTimer(this, () -> manager.tick(interval), interval, interval);
50
50
51 getLogger().info("CopperFarmer 0.1.8 enabled for Paper 26.2.");
51 getLogger().info("CopperFarmer 0.1.9 enabled for Paper 26.2.");
52 }
52 }
53
53
54 @Override
54 @Override
src/main/java/nl/timfalken/copperfarmer/FarmerManager.java +113 −32
@@ -4,12 +4,15 @@ import com.destroystokyo.paper.entity.Pathfinder;
4 4 import org.bukkit.Bukkit;
5 5 import org.bukkit.Chunk;
6 6 import org.bukkit.Effect;
7 import org.bukkit.FluidCollisionMode;
7 8 import org.bukkit.Location;
8 9 import org.bukkit.Material;
9 10 import org.bukkit.Sound;
10 11 import org.bukkit.SoundCategory;
11 12 import org.bukkit.Tag;
12 13 import org.bukkit.World;
14 import org.bukkit.attribute.Attribute;
15 import org.bukkit.attribute.AttributeInstance;
13 16 import org.bukkit.block.Block;
14 17 import org.bukkit.block.BlockFace;
15 18 import org.bukkit.block.BlockState;
@@ -30,6 +33,8 @@ import org.bukkit.inventory.ItemStack;
30 33 import org.bukkit.persistence.PersistentDataContainer;
31 34 import org.bukkit.persistence.PersistentDataType;
32 35 import org.bukkit.plugin.Plugin;
36 import org.bukkit.util.RayTraceResult;
37 import org.bukkit.util.Vector;
33 38
34 39 import java.util.ArrayDeque;
35 40 import java.util.ArrayList;
@@ -62,7 +67,9 @@ final class FarmerManager {
62 67 private final long stuckTimeout;
63 68 private final int maxPathRetries;
64 69 private final long unreachableCooldown;
65 private final double actionDistanceSquared;
70 private final double actionHorizontalSquared;
71 private final double actionVertical;
72 private final double pathfindingRange;
66 73 private final long plantWindupTicks;
67 74 private final long afterDepositScanTicks;
68 75 private final int cargoSlots;
@@ -84,8 +91,12 @@ final class FarmerManager {
84 91 this.stuckTimeout = Math.max(20, plugin.getConfig().getLong("stuck-timeout-ticks", 60));
85 92 this.maxPathRetries = Math.max(0, plugin.getConfig().getInt("max-path-retries", 1));
86 93 this.unreachableCooldown = Math.max(20, plugin.getConfig().getLong("unreachable-crop-cooldown-ticks", 600));
87 double actionDistance = Math.max(1.25, plugin.getConfig().getDouble("action-distance", 2.25));
88 this.actionDistanceSquared = actionDistance * actionDistance;
94 double legacyAction = plugin.getConfig().getDouble("action-distance", 1.8);
95 double actionHorizontal = Math.max(0.75, plugin.getConfig().getDouble("action-distance-horizontal", legacyAction));
96 this.actionHorizontalSquared = actionHorizontal * actionHorizontal;
97 this.actionVertical = Math.max(0.75, plugin.getConfig().getDouble("action-distance-vertical",
98 Math.max(legacyAction, 2.25)));
99 this.pathfindingRange = Math.max(16.0, plugin.getConfig().getDouble("pathfinding-range", 100.0));
89 100 this.plantWindupTicks = Math.max(0L, plugin.getConfig().getLong("plant-windup-ticks", 30L));
90 101 this.afterDepositScanTicks = Math.max(1L, plugin.getConfig().getLong("after-deposit-scan-ticks", 40L));
91 102 this.cargoSlots = Math.max(1, plugin.getConfig().getInt("cargo-slots", 9));
@@ -138,6 +149,8 @@ final class FarmerManager {
138 149 golem.getPathfinder().setCanFloat(true);
139 150 golem.getPathfinder().setCanPassDoors(true);
140 151 golem.getPathfinder().setCanOpenDoors(true);
152 AttributeInstance followRange = golem.getAttribute(Attribute.FOLLOW_RANGE);
153 if (followRange != null) followRange.setBaseValue(pathfindingRange);
141 154 golem.setGolemState(CopperGolem.State.IDLE);
142 155 if (preventOxidation) golem.setOxidizing(CopperGolem.Oxidizing.waxed());
143 156 equipHand(golem, itemCodec.readTool(golem), false);
@@ -296,16 +309,18 @@ final class FarmerManager {
296 309 CropRegistry.Crop crop = cropRegistry.get(target.getType());
297 310 if (crop == null) {
298 311 markCropUnreachable(BlockRef.of(target));
312 nextScanTick = clock + 20;
299 313 return;
300 314 }
301 315
302 316 cropTarget = BlockRef.of(target);
303 317 activeCrop = crop;
304 if (!beginPathToBlock(target, State.MOVING_TO_CROP)) {
305 markCropUnreachable(cropTarget);
306 cropTarget = null;
307 activeCrop = null;
308 }
318 if (beginPathToBlock(target, State.MOVING_TO_CROP)) return;
319
320 markCropUnreachable(cropTarget);
321 cropTarget = null;
322 activeCrop = null;
323 nextScanTick = clock + 20;
309 324 }
310 325
311 326 private void tickMovingToCrop() {
@@ -993,22 +1008,6 @@ final class FarmerManager {
993 1008 return true;
994 1009 }
995 1010
996 private boolean beginPath(Location destination, State newState) {
997 navigationTarget = destination.clone();
998 pathRetries = 0;
999 lastProgressLocation = golem.getLocation().clone();
1000 lastProgressTick = clock;
1001 state = newState;
1002
1003 // If native navigation considers this too close to build a path, the
1004 // state machine can still perform the action on the next brain tick.
1005 if (reachedNavigationTarget()) {
1006 golem.getPathfinder().stopPathfinding();
1007 return true;
1008 }
1009 return golem.getPathfinder().moveTo(destination, moveSpeed);
1010 }
1011
1012 1011 private boolean navigationHealthy() {
1013 1012 if (navigationTarget == null) return false;
1014 1013 Location now = golem.getLocation();
@@ -1026,10 +1025,12 @@ final class FarmerManager {
1026 1025
1027 1026 private boolean retryPath() {
1028 1027 if (navigationTarget == null || pathRetries >= maxPathRetries) return false;
1028 Pathfinder.PathResult path = findCompletePath(navigationTarget);
1029 if (path == null) return false;
1029 1030 pathRetries++;
1030 1031 lastProgressLocation = golem.getLocation().clone();
1031 1032 lastProgressTick = clock;
1032 return golem.getPathfinder().moveTo(navigationTarget, moveSpeed);
1033 return golem.getPathfinder().moveTo(path, moveSpeed);
1033 1034 }
1034 1035
1035 1036 private boolean beginPathToBlock(Block target, State newState) {
@@ -1047,15 +1048,66 @@ final class FarmerManager {
1047 1048 if (seen.add(BlockRef.of(below)) && isStandable(below)) positions.add(below);
1048 1049 }
1049 1050 }
1050 positions.sort(Comparator.comparingDouble(block -> block.getLocation().add(0.5, 0, 0.5)
1051 .distanceSquared(golem.getLocation())));
1052 1051
1053 // Try each valid stand position once. After a side has produced a native path,
1054 // Minecraft owns that path; we do not recalculate it every brain tick.
1052 Pathfinder.PathResult bestPath = null;
1053 Location bestDestination = null;
1054 double bestLength = Double.POSITIVE_INFINITY;
1055 Location previousTarget = navigationTarget;
1056
1055 1057 for (Block feet : positions) {
1056 if (beginPath(feet.getLocation().add(0.5, 0.0, 0.5), newState)) return true;
1058 Location destination = feet.getLocation().add(0.5, 0.0, 0.5);
1059 navigationTarget = destination;
1060 if (reachedNavigationTarget()) {
1061 pathRetries = 0;
1062 lastProgressLocation = golem.getLocation().clone();
1063 lastProgressTick = clock;
1064 golem.getPathfinder().stopPathfinding();
1065 state = newState;
1066 return true;
1067 }
1068
1069 Pathfinder.PathResult path = findCompletePath(destination);
1070 if (path == null) continue;
1071 double length = pathLength(path);
1072 if (length < bestLength) {
1073 bestLength = length;
1074 bestPath = path;
1075 bestDestination = destination;
1076 }
1057 1077 }
1058 return false;
1078
1079 if (bestPath == null || bestDestination == null) {
1080 navigationTarget = previousTarget;
1081 return false;
1082 }
1083
1084 navigationTarget = bestDestination.clone();
1085 pathRetries = 0;
1086 lastProgressLocation = golem.getLocation().clone();
1087 lastProgressTick = clock;
1088 state = newState;
1089 return golem.getPathfinder().moveTo(bestPath, moveSpeed);
1090 }
1091
1092 private Pathfinder.PathResult findCompletePath(Location destination) {
1093 Pathfinder.PathResult path = golem.getPathfinder().findPath(destination);
1094 if (path == null || !path.canReachFinalPoint()) return null;
1095 return path;
1096 }
1097
1098 private static double pathLength(Pathfinder.PathResult path) {
1099 List<Location> points = path.getPoints();
1100 if (points.isEmpty()) return Double.POSITIVE_INFINITY;
1101 double length = 0.0;
1102 Location previous = points.getFirst();
1103 for (int i = 1; i < points.size(); i++) {
1104 Location next = points.get(i);
1105 if (previous.getWorld() == next.getWorld()) {
1106 length += previous.distance(next);
1107 }
1108 previous = next;
1109 }
1110 return length;
1059 1111 }
1060 1112
1061 1113 private boolean reachedNavigationTarget() {
@@ -1173,7 +1225,36 @@ final class FarmerManager {
1173 1225
1174 1226 private boolean near(Block block) {
1175 1227 if (block == null || block.getWorld() != golem.getWorld()) return false;
1176 return golem.getLocation().distanceSquared(block.getLocation().add(0.5, 0.5, 0.5)) <= actionDistanceSquared;
1228 Location eye = golem.getEyeLocation();
1229 Location target = block.getLocation().add(0.5, 0.5, 0.5);
1230 double dx = eye.getX() - target.getX();
1231 double dy = eye.getY() - target.getY();
1232 double dz = eye.getZ() - target.getZ();
1233 if (dx * dx + dz * dz > actionHorizontalSquared) return false;
1234 if (Math.abs(dy) > actionVertical) return false;
1235 return hasLineOfSight(eye, block, target);
1236 }
1237
1238 private boolean hasLineOfSight(Location from, Block target, Location targetCenter) {
1239 World world = from.getWorld();
1240 if (world == null) return false;
1241 double distance = from.distance(targetCenter);
1242 if (distance < 1.0E-4) return true;
1243
1244 Vector direction = targetCenter.toVector().subtract(from.toVector());
1245 if (direction.lengthSquared() < 1.0E-8) return true;
1246 direction.normalize();
1247
1248 // Limit the ray to the target centre so solids behind passable crops (cocoa, air
1249 // plant spaces) do not block interaction. ignorePassableBlocks keeps crops/air
1250 // from occluding while still stopping on full blocks like stone walls.
1251 RayTraceResult hit = world.rayTraceBlocks(
1252 from, direction, distance, FluidCollisionMode.NEVER, true);
1253 if (hit == null || hit.getHitBlock() == null) return true;
1254 Block hitBlock = hit.getHitBlock();
1255 return hitBlock.getX() == target.getX()
1256 && hitBlock.getY() == target.getY()
1257 && hitBlock.getZ() == target.getZ();
1177 1258 }
1178 1259
1179 1260 private boolean withinCropRadius(Block block) {
@@ -4,12 +4,15 @@ import com.destroystokyo.paper.entity.Pathfinder;
4import org.bukkit.Bukkit;
4import org.bukkit.Bukkit;
5import org.bukkit.Chunk;
5import org.bukkit.Chunk;
6import org.bukkit.Effect;
6import org.bukkit.Effect;
7import org.bukkit.FluidCollisionMode;
7import org.bukkit.Location;
8import org.bukkit.Location;
8import org.bukkit.Material;
9import org.bukkit.Material;
9import org.bukkit.Sound;
10import org.bukkit.Sound;
10import org.bukkit.SoundCategory;
11import org.bukkit.SoundCategory;
11import org.bukkit.Tag;
12import org.bukkit.Tag;
12import org.bukkit.World;
13import org.bukkit.World;
14import org.bukkit.attribute.Attribute;
15import org.bukkit.attribute.AttributeInstance;
13import org.bukkit.block.Block;
16import org.bukkit.block.Block;
14import org.bukkit.block.BlockFace;
17import org.bukkit.block.BlockFace;
15import org.bukkit.block.BlockState;
18import org.bukkit.block.BlockState;
@@ -30,6 +33,8 @@ import org.bukkit.inventory.ItemStack;
30import org.bukkit.persistence.PersistentDataContainer;
33import org.bukkit.persistence.PersistentDataContainer;
31import org.bukkit.persistence.PersistentDataType;
34import org.bukkit.persistence.PersistentDataType;
32import org.bukkit.plugin.Plugin;
35import org.bukkit.plugin.Plugin;
36import org.bukkit.util.RayTraceResult;
37import org.bukkit.util.Vector;
33
38
34import java.util.ArrayDeque;
39import java.util.ArrayDeque;
35import java.util.ArrayList;
40import java.util.ArrayList;
@@ -62,7 +67,9 @@ final class FarmerManager {
62 private final long stuckTimeout;
67 private final long stuckTimeout;
63 private final int maxPathRetries;
68 private final int maxPathRetries;
64 private final long unreachableCooldown;
69 private final long unreachableCooldown;
65 private final double actionDistanceSquared;
70 private final double actionHorizontalSquared;
71 private final double actionVertical;
72 private final double pathfindingRange;
66 private final long plantWindupTicks;
73 private final long plantWindupTicks;
67 private final long afterDepositScanTicks;
74 private final long afterDepositScanTicks;
68 private final int cargoSlots;
75 private final int cargoSlots;
@@ -84,8 +91,12 @@ final class FarmerManager {
84 this.stuckTimeout = Math.max(20, plugin.getConfig().getLong("stuck-timeout-ticks", 60));
91 this.stuckTimeout = Math.max(20, plugin.getConfig().getLong("stuck-timeout-ticks", 60));
85 this.maxPathRetries = Math.max(0, plugin.getConfig().getInt("max-path-retries", 1));
92 this.maxPathRetries = Math.max(0, plugin.getConfig().getInt("max-path-retries", 1));
86 this.unreachableCooldown = Math.max(20, plugin.getConfig().getLong("unreachable-crop-cooldown-ticks", 600));
93 this.unreachableCooldown = Math.max(20, plugin.getConfig().getLong("unreachable-crop-cooldown-ticks", 600));
87 double actionDistance = Math.max(1.25, plugin.getConfig().getDouble("action-distance", 2.25));
94 double legacyAction = plugin.getConfig().getDouble("action-distance", 1.8);
88 this.actionDistanceSquared = actionDistance * actionDistance;
95 double actionHorizontal = Math.max(0.75, plugin.getConfig().getDouble("action-distance-horizontal", legacyAction));
96 this.actionHorizontalSquared = actionHorizontal * actionHorizontal;
97 this.actionVertical = Math.max(0.75, plugin.getConfig().getDouble("action-distance-vertical",
98 Math.max(legacyAction, 2.25)));
99 this.pathfindingRange = Math.max(16.0, plugin.getConfig().getDouble("pathfinding-range", 100.0));
89 this.plantWindupTicks = Math.max(0L, plugin.getConfig().getLong("plant-windup-ticks", 30L));
100 this.plantWindupTicks = Math.max(0L, plugin.getConfig().getLong("plant-windup-ticks", 30L));
90 this.afterDepositScanTicks = Math.max(1L, plugin.getConfig().getLong("after-deposit-scan-ticks", 40L));
101 this.afterDepositScanTicks = Math.max(1L, plugin.getConfig().getLong("after-deposit-scan-ticks", 40L));
91 this.cargoSlots = Math.max(1, plugin.getConfig().getInt("cargo-slots", 9));
102 this.cargoSlots = Math.max(1, plugin.getConfig().getInt("cargo-slots", 9));
@@ -138,6 +149,8 @@ final class FarmerManager {
138 golem.getPathfinder().setCanFloat(true);
149 golem.getPathfinder().setCanFloat(true);
139 golem.getPathfinder().setCanPassDoors(true);
150 golem.getPathfinder().setCanPassDoors(true);
140 golem.getPathfinder().setCanOpenDoors(true);
151 golem.getPathfinder().setCanOpenDoors(true);
152 AttributeInstance followRange = golem.getAttribute(Attribute.FOLLOW_RANGE);
153 if (followRange != null) followRange.setBaseValue(pathfindingRange);
141 golem.setGolemState(CopperGolem.State.IDLE);
154 golem.setGolemState(CopperGolem.State.IDLE);
142 if (preventOxidation) golem.setOxidizing(CopperGolem.Oxidizing.waxed());
155 if (preventOxidation) golem.setOxidizing(CopperGolem.Oxidizing.waxed());
143 equipHand(golem, itemCodec.readTool(golem), false);
156 equipHand(golem, itemCodec.readTool(golem), false);
@@ -296,16 +309,18 @@ final class FarmerManager {
296 CropRegistry.Crop crop = cropRegistry.get(target.getType());
309 CropRegistry.Crop crop = cropRegistry.get(target.getType());
297 if (crop == null) {
310 if (crop == null) {
298 markCropUnreachable(BlockRef.of(target));
311 markCropUnreachable(BlockRef.of(target));
312 nextScanTick = clock + 20;
299 return;
313 return;
300 }
314 }
301
315
302 cropTarget = BlockRef.of(target);
316 cropTarget = BlockRef.of(target);
303 activeCrop = crop;
317 activeCrop = crop;
304 if (!beginPathToBlock(target, State.MOVING_TO_CROP)) {
318 if (beginPathToBlock(target, State.MOVING_TO_CROP)) return;
305 markCropUnreachable(cropTarget);
319
306 cropTarget = null;
320 markCropUnreachable(cropTarget);
307 activeCrop = null;
321 cropTarget = null;
308 }
322 activeCrop = null;
323 nextScanTick = clock + 20;
309 }
324 }
310
325
311 private void tickMovingToCrop() {
326 private void tickMovingToCrop() {
@@ -993,22 +1008,6 @@ final class FarmerManager {
993 return true;
1008 return true;
994 }
1009 }
995
1010
996 private boolean beginPath(Location destination, State newState) {
997 navigationTarget = destination.clone();
998 pathRetries = 0;
999 lastProgressLocation = golem.getLocation().clone();
1000 lastProgressTick = clock;
1001 state = newState;
1002
1003 // If native navigation considers this too close to build a path, the
1004 // state machine can still perform the action on the next brain tick.
1005 if (reachedNavigationTarget()) {
1006 golem.getPathfinder().stopPathfinding();
1007 return true;
1008 }
1009 return golem.getPathfinder().moveTo(destination, moveSpeed);
1010 }
1011
1012 private boolean navigationHealthy() {
1011 private boolean navigationHealthy() {
1013 if (navigationTarget == null) return false;
1012 if (navigationTarget == null) return false;
1014 Location now = golem.getLocation();
1013 Location now = golem.getLocation();
@@ -1026,10 +1025,12 @@ final class FarmerManager {
1026
1025
1027 private boolean retryPath() {
1026 private boolean retryPath() {
1028 if (navigationTarget == null || pathRetries >= maxPathRetries) return false;
1027 if (navigationTarget == null || pathRetries >= maxPathRetries) return false;
1028 Pathfinder.PathResult path = findCompletePath(navigationTarget);
1029 if (path == null) return false;
1029 pathRetries++;
1030 pathRetries++;
1030 lastProgressLocation = golem.getLocation().clone();
1031 lastProgressLocation = golem.getLocation().clone();
1031 lastProgressTick = clock;
1032 lastProgressTick = clock;
1032 return golem.getPathfinder().moveTo(navigationTarget, moveSpeed);
1033 return golem.getPathfinder().moveTo(path, moveSpeed);
1033 }
1034 }
1034
1035
1035 private boolean beginPathToBlock(Block target, State newState) {
1036 private boolean beginPathToBlock(Block target, State newState) {
@@ -1047,15 +1048,66 @@ final class FarmerManager {
1047 if (seen.add(BlockRef.of(below)) && isStandable(below)) positions.add(below);
1048 if (seen.add(BlockRef.of(below)) && isStandable(below)) positions.add(below);
1048 }
1049 }
1049 }
1050 }
1050 positions.sort(Comparator.comparingDouble(block -> block.getLocation().add(0.5, 0, 0.5)
1051 .distanceSquared(golem.getLocation())));
1052
1051
1053 // Try each valid stand position once. After a side has produced a native path,
1052 Pathfinder.PathResult bestPath = null;
1054 // Minecraft owns that path; we do not recalculate it every brain tick.
1053 Location bestDestination = null;
1054 double bestLength = Double.POSITIVE_INFINITY;
1055 Location previousTarget = navigationTarget;
1056
1055 for (Block feet : positions) {
1057 for (Block feet : positions) {
1056 if (beginPath(feet.getLocation().add(0.5, 0.0, 0.5), newState)) return true;
1058 Location destination = feet.getLocation().add(0.5, 0.0, 0.5);
1059 navigationTarget = destination;
1060 if (reachedNavigationTarget()) {
1061 pathRetries = 0;
1062 lastProgressLocation = golem.getLocation().clone();
1063 lastProgressTick = clock;
1064 golem.getPathfinder().stopPathfinding();
1065 state = newState;
1066 return true;
1067 }
1068
1069 Pathfinder.PathResult path = findCompletePath(destination);
1070 if (path == null) continue;
1071 double length = pathLength(path);
1072 if (length < bestLength) {
1073 bestLength = length;
1074 bestPath = path;
1075 bestDestination = destination;
1076 }
1057 }
1077 }
1058 return false;
1078
1079 if (bestPath == null || bestDestination == null) {
1080 navigationTarget = previousTarget;
1081 return false;
1082 }
1083
1084 navigationTarget = bestDestination.clone();
1085 pathRetries = 0;
1086 lastProgressLocation = golem.getLocation().clone();
1087 lastProgressTick = clock;
1088 state = newState;
1089 return golem.getPathfinder().moveTo(bestPath, moveSpeed);
1090 }
1091
1092 private Pathfinder.PathResult findCompletePath(Location destination) {
1093 Pathfinder.PathResult path = golem.getPathfinder().findPath(destination);
1094 if (path == null || !path.canReachFinalPoint()) return null;
1095 return path;
1096 }
1097
1098 private static double pathLength(Pathfinder.PathResult path) {
1099 List<Location> points = path.getPoints();
1100 if (points.isEmpty()) return Double.POSITIVE_INFINITY;
1101 double length = 0.0;
1102 Location previous = points.getFirst();
1103 for (int i = 1; i < points.size(); i++) {
1104 Location next = points.get(i);
1105 if (previous.getWorld() == next.getWorld()) {
1106 length += previous.distance(next);
1107 }
1108 previous = next;
1109 }
1110 return length;
1059 }
1111 }
1060
1112
1061 private boolean reachedNavigationTarget() {
1113 private boolean reachedNavigationTarget() {
@@ -1173,7 +1225,36 @@ final class FarmerManager {
1173
1225
1174 private boolean near(Block block) {
1226 private boolean near(Block block) {
1175 if (block == null || block.getWorld() != golem.getWorld()) return false;
1227 if (block == null || block.getWorld() != golem.getWorld()) return false;
1176 return golem.getLocation().distanceSquared(block.getLocation().add(0.5, 0.5, 0.5)) <= actionDistanceSquared;
1228 Location eye = golem.getEyeLocation();
1229 Location target = block.getLocation().add(0.5, 0.5, 0.5);
1230 double dx = eye.getX() - target.getX();
1231 double dy = eye.getY() - target.getY();
1232 double dz = eye.getZ() - target.getZ();
1233 if (dx * dx + dz * dz > actionHorizontalSquared) return false;
1234 if (Math.abs(dy) > actionVertical) return false;
1235 return hasLineOfSight(eye, block, target);
1236 }
1237
1238 private boolean hasLineOfSight(Location from, Block target, Location targetCenter) {
1239 World world = from.getWorld();
1240 if (world == null) return false;
1241 double distance = from.distance(targetCenter);
1242 if (distance < 1.0E-4) return true;
1243
1244 Vector direction = targetCenter.toVector().subtract(from.toVector());
1245 if (direction.lengthSquared() < 1.0E-8) return true;
1246 direction.normalize();
1247
1248 // Limit the ray to the target centre so solids behind passable crops (cocoa, air
1249 // plant spaces) do not block interaction. ignorePassableBlocks keeps crops/air
1250 // from occluding while still stopping on full blocks like stone walls.
1251 RayTraceResult hit = world.rayTraceBlocks(
1252 from, direction, distance, FluidCollisionMode.NEVER, true);
1253 if (hit == null || hit.getHitBlock() == null) return true;
1254 Block hitBlock = hit.getHitBlock();
1255 return hitBlock.getX() == target.getX()
1256 && hitBlock.getY() == target.getY()
1257 && hitBlock.getZ() == target.getZ();
1177 }
1258 }
1178
1259
1179 private boolean withinCropRadius(Block block) {
1260 private boolean withinCropRadius(Block block) {
src/main/resources/config.yml +11 −2
@@ -27,8 +27,17 @@ max-path-retries: 1
27 27 # Don't repeatedly select an unreachable crop until this cooldown expires.
28 28 unreachable-crop-cooldown-ticks: 600
29 29
30 # Distance from the target block at which harvesting/planting/depositing is allowed.
31 action-distance: 2.25
30 # Max horizontal (XZ) distance from the target block centre for harvest/plant/deposit.
31 action-distance-horizontal: 1.8
32
33 # Max vertical (Y) distance from the target block centre for harvest/plant/deposit.
34 action-distance-vertical: 2.25
35
36 # Interaction also requires line-of-sight (solid blocks between golem and target block).
37
38 # Vanilla FOLLOW_RANGE used for farmer path searches. Large enough for complex indoor
39 # routes; crop-radius still limits which targets are considered.
40 pathfinding-range: 100
32 41
33 42 # After arriving at empty farmland, wait this long before planting. 30 ticks = 1.5 seconds.
34 43 plant-windup-ticks: 30
@@ -27,8 +27,17 @@ max-path-retries: 1
27# Don't repeatedly select an unreachable crop until this cooldown expires.
27# Don't repeatedly select an unreachable crop until this cooldown expires.
28unreachable-crop-cooldown-ticks: 600
28unreachable-crop-cooldown-ticks: 600
29
29
30# Distance from the target block at which harvesting/planting/depositing is allowed.
30# Max horizontal (XZ) distance from the target block centre for harvest/plant/deposit.
31action-distance: 2.25
31action-distance-horizontal: 1.8
32
33# Max vertical (Y) distance from the target block centre for harvest/plant/deposit.
34action-distance-vertical: 2.25
35
36# Interaction also requires line-of-sight (solid blocks between golem and target block).
37
38# Vanilla FOLLOW_RANGE used for farmer path searches. Large enough for complex indoor
39# routes; crop-radius still limits which targets are considered.
40pathfinding-range: 100
32
41
33# After arriving at empty farmland, wait this long before planting. 30 ticks = 1.5 seconds.
42# After arriving at empty farmland, wait this long before planting. 30 ticks = 1.5 seconds.
34plant-windup-ticks: 30
43plant-windup-ticks: 30
src/main/resources/plugin.yml +1 −1
@@ -1,5 +1,5 @@
1 1 name: CopperFarmer
2 version: 0.1.8
2 version: 0.1.9
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.8
2version: 0.1.9
3main: nl.timfalken.copperfarmer.CopperFarmerPlugin
3main: nl.timfalken.copperfarmer.CopperFarmerPlugin
4api-version: '26.2'
4api-version: '26.2'
5author: Tim Falken
5author: Tim Falken