Skip to content

VillagerV2 5.46.0 ​

class · pocketmine\entity\npc\VillagerV2 · source

Extends Animal
Implements Trader

The current villager (Bedrock "minecraft:villager_v2", entities/villager_v2.json): 20 health, 0.6x1.9, movement 0.5 (strolls and panics at 0.6x), flees from zombies, babies scale 0.5. Profession (VARIANT 0-14: unemployed, farmer, fisherman, shepherd, fletcher, librarian, cartographer, cleric, armorer, weaponsmith, toolsmith, butcher, leatherworker, mason, nitwit), biome type (MARK_VARIANT 0-6: plains, desert, jungle, savanna, snow, swamp, taiga; plains by default) and skin (SKIN_ID 0-5). A spawn egg gives a baby 5% of the time, otherwise a random profession.

Village life ("minecraft:dweller", updated every 3-5 seconds of AI time):

  • Claims: a villager claims the nearest free bed within 48 blocks; an unemployed adult (not a nitwit) with a bed claims the nearest free job site block within 32 blocks and takes its profession (JobSites); employed villagers without a job site claim one of their profession. If the job site is broken, a villager that never traded becomes unemployed again; one that traded keeps its profession and looks for another job site. Claims are saved in the NBT, registered in the world's \pocketmine\world\raid\PoiManager and released on death.
  • Schedule (VillagerSchedule): work at the job site (farmers also harvest and replant crops), gather and gossip at the bell, wander, go home and sleep in the claimed bed at night (SLEEPING flag, bed occupied). Babies play. When it rains (or snows) where they are, villagers go home instead. A sleeping villager wakes up when hurt, when its schedule moves on or when a player uses its bed.
  • Trades restock when the villager arrives at its job site, at most twice a day, if it has a bed (Bedrock).
  • Food: villagers pick up bread, carrots, potatoes and beetroots within reach (farmers also wheat, seeds and bone meal) into 8 hidden slots; farmers bake 3 wheat into bread; villagers with a surplus throw half of it to hungry villagers nearby.
  • Breeding: an adult with at least 12 food points (bread 4, carrot/potato/beetroot 1), outside its work hours and with a free bed nearby, spends them and becomes willing (in love) for 30 seconds; two willing villagers make a baby, which grows up unemployed in 20 minutes.
  • Gossip (Gossips): curing, trading, hitting and killing villagers change the reputation of the player, which lowers prices (see TradePrices) and makes iron golems hostile at -100 or less.
  • Iron golems: when villagers gossip, a village with at least 10 villagers per golem (plus one), 20 beds and 75% of its villagers having worked in the last day summons an iron golem nearby (Bedrock conditions).

Trading: players open the vanilla trading screen (UpdateTradePacket, see TradeInventory); trades come from VillagerTrades, two per level. Trading gives the villager experience (TRADE_XP) and levels it up (TRADE_TIER, up to master) with new trades, and gives the player experience. Players with Hero of the Village pay less.

During a raid villagers run to the beds of the village (HideFromRaidGoal); they celebrate when it is won. Struck by lightning a villager becomes a witch.

Not implemented: trade demand, bone meal fertilizing, librarians inspecting bookshelves, fishermen/shepherd specific work goals, waking up when a bell rings, village migration and the Java panic golem summoning.

Constants ​

BED_SEARCH_HORIZONTAL ​

php
public const BED_SEARCH_HORIZONTAL = 48

Range of the bed and job site searches.

BED_SEARCH_VERTICAL ​

php
public const BED_SEARCH_VERTICAL = 16

BREEDING_FOOD_POINTS ​

php
public const BREEDING_FOOD_POINTS = 12

Food points spent to become willing to breed (3 bread, or 12 carrots, potatoes or beetroots).

DWELLER_INTERVAL ​

php
public const DWELLER_INTERVAL = 60

Ticks between two village updates (Bedrock dweller update_interval_base 60 + variant 40).

DWELLER_INTERVAL_VARIANT ​

php
public const DWELLER_INTERVAL_VARIANT = 40

GOLEM_CHECK_INTERVAL ​

php
public const GOLEM_CHECK_INTERVAL = 700

GOLEM_DEFEND_RANGE ​

php
public const GOLEM_DEFEND_RANGE = 16.0

Iron golems within this range help a villager that is hurt (see IronGolem::onVillagerAttacked()).

GOLEM_MIN_BEDS ​

php
public const GOLEM_MIN_BEDS = 20

GOLEM_VILLAGERS_PER_GOLEM ​

php
public const GOLEM_VILLAGERS_PER_GOLEM = 10

Iron golem summoning (Bedrock): villagers per golem, beds and share of villagers that worked in the last day.

GOLEM_WORKED_RATIO ​

php
public const GOLEM_WORKED_RATIO = 0.75

GOSSIP_CURE_MAJOR ​

php
public const GOSSIP_CURE_MAJOR = 20

GOSSIP_CURE_MINOR ​

php
public const GOSSIP_CURE_MINOR = 25

GOSSIP_HIT ​

php
public const GOSSIP_HIT = 25

GOSSIP_KILL ​

php
public const GOSSIP_KILL = 25

GOSSIP_TRADE ​

php
public const GOSSIP_TRADE = 2

INVENTORY_SIZE ​

php
public const INVENTORY_SIZE = 8

JOB_SITE_SEARCH_HORIZONTAL ​

php
public const JOB_SITE_SEARCH_HORIZONTAL = 32

JOB_SITE_SEARCH_VERTICAL ​

php
public const JOB_SITE_SEARCH_VERTICAL = 8

KILL_WITNESS_RANGE ​

php
public const KILL_WITNESS_RANGE = 16.0

Villagers this close to a villager killed by a player remember it.

MAX_RESTOCKS_PER_DAY ​

php
public const MAX_RESTOCKS_PER_DAY = 2

Restocks a day at most (vanilla: twice, when working at the job site).

MAX_TIER ​

php
public const MAX_TIER = 4

PROFESSION_ARMORER ​

php
public const PROFESSION_ARMORER = 8

PROFESSION_BUTCHER ​

php
public const PROFESSION_BUTCHER = 11

PROFESSION_CARTOGRAPHER ​

php
public const PROFESSION_CARTOGRAPHER = 6

PROFESSION_CLERIC ​

php
public const PROFESSION_CLERIC = 7

PROFESSION_FARMER ​

php
public const PROFESSION_FARMER = 1

PROFESSION_FISHERMAN ​

php
public const PROFESSION_FISHERMAN = 2

PROFESSION_FLETCHER ​

php
public const PROFESSION_FLETCHER = 4

PROFESSION_LEATHERWORKER ​

php
public const PROFESSION_LEATHERWORKER = 12

PROFESSION_LIBRARIAN ​

php
public const PROFESSION_LIBRARIAN = 5

PROFESSION_MASON ​

php
public const PROFESSION_MASON = 13

PROFESSION_NITWIT ​

php
public const PROFESSION_NITWIT = 14

PROFESSION_SHEPHERD ​

php
public const PROFESSION_SHEPHERD = 3

PROFESSION_TOOLSMITH ​

php
public const PROFESSION_TOOLSMITH = 10

PROFESSION_UNEMPLOYED ​

php
public const PROFESSION_UNEMPLOYED = 0

PROFESSION_WEAPONSMITH ​

php
public const PROFESSION_WEAPONSMITH = 9

TAG_BED ​

php
public const TAG_BED = "PmBedPosition"

TAG_GOSSIPS ​

php
public const TAG_GOSSIPS = "Gossips"

TAG_GOSSIP_DECAY ​

php
public const TAG_GOSSIP_DECAY = "PmGossipDecay"

TAG_INVENTORY ​

php
public const TAG_INVENTORY = "Inventory"

TAG_JOB_SITE ​

php
public const TAG_JOB_SITE = "PmJobSitePosition"

TAG_OFFERS ​

php
public const TAG_OFFERS = "Offers"

TAG_PROFESSION ​

php
public const TAG_PROFESSION = "Variant"

TAG_RESTOCKS ​

php
public const TAG_RESTOCKS = "PmRestocks"

TAG_RESTOCK_DAY ​

php
public const TAG_RESTOCK_DAY = "PmRestockDay"

TAG_SKIN ​

php
public const TAG_SKIN = "SkinID"

TAG_TRADE_EXP ​

php
public const TAG_TRADE_EXP = "TradeExperience"

TAG_TRADE_TIER ​

php
public const TAG_TRADE_TIER = "TradeTier"

TAG_TYPE ​

php
public const TAG_TYPE = "MarkVariant"

TYPE_DESERT ​

php
public const TYPE_DESERT = 1

TYPE_JUNGLE ​

php
public const TYPE_JUNGLE = 2

TYPE_PLAINS ​

php
public const TYPE_PLAINS = 0

TYPE_SAVANNA ​

php
public const TYPE_SAVANNA = 3

TYPE_SNOW ​

php
public const TYPE_SNOW = 4

TYPE_SWAMP ​

php
public const TYPE_SWAMP = 5

TYPE_TAIGA ​

php
public const TYPE_TAIGA = 6

UNREACHABLE_BED_MEMORY ​

php
public const UNREACHABLE_BED_MEMORY = 1200

Ticks an unreachable bed is skipped when looking for one.

VILLAGE_RANGE_HORIZONTAL ​

php
public const VILLAGE_RANGE_HORIZONTAL = 32

VILLAGE_RANGE_VERTICAL ​

php
public const VILLAGE_RANGE_VERTICAL = 16

Methods ​

addToInventory() ​

php
public function addToInventory(\pocketmine\item\Item $item) : void

Puts the item in the villager's inventory; what doesn't fit is dropped.

addTradeExperience() ​

php
public function addTradeExperience(int $exp) : void

Adds trading experience, levelling the villager up (with new trades) when it reaches the next level.

attack() ​

php
public function attack(\pocketmine\event\entity\EntityDamageEvent $source) : void

breed() ​

php
public function breed(\pocketmine\entity\animal\Animal $partner) : ?\pocketmine\entity\animal\Animal

canBeLeashed() ​

php
public function canBeLeashed() : bool

Villagers cannot be led (vanilla).

canTradeWith() ​

php
public function canTradeWith(\pocketmine\player\Player $player) : bool

canWalkTo() ​

php
public function canWalkTo(\pocketmine\math\Vector3 $block) : bool

Whether the path finder finds a way to the block (within 1.5 blocks of it).

claimBed() ​

php
public function claimBed(\pocketmine\math\Vector3 $bed) : bool

Claims the bed (its head block). Returns false if another villager has it.

claimJobSite() ​

php
public function claimJobSite(\pocketmine\math\Vector3 $pos) : bool

Claims the job site block; an unemployed villager takes its profession. Returns false if another villager has it or it isn't a job site of the villager's profession.

countFreeBeds() ​

php
public function countFreeBeds() : int

Unclaimed beds around the villager (in the village).

createBaby() protected ​

php
protected function createBaby(\pocketmine\entity\animal\Animal $partner, \pocketmine\entity\Location $location) : \pocketmine\entity\animal\Animal

createNavigation() protected ​

php
protected function createNavigation() : \pocketmine\entity\ai\navigation\PathNavigator

Villagers path through doors and open them (vanilla navigation can_open_doors, see DoorInteraction).

entityBaseTick() protected ​

php
protected function entityBaseTick(int $tickDiff = 1) : bool

findSleeperAt() ​

php
public static function findSleeperAt(\pocketmine\world\World $world, \pocketmine\math\Vector3 $bedHead) : ?\pocketmine\entity\npc\VillagerV2

Returns the villager sleeping in the bed whose head is at the position, if any.

getActivity() ​

php
public function getActivity() : \pocketmine\entity\npc\VillagerActivity

What the villager does now according to its schedule.

getBedPosition() ​

php
public function getBedPosition() : ?\pocketmine\math\Vector3

getFoodPoints() ​

php
public static function getFoodPoints(\pocketmine\item\Item $item) : int

Food points of an item: bread 4, carrot, potato and beetroot 1.

getFoodPointsInInventory() ​

php
public function getFoodPointsInInventory() : int

getGossips() ​

php
public function getGossips() : \pocketmine\entity\npc\Gossips

getInitialSizeInfo() protected ​

php
protected function getInitialSizeInfo() : \pocketmine\entity\EntitySizeInfo

getInventory() ​

php
public function getInventory() : \pocketmine\inventory\SimpleInventory

getJobSitePosition() ​

php
public function getJobSitePosition() : ?\pocketmine\math\Vector3

getName() ​

php
public function getName() : string

getNetworkTypeId() ​

php
public static function getNetworkTypeId() : string

getPlayerReputation() ​

php
public function getPlayerReputation(\pocketmine\player\Player $player) : int

getProfession() ​

php
public function getProfession() : int

getRestocksToday() ​

php
public function getRestocksToday() : int

getShareableFood() ​

php
public function getShareableFood() : ?\pocketmine\item\Item

Food the villager can give away: half of a stack of at least 6 bread or 24 carrots, potatoes or beetroots.

getTierExpRequirements() ​

php
public function getTierExpRequirements() : array

getTradeDisplayName() ​

php
public function getTradeDisplayName() : string

getTradeExperience() ​

php
public function getTradeExperience() : int

getTradeOffers() ​

php
public function getTradeOffers() : array

getTradeTier() ​

php
public function getTradeTier() : int

getTradingPlayer() ​

php
public function getTradingPlayer() : ?\pocketmine\player\Player

getVillagerType() ​

php
public function getVillagerType() : int

getXpDropAmount() ​

php
public function getXpDropAmount() : int

gossipWith() ​

php
public function gossipWith(\pocketmine\entity\npc\VillagerV2 $other) : void

Two villagers talk: they share their gossip, and the village may summon an iron golem.

hasTraded() ​

php
public function hasTraded() : bool

Whether a player traded with this villager: it then keeps its profession for good.

hasWorkedRecently() ​

php
public function hasWorkedRecently() : bool

Whether the villager worked at its job site during the last day.

initEntity() protected ​

php
protected function initEntity(\pocketmine\nbt\tag\CompoundTag $nbt) : void

isBreedingItem() ​

php
public function isBreedingItem(\pocketmine\item\Item $item) : bool

isCelebrating() ​

php
public function isCelebrating() : bool

isSleeping() ​

php
public function isSleeping() : bool

isWillingToBreed() ​

php
public function isWillingToBreed() : bool

Whether the villager becomes willing to breed now: an adult (not on cooldown) with enough food, outside its work hours (unemployed villagers and nitwits any time), awake, with a free bed in the village.

markBedUnreachable() ​

php
public function markBedUnreachable(\pocketmine\math\Vector3 $bed) : void

Forgets the bed if it is the claimed one and skips it when looking for a bed for a while (the villager couldn't get there).

onArriveAtWork() ​

php
public function onArriveAtWork() : void

Called by WorkAtJobSiteGoal when the villager reaches its job site: restocks the trades if some were used, at most VillagerV2::MAX_RESTOCKS_PER_DAY times a day and only with a bed (Bedrock).

onCuredBy() ​

php
public function onCuredBy(string $playerUuid) : void

Called when a player cured this villager from a zombie villager: major and minor positive gossip (the cure discount).

onDeath() protected ​

php
protected function onDeath() : void

onDispose() protected ​

php
protected function onDispose() : void

onInteractWithItem() protected ​

php
protected function onInteractWithItem(\pocketmine\player\Player $player, \pocketmine\item\Item $item) : bool

onStruckByLightning() ​

php
public function onStruckByLightning(\pocketmine\entity\object\LightningBolt $bolt) : void

Vanilla: lightning turns a villager into a witch (not on Peaceful), which frees its bed and job site.

onTrade() ​

php
public function onTrade(\pocketmine\player\Player $player, \pocketmine\entity\npc\TradeOffer $offer, int $times) : void

registerGoals() protected ​

php
protected function registerGoals() : void

releaseBed() ​

php
public function releaseBed() : void

releaseJobSite() ​

php
public function releaseJobSite() : void

restock() ​

php
public function restock() : void

Resets the uses of every trade.

saveNBT() ​

php
public function saveNBT() : \pocketmine\nbt\tag\CompoundTag

setCelebrating() ​

php
public function setCelebrating(bool $celebrating) : void

Villagers celebrate when their village wins a raid.

setProfession() ​

php
public function setProfession(int $profession) : void

Changes the profession; the trades are replaced by the new profession's for the current level.

setTradingPlayer() ​

php
public function setTradingPlayer(?\pocketmine\player\Player $player) : void

setVillagerType() ​

php
public function setVillagerType(int $type) : void

startSleeping() ​

php
public function startSleeping() : bool

Lies down in the claimed bed (the villager must be next to it). Returns false if there is no bed or someone else sleeps in it.

syncNetworkData() protected ​

php
protected function syncNetworkData(\pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection $properties) : void

throwFoodTo() ​

php
public function throwFoodTo(\pocketmine\entity\npc\VillagerV2 $receiver) : void

Throws half of a food surplus towards the other villager.

tickAi() protected ​

php
protected function tickAi(int $currentTick) : void

trySummonGolem() ​

php
public function trySummonGolem() : ?\pocketmine\entity\golem\IronGolem

Summons an iron golem near the villager if the village around it meets the Bedrock conditions (see the class doc). Each 32x32 area is checked at most once every VillagerV2::GOLEM_CHECK_INTERVAL ticks.

typeForBiome() ​

php
public static function typeForBiome(\pocketmine\world\World $world, \pocketmine\math\Vector3 $pos) : int

Villager type of a biome (Bedrock biome tags).

updateDweller() ​

php
public function updateDweller() : void

Registers the saved claims, checks they still stand, claims a bed and a job site if needed, and updates food and breeding (vanilla dweller update, every 3-5 seconds).

wakeUp() ​

php
public function wakeUp() : void

Gets out of bed. A villager that slept until morning heals (Bedrock).

wantsFood() ​

php
public function wantsFood() : bool

Whether the villager has less food than it wants (less than 12 food points).

wantsItem() ​

php
public function wantsItem(\pocketmine\item\Item $item) : bool

Whether the villager picks up the item: food, plus wheat, seeds and bone meal for farmers.

VanillaMine is a fork of PocketMine-MP, maintained by Luxury Nodes. Not affiliated with the PMMP team or Mojang.