PrismarineJS / mineflayer

Create Minecraft bots with a powerful, stable, and high level JavaScript API.
https://prismarinejs.github.io/mineflayer/
MIT License
4.96k stars 904 forks source link

Invalid Item object in equip #2383

Closed aloualou56 closed 2 years ago

aloualou56 commented 2 years ago

Versions

Detailed description of a problem

A bug?

What did you try yet?

Did you try any method from the API? Did you try any example? Any error from those?

Your current code


/*
E:\alouBOT-main\node_modules\mineflayer\lib\plugins\simple_inventory.js:94
      throw new Error('Invalid item object in equip')
            ^

Error: Invalid item object in equip
    at equip (E:\alouBOT-main\node_modules\mineflayer\lib\plugins\simple_inventory.js:94:13)
    at EventEmitter.equip (E:\alouBOT-main\node_modules\mineflayer\lib\promise_utils.js:4:12)
    at EventEmitter.monitorMovement (E:\alouBOT-main\node_modules\mineflayer-pathfinder\index.js:395:13)
    at EventEmitter.emit (node:events:402:35)
    at Timeout.doPhysics [as _onTimeout] (E:\alouBOT-main\node_modules\mineflayer\lib\plugins\physics.js:63:13)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7)
*/

Expected behavior

break the block

Additional context

the invalid item was the hand. The bot didn't had anything in its inventory

amoraschi commented 2 years ago

That's probably caused by bot.equip, what's your code, maybe you're passing a null object to bot.equip

aloualou56 commented 2 years ago

my code is:

Click to expand! ```js my code is: const mineflayer = require("mineflayer") const fs = require('fs') let rawdata = fs.readFileSync('config.json'); let data = JSON.parse(rawdata); const worker = require('worker_threads'); const mc = require('minecraft-protocol') const async = require('async') const vec3 = require('vec3') const toolPlugin = require("mineflayer-tool").plugin const pvp = require("mineflayer-pvp").plugin const autoeat = require("mineflayer-auto-eat") const scanffold = require("mineflayer-scaffold") const Inventory = require("./lib/inventory"); const owner = process.argv[3] || data['OWNER'] const pass = process.argv[4] const armorManager = require("mineflayer-armor-manager") const host = data['IP'] const port = data['PORT'] const quiet = data['QuietMODE'] console.log('################################################################') console.log('# Welcome to alouBOT #') console.log('################################################################') console.log(' ') console.log(' ') console.log('###########!Checking if the config options are valid!###########') function checkbeforestarting(host, port, quiet, owner) { switch (quiet) { case "Y": case "Yes": case "yes": case "N": case "No": case "Nope": case "true": case "no": case "false": console.log("Valid QuietMODE option.") break; default: console.log('please enter a valid QuietMODE value (Y or Yes or yes or N or No ro no or Nope).' + ' The program will close.') process.exit(0) } if (host == '') { console.log('Please enter a server IP.' + ' The program will close.') process.exit(0) } else { console.log("Valid host IP.") } if (port == '') { console.log('Please enter a valid port.' + " If the server doesn't have a IP please enter the default IP '25565'. " + ' The program will close.') process.exit(0) } else { console.log("Valid port number.") } if (owner == '') { console.log("Please enter the owner's name. " + "the program will close.") process.exit(0) } else { console.log("Valid owner's name") } } switch (quiet) { case "Y": case "Yes": case "yes": case 'true': case "N": case "No": case "Nope": case "no": case "false": console.log("Valid QuietMODE option.") break; default: console.log('please enter a valid QuietMODE value (Y or Yes or yes or N or No ro no or Nope).' + ' The program will close.') process.exit(0) } if (host == '') { console.log('Please enter a server IP.' + ' The program will close.') process.exit(0) } else { console.log("Valid host IP.") } if (port == '') { console.log('Please enter a valid port.' + " If the server doesn't have a IP please enter the default IP '25565'. " + ' The program will close.') process.exit(0) } else { console.log("Valid port number.") } if (owner == '') { console.log("Please enter the owner's name. " + "the program will close.") process.exit(0) } else { console.log("Valid owner's name") } console.log(' ') console.log(' ') console.log('#########!Starting alouBOT with ' + owner + ' as owner!###########') console.log('Attempting connection to ' + host + ':' + port ) const bot = mineflayer.createBot({ username: "alouBOT", host: host, port: port }) const inventory = new Inventory({ bot }); const { pathfinder, Movements, goals } = require("mineflayer-pathfinder") const { GoalNear, GoalBlock, GoalXZ, GoalY, GoalInvert, GoalFollow } = require("mineflayer-pathfinder").goals setInterval(() =>{ if (isAttackingMobs === "false") return const mobFilter = e => e.type === "mob" const mob = bot.nearestEntity(mobFilter) if (!mob) return if (mob && mob.kind.toString().toLowerCase().includes('hostile')) { bot.pvp.attack(mob) } },1000); bot.once('spawn', () => { bot.loadPlugin(pathfinder) bot.loadPlugin(pvp) bot.loadPlugin(scanffold) bot.loadPlugin(autoeat) bot.loadPlugin(toolPlugin) bot.loadPlugin(armorManager) const mcData = require('minecraft-data')(bot.version) const movements = new Movements(bot, mcData) movements.canDig = false bot.pathfinder.setMovements(movements) console.log("connected") bot.settings.viewDistance = 'normal' }) var isAttackingMobs = "false" bot.once('spawn', () => { bot.chat('/login 1234567890') }) bot.on("kicked", (reason, loggedIn) => { console.log("KICKED"+ reason) }) bot.on("banned", (reason, loggedIn) => { console.log("BANNED" + reason) }) bot.on('playerCollect', (collector, itemDrop) => { if (collector !== bot.entity) return setTimeout(() => { const sword = bot.inventory.items().find(item => item.name.includes('sword')) if (sword) bot.equip(sword, 'hand') }, 150) }) bot.on('playerCollect', (collector, itemDrop) => { if (collector !== bot.entity) return setTimeout(() => { const shield = bot.inventory.items().find(item => item.name.includes('shield')) if (shield) bot.equip(shield, 'off-hand') }, 250) }) let guardPos = null function guardArea (pos) { guardPos = pos.clone() if (!bot.pvp.target) { moveToGuardPos() } } function stopGuarding () { guardPos = null bot.pvp.stop() bot.pathfinder.setGoal(null) } function moveToGuardPos () { const mcData = require('minecraft-data')(bot.version) bot.pathfinder.setMovements(new Movements(bot, mcData)) bot.pathfinder.setGoal(new goals.GoalBlock(guardPos.x, guardPos.y, guardPos.z)) } bot.on('stoppedAttacking', () => { if (guardPos) { moveToGuardPos() } }) setInterval(() =>{ if (!guardPos) return const mobFilter = e => e.type === "mob" const mob = bot.nearestEntity(mobFilter) if (!mob) return if (mob && mob.kind.toString().toLowerCase().includes('hostile')) { bot.pvp.attack(mob) } },1000); bot.on("chat", function(username, message) { if (message === "hi") bot.chat("Hi there") } ,function itemToString (item) { if (item) { return `${item.name} x ${item.count}` } else { return '(nothing)' } }) bot.once('spawn', () => { bot.autoEat.options = { priority: 'foodPoints', startAt: 17, bannedFood: [] } }) function sayItems (items = bot.inventory.items()) { const output = items.map(itemToString).join(', ') if (output) { bot.chat(output) } else { bot.chat('empty') } } function itemToString (item) { if (item) { return `${item.name} x ${item.count}` } else { return '(nothing)' } } bot.once("spawn", () => { const mcData = require("minecraft-data")(bot.version); const defaultMove = new Movements(bot, mcData); bot.on("path_update", r => { const nodesPerTick = ((r.visitedNodes * 50) / r.time).toFixed(2); console.log( `I can get there in ${ r.path.length } moves. Computation took ${r.time.toFixed(2)} ms (${ r.visitedNodes } nodes, ${nodesPerTick} nodes/tick)` ); }); bot.on("goal_reached", goal => { console.log("Here I am !"); }); if (quiet === 'true' || quiet === 'Y' || quiet === 'Yes' || quiet === 'yes' ) { bot.on("chat", (username, message) => { if (username === bot.username) return; var pmessage = message.split(' ') const target = bot.players[username] ? bot.players[username].entity : null; if (username === bot.username) return if (username != owner) { console.log("Player " + username + "OXI FREE COMMAND FOR YOU") return } // command gia ELA if (message === "_come") { if (!target) { console.log("I don't see you !"); return; } const p = target.position; bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalNear(p.x, p.y, p.z, 1)); // command gia Pane kapou x y z } else if (message.startsWith("goto")) { const cmd = message.split(" "); if (cmd.length === 4) { // goto x y z const x = parseInt(cmd[1], 10); const y = parseInt(cmd[2], 10); const z = parseInt(cmd[3], 10); bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalBlock(x, y, z)); } else if (cmd.length === 3) { // goto x z const x = parseInt(cmd[1], 10); const z = parseInt(cmd[2], 10); bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalXZ(x, z)); } else if (cmd.length === 2) { // goto y const y = parseInt(cmd[1], 10); bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalY(y)); } // command akolouda } else if (message === "_follow") { if (!target) { console.log("I can't see " + username) return } bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalFollow(target, 3), true); isAttackingMobs = "false" // command apofige } else if (message === "_avoid") { bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalInvert(new GoalFollow(target, 5)), true); // command stamata(to Pathfinder an dimame kala) // command gia na parei block(dokimastiko gia allo command pio poly xD) } if (message === "_stop") { console.log('stopping') stopGuarding() } if (message == "_tp") { bot.chat('/teleport ' + bot.username + " " + username) console.log("Teleported to " + username) } if (message === '_guard') { const player = bot.players[username] if (!player) { console.log("I can't see the eria. If I can't see the player I can't find the eria to guard") return } bot.chat("I will guard this area from mobs") guardArea(player.entity.position) } if (message === "_list") { sayItems() } if (message === "_fight me") { const player = bot.players[username] if (!player) { console.log("can't see the player") return } bot.pvp.attack(player.entity) } if (message === '_fight') { const player = bot.players[username] if (!player) { console.log("can't see the" + username) return } bot.pvp.attack(player.entity) } if (message === "_help") { bot.chat("Help") bot.chat(' ') bot.chat('_guard (It guards a selected area from hostile mobs)') bot.chat('_list (It tells which items the bot has)') bot.chat('_follow (It follows you everywhere)') bot.chat('_fight (It fights you)') bot.chat('_attack (It attacks players by username or hostiles)') bot.chat("_goto (it comes to a specific location with the game's cordinates (x y z) or (x z) or (y))") bot.chat('_avoid (It avoids entities around it)') bot.chat('_tp (teleports to you (this options needs op))') bot.chat('_stop (It stops)') console.log("Help") console.log(' ') console.log('_guard (It guards a selected area from hostile mobs)') console.log('_list (It tells which items the bot has)') console.log('_follow (It follows you everywhere)') console.log('_fight (It fights you)') console.log('_attack (It attacks players by username or hostiles)') console.log("_goto (it comes to a specific location with the game's cordinates (x y z) or (x z) or (y))") console.log('_avoid (It avoids entities around it)') console.log('_tp (teleports to you (this options needs op))') console.log('_stop (It stops)') } if (pmessage[0] === "_attack") { if (pmessage[1] === "hostiles") { isAttackingMobs = "true" console.log("Attacking Hostiles!") } else { const player = bot.players[pmessage[1]] if (!player) { return } if (pmessage[1]) { bot.pvp.attack(player.entity) } } } else if (quiet === 'false' || quiet === 'N' || quiet === 'no' || quiet === 'No' || quiet === 'Nope') { bot.on("chat", (username, message) => { if (username === bot.username) return; var pmessage = message.split(' ') const target = bot.players[username] ? bot.players[username].entity : null; if (username === bot.username) return if (username != owner) { console.log("Player " + username + "OXI FREE COMMAND FOR YOU") return } // command gia ELA if (message === "_come") { if (!target) { bot.chat("I don't see you !"); return; } const p = target.position; bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalNear(p.x, p.y, p.z, 1)); // command gia Pane kapou x y z } else if (message.startsWith("_goto")) { const cmd = message.split(" "); if (cmd.length === 4) { // goto x y z const x = parseInt(cmd[1], 10); const y = parseInt(cmd[2], 10); const z = parseInt(cmd[3], 10); bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalBlock(x, y, z)); } else if (cmd.length === 3) { // goto x z const x = parseInt(cmd[1], 10); const z = parseInt(cmd[2], 10); bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalXZ(x, z)); } else if (cmd.length === 2) { // goto y const y = parseInt(cmd[1], 10); bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalY(y)); } // command akolouda } else if (message === "follow") { bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalFollow(target, 3), true); // command apofige } else if (message === "avoid") { bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalInvert(new GoalFollow(target, 5)), true); // command stamata(to Pathfinder an dimame kala) // command gia na parei block(dokimastiko gia allo command pio poly xD) } if (message === "_stop") { console.log('stopping') stopGuarding() } if (message == "_tp") { bot.chat('/teleport ' + bot.username + " " + username) bot.whisper(username, "I AM HERE " + username) console.log("Teleported to " + username) } if (message == "_follow") { if (!target) { bot.chat("i cant see you") return } bot.whisper(username, "FOLLOWING YOU") console.log("FOLLOWING " + username) bot.pathfinder.setGoal(new GoalFollow(target, 3), true) isAttackingMobs = "false" } if (message === '_guard') { const player = bot.players[username] if (!player) { bot.chat("COME HERE TO PLAY") return } bot.chat("I will guard this area from mobs") guardArea(player.entity.position) } if (message === "_list") { sayItems() } if (message === "_fight me") { const player = bot.players[username] if (!player) { bot.chat("I can't see you") return } bot.chat("You are dead my friend") bot.pvp.attack(player.entity) } if (message === '_fight') { const player = bot.players[username] if (!player) { bot.chat("come here looser") return } bot.chat("LETS DANCE") bot.pvp.attack(player.entity) } if (message === "_help") { bot.chat("Help") bot.chat(' ') bot.chat('_guard (It guards a selected area from hostile mobs)') bot.chat('_list (It tells which items the bot has)') bot.chat('_follow (It follows you everywhere)') bot.chat('_fight (It fights you)') bot.chat('_attack (It attacks players by username or hostiles)') bot.chat("_goto (it comes to a specific location with the game's cordinates (x y z) or (x z) or (y))") bot.chat('_avoid (It avoids entities around it)') bot.chat('_tp (teleports to you (this options needs op))') bot.chat('_stop (It stops)') console.log("Help") console.log(' ') console.log('_guard (It guards a selected area from hostile mobs)') console.log('_list (It tells which items the bot has)') console.log('_follow (It follows you everywhere)') console.log('_fight (It fights you)') console.log('_attack (It attacks players by username or hostiles)') console.log("_goto (it comes to a specific location with the game's cordinates (x y z) or (x z) or (y))") console.log('_avoid (It avoids entities around it)') console.log('_tp (teleports to you (this options needs op))') console.log('_stop (It stops)') } if (pmessage[0] === "_attack") { if (pmessage[1] === "hostiles") { isAttackingMobs = "true" console.log("Attacking Hostiles!") bot.chat("Attacking hostiles!") } else { const player = bot.players[pmessage[1]] if (!player) { bot.chat("Come here you cower "+ player) return } if (pmessage[1]) { bot.pvp.attack(player.entity) } } } } ,); } })}}) And it the inventory is: (this isn;t my code) /* A library for working with inventory */ let _this; class Inventory { constructor(config) { this.bot = config.bot; _this = this; } // This is the handler that is called when the bot detects an 'inv' command. handleInventory(cmd) { try { const invCmd = cmd[1]; if (invCmd === "list") { this.sayItems(); } else if (invCmd === "toss") { this.tossItem(cmd); } else if (invCmd === "equip") { this.equipItem(cmd); } } catch (err) { console.error("Error in handleInventory: ", err); _this.bot.chat(`Error trying to handle 'inv' command.`); } } // async equipItem (name, destination) { async equipItem(cmd) { try { const name = cmd[2]; const item = _this.itemByName(name); const destination = "hand"; if (item) { try { await _this.bot.equip(item, destination); _this.bot.chat(`equipped ${name}`); } catch (err) { _this.bot.chat(`cannot equip ${name}: ${err.message}`); } } else { _this.bot.chat(`I have no ${name}`); } } catch (err) { console.log("Error in equipItem: ", err); _this.bot.chat(`Error trying to equipItem()`); } } tossItem(cmd) { try { const amount = 1; const name = cmd[2]; const item = _this.itemByName(name); if (!item) { _this.bot.chat(`I have no ${name}`); } else if (amount) { _this.bot.toss(item.type, null, amount, checkIfTossed); } else { _this.bot.tossStack(item, checkIfTossed); } function checkIfTossed(err) { if (err) { _this.bot.chat(`unable to toss: ${err.message}`); } else if (amount) { _this.bot.chat(`tossed ${amount} x ${name}`); } else { _this.bot.chat(`tossed ${name}`); } } } catch (err) { console.error("Error in tossItem: ", err); _this.bot.chat(`Error trying to tossItem()`); } } itemByName(name) { return _this.bot.inventory.items().filter(item => item.name === name)[0]; } sayItems(items = _this.bot.inventory.items()) { const output = items.map(_this.itemToString).join(", "); if (output) { _this.bot.chat(output); } else { _this.bot.chat("empty"); } } itemToString(item) { if (item) { return `${item.name} x ${item.count}`; } else { return "(nothing)"; } } } module.exports = Inventory; ```
amoraschi commented 2 years ago

That really is understandable, thanks. Maybe it's this

await _this.bot.equip(item, destination);
aloualou56 commented 2 years ago

Thanks for the reply, I will try your suggestion!