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

pathfinder help!!!! #3442

Closed eeeemptyyyy closed 1 month ago

eeeemptyyyy commented 1 month ago

Hello!! I wanted to write a bot that would run after a specified list of items and pick them up. But I didn’t succeed, the bot runs halfway then stands and turns its head, the movement is very broken

If possible for version 1.16.5 minimum

OmegaM1 commented 1 month ago

Hello!! I wanted to write a bot that would run after a specified list of items and pick them up. But I didn’t succeed, the bot runs halfway then stands and turns its head, the movement is very broken

If possible for version 1.16.5 minimum

Have you tried using a more stable framework for the bot's movement? Consider implementing error handling or try optimizing the code to improve its performance.

eeeemptyyyy commented 1 month ago

In despair, I even tried through ChatGPT, but even that didn’t help((

TerminalCalamitas commented 1 month ago

Is your code for the pathfinder different from the pathfinder example? If you could post the code you're using it would be easier to help find a solution.

eeeemptyyyy commented 1 month ago

''' const mineflayer = require('mineflayer'); const { pathfinder, Movements, goals } = require('mineflayer-pathfinder'); const mcData = require('minecraft-data')('1.16.4'); // Replace with your Minecraft version const readline = require('readline');

// Create the bot const bot = mineflayer.createBot({ host: 'host.mine', username: 'emptyyyy' });

// Load the pathfinder plugin into the bot bot.loadPlugin(pathfinder);

// Set up the interface for reading console input const rl = readline.createInterface({ input: process.stdin, output: process.stdout });

// List of items to search for const itemsToCollect = [ 'stone' ];

const searchRadius = 25; // Radius for searching items

let collectingItem = false; // Flag to check if the bot is currently collecting an item

// Event handler for logging into the game bot.on('login', () => { console.log(Bot ${bot.username} successfully logged into the server!); rl.prompt(); });

// Error handler bot.on('error', (err) => { console.log(An error occurred: ${err.message}); });

// Handler for disconnection bot.on('end', () => { console.log('Bot disconnected from the server.'); rl.close(); });

// Function to search for all items within the radius and pick up the first one found function findAndPickUpItem() { if (collectingItem) return; // Don't search for new items while collecting the current one

const itemsFound = Object.values(bot.entities).filter(entity => { if (entity.name === 'item') { const itemId = entity.metadata[8].itemId; const itemName = mcData.items[itemId]?.name; const distance = bot.entity.position.distanceTo(entity.position); return itemsToCollect.includes(itemName) && distance <= searchRadius; } return false; });

if (itemsFound.length > 0) { const targetItem = itemsFound[0]; // First item found moveToAndPickUpItem(targetItem); } else { console.log('No items found within the search radius.'); setTimeout(findAndPickUpItem, 10000); // Try again in 10 seconds } }

// Function to move to the item and pick it up function moveToAndPickUpItem(item) { collectingItem = true;

const movements = new Movements(bot, mcData); bot.pathfinder.setMovements(movements);

const goal = new goals.GoalBlock(item.position.x, item.position.y, item.position.z); bot.pathfinder.setGoal(goal, true); // Set the goal for movement

bot.once('goal_reached', () => { console.log(Reached item ${mcData.items[item.metadata[8].itemId]?.name}. Starting to collect.);

// Ensure the bot is correctly looking at the item
bot.lookAt(item.position.offset(0, 1, 0), false); // Look slightly above the item to collect it

bot.once('entitySpawn', (entity) => {
  if (entity === item) {
    bot.collectBlock.collect(item, (err) => {
      if (err) {
        console.log(`Error while collecting item: ${err}`);
      } else {
        console.log(`Item ${mcData.items[item.metadata[8].itemId]?.name} collected.`);
      }

      collectingItem = false;
      setTimeout(findAndPickUpItem, 5000); // Wait a bit and search for items again
    });
  }
});

});

bot.once('path_update', (results) => { if (results.status === 'noPath') { console.log('Unable to find a direct path. Will try again.'); collectingItem = false; setTimeout(findAndPickUpItem, 1000); // Restart search in a second } });

bot.once('stuck', () => { console.log('Bot got stuck or couldn’t reach the item.'); collectingItem = false; setTimeout(findAndPickUpItem, 1000); // Restart search in a second }); }

// Handling console input rl.on('line', (input) => { if (input.toLowerCase() === 'stop') { console.log('Disconnecting bot...'); bot.end(); } else if (input.toLowerCase() === 'start') { findAndPickUpItem(); // Start searching for items } else { bot.chat(input); // Send a message to the chat } rl.prompt(); });

// When the console closes, the bot also disconnects rl.on('close', () => { bot.end(); console.log('Console closed, bot disconnected.'); }); '''

eeeemptyyyy commented 1 month ago

Is your code for the pathfinder different from the pathfinder example? If you could post the code you're using it would be easier to help find a solution.

here is the code, please help if you can

OmegaM1 commented 1 month ago

In despair, I even tried through ChatGPT, but even that didn’t help((

Never use chatgpt for developing lol, Chatgpt gives you codes that look real, but the structure of all of them is old and illogical. I suggest you to search on google or check out Stack Overflow to find your coding problems, it will help you to learn and you won't depend on chatgpt.