minecraft-java-edition – How to Handle Initial Villager Spawning in Minecraft

minecraft-java-edition

On the initial generation (before any player interactions, and not repopulation) of a village, what determines where the villagers will spawn? Will they spawn inside houses within a radius of the center? Or outside on the gravel paths/farms?.

Best Answer

During generation, a structure has the option to run the following function:

protected void spawnVillagers(World worldIn, StructureBoundingBox sbb, int X, int Y, int Z, int maxVillagers)
{
    if (this.villagersSpawned < maxVillagers)
    {
        for (int i = this.villagersSpawned; i < maxVillagers; ++i)
        {
            int xOffset = this.getXWithOffset(X + i, Z);
            int yOffset = this.getYWithOffset(Y);
            int zOffset = this.getZWithOffset(X + i, Z);

            if (!sbb.isVecInside(new BlockPos(xOffset, yOffset, zOffset)))
            {
                break;
            }

            ++this.villagersSpawned;
            EntityVillager entityvillager = new EntityVillager(worldIn);
            entityvillager.setLocationAndAngles(xOffset + 0.5D, yOffset, zOffset + 0.5D, 0.0F, 0.0F);
            entityvillager.onInitialSpawn(worldIn.getDifficultyForLocation(new BlockPos(entityvillager)), (IEntityLivingData)null);
            entityvillager.setProfession(this.func_180779_c(i, entityvillager.getProfession()));
            worldIn.spawnEntityInWorld(entityvillager);
        }
    }
}

As long as the number villagers spawned for this individual structure is less than the maximum input, it will attempt to spawn a villager at the specified coordinates (which are initially relative to the corner of the structure's bounding box) provided it's not inside a block. To reiterate: the villagersSpawned is not for the entire village, but for this individually-spawned structure.

An example of the function being called, which is used by the "church" structure:

this.spawnVillagers(worldIn, structureBoundingBoxIn, 2, 1, 2, 1);

The maximum number of villagers to spawn inside a church is 1 and will always spawn at the provided offset coordinates.

Out of all the structures available, only large and small farms do not use this function.

Here are images where villagers will always spawn, represented by armor stands (though rotation of the structure may cause them to be mirrored on the other side).

Overview

Here is an example village where villager spawns are highlighted (armor stands with the Glowing effect):

Entire village with glowing armor stands

Church

Church

Wood Hut

Wood hut

Large House

Large House

Butcher's Shop

Butcher's Shop

Library

Library

Small House

Small House

Blacksmith

Blacksmith