Wandering Trader

"A mysterious blue-clad villager has appeared out of nowhere! You gasp and drop your controller. You fall to your knees. Your mind explodes with questions: "How did it get here?", "Why is it trying to trade with me?" and "Minecraft villagers outside of villages? What blasphemy is this?!" Calm down, friend! Everything is fine! You've just encountered Minecraft's latest and most experienced salesman – the Wandering Trader!"

- Per Landin

The wandering trader is a villager-like passive NPC mob found randomly across the world. But mostly in villages.

Spawning
The wandering trader appears randomly across the world or periodically in village gathering sites, usually near village meeting points, with two leashed trader llamas.

At any time, there can be only one wandering trader naturally spawned in the world. After 24000 ticks (20 real-life minutes) have passed since the world is created, the game attempts to spawn a wandering trader. If there are no wandering traders currently in the world, the game tries to spawn a new wandering trader after every following 24000 ticks, within a 48-block radius of a player. The first spawn attempt has a 2.5% chance of success; if it fails, after another 24000 ticks another spawn attempt is made with a 5% chance of success. If that fails, for all subsequent spawn attempts the chance caps at 7.5%. After 48000 or 72000 ticks, the trader despawns along with its llamas, resetting the cycle.

Trader llamas will not despawn if they are detached from the wandering trader's leads.

In Bedrock Edition, when using spawn eggs or the command to spawn a wandering trader, llamas always spawn with it. Wandering traders never spawn if   is set to false.

Behavior
The wandering trader has six random trades. New trades are not unlocked after trading with it.

After spawning, the wandering trader has a managed schedule for 40–60 minutes. After the time expires, the wandering trader despawns, even if the wandering trader is named with a name tag or put in a vehicle such as a minecart or boat. A wandering trader despawns sooner if all trades have been locked. The trader may later respawn with new trades.

The wandering trader can sometimes form a caravan, due to wild llamas that follow the leashed trader llama(s).

Wandering traders may drink a potion of invisibility during dusk. At dawn they drink milk to remove the invisibility.

Wandering traders avoid zombies and their variants, all illager variants, and vexes, staying at least 8 blocks away. Unlike other villagers, a wandering trader killed by zombies does not become a zombie.

Wandering traders drink potions of invisibility if hurt by magic attacks or projectiles from hostile mobs, or when avoiding illagers, vexes, and zombies.

When attacked by a player, a wandering trader flees from the player as a villager would do.

Drops
A wandering trader can drop:
 * leads when killed, or when separated too far from its llamas (obstructions or deep water can prevent the llamas from following). The leads break and appear at the position of the llamas. The llamas also drops their leads when killed.
 * a milk bucket (8.5% chance, increased 1% per level of Looting) when killed.
 * a potion of invisibility (8.5% chance, increased 1% per level of Looting) when killed while holding or before finishing drinking.

Trading
The trading system is a gameplay mechanic that allows players to buy items with emeralds.

A player can right-click on the wandering trader to view the items offered for sale. Different offers may be viewed by pressing the left and right buttons next to the currently displayed offer. All offers involve emeralds as a currency. Wandering Traders typically sell items generated in the world or otherwise related to nature, such as plants, dyes, and buckets of fish. They can also trade less common items, such as coral or blue ice.

After the player purchases the same item several times, the wandering trader will lock the trade, but unlike villagers, is unable to unlock the trade.

Unlike villagers who both give and receive emeralds in exchange for items, wandering traders only sell items for emeralds. Wandering traders also have no experience bar and they don't modify their offers based on increased or decreased demand.

Data values
Wandering traders have entity data associated with them that contain various properties of the mob.