BlueGit

timfa copperfarmer

Suppress vanilla Copper Golem brain AI so farmers keep pathfinding control.

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

timfa Tim Falken · pushed by timfa

Authored 2026-08-14 · 3 months ago

Hash
cf5d29aeda2ec2e566b4c359b228b5670fc00167
Parent
c207d36b
2 files changed +112 −0
src/main/java/nl/timfalken/copperfarmer/CopperGolemVanillaAi.java added +109 −0
@@ -0,0 +1,109 @@
1 package nl.timfalken.copperfarmer;
2
3 import org.bukkit.entity.CopperGolem;
4 import org.bukkit.plugin.Plugin;
5
6 import java.lang.reflect.Field;
7 import java.lang.reflect.Method;
8 import java.util.Collection;
9 import java.util.Map;
10
11 /**
12 * Copper golems do not use the old GoalSelector for their main behaviour.
13 * Sorting chests, idle wandering and statue conversion live in Mojang's Brain.
14 * Paper's MobGoals API therefore cannot turn that off; we strip the Brain instead
15 * and keep the real {@code minecraft:copper_golem} so vanilla clients still render it.
16 */
17 final class CopperGolemVanillaAi {
18 private static final String[] SENSOR_FIELDS = {"sensors", "availableBehaviorsByPriority"};
19
20 private static Method getHandle;
21 private static Method getBrain;
22 private static Method removeAllBehaviors;
23 private static Method clearMemories;
24 private static boolean resolved;
25 private static boolean available = true;
26 private static boolean loggedFailure;
27
28 private CopperGolemVanillaAi() {}
29
30 static void suppress(CopperGolem golem, Plugin plugin) {
31 if (!available) return;
32 try {
33 Object nms = handle(golem);
34 Object brain = brain(nms);
35 resolveBrainMethods(brain);
36 removeAllBehaviors.invoke(brain);
37 clearMemories.invoke(brain);
38 clearMutableMaps(brain);
39 } catch (ReflectiveOperationException | RuntimeException ex) {
40 available = false;
41 if (!loggedFailure) {
42 loggedFailure = true;
43 plugin.getLogger().warning("Could not suppress vanilla Copper Golem brain AI: " + ex.getMessage());
44 }
45 }
46 }
47
48 static void keepIdle(CopperGolem golem) {
49 if (golem.getGolemState() != CopperGolem.State.IDLE) {
50 golem.setGolemState(CopperGolem.State.IDLE);
51 }
52 }
53
54 private static Object handle(CopperGolem golem) throws ReflectiveOperationException {
55 if (getHandle == null) {
56 getHandle = golem.getClass().getMethod("getHandle");
57 }
58 return getHandle.invoke(golem);
59 }
60
61 private static Object brain(Object nms) throws ReflectiveOperationException {
62 if (getBrain == null) {
63 getBrain = findMethod(nms.getClass(), "getBrain");
64 }
65 return getBrain.invoke(nms);
66 }
67
68 private static void resolveBrainMethods(Object brain) throws ReflectiveOperationException {
69 if (resolved) return;
70 Class<?> type = brain.getClass();
71 removeAllBehaviors = findMethod(type, "removeAllBehaviors");
72 clearMemories = findMethod(type, "clearMemories");
73 resolved = true;
74 }
75
76 private static Method findMethod(Class<?> type, String name) throws NoSuchMethodException {
77 Class<?> current = type;
78 while (current != null && current != Object.class) {
79 try {
80 Method method = current.getDeclaredMethod(name);
81 method.setAccessible(true);
82 return method;
83 } catch (NoSuchMethodException ignored) {
84 current = current.getSuperclass();
85 }
86 }
87 throw new NoSuchMethodException(type.getName() + "#" + name);
88 }
89
90 private static void clearMutableMaps(Object brain) {
91 Class<?> type = brain.getClass();
92 while (type != null && type != Object.class) {
93 for (String fieldName : SENSOR_FIELDS) {
94 try {
95 Field field = type.getDeclaredField(fieldName);
96 field.setAccessible(true);
97 Object value = field.get(brain);
98 if (value instanceof Map<?, ?> map) map.clear();
99 else if (value instanceof Collection<?> collection) collection.clear();
100 } catch (NoSuchFieldException ignored) {
101 // Field lives on a superclass or is named differently.
102 } catch (ReflectiveOperationException | UnsupportedOperationException ignored) {
103 // Immutable maps cannot be cleared; behaviours are already removed.
104 }
105 }
106 type = type.getSuperclass();
107 }
108 }
109 }
@@ -0,0 +1,109 @@
1package nl.timfalken.copperfarmer;
2
3import org.bukkit.entity.CopperGolem;
4import org.bukkit.plugin.Plugin;
5
6import java.lang.reflect.Field;
7import java.lang.reflect.Method;
8import java.util.Collection;
9import java.util.Map;
10
11/**
12 * Copper golems do not use the old GoalSelector for their main behaviour.
13 * Sorting chests, idle wandering and statue conversion live in Mojang's Brain.
14 * Paper's MobGoals API therefore cannot turn that off; we strip the Brain instead
15 * and keep the real {@code minecraft:copper_golem} so vanilla clients still render it.
16 */
17final class CopperGolemVanillaAi {
18 private static final String[] SENSOR_FIELDS = {"sensors", "availableBehaviorsByPriority"};
19
20 private static Method getHandle;
21 private static Method getBrain;
22 private static Method removeAllBehaviors;
23 private static Method clearMemories;
24 private static boolean resolved;
25 private static boolean available = true;
26 private static boolean loggedFailure;
27
28 private CopperGolemVanillaAi() {}
29
30 static void suppress(CopperGolem golem, Plugin plugin) {
31 if (!available) return;
32 try {
33 Object nms = handle(golem);
34 Object brain = brain(nms);
35 resolveBrainMethods(brain);
36 removeAllBehaviors.invoke(brain);
37 clearMemories.invoke(brain);
38 clearMutableMaps(brain);
39 } catch (ReflectiveOperationException | RuntimeException ex) {
40 available = false;
41 if (!loggedFailure) {
42 loggedFailure = true;
43 plugin.getLogger().warning("Could not suppress vanilla Copper Golem brain AI: " + ex.getMessage());
44 }
45 }
46 }
47
48 static void keepIdle(CopperGolem golem) {
49 if (golem.getGolemState() != CopperGolem.State.IDLE) {
50 golem.setGolemState(CopperGolem.State.IDLE);
51 }
52 }
53
54 private static Object handle(CopperGolem golem) throws ReflectiveOperationException {
55 if (getHandle == null) {
56 getHandle = golem.getClass().getMethod("getHandle");
57 }
58 return getHandle.invoke(golem);
59 }
60
61 private static Object brain(Object nms) throws ReflectiveOperationException {
62 if (getBrain == null) {
63 getBrain = findMethod(nms.getClass(), "getBrain");
64 }
65 return getBrain.invoke(nms);
66 }
67
68 private static void resolveBrainMethods(Object brain) throws ReflectiveOperationException {
69 if (resolved) return;
70 Class<?> type = brain.getClass();
71 removeAllBehaviors = findMethod(type, "removeAllBehaviors");
72 clearMemories = findMethod(type, "clearMemories");
73 resolved = true;
74 }
75
76 private static Method findMethod(Class<?> type, String name) throws NoSuchMethodException {
77 Class<?> current = type;
78 while (current != null && current != Object.class) {
79 try {
80 Method method = current.getDeclaredMethod(name);
81 method.setAccessible(true);
82 return method;
83 } catch (NoSuchMethodException ignored) {
84 current = current.getSuperclass();
85 }
86 }
87 throw new NoSuchMethodException(type.getName() + "#" + name);
88 }
89
90 private static void clearMutableMaps(Object brain) {
91 Class<?> type = brain.getClass();
92 while (type != null && type != Object.class) {
93 for (String fieldName : SENSOR_FIELDS) {
94 try {
95 Field field = type.getDeclaredField(fieldName);
96 field.setAccessible(true);
97 Object value = field.get(brain);
98 if (value instanceof Map<?, ?> map) map.clear();
99 else if (value instanceof Collection<?> collection) collection.clear();
100 } catch (NoSuchFieldException ignored) {
101 // Field lives on a superclass or is named differently.
102 } catch (ReflectiveOperationException | UnsupportedOperationException ignored) {
103 // Immutable maps cannot be cleared; behaviours are already removed.
104 }
105 }
106 type = type.getSuperclass();
107 }
108 }
109}
src/main/java/nl/timfalken/copperfarmer/FarmerManager.java +3 −0
@@ -110,6 +110,7 @@ final class FarmerManager {
110 110
111 111 void configureEntity(CopperGolem golem) {
112 112 plugin.getServer().getMobGoals().removeAllGoals(golem);
113 CopperGolemVanillaAi.suppress(golem, plugin);
113 114 golem.setAI(true);
114 115 golem.setAware(true);
115 116 golem.setCanPickupItems(false);
@@ -163,6 +164,8 @@ final class FarmerManager {
163 164 }
164 165
165 166 void tick() {
167 CopperGolemVanillaAi.suppress(golem, plugin);
168 CopperGolemVanillaAi.keepIdle(golem);
166 169 if (preventOxidation && golem.getOxidizing() != CopperGolem.Oxidizing.waxed()) {
167 170 golem.setOxidizing(CopperGolem.Oxidizing.waxed());
168 171 }
@@ -110,6 +110,7 @@ final class FarmerManager {
110
110
111 void configureEntity(CopperGolem golem) {
111 void configureEntity(CopperGolem golem) {
112 plugin.getServer().getMobGoals().removeAllGoals(golem);
112 plugin.getServer().getMobGoals().removeAllGoals(golem);
113 CopperGolemVanillaAi.suppress(golem, plugin);
113 golem.setAI(true);
114 golem.setAI(true);
114 golem.setAware(true);
115 golem.setAware(true);
115 golem.setCanPickupItems(false);
116 golem.setCanPickupItems(false);
@@ -163,6 +164,8 @@ final class FarmerManager {
163 }
164 }
164
165
165 void tick() {
166 void tick() {
167 CopperGolemVanillaAi.suppress(golem, plugin);
168 CopperGolemVanillaAi.keepIdle(golem);
166 if (preventOxidation && golem.getOxidizing() != CopperGolem.Oxidizing.waxed()) {
169 if (preventOxidation && golem.getOxidizing() != CopperGolem.Oxidizing.waxed()) {
167 golem.setOxidizing(CopperGolem.Oxidizing.waxed());
170 golem.setOxidizing(CopperGolem.Oxidizing.waxed());
168 }
171 }