-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
188 lines (153 loc) · 6.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const { getWindowName, addItemData } = require('./utils')
const DEFAULT_VERSION = '1.18'
module.exports = function (bot, options = {}) {
options.webPath = options.webPath ?? options.path ?? '/'
const express = options.express ?? require('express')
const app = options.app ?? express()
const http = options.http ?? require('http').createServer(app)
const io = options.io ?? require('socket.io')(http)
options.port = options.port ?? 3000
options.windowUpdateDebounceTime = options.windowUpdateDebounceTime ?? options.debounceTime ?? 100
if (!options.webPath.startsWith('/')) options.webPath = '/' + options.webPath
const path = require('path')
const _ = require('lodash')
bot.webInventory = {
options,
isRunning: false
}
const start = () => {
return new Promise((resolve, reject) => {
if (bot.webInventory.isRunning) return reject(new Error('(mineflayer-web-inventory) INFO: mineflayer-web-inventory is already running'))
http.listen(options.port, () => {
bot.webInventory.isRunning = true
console.log(`(mineflayer-web-inventory) INFO: Inventory web server running on *:${options.port}`)
resolve()
})
})
}
const stop = () => {
return new Promise((resolve, reject) => {
if (!bot.webInventory.isRunning) return reject(new Error('(mineflayer-web-inventory) INFO: mineflayer-web-inventory is not running'))
http.close(() => {
bot.webInventory.isRunning = false
console.log('(mineflayer-web-inventory) INFO: Inventory web server closed')
resolve()
})
})
}
// Try to load mcData
let mcData = require('minecraft-data')(bot.version)
if (!mcData) {
mcData = require('minecraft-assets')(DEFAULT_VERSION)
if (mcData) {
console.log(`(mineflayer-web-inventory) WARNING: Please, specify a bot.version or mineflayer-web-inventory may not work properly. Using version ${DEFAULT_VERSION} for minecraft-mcData`)
} else {
console.log('(mineflayer-web-inventory) ERROR: Unable to load minecraft-mcData')
return
}
}
// Try to load mcAssets
let mcAssets = require('minecraft-assets')(bot.version)
if (!mcAssets) {
mcAssets = require('minecraft-assets')(DEFAULT_VERSION)
if (mcAssets) {
console.log(`(mineflayer-web-inventory) WARNING: Please, specify a bot.version or mineflayer-web-inventory may not work properly. Using version ${DEFAULT_VERSION} for minecraft-assets`)
} else {
console.log('(mineflayer-web-inventory) ERROR: Couldn\'t load minecraft-assets')
return
}
}
app.use(options.webPath, express.static(path.join(__dirname, 'client', 'public')))
io.on('connection', (socket) => {
function emitWindow (window) {
// Use a copy of window to avoid modifying the internal state of mineflayer
window = Object.assign({}, window)
const windowUpdate = { id: window.id, type: getWindowName(window), slots: {} }
// If the window is not supported, we transform it into a inventory update
if (!windowUpdate.type) {
windowUpdate.id = bot.inventory.id
windowUpdate.type = getWindowName(bot.inventory)
window.slots = Array(9).fill(null, 0, 9).concat(window.slots.slice(window.inventoryStart, window.inventoryEnd)) // The 9 empty slots that we add are the armor and crafting slots that are not included in inventoryStart and inventoryEnd
window.slots.forEach(item => { if (item) item.slot -= window.inventoryStart - 9 })
windowUpdate.unsupported = true
windowUpdate.realId = window.id
windowUpdate.realType = window.type
}
const slots = Object.assign({}, window.slots)
for (const item in slots) {
if (slots[item]) slots[item] = addItemData(mcData, mcAssets, slots[item])
}
windowUpdate.slots = slots
socket.emit('window', windowUpdate)
}
// On connection sends the initial state of the current window or inventory if there's no window open
emitWindow(bot.currentWindow ?? bot.inventory)
// Updates are queued here to allow debouncing the 'windowUpdate' event
const defaultUpdateObject = { id: null, type: null, slots: {} }
let updateObject = defaultUpdateObject
const debounceUpdate = _.debounce(() => {
socket.emit('windowUpdate', updateObject)
updateObject = defaultUpdateObject
}, bot.webInventory.options.windowUpdateDebounceTime)
function update (slot, oldItem, newItem, window) {
const originalSlot = slot
// Use copies of oldItem and newItem to avoid modifying the internal state of mineflayer
if (oldItem) oldItem = Object.assign({}, oldItem)
if (newItem) newItem = Object.assign({}, newItem)
if (!getWindowName(window)) { // If the update comes from an unsupported window, we transform it into a inventory update
updateObject.id = bot.inventory.id
updateObject.type = getWindowName(bot.inventory)
slot -= window.inventoryStart - 9
if (newItem) newItem.slot = slot
} else {
// If the update comes from a window different to the current window, we can just ignore it
if (bot.currentWindow && window.id !== bot.currentWindow.id) return
// If the window has changed but there are updates from the older window still on the queue, we can just scrap those
if ((bot.currentWindow ?? bot.inventory).id !== updateObject.id) {
updateObject.id = window.id
updateObject.type = getWindowName(window)
updateObject.slots = {}
}
}
if (newItem) {
newItem.durabilityUsed = window.slots[originalSlot]?.durabilityUsed
newItem = addItemData(mcData, mcAssets, newItem)
}
updateObject.slots[slot] = newItem
debounceUpdate()
}
bot.inventory.on('updateSlot', (slot, oldItem, newItem) => update(slot, oldItem, newItem, bot.inventory))
let previousWindow
const windowOpenHandler = (window) => {
const windowUpdateHandler = (slot, oldItem, newItem) => {
update(slot, oldItem, newItem, window)
}
const windowCloseHandler = () => {
emitWindow(bot.inventory)
window.removeListener('updateSlot', windowUpdateHandler)
}
// Remove previous listeners
if (previousWindow && previousWindow.id !== bot.inventory.id) {
previousWindow.removeListener('updateSlot', windowUpdateHandler)
previousWindow.removeListener('close', windowCloseHandler)
}
previousWindow = window
emitWindow(window)
window.on('updateSlot', windowUpdateHandler)
window.once('close', windowCloseHandler)
}
bot.on('windowOpen', windowOpenHandler)
socket.once('disconnect', () => {
debounceUpdate.cancel()
bot.inventory.removeListener('updateSlot', update)
bot.removeListener('windowOpen', windowOpenHandler)
})
})
bot.once('end', stop)
if (options.startOnLoad !== false) start() // Start the server by default when the plugin is loaded
bot.webInventory = {
...bot.webInventory,
start,
stop
}
}