Path: blob/master/test/externalTests/crafting.js
3042 views
const { once } = require('../../lib/promise_utils')1const { Vec3 } = require('vec3')23module.exports = () => async (bot) => {4const { blocksByName, itemsByName } = bot.registry5const Item = require('prismarine-item')(bot.registry)67let populateBlockInventory8let craftItem9if (bot.supportFeature('oneBlockForSeveralVariations')) {10populateBlockInventory = blocksByName.log11craftItem = 'planks'12} else if (bot.supportFeature('blockSchemeIsFlat')) {13populateBlockInventory = itemsByName.birch_log14craftItem = 'birch_planks'15}1617function findCraftingTable () {18const cursor = new Vec3(0, 0, 0)19for (cursor.x = bot.entity.position.x - 4; cursor.x < bot.entity.position.x + 4; cursor.x++) {20for (cursor.y = bot.entity.position.y - 4; cursor.y < bot.entity.position.y + 4; cursor.y++) {21for (cursor.z = bot.entity.position.z - 4; cursor.z < bot.entity.position.z + 4; cursor.z++) {22const block = bot.blockAt(cursor)23if (block.type === blocksByName.crafting_table.id) return block24}25}26}27}2829async function craft (amount, name) {30const item = itemsByName[name]31const craftingTable = findCraftingTable()32const wbText = craftingTable ? 'with a crafting table, ' : 'without a crafting table, '33if (item == null) {34bot.test.sayEverywhere(`${wbText}unknown item: ${name}`)35throw new Error(`${wbText}unknown item: ${name}`)36} else {37const recipes = bot.recipesFor(item.id, null, 1, craftingTable) // doesn't check if it's possible to do it amount times38if (recipes.length) {39bot.test.sayEverywhere(`${wbText}I can make ${item.name}`)40await bot.craft(recipes[0], amount, craftingTable)41bot.test.sayEverywhere(`did the recipe for ${item.name} ${amount} times`)42} else {43bot.test.sayEverywhere(`${wbText}I can't make ${item.name}`)44throw new Error(`${wbText}I can't make ${item.name}`)45}46}47}4849await bot.test.setInventorySlot(36, new Item(populateBlockInventory.id, 1, 0))50await bot.test.becomeSurvival()51await craft(1, craftItem)52await bot.test.setBlock({ x: 1, y: 0, z: 0, relative: true, blockName: 'crafting_table' })53bot.chat('/give @p stick 7')54await once(bot.inventory, 'updateSlot')55const craftingTable = bot.findBlock({ matching: blocksByName.crafting_table.id })56await bot.craft(bot.recipesFor(itemsByName.ladder.id, null, null, true)[0], 1, craftingTable)57}585960