Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.18] Villager AI Help


Will Bradley
 Share

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.