Jump to content

[1.18] Villager AI Help


Will Bradley

Recommended Posts

I'm new to Forge, but I thought injecting some AI into Villagers would be relatively simple— just create a class that extends Goal, and use targetSelector (or goalSelector).addGoal inside an EntityJoinWorldEvent listener. But during testing, I discovered that all running and available goals are always empty for Villagers. Below is the test code— running near an established Village just returns empty lists.

@SubscribeEvent
public void test(EntityEvent event) {
    if (event.getEntity() instanceof Villager) {
        Villager villager = (Villager) event.getEntity();
        if (villager.goalSelector != null) {
            LOGGER.info("available goals: {}",
                    villager.goalSelector.getAvailableGoals()
                            .stream()
                            .map(goal -> new Object[]{goal.getGoal(), goal.getFlags(), goal.getPriority()})
                            .toList()
            );
            LOGGER.info("current goals: {}",
                    villager.goalSelector.getRunningGoals()
                            .map(goal -> new Object[]{goal.getGoal(), goal.getFlags(), goal.getPriority()})
                            .toList()
            );
        }
        if (villager.targetSelector != null) {
            LOGGER.info("available target goals: {}",
                    villager.targetSelector.getAvailableGoals()
                            .stream()
                            .map(goal -> new Object[]{goal.getGoal(), goal.getFlags(), goal.getPriority()})
                            .toList()
            );

            LOGGER.info("current target goals: {}",
                    villager.targetSelector.getRunningGoals()
                            .map(goal -> new Object[]{goal.getGoal(), goal.getFlags(), goal.getPriority()})
                            .toList()
            );
        }
    }
}

Why is this the case, and how will it affect Goals that I add myself?

Link to comment
Share on other sites

EntityEvent is a base class for all entity-related events. Subscribing to this base class will receive all entity events, which includes (for example) LivingJumpEvent but also RenderPlayerEvent.Pre. This achieves absolutely zero useful results. Pick the event class you want and only subscribe to it.

You already mentioned that you want to use EntityJoinWorldEvent, which is indeed the correct event to add / modify the AI of entities. You need to actually use this event then.

Villagers no longer use the goal-based AI system. They use the new brain and activity based AI system. You should still be able to use EntityJoinWorldEvent to modify it though.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.