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/anvil.js
Views: 789
1
const assert = require('assert')
2
const { once } = require('../../lib/promise_utils')
3
4
module.exports = () => {
5
async function runTest (bot, testFunction) {
6
const Item = require('prismarine-item')(bot.registry)
7
const renameCost = () => bot.registry.isNewerOrEqualTo('1.8.9') ? 0 : 1 // weird quirk of anvils
8
const renameName = (name) => bot.registry.isOlderThan('1.13.2') ? name : JSON.stringify({ text: name }) // weird quirk of anvils
9
await bot.test.becomeCreative()
10
await bot.test.setInventorySlot(36, new Item(bot.registry.itemsByName.anvil.id, 1))
11
await bot.test.becomeSurvival()
12
await bot.test.placeBlock(36, bot.entity.position.offset(1, 0, 0))
13
14
if (bot.registry.isNewerOrEqualTo('1.13')) bot.chat(`/xp set ${bot.username} 999 levels`)
15
else {
16
bot.chat(`/xp -2147483648L ${bot.username}`)
17
await once(bot, 'experience')
18
bot.chat(`/xp 999L ${bot.username}`)
19
}
20
21
await once(bot, 'experience')
22
23
const b = bot.findBlock({ matching: bot.registry.blocksByName.anvil.id }) // find anvil before tests so all tests can use it
24
25
function makeBook (enchants) {
26
return makeItem({ type: bot.registry.itemsByName.enchanted_book.id, count: 1, enchants })
27
}
28
29
function makeItem (opts) {
30
const { type, count = 1, enchants, repairCost } = opts
31
const item = new Item(type, count)
32
if (enchants) item.enchants = enchants
33
if (repairCost) item.repairCost = repairCost
34
return item
35
}
36
await testFunction(b, renameCost, renameName, Item, bot, makeBook, makeItem)
37
}
38
39
const tests = []
40
41
function addTest (name, f) {
42
tests[name] = bot => runTest(bot, f)
43
}
44
45
addTest('combine two items', async (b, renameCost, renameName, Item, bot, makeBook, makeItem) => { // combine two items
46
await bot.test.becomeCreative()
47
// get items
48
await bot.test.setInventorySlot(36, new Item(bot.registry.itemsByName.diamond_sword.id, 1))
49
await bot.test.setInventorySlot(37, makeBook([{ name: 'sharpness', lvl: 5 }]))
50
await bot.test.becomeSurvival()
51
52
const anvil = await bot.openAnvil(b)
53
54
const sword = anvil.findInventoryItem(bot.registry.itemsByName.diamond_sword.id)
55
const book = anvil.findInventoryItem(bot.registry.itemsByName.enchanted_book.id)
56
57
await anvil.combine(sword, book)
58
// test result
59
assert.strictEqual(bot.experience.level, 994)
60
assert.strictEqual(anvil.slots[3].repairCost, 1)
61
assert.deepStrictEqual(anvil.slots[3].enchants, [{ name: 'sharpness', lvl: 5 }])
62
anvil.close()
63
await bot.test.wait(1000)
64
})
65
66
addTest('combine with nbt selection two items', async (b, renameCost, renameName, Item, bot, makeBook, makeItem) => { // combining two items in inventory, but there are three items, so this is more a test of using nbt when picking the item in inventory
67
bot.chat(`/clear ${bot.username}`)
68
await bot.test.becomeCreative()
69
70
await bot.test.setInventorySlot(36, new Item(bot.registry.itemsByName.diamond_sword.id, 1))
71
await bot.test.setInventorySlot(37, makeItem({ type: bot.registry.itemsByName.diamond_sword.id, enchants: [{ name: 'sharpness', lvl: 5 }] }))
72
await bot.test.setInventorySlot(38, makeBook([{ name: 'unbreaking', lvl: 3 }]))
73
74
await bot.test.becomeSurvival()
75
76
const anvil = await bot.openAnvil(b)
77
78
const sword = bot.inventory.slots[37]
79
const book = anvil.findInventoryItem(bot.registry.itemsByName.enchanted_book.id)
80
81
await anvil.combine(sword, book)
82
// test result
83
assert.strictEqual(bot.experience.level, 996)
84
assert.strictEqual(anvil.slots[3].repairCost, 1)
85
assert.deepStrictEqual(anvil.slots[3].enchants, [{ name: 'sharpness', lvl: 5 }, { name: 'unbreaking', lvl: 3 }])
86
anvil.close()
87
await bot.test.wait(1000)
88
})
89
90
addTest('using anvil.rename', async (b, renameCost, renameName, Item, bot, makeBook, makeItem) => { // using anvil.rename
91
bot.chat(`/clear ${bot.username}`)
92
await bot.test.becomeCreative()
93
94
await bot.test.setInventorySlot(36, makeItem({ type: bot.registry.itemsByName.diamond_sword.id }))
95
96
await bot.test.becomeSurvival()
97
98
const anvil = await bot.openAnvil(b)
99
100
const sword = anvil.findInventoryItem(bot.registry.itemsByName.diamond_sword.id)
101
await anvil.rename(sword, 'hello')
102
// test result
103
assert.strictEqual(bot.experience.level, 998)
104
assert.strictEqual(anvil.slots[3].repairCost, renameCost())
105
assert.deepStrictEqual(anvil.slots[3].customName, renameName('hello'))
106
anvil.close()
107
await bot.test.wait(1000)
108
})
109
110
addTest('two item + rename', async (b, renameCost, renameName, Item, bot, makeBook, makeItem) => { // test 2 + a rename
111
bot.chat(`/clear ${bot.username}`)
112
await bot.test.becomeCreative()
113
114
await bot.test.setInventorySlot(36, new Item(bot.registry.itemsByName.diamond_sword.id, 1))
115
await bot.test.setInventorySlot(37, makeItem({ type: bot.registry.itemsByName.diamond_sword.id, enchants: [{ name: 'sharpness', lvl: 5 }] }))
116
await bot.test.setInventorySlot(38, makeBook([{ name: 'unbreaking', lvl: 3 }]))
117
118
await bot.test.becomeSurvival()
119
120
const anvil = await bot.openAnvil(b)
121
122
const sword = bot.inventory.slots[37]
123
const book = anvil.findInventoryItem(bot.registry.itemsByName.enchanted_book.id)
124
125
await anvil.combine(sword, book, 'lol')
126
// test result
127
assert.strictEqual(bot.experience.level, 995)
128
assert.strictEqual(anvil.slots[3].repairCost, 1)
129
assert.deepStrictEqual(anvil.slots[3].enchants, [{ name: 'sharpness', lvl: 5 }, { name: 'unbreaking', lvl: 3 }])
130
assert.strictEqual(anvil.slots[3].customName, renameName('lol'))
131
anvil.close()
132
await bot.test.wait(1000)
133
})
134
135
return tests
136
}
137
138