CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/test/externalTests/creative.js
Views: 789
1
const assert = require('assert')
2
const wait = require('util').promisify(setTimeout)
3
const SLOT = 36
4
5
module.exports = () => async (bot) => {
6
const Item = require('prismarine-item')(bot.registry)
7
8
const item1 = new Item(1, 1, 0)
9
const item2 = new Item(2, 1, 0)
10
11
const promise = bot.creative.setInventorySlot(SLOT, item2)
12
13
try {
14
bot.creative.setInventorySlot(SLOT, item1)
15
} catch (err) {
16
assert.ok(err instanceof Error, 'The error has not been passed')
17
assert.ok(bot.inventory.slots[SLOT] == null)
18
}
19
20
// setting a slot once works
21
await promise
22
assert.ok(bot.inventory.slots[SLOT] != null)
23
assert.ok(bot.inventory.slots[SLOT].type === item2.type)
24
// set the same item in the same slot again to ensure we don't hang
25
const returnValue = await Promise.race([
26
bot.creative.setInventorySlot(SLOT, item2),
27
(async () => {
28
await wait(5000) // after 5 seconds just fail the test
29
return 'Setting the same item in the same slot took too long'
30
})()
31
])
32
if (typeof returnValue === 'string') {
33
throw new Error(returnValue)
34
}
35
// setting that same slot again works
36
await bot.creative.setInventorySlot(SLOT, new Item(3, 1, 0))
37
assert.strictEqual(bot.inventory.slots[SLOT].type, 3)
38
// and again works
39
await bot.creative.setInventorySlot(SLOT, new Item(4, 1, 0))
40
assert.strictEqual(bot.inventory.slots[SLOT].type, 4)
41
// setting a slot to null
42
await bot.creative.setInventorySlot(SLOT, null)
43
assert.strictEqual(bot.inventory.slots[SLOT], null)
44
// clear slot
45
await bot.creative.setInventorySlot(SLOT, new Item(4, 1, 0))
46
assert.strictEqual(bot.inventory.slots[SLOT].type, 4)
47
await bot.creative.clearSlot(SLOT)
48
assert.strictEqual(bot.inventory.slots[SLOT], null)
49
// clear inventory
50
for (let i = 0; i < 9; i++) {
51
await bot.creative.setInventorySlot(36 + i, new Item(1, 1, 0))
52
}
53
assert.strictEqual(bot.inventory.slots.filter(item => item).length, 9)
54
await bot.creative.clearInventory()
55
assert.strictEqual(bot.inventory.slots.filter(item => item).length, 0)
56
}
57
58