Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/simple_inventory.js
1467 views
1
const assert = require('assert')
2
3
module.exports = inject
4
5
const QUICK_BAR_COUNT = 9
6
const QUICK_BAR_START = 36
7
8
function inject (bot) {
9
let nextQuickBarSlot = 0
10
11
const armorSlots = {
12
head: 5,
13
torso: 6,
14
legs: 7,
15
feet: 8
16
}
17
18
if (!bot.supportFeature('doesntHaveOffHandSlot')) {
19
armorSlots['off-hand'] = 45
20
}
21
22
async function tossStack (item) {
23
assert.ok(item)
24
await bot.clickWindow(item.slot, 0, 0)
25
await bot.clickWindow(-999, 0, 0)
26
bot.closeWindow(bot.currentWindow || bot.inventory)
27
}
28
29
async function toss (itemType, metadata, count) {
30
const window = bot.currentWindow || bot.inventory
31
const options = {
32
window,
33
itemType,
34
metadata,
35
count,
36
sourceStart: window.inventoryStart,
37
sourceEnd: window.inventoryEnd,
38
destStart: -999
39
}
40
await bot.transfer(options)
41
}
42
43
async function unequip (destination) {
44
if (destination === 'hand') {
45
await equipEmpty()
46
} else {
47
await disrobe(destination)
48
}
49
}
50
51
function setQuickBarSlot (slot) {
52
assert.ok(slot >= 0)
53
assert.ok(slot < 9)
54
if (bot.quickBarSlot === slot) return
55
bot.quickBarSlot = slot
56
bot._client.write('held_item_slot', {
57
slotId: slot
58
})
59
bot.updateHeldItem()
60
}
61
62
async function equipEmpty () {
63
for (let i = 0; i < QUICK_BAR_COUNT; ++i) {
64
if (!bot.inventory.slots[QUICK_BAR_START + i]) {
65
setQuickBarSlot(i)
66
return
67
}
68
}
69
const slot = bot.inventory.firstEmptyInventorySlot()
70
if (!slot) {
71
await bot.tossStack(bot.heldItem)
72
return
73
}
74
const equipSlot = QUICK_BAR_START + bot.quickBarSlot
75
await bot.clickWindow(equipSlot, 0, 0)
76
await bot.clickWindow(slot, 0, 0)
77
if (bot.inventory.selectedItem) {
78
await bot.clickWindow(-999, 0, 0)
79
}
80
}
81
82
async function disrobe (destination) {
83
assert.strictEqual(bot.currentWindow, null)
84
const destSlot = getDestSlot(destination)
85
await bot.putAway(destSlot)
86
}
87
88
async function equip (item, destination) {
89
if (typeof item === 'number') {
90
item = bot.inventory.findInventoryItem(item)
91
}
92
if (item == null || typeof item !== 'object') {
93
throw new Error('Invalid item object in equip (item is null or typeof item is not object)')
94
}
95
if (!destination || destination === null) {
96
destination = 'hand'
97
}
98
const sourceSlot = item.slot
99
let destSlot = getDestSlot(destination)
100
101
if (sourceSlot === destSlot) {
102
// don't need to do anything
103
return
104
}
105
106
if (destination !== 'hand') {
107
await bot.moveSlotItem(sourceSlot, destSlot)
108
return
109
}
110
111
if (destSlot >= QUICK_BAR_START && destSlot < (QUICK_BAR_START + QUICK_BAR_COUNT) && sourceSlot >= QUICK_BAR_START && sourceSlot < (QUICK_BAR_START + QUICK_BAR_COUNT)) {
112
// all we have to do is change the quick bar selection
113
bot.setQuickBarSlot(sourceSlot - QUICK_BAR_START)
114
return
115
}
116
117
// find an empty slot on the quick bar to put the source item in
118
destSlot = bot.inventory.firstEmptySlotRange(QUICK_BAR_START, QUICK_BAR_START + QUICK_BAR_COUNT)
119
if (destSlot == null) {
120
// LRU cache for the quick bar items
121
destSlot = QUICK_BAR_START + nextQuickBarSlot
122
nextQuickBarSlot = (nextQuickBarSlot + 1) % QUICK_BAR_COUNT
123
}
124
setQuickBarSlot(destSlot - QUICK_BAR_START)
125
await bot.moveSlotItem(sourceSlot, destSlot)
126
}
127
128
function getDestSlot (destination) {
129
if (destination === 'hand') {
130
return QUICK_BAR_START + bot.quickBarSlot
131
} else {
132
const destSlot = armorSlots[destination]
133
assert.ok(destSlot != null, `invalid destination: ${destination}`)
134
return destSlot
135
}
136
}
137
138
function leftMouse (slot) {
139
return bot.clickWindow(slot, 0, 0)
140
}
141
142
function rightMouse (slot) {
143
return bot.clickWindow(slot, 1, 0)
144
}
145
146
bot.equip = equip
147
bot.unequip = unequip
148
bot.toss = toss
149
bot.tossStack = tossStack
150
bot.setQuickBarSlot = setQuickBarSlot
151
bot.getEquipmentDestSlot = getDestSlot
152
bot.simpleClick = { leftMouse, rightMouse }
153
154
// constants
155
bot.QUICK_BAR_START = QUICK_BAR_START
156
}
157
158