I don't know why this is marked as WAI which is not supposed to happen as Player-built Iron Golems DO NOT wander away on Java Edition but I found the core Issue, and it is an easy fix.
},
"component_groups": {
"minecraft:player_created": {
"minecraft:behavior.hurt_by_target": {
"priority": 2,
"entity_types": {
"filters": {
"all_of": [
{ "test": "is_family", "subject": "other", "operator": "not", "value": "player" },
{ "test": "is_family", "subject": "other", "operator": "not", "value": "creeper" }
]
}
}
}
},
"minecraft:village_created": {
"minecraft:behavior.defend_village_target": {
"priority": 1,
"must_reach": true,
"attack_chance": 0.05,
"entity_types": {
"filters": {
"any_of": [
{ "test": "is_family", "subject": "other", "value": "mob" },
{ "test": "is_family", "subject": "other", "value": "player" }
]
}
}
},
"minecraft:dweller": {
"dwelling_type": "village",
"dweller_role": "defender",
"update_interval_base": 60,
"update_interval_variant": 40,
"can_find_poi": false,
"can_migrate": true,
"first_founding_reward": 0
}
}
See how the “minecraft:village_created” block contains this portion:
},
"minecraft:dweller": {
"dwelling_type": "village",
"dweller_role": "defender",
"update_interval_base": 60,
"update_interval_variant": 40,
"can_find_poi": false,
"can_migrate": true,
"first_founding_reward": 0
}
}
This block of code is what keeps the Village-created Iron Golems from wandering away from villages. This is what registers them to any Villages they spawn at and also the very code that makes them search for a nearest Village. Now take a look at the block just above the “minecraft:village_created” called “minecraft:player_created”.
}, "component_groups": { "minecraft:player_created": { "minecraft:behavior.hurt_by_target": { "priority": 2, "entity_types": { "filters": { "all_of": [ { "test": "is_family", "subject": "other", "operator": "not", "value": "player" }, { "test": "is_family", "subject": "other", "operator": "not", "value": "creeper" } ] } } }
As you can see, The “minecraft:dweller” block is missing. Which is the root-cause of Player-created Iron Golems to wander off from Villages. In short, Player-created Iron Golems do not have the code that automatically registers it to any Village it is spawned in.
The fix is easy, All you need to do is remove the default “minecraft:player_created” block and replace it with this:
}, "component_groups": { "minecraft:player_created": { "minecraft:behavior.hurt_by_target": { "priority": 2, "entity_types": { "filters": { "all_of": [ { "test": "is_family", "subject": "other", "operator": "not", "value": "player" }, { "test": "is_family", "subject": "other", "operator": "not", "value": "creeper" } ] } } }, "minecraft:dweller": { "dwelling_type": "village", "dweller_role": "defender", "update_interval_base": 60, "update_interval_variant": 40, "can_find_poi": false, "can_migrate": true, "first_founding_reward": 0 }
Can Confirm for the lastest 1.21.130 Release
I provided a fix which should stop the Player-created Iron Golems from wandering away from villages or any structures that contains a bell, beds, and villagers.