Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/enchantment_table.js
1467 views
1
const assert = require('assert')
2
const { once } = require('../promise_utils')
3
4
module.exports = inject
5
6
function inject (bot) {
7
async function openEnchantmentTable (enchantmentTableBlock) {
8
assert.strictEqual(enchantmentTableBlock.name, 'enchanting_table')
9
let ready = false
10
const enchantmentTable = await bot.openBlock(enchantmentTableBlock)
11
if (!enchantmentTable.type.startsWith('minecraft:enchant')) {
12
throw new Error('Expected minecraft:enchant when opening table but got ' + enchantmentTable.type)
13
}
14
15
resetEnchantmentOptions()
16
17
enchantmentTable.enchant = enchant
18
enchantmentTable.takeTargetItem = takeTargetItem
19
enchantmentTable.putTargetItem = putTargetItem
20
enchantmentTable.putLapis = putLapis
21
enchantmentTable.targetItem = function () { return this.slots[0] }
22
23
bot._client.on('craft_progress_bar', onUpdateWindowProperty)
24
enchantmentTable.once('close', () => {
25
bot._client.removeListener('craft_progress_bar', onUpdateWindowProperty)
26
})
27
28
return enchantmentTable
29
30
function onUpdateWindowProperty (packet) {
31
if (packet.windowId !== enchantmentTable.id) return
32
assert.ok(packet.property >= 0)
33
34
const slots = enchantmentTable.enchantments
35
36
if (packet.property < 3) {
37
const slot = slots[packet.property]
38
slot.level = packet.value
39
} else if (packet.property === 3) {
40
enchantmentTable.xpseed = packet.value
41
} else if (packet.property < 7) {
42
const slot = slots[packet.property - 4]
43
slot.expected.enchant = packet.value
44
} else if (packet.property < 10) {
45
const slot = slots[packet.property - 7]
46
slot.expected.level = packet.value
47
}
48
49
if (slots[0].level >= 0 && slots[1].level >= 0 && slots[2].level >= 0) {
50
if (!ready) {
51
ready = true
52
enchantmentTable.emit('ready')
53
}
54
} else {
55
ready = false
56
}
57
}
58
59
function resetEnchantmentOptions () {
60
enchantmentTable.xpseed = -1
61
enchantmentTable.enchantments = []
62
for (let slot = 0; slot < 3; slot++) {
63
enchantmentTable.enchantments.push({
64
level: -1,
65
expected: {
66
enchant: -1,
67
level: -1
68
}
69
})
70
}
71
ready = false
72
}
73
74
async function enchant (choice) {
75
if (!ready) await once(enchantmentTable, 'ready')
76
choice = parseInt(choice, 10) // allow string argument
77
assert.notStrictEqual(enchantmentTable.enchantments[choice].level, -1)
78
bot._client.write('enchant_item', {
79
windowId: enchantmentTable.id,
80
enchantment: choice
81
})
82
const [, newItem] = await once(enchantmentTable, 'updateSlot:0')
83
return newItem
84
}
85
86
async function takeTargetItem () {
87
const item = enchantmentTable.targetItem()
88
assert.ok(item)
89
await bot.putAway(item.slot)
90
return item
91
}
92
93
async function putTargetItem (item) {
94
await bot.moveSlotItem(item.slot, 0)
95
}
96
97
async function putLapis (item) {
98
await bot.moveSlotItem(item.slot, 1)
99
}
100
}
101
102
bot.openEnchantmentTable = openEnchantmentTable
103
}
104
105