Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/test/externalTests/crafting.js
3042 views
1
const { once } = require('../../lib/promise_utils')
2
const { Vec3 } = require('vec3')
3
4
module.exports = () => async (bot) => {
5
const { blocksByName, itemsByName } = bot.registry
6
const Item = require('prismarine-item')(bot.registry)
7
8
let populateBlockInventory
9
let craftItem
10
if (bot.supportFeature('oneBlockForSeveralVariations')) {
11
populateBlockInventory = blocksByName.log
12
craftItem = 'planks'
13
} else if (bot.supportFeature('blockSchemeIsFlat')) {
14
populateBlockInventory = itemsByName.birch_log
15
craftItem = 'birch_planks'
16
}
17
18
function findCraftingTable () {
19
const cursor = new Vec3(0, 0, 0)
20
for (cursor.x = bot.entity.position.x - 4; cursor.x < bot.entity.position.x + 4; cursor.x++) {
21
for (cursor.y = bot.entity.position.y - 4; cursor.y < bot.entity.position.y + 4; cursor.y++) {
22
for (cursor.z = bot.entity.position.z - 4; cursor.z < bot.entity.position.z + 4; cursor.z++) {
23
const block = bot.blockAt(cursor)
24
if (block.type === blocksByName.crafting_table.id) return block
25
}
26
}
27
}
28
}
29
30
async function craft (amount, name) {
31
const item = itemsByName[name]
32
const craftingTable = findCraftingTable()
33
const wbText = craftingTable ? 'with a crafting table, ' : 'without a crafting table, '
34
if (item == null) {
35
bot.test.sayEverywhere(`${wbText}unknown item: ${name}`)
36
throw new Error(`${wbText}unknown item: ${name}`)
37
} else {
38
const recipes = bot.recipesFor(item.id, null, 1, craftingTable) // doesn't check if it's possible to do it amount times
39
if (recipes.length) {
40
bot.test.sayEverywhere(`${wbText}I can make ${item.name}`)
41
await bot.craft(recipes[0], amount, craftingTable)
42
bot.test.sayEverywhere(`did the recipe for ${item.name} ${amount} times`)
43
} else {
44
bot.test.sayEverywhere(`${wbText}I can't make ${item.name}`)
45
throw new Error(`${wbText}I can't make ${item.name}`)
46
}
47
}
48
}
49
50
await bot.test.setInventorySlot(36, new Item(populateBlockInventory.id, 1, 0))
51
await bot.test.becomeSurvival()
52
await craft(1, craftItem)
53
await bot.test.setBlock({ x: 1, y: 0, z: 0, relative: true, blockName: 'crafting_table' })
54
bot.chat('/give @p stick 7')
55
await once(bot.inventory, 'updateSlot')
56
const craftingTable = bot.findBlock({ matching: blocksByName.crafting_table.id })
57
await bot.craft(bot.recipesFor(itemsByName.ladder.id, null, null, true)[0], 1, craftingTable)
58
}
59
60