Jump to content

Access to ApplyRandomTrade in VillagerRegistry


Recommended Posts

I'm currently working on a mod that will add villagers. How ever, I want to have custom conditions that needs to be fulfilled before a certain villager type can be selected when a new willager is spawned.


I have not found a way to do this currently, but then again I'm compleatley new to minecraft moding so I might be missing something. If so I'd greatley apreciate being pointed in the right direction :)


If how ever there currently is no method, I have done the following modification to VillagerRegistry to allow it.


Added the following:

private Map<Integer, IVillageSpawnCondition> spawnConditionHandlers = Maps.newHashMap();

     * Allow access to the random profession selection for new villagers.
    public interface IVillageSpawnCondition
         * Called to allow profession to be excluded from the random selection when a new villager is created.
         * @param villager
        boolean typeIsValidForVillager( EntityVillager villager, Random random );

     * Register a new villager spawn condition for the specified villager type
     * @param villagerId
     * @param handler
    public void registerVillageSpawnCondition(int villagerId, IVillageSpawnCondition handler)
        spawnConditionHandlers.put(villagerId, handler);


Modified ApplyRandomTrade as follows:

public static void applyRandomTrade(EntityVillager villager, Random rand)
        // Select only the villager id's that fulfills their validation for spawning at this time.
        // If no spawn condition is set for an id we assume it is valid.
        List<Integer> validVillagerIds = Lists.newArrayList();
        for ( int i = 0; i < instance().newVillagerIds.size(); i++ ) {
            if ( instance().spawnConditionHandlers.get( instance().newVillagerIds.get( i ) ) == null )
                validVillagerIds.add( instance().newVillagerIds.get( i ) );
            else if ( instance().spawnConditionHandlers.get( instance().newVillagerIds.get( i ) ).typeIsValidForVillager( villager, rand ) )
                validVillagerIds.add( instance().newVillagerIds.get( i ) );

        // Randomize from the list of validated types instead of the entire list.
        int extra = validVillagerIds.size();
        int trade = rand.nextInt(5 + extra);
        villager.setProfession(trade < 5 ? trade : validVillagerIds.get(trade - 5));


This was based on how the villager trade registering is set up. Any feedback is welcome, as I'm so new to this I don't think a dare post a pull request before I have had some feedback  :o

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.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.


  • Create New...

Important Information

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