From 0e51d8c8f6fdc989f4e6333fcfad609276a0afa8 Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Wed, 15 Nov 2023 17:01:06 -0600 Subject: [PATCH] getnearestblock replaced getcraftingtable --- utils/context.js | 6 +++--- utils/mcdata.js | 3 +++ utils/world.js | 30 +++++++++++------------------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/utils/context.js b/utils/context.js index 1dbdb2d..3518b10 100644 --- a/utils/context.js +++ b/utils/context.js @@ -1,6 +1,6 @@ import { readFileSync } from 'fs'; -import { getCraftingTable, getNearbyMobTypes, getNearbyPlayerNames, getNearbyBlockTypes, getInventoryCounts } from './world.js'; +import { getNearestBlock, getNearbyMobTypes, getNearbyPlayerNames, getNearbyBlockTypes, getInventoryCounts } from './world.js'; import { getAllItems } from './mcdata.js'; @@ -23,7 +23,7 @@ export function getInventory(bot) { let inventory = getInventoryCounts(bot); let res = 'INVENTORY'; for (const item in inventory) { - if (inventory[item] > 0) + if (inventory[item] && inventory[item] > 0) res += `\n- ${item}: ${inventory[item]}`; } if (res == 'INVENTORY') { @@ -62,7 +62,7 @@ export function getNearbyEntities(bot) { export function getCraftable(bot) { - const table = getCraftingTable(bot); + const table = getNearestBlock(bot, 'crafting_table'); let res = 'CRAFTABLE_ITEMS'; for (const item of getAllItems()) { let recipes = bot.recipesFor(item.id, null, 1, table); diff --git a/utils/mcdata.js b/utils/mcdata.js index 2b22787..0e256b3 100644 --- a/utils/mcdata.js +++ b/utils/mcdata.js @@ -23,6 +23,9 @@ export function getItemId(item) { return mcdata.itemsByName[item].id; } +export function getItemName(itemId) { + return mcdata.items[itemId].name; +} export function getAllItems(ignore) { if (!ignore) { diff --git a/utils/world.js b/utils/world.js index 11ca92b..a41407f 100644 --- a/utils/world.js +++ b/utils/world.js @@ -1,16 +1,18 @@ import { getAllBlockIds, getAllBlocks, getAllItems } from './mcdata.js'; -export function getCraftingTable(bot) { - const blocks = getNearbyBlocks(bot, 50); - let table = null; - for (const block of blocks) { - if (block.name == 'crafting_table') { - table = block; - break; - } +export function getNearestBlock(bot, block_type) { + let block_locs = bot.findBlocks({ + matching: (block) => { + return block && block.type === bot.registry.blocksByName[block_type].id + }, + maxDistance: 6, + count: 1 + }); + if (block_locs.length > 0) { + return bot.blockAt(block_locs[0]); } - return table; + return null; } @@ -99,16 +101,6 @@ export function getInventoryCounts(bot) { inventory[item.name] = item.count; } } - for (const item of getAllItems()) { - if (!inventory.hasOwnProperty(item.name)) { - inventory[item.name] = 0; - } - } - for (const item of getAllBlocks()) { - if (!inventory.hasOwnProperty(item.name)) { - inventory[item.name] = 0; - } - } return inventory; }