MCPcopy
hub / github.com/PrismarineJS/mineflayer / openVillager

Function openVillager

lib/plugins/villager.js:72–124  ·  view source on GitHub ↗
(villagerEntity)

Source from the content-addressed store, hash-verified

70 }
71
72 async function openVillager (villagerEntity) {
73 const villagerType = entitiesByName.villager ? entitiesByName.villager.id : entitiesByName.Villager.id
74 assert.strictEqual(villagerEntity.entityType, villagerType)
75 let ready = false
76
77 const villagerPromise = bot.openEntity(villagerEntity)
78 bot._client.on(tradeListPacket, gotTrades)
79 const villager = await villagerPromise
80 if (villager.type !== 'minecraft:villager' && villager.type !== 'minecraft:merchant') {
81 throw new Error('Expected minecraft:villager or minecraft:mechant type, but got ' + villager.type)
82 }
83
84 villager.trades = null
85 villager.selectedTrade = null
86
87 villager.once('close', () => {
88 bot._client.removeListener(tradeListPacket, gotTrades)
89 })
90
91 villager.trade = async (index, count) => {
92 await bot.trade(villager, index, count)
93 }
94
95 if (!ready) await once(villager, 'ready')
96 return villager
97
98 async function gotTrades (packet) {
99 const villager = await villagerPromise
100 if (packet.windowId !== villager.id) return
101 assert.ok(packet.trades)
102 villager.trades = packet.trades.map(trade => {
103 trade.inputs = [trade.inputItem1 = Item.fromNotch(trade.inputItem1 || { blockId: -1 })]
104 if (trade.inputItem2?.itemCount != null) {
105 trade.inputs.push(trade.inputItem2 = Item.fromNotch(trade.inputItem2 || { blockId: -1 }))
106 }
107
108 trade.hasItem2 = !!(trade.inputItem2 && trade.inputItem2.type && trade.inputItem2.count)
109 trade.outputs = [trade.outputItem = Item.fromNotch(trade.outputItem || { blockId: -1 })]
110
111 if (trade.demand !== undefined && trade.specialPrice !== undefined) { // the price is affected by demand and reputation
112 const demandDiff = Math.max(0, Math.floor(trade.inputItem1.count * trade.demand * trade.priceMultiplier))
113 trade.realPrice = Math.min(Math.max((trade.inputItem1.count + trade.specialPrice + demandDiff), 1), trade.inputItem1.stackSize)
114 } else {
115 trade.realPrice = trade.inputItem1.count
116 }
117 return trade
118 })
119 if (!ready) {
120 ready = true
121 villager.emit('ready')
122 }
123 }
124 }
125
126 async function trade (villager, index, count) {
127 const choice = parseInt(index, 10) // allow string argument

Callers

nothing calls this directly

Calls 1

onceFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…