mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-04-23 14:42:07 +02:00
fixed up crafting skill
This commit is contained in:
parent
0e51d8c8f6
commit
7f1b954f12
1 changed files with 26 additions and 8 deletions
|
@ -1,5 +1,5 @@
|
||||||
import { getItemId } from "./mcdata.js";
|
import { getItemId, getItemName } from "./mcdata.js";
|
||||||
import { getCraftingTable, getInventoryCounts, getInventoryStacks, getNearbyMobs, getNearbyBlocks } from "./world.js";
|
import { getNearestBlock, getInventoryCounts, getInventoryStacks, getNearbyMobs, getNearbyBlocks } from "./world.js";
|
||||||
import pf from 'mineflayer-pathfinder';
|
import pf from 'mineflayer-pathfinder';
|
||||||
import Vec3 from 'vec3';
|
import Vec3 from 'vec3';
|
||||||
|
|
||||||
|
@ -8,18 +8,36 @@ export function log(bot, message) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function craftItem(bot, itemName) {
|
export async function craftItem(bot, itemName, num=1) {
|
||||||
/**
|
/**
|
||||||
* Attempt to craft the given item.
|
* Attempt to craft the given item.
|
||||||
* @param {MinecraftBot} bot, reference to the minecraft bot.
|
* @param {MinecraftBot} bot, reference to the minecraft bot.
|
||||||
* @param {string} item_name, the item name to craft.
|
* @param {string} item_name, the item name to craft.
|
||||||
|
* @param {number} num, the number of items to craft. Defaults to 1.
|
||||||
* @returns {Promise<boolean>} true if the item was crafted, false otherwise.
|
* @returns {Promise<boolean>} true if the item was crafted, false otherwise.
|
||||||
* @example
|
* @example
|
||||||
* await skills.craftItem(bot, "wooden_pickaxe");
|
* await skills.craftItem(bot, "wooden_pickaxe", 2);
|
||||||
**/
|
**/
|
||||||
const table = getCraftingTable(bot);
|
|
||||||
let recipes = bot.recipesFor(getItemId(itemName), null, 1, table);
|
let recipes = bot.recipesFor(getItemId(itemName), null, num, null); // get recipes that don't require a crafting table
|
||||||
await bot.craft(recipes[0], 1, null);
|
let craftingTable = undefined;
|
||||||
|
if (!recipes || recipes.length === 0) {
|
||||||
|
craftingTable = getNearestBlock(bot, 'crafting_table');
|
||||||
|
if (craftingTable === null){
|
||||||
|
log(bot, `${itemName} requires crafting table, but there is none nearby.`)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
recipes = bot.recipesFor(getItemId(itemName), null, num, craftingTable);
|
||||||
|
}
|
||||||
|
if (!recipes || recipes.length === 0) {
|
||||||
|
log(bot, `You do not have the resources to craft ${num} ${itemName}(s).`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const recipe = recipes[0];
|
||||||
|
|
||||||
|
console.log('crafting...');
|
||||||
|
await bot.craft(recipe, num, craftingTable);
|
||||||
|
console.log('crafted');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,7 +234,7 @@ export async function giveToPlayer(bot, itemType, username) {
|
||||||
let player = bot.players[username].entity
|
let player = bot.players[username].entity
|
||||||
if (!player)
|
if (!player)
|
||||||
return false;
|
return false;
|
||||||
if (getInventoryCounts(bot)[itemType] == 0)
|
if (!getInventoryCounts(bot)[itemType])
|
||||||
return false;
|
return false;
|
||||||
await goToPlayer(bot, username);
|
await goToPlayer(bot, username);
|
||||||
let pos = player.position;
|
let pos = player.position;
|
||||||
|
|
Loading…
Add table
Reference in a new issue