-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathmineflayer.test.js
361 lines (324 loc) · 12.4 KB
/
mineflayer.test.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* eslint-env mocha */
globalThis.isMocha = true
const fs = require('fs')
const { join } = require('path')
const squid = require('flying-squid')
const settings = require('../config/default-settings.json')
const mineflayer = require('mineflayer')
const { Vec3 } = require('vec3')
const { onceWithTimeout } = require('../src/lib/utils')
const expect = require('expect').default
function assertPosEqual (actual, expected, precision = 1) {
expect(actual.distanceTo(expected)).toBeLessThan(precision)
}
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
const { once } = require('events')
squid.testedVersions.forEach((testedVersion, i) => {
const registry = require('prismarine-registry')(testedVersion)
const version = registry.version
const Item = require('prismarine-item')(testedVersion)
describe('server with mineflayer connection ' + testedVersion + 'v', () => {
/** @type {import('mineflayer').Bot} */
let bot
/** @type {import('mineflayer').Bot} */
let bot2
let serv
let entityName
async function onGround (bot) {
await new Promise((resolve) => {
const l = () => {
if (bot.entity.onGround) {
bot.removeListener('move', l)
resolve()
}
}
bot.on('move', l)
})
}
async function waitMessage (bot, message) {
console.log('Waiting for message', [message])
onceWithTimeout(bot, 'message', 5000, (msg) => {
console.log('*msg', msg)
return msg.toString() === message
})
}
// Clear the world dir before each test
const worldFolder = 'world/test_' + testedVersion
const dir = join(__dirname, '../', worldFolder)
console.log('Clearing world dir', dir)
fs.rmSync(dir, { recursive: true, force: true })
beforeEach(async function () {
console.log('🔻 Running test: ' + this.currentTest.title)
const options = settings
options['online-mode'] = false
options['everybody-op'] = true
options.port = 0
options['view-distance'] = 2
options.worldFolder = undefined
options.logging = false
options.version = version.minecraftVersion
options.generation = { // TODO: fix block tests failing at random without manually specifying seed
name: 'diamond_square',
options: {
seed: 2116746182
}
}
options.worldFolder = worldFolder
options.debug = console.log
serv = squid.createMCServer(options)
if (registry.supportFeature('entityCamelCase')) {
entityName = 'EnderDragon'
} else {
entityName = 'ender_dragon'
}
console.log('[test] Waiting for server to start')
const [port] = await once(serv, 'listening')
await serv.waitForReady()
console.log('[test] Server is started on', port, version.minecraftVersion)
bot = mineflayer.createBot({
host: 'localhost',
port,
username: 'bot',
version: version.minecraftVersion
})
bot2 = mineflayer.createBot({
host: 'localhost',
port,
username: 'bot2',
version: version.minecraftVersion
})
await Promise.all([once(bot, 'spawn'), once(bot2, 'spawn')])
bot.entity.onGround = false
bot2.entity.onGround = false
// log what the bot is standing on for debugging
const bot1StandingOn = bot.blockAt(bot.entity.position.floored().offset(0, -1, 0))
const bot2StandingOn = bot2.blockAt(bot2.entity.position.floored().offset(0, -1, 0))
console.log('bot1 is standing on', bot1StandingOn)
console.log('bot2 is standing on', bot2StandingOn)
})
// function waitForReady (bot) {
// const viewDistance = 2
// const testExpectedNoChunks = (viewDistance * 2) * (viewDistance * 2)
// return new Promise((resolve) => {
// let recvChunks = 0
// function onColumnLoad () {
// recvChunks++
// if (recvChunks === testExpectedNoChunks) {
// bot.removeListener('chunkColumnLoad', onColumnLoad)
// resolve()
// }
// }
// bot.on('chunkColumnLoad', onColumnLoad)
// })
// }
afterEach(async () => {
console.log('Quitting server...')
await serv.quit()
console.log('Quit server!')
})
describe('actions', () => {
// Log the name of the test being run
beforeEach(function () {
console.log('🔻 Running actions test: ' + this.currentTest.title)
})
it('can dig', async () => {
const pos = bot.entity.position.offset(0, -1, 0).floored()
// Set a dirt block below the bot so we can easily dig
bot.chat(`/setblock ${pos.x} ${pos.y} ${pos.z} dirt`)
await sleep(1000)
console.log('Block at', bot.entity.position, bot.blockAt(pos))
const p = once(bot2, 'blockUpdate')
bot.dig(bot.blockAt(pos))
console.log('Digging...')
const [, newBlock] = await p
console.log('Dug.')
assertPosEqual(newBlock.position, pos)
expect(newBlock.type).toEqual(0)
})
it('can place a block', async () => {
const pos = bot.entity.position.offset(0, -2, 0).floored()
const digPromise = once(bot2, 'blockUpdate')
bot.dig(bot.blockAt(pos))
console.log(' ✔️ dug block at', pos)
let [, newBlock] = await digPromise
assertPosEqual(newBlock.position, pos)
expect(newBlock.type).toEqual(0)
const invPromise = new Promise((resolve) => {
bot.inventory.on('updateSlot', (slot, oldItem, newItem) => {
if (slot === 36 && newItem && newItem.type === 1) { resolve() }
})
})
bot.creative.setInventorySlot(36, new Item(1, 1))
await invPromise
console.log(' ✔️ updated inventory')
const placePromise = once(bot2, 'blockUpdate', { array: true })
bot.placeBlock(bot.blockAt(pos.offset(0, -1, 0)), new Vec3(0, 1, 0));
[, newBlock] = await placePromise
console.log(' ✔️ placed block at', pos)
assertPosEqual(newBlock.position, pos)
expect(newBlock.type).toEqual(1)
})
it('can open and close a chest', async () => {
const chestId = registry.blocksByName.chest.id
const [x, y, z] = [1, 2, 3]
const states = {
open: {
location: { x, y, z },
byte1: 1,
byte2: 1, // open
blockId: chestId
},
closed: {
location: { x, y, z },
byte1: 1,
byte2: 0, // closed
blockId: chestId
}
}
const setBlockPromise = once(bot, 'blockUpdate')
bot.chat(`/setblock ${x} ${y} ${z} ${chestId} 2`) // place a chest facing north
await setBlockPromise
const openPromise1 = once(bot._client, 'block_action', { array: true })
const openPromise2 = once(bot2._client, 'block_action', { array: true })
bot.chat(`/setblockaction ${x} ${y} ${z} 1 1`) // open the chest
const [blockActionOpen] = await openPromise1
const [blockActionOpen2] = await openPromise2
expect(blockActionOpen).toEqual(states.open)
expect(blockActionOpen2).toEqual(states.open)
const closePromise1 = once(bot._client, 'block_action', { array: true })
const closePromise2 = once(bot2._client, 'block_action', { array: true })
bot.chat(`/setblockaction ${x} ${y} ${z} 1 0`) // close the chest
const [blockActionClosed] = await closePromise1
const [blockActionClosed2] = await closePromise2
expect(blockActionClosed).toEqual(states.closed)
expect(blockActionClosed2).toEqual(states.closed)
})
})
describe('commands', () => {
beforeEach(function () {
console.log('🔻 Running commands test: ' + this.currentTest.title)
})
it('has an help command', async () => {
bot.chat('/help')
await once(bot, 'message')
})
it('can use /particle', async () => {
bot.chat('/particle 5 10 100 100 100')
await once(bot._client, 'world_particles')
})
it('can use /playsound', async () => {
bot.chat('/playsound ambient.weather.rain')
// TODO: why are there 2 mineflayer events for this as opposed to one with extra fields?
await once(bot, serv.supportFeature('removedNamedSoundEffectPacket')
? 'hardcodedSoundEffectHeard' // 1.19.3+
: 'soundEffectHeard'
)
})
function waitDragon () {
return new Promise((resolve) => {
const listener = (entity) => {
if (entity.name === entityName) {
bot.removeListener('entitySpawn', listener)
resolve()
}
}
bot.on('entitySpawn', listener)
})
}
it('can use /summon', async () => {
bot.chat('/summon ' + entityName)
await waitDragon()
})
it('can use /kill', async () => {
bot.chat('/summon ' + entityName)
await waitDragon()
bot.chat('/kill @e[type=' + entityName + ']')
const [entity] = await once(bot, 'entityDead')
expect(entity.name).toEqual(entityName)
})
describe('can use /tp', () => {
it('can tp myself', async () => {
bot.chat('/tp 2 3 4')
await once(bot, 'forcedMove')
assertPosEqual(bot.entity.position, new Vec3(2, 3, 4))
})
it('can tp somebody else', async () => {
bot.chat('/tp bot2 2 3 4')
await once(bot2, 'forcedMove')
assertPosEqual(bot2.entity.position, new Vec3(2, 3, 4))
})
it('can tp to somebody else', async () => {
await onGround(bot)
bot.chat('/tp bot2 bot')
await once(bot2, 'forcedMove')
assertPosEqual(bot2.entity.position, bot.entity.position)
})
it('can tp with relative positions', async () => {
const initialPosition = bot.entity.position.clone()
bot.chat('/tp ~1 ~-2 ~3')
await once(bot, 'forcedMove')
assertPosEqual(bot.entity.position, initialPosition.offset(1, -2, 3), 2)
})
it('can tp somebody else with relative positions', async () => {
const initialPosition = bot2.entity.position.clone()
bot.chat('/tp bot2 ~1 ~-2 ~3')
await once(bot2, 'forcedMove')
assertPosEqual(bot2.entity.position, initialPosition.offset(1, -2, 3), 2)
})
})
it('can use /deop', async () => {
bot.chat('/deop bot')
await waitMessage(bot, '§7§o[Server: Deopped bot]')
bot.chat('/op bot')
await waitMessage(bot, 'You do not have permission to use this command')
serv.getPlayer('bot').op = true
})
it('can use /setblock', async () => {
const chestId = registry.blocksByName.chest.id
const p = once(bot, 'blockUpdate:' + new Vec3(1, 2, 3), { array: true })
bot.chat(`/setblock 1 2 3 ${chestId} 0`)
const [, newBlock] = await p
expect(newBlock.type).toEqual(chestId)
})
it('can use /xp', async () => {
bot.chat('/xp 100')
await once(bot, 'experience')
expect(bot.experience.points).toEqual(100)
})
it('can use /give', async () => {
bot.chat('/give bot2 1 1')
await once(bot2.inventory, 'updateSlot')
expect(bot2.inventory.slots[36].type).toEqual(1)
})
it.skip('can use tabComplete', async () => {
const data = await bot.tabComplete('/give ')
expect(data).toEqual(['bot', 'bot2', '@p', '@a', '@e', '@r'])
})
function waitMessagePromise (message) {
return new Promise((resolve) => {
const listener = (msg) => {
if (msg.extra[0].text === message) {
bot.removeListener('message', listener)
resolve()
}
}
bot.on('message', listener)
})
}
it('can use /banlist, /ban, /pardon', async () => {
bot.chat('/banlist')
await waitMessagePromise('There are 0 total banned players')
bot.chat('/ban bot2')
await waitMessagePromise('bot2 was banned')
bot.chat('/banlist')
await waitMessagePromise('There are 1 total banned players:')
bot.chat('/pardon bot2')
await waitMessagePromise('bot2 is unbanned')
bot.chat('/banlist')
await waitMessagePromise('There are 0 total banned players')
})
}).timeout(120 * 1000)
}).timeout(100 * 1000)
})