### Basic Usage of mineflayer-auto-eat Source: https://github.com/linkle69/mineflayer-auto-eat/blob/main/README.md This example demonstrates how to load the mineflayer-auto-eat plugin into a Mineflayer bot, enable automatic eating, and listen for eating events. It requires the mineflayer package and is intended to be run with command-line arguments for bot connection details. ```js import { createBot } from 'mineflayer' import { loader as autoEat } from 'mineflayer-auto-eat' const bot = createBot({ host: process.argv[2] || 'localhost', port: process.argv[3] || 25565, username: process.argv[4] || 'bot', auth: process.argv[5] || 'microsoft' }) bot.once('spawn', async () => { bot.loadPlugin(autoEat) bot.autoEat.enableAuto() bot.autoEat.on('eatStart', (opts) => { console.log(`Started eating ${opts.food.name} in ${opts.offhand ? 'offhand' : 'hand'}`) }) bot.autoEat.on('eatFinish', (opts) => { console.log(`Finished eating ${opts.food.name}`) }) bot.autoEat.on('eatFail', (error) => { console.error('Eating failed:', error) }) }) ``` -------------------------------- ### Listen to Eat Start Event in JavaScript Source: https://github.com/linkle69/mineflayer-auto-eat/blob/main/README.md This snippet shows how to listen for the 'eatStart' event emitted by the auto-eat plugin. It logs the name of the food item being eaten to the console. This is useful for tracking eating actions in real-time. ```javascript bot.autoEat.on('eatStart', (opts) => { console.log(`Started eating ${opts.food.name}`) }) ``` -------------------------------- ### Install mineflayer-auto-eat Plugin Source: https://github.com/linkle69/mineflayer-auto-eat/blob/main/README.md This snippet shows how to install the mineflayer-auto-eat package using npm. Note that this package is ESM only and does not support CommonJS. ```sh npm install mineflayer-auto-eat ``` -------------------------------- ### Manually Trigger Eating with mineflayer-auto-eat Source: https://github.com/linkle69/mineflayer-auto-eat/blob/main/README.md This example demonstrates how to manually trigger the eating function using the `eat` method. You can specify which food to eat and other options, or let the plugin automatically select the best food if no specific food is provided. It returns a Promise that resolves on success or rejects on failure. ```js bot.autoEat .eat({ food: 'apple', // optional offhand: true, // optional equipOldItem: false, // optional priority: 'saturation' // optional }) .then(() => { console.log('Successfully ate the food!') }) .catch((err) => { console.error('Failed to eat:', err) }) ``` -------------------------------- ### Complete mineflayer-auto-eat Plugin Integration and Configuration Source: https://context7.com/linkle69/mineflayer-auto-eat/llms.txt This JavaScript code demonstrates the full integration of the mineflayer-auto-eat plugin. It initializes a mineflayer bot, loads the auto-eat plugin, configures various eating options (like priority, minimum hunger/health, banned foods, and timeouts), sets up event listeners for eating start, finish, and failure, and enables auto-eating. It also includes a chat interface for manual control like checking status, triggering a manual eat, toggling auto-eat, and eating specific food items. Error handling for bot connection and command execution is also present. ```javascript import { createBot } from 'mineflayer' import { loader as autoEat } from 'mineflayer-auto-eat' const bot = createBot({ host: process.env.SERVER_HOST || 'localhost', port: parseInt(process.env.SERVER_PORT) || 25565, username: process.env.BOT_USERNAME || 'auto_eat_bot', auth: process.env.AUTH_TYPE || 'microsoft' }) bot.once('spawn', async () => { // Load plugin bot.loadPlugin(autoEat) // Configure auto-eat settings bot.autoEat.setOpts({ priority: 'effectiveQuality', minHunger: 14, minHealth: 12, bannedFood: [ 'rotten_flesh', 'pufferfish', 'chorus_fruit', 'poisonous_potato', 'spider_eye' ], returnToLastItem: true, offhand: false, eatingTimeout: 5000, strictErrors: false }) // Set up event listeners bot.autoEat.on('eatStart', (opts) => { const food = opts.food.name const foodData = bot.autoEat.foodsByName[food] console.log(`[${new Date().toISOString()}] Starting to eat ${food}`) console.log(` - Food Points: ${foodData.foodPoints}`) console.log(` - Saturation: ${foodData.saturation}`) console.log(` - Current Hunger: ${bot.food}`) console.log(` - Current Health: ${bot.health}`) }) bot.autoEat.on('eatFinish', (opts) => { console.log(`[${new Date().toISOString()}] Finished eating ${opts.food.name}`) console.log(` - New Hunger: ${bot.food}`) console.log(` - New Health: ${bot.health}`) }) bot.autoEat.on('eatFail', (error) => { console.error(`[${new Date().toISOString()}] Eating failed: ${error.message}`) }) // Enable auto-eat bot.autoEat.enableAuto() console.log('Auto-eat enabled and configured') // Chat command interface bot.on('chat', async (username, message) => { if (username === bot.username) return try { if (message === '!status') { console.log(`Hunger: ${bot.food}/20, Health: ${bot.health}/20`) console.log(`Auto-eat: ${bot.autoEat.enabled ? 'enabled' : 'disabled'}`) console.log(`Currently eating: ${bot.autoEat.isEating}`) } if (message === '!eat') { await bot.autoEat.eat() console.log('Manual eat triggered') } if (message === '!toggle') { if (bot.autoEat.enabled) { bot.autoEat.disableAuto() console.log('Auto-eat disabled') } else { bot.autoEat.enableAuto() console.log('Auto-eat enabled') } } if (message.startsWith('!eatfood ')) { const foodName = message.split(' ')[1] await bot.autoEat.eat({ food: foodName }) console.log(`Ate ${foodName}`) } } catch (err) { console.error(`Command error: ${err.message}`) } }) }) bot.on('kicked', (reason) => { console.error('Bot was kicked:', reason) }) bot.on('error', (err) => { console.error('Bot error:', err) }) console.log('Bot starting...') ``` -------------------------------- ### Configure mineflayer-auto-eat Options Source: https://github.com/linkle69/mineflayer-auto-eat/blob/main/README.md This code snippet shows how to dynamically update the configuration options for the mineflayer-auto-eat utility using the `setOpts` method. It allows for adjustments to parameters like minimum hunger and food priority. ```js bot.autoEat.setOpts({ minHunger: 10, priority: 'saturation' }) ``` -------------------------------- ### Listen to Eat Finish Event in JavaScript Source: https://github.com/linkle69/mineflayer-auto-eat/blob/main/README.md This snippet demonstrates how to subscribe to the 'eatFinish' event. When the bot successfully finishes eating, this event is triggered, and the name of the food consumed is logged to the console. It helps in confirming successful eating actions. ```javascript bot.autoEat.on('eatFinish', (opts) => { console.log(`Finished eating ${opts.food.name}`) }) ``` -------------------------------- ### Advanced Configuration - Priority Strategies - mineflayer-auto-eat Source: https://context7.com/linkle69/mineflayer-auto-eat/llms.txt Allows configuration of food selection priorities based on different gameplay scenarios. Users can set strategies like 'foodPoints', 'saturation', 'effectiveQuality', and 'saturationRatio', and dynamically adjust them based on the bot's health and hunger levels. ```javascript bot.loadPlugin(autoEat) // foodPoints priority: maximizes hunger restoration bot.autoEat.setOpts({ priority: 'foodPoints', minHunger: 15, bannedFood: ['rotten_flesh', 'pufferfish', 'poisonous_potato'] }) // saturation priority: maximizes saturation for longer lasting food bot.autoEat.setOpts({ priority: 'saturation', minHealth: 12 }) // effectiveQuality priority: balances food points and saturation bot.autoEat.setOpts({ priority: 'effectiveQuality', minHunger: 14 }) // saturationRatio priority: optimizes saturation per food point bot.autoEat.setOpts({ priority: 'saturationRatio', minHunger: 16 }) // Dynamic priority based on situation bot.on('health', () => { if (bot.health < 10) { bot.autoEat.setOpts({ priority: 'saturation' }) } else if (bot.food < 10) { bot.autoEat.setOpts({ priority: 'foodPoints' }) } else { bot.autoEat.setOpts({ priority: 'effectiveQuality' }) } }) bot.autoEat.enableAuto() ``` -------------------------------- ### Manual Eating with Specific Food and Options Source: https://context7.com/linkle69/mineflayer-auto-eat/llms.txt Allows manual triggering of eating specific food items or the best available food according to a priority. Options include specifying the food item, using the offhand, and equipping previously held items. ```javascript bot.loadPlugin(autoEat) bot.on('chat', async (username, message) => { if (message === 'eat apple') { try { // Eat a specific food item await bot.autoEat.eat({ food: 'apple', offhand: false, equipOldItem: true }) console.log('Successfully ate apple') } catch (err) { console.error('Failed to eat apple:', err.message) } } if (message === 'eat best') { try { // Let plugin choose best food based on saturation await bot.autoEat.eat({ priority: 'saturation', offhand: true }) console.log('Ate best food from offhand') } catch (err) { console.error('Failed to eat:', err.message) } } }) ``` -------------------------------- ### Configure and Enable Automatic Eating with Custom Thresholds Source: https://context7.com/linkle69/mineflayer-auto-eat/llms.txt Enables automatic eating after setting custom minimum hunger and health thresholds, and specifying a food prioritization strategy. The plugin will then automatically eat when these conditions are met. ```javascript import { createBot } from 'mineflayer' import { loader as autoEat } from 'mineflayer-auto-eat' const bot = createBot({ host: 'localhost', port: 25565, username: 'bot' }) bot.once('spawn', async () => { bot.loadPlugin(autoEat) // Set custom thresholds before enabling bot.autoEat.setOpts({ minHunger: 14, minHealth: 10, priority: 'saturation' }) // Enable automatic eating bot.autoEat.enableAuto() console.log(`Auto-eat enabled. Will eat when hunger <= ${bot.autoEat.opts.minHunger}`) }) ``` -------------------------------- ### Dynamically Configure Plugin Behavior with setOpts Source: https://context7.com/linkle69/mineflayer-auto-eat/llms.txt Enables dynamic modification of the auto-eat plugin's configuration during runtime. This allows for adjusting eating priorities, thresholds, banned items, and other behaviors based on game conditions. ```javascript bot.loadPlugin(autoEat) bot.autoEat.enableAuto() // Set initial configuration bot.autoEat.setOpts({ priority: 'foodPoints', minHunger: 15, minHealth: 14, bannedFood: ['rotten_flesh', 'pufferfish', 'spider_eye'], returnToLastItem: true, offhand: false, eatingTimeout: 3000, strictErrors: true }) // Modify configuration based on situation bot.on('health', () => { if (bot.health < 10) { // In danger - prioritize saturation and eat earlier bot.autoEat.setOpts({ priority: 'saturation', minHunger: 18, offhand: true }) } else { // Safe - use normal settings bot.autoEat.setOpts({ priority: 'foodPoints', minHunger: 14, offhand: false }) } }) ``` -------------------------------- ### Event Handling for Eating Actions - mineflayer-auto-eat Source: https://context7.com/linkle69/mineflayer-auto-eat/llms.txt Allows monitoring of eating actions through event listeners for logging and custom behavior. It covers `eatStart`, `eatFinish`, and `eatFail` events, providing details about the food being eaten, the outcome, and enabling retry logic for failed attempts. ```javascript bot.loadPlugin(autoEat) // Track when eating starts bot.autoEat.on('eatStart', (opts) => { console.log(`[EAT START] Eating ${opts.food.name} using ${opts.offhand ? 'offhand' : 'main hand'}`) console.log(`[EAT START] Food points: ${bot.autoEat.foodsByName[opts.food.name].foodPoints}`) }) // Track successful eating completion bot.autoEat.on('eatFinish', (opts) => { console.log(`[EAT FINISH] Finished eating ${opts.food.name}`) console.log(`[EAT FINISH] Current hunger: ${bot.food}, health: ${bot.health}`) }) // Handle eating failures with retry logic bot.autoEat.on('eatFail', (error) => { console.error(`[EAT FAIL] ${error.message}`) // Retry eating after a delay if it failed setTimeout(async () => { try { await bot.autoEat.eat() } catch (retryErr) { console.error('Retry failed:', retryErr.message) } }, 1000) }) bot.autoEat.enableAuto() ``` -------------------------------- ### Disable Automatic Eating and Enable Manual Eating Source: https://context7.com/linkle69/mineflayer-auto-eat/llms.txt Demonstrates how to disable the automatic eating functionality while keeping the manual eating capability intact. This allows for controlled eating on demand. ```javascript bot.loadPlugin(autoEat) bot.autoEat.enableAuto() // Disable auto-eat after 60 seconds setTimeout(() => { bot.autoEat.disableAuto() console.log('Auto-eat disabled') }, 60000) // Manual eating still works bot.on('chat', async (username, message) => { if (message === 'eat') { try { await bot.autoEat.eat() console.log('Manually ate food') } catch (err) { console.error('Failed to eat:', err) } } }) ``` -------------------------------- ### Load mineflayer-auto-eat Plugin and Enable Auto Eating Source: https://context7.com/linkle69/mineflayer-auto-eat/llms.txt Initializes the mineflayer-auto-eat plugin and enables its automatic eating functionality upon bot spawn. This sets up the bot to manage its food consumption automatically. ```javascript import { createBot } from 'mineflayer' import { loader as autoEat } from 'mineflayer-auto-eat' const bot = createBot({ host: 'localhost', port: 25565, username: 'bot', auth: 'microsoft' }) bot.once('spawn', async () => { // Load the plugin bot.loadPlugin(autoEat) // Enable automatic eating bot.autoEat.enableAuto() console.log('Auto-eat plugin loaded and enabled') }) ``` -------------------------------- ### Listen to Eat Fail Event in JavaScript Source: https://github.com/linkle69/mineflayer-auto-eat/blob/main/README.md This snippet illustrates how to handle the 'eatFail' event, which is emitted when the auto-eat plugin encounters an error during the eating process. The error is logged to the console, aiding in debugging and error handling. ```javascript bot.autoEat.on('eatFail', (error) => { console.error('Eating failed:', error) }) ``` -------------------------------- ### Enable Automatic Eating with mineflayer-auto-eat Source: https://github.com/linkle69/mineflayer-auto-eat/blob/main/README.md This snippet shows how to enable the automatic eating functionality in the mineflayer-auto-eat plugin. Once enabled, the bot will automatically check and initiate eating based on its hunger and health levels during each `physicsTick`. ```js bot.autoEat.enableAuto() ``` -------------------------------- ### Access Food Properties - mineflayer-auto-eat Source: https://context7.com/linkle69/mineflayer-auto-eat/llms.txt Provides access to Minecraft food data, including all available foods, specific food properties by name, and the ability to find the best food choices from the bot's inventory. This is useful for custom logic and analysis of food items. ```javascript bot.loadPlugin(autoEat) bot.once('spawn', () => { // Get all available foods const allFoods = bot.autoEat.foodsArray console.log(`Total food types available: ${allFoods.length}`) // Access food by name const steakInfo = bot.autoEat.foodsByName['cooked_beef'] console.log(`Steak - Food Points: ${steakInfo.foodPoints}, Saturation: ${steakInfo.saturation}`) // Find best foods by saturation const sortedBySaturation = allFoods .sort((a, b) => b.saturation - a.saturation) .slice(0, 5) console.log('Top 5 foods by saturation:') sortedBySaturation.forEach((food, i) => { console.log(`${i + 1}. ${food.name}: ${food.saturation}`) }) // Get current inventory foods const inventoryItems = bot.inventory.items() const bestChoices = bot.autoEat.findBestChoices(inventoryItems, 'effectiveQuality') if (bestChoices.length > 0) { console.log(`Best food in inventory: ${bestChoices[0].name}`) } else { console.log('No food in inventory!') } }) ``` -------------------------------- ### Cancel Current Eating Action with mineflayer-auto-eat Source: https://github.com/linkle69/mineflayer-auto-eat/blob/main/README.md This snippet shows how to cancel the bot's current eating action if it is in the process of eating. This can be useful for interrupting the eating process under specific conditions. ```js bot.autoEat.cancelEat() ``` -------------------------------- ### Disable Automatic Eating with mineflayer-auto-eat Source: https://github.com/linkle69/mineflayer-auto-eat/blob/main/README.md This code demonstrates how to disable the automatic eating functionality provided by the mineflayer-auto-eat plugin. This will stop the bot from automatically initiating eating actions. ```js bot.autoEat.disableAuto() ``` -------------------------------- ### Cancel Current Eating Action - mineflayer-auto-eat Source: https://context7.com/linkle69/mineflayer-auto-eat/llms.txt Interrupts an ongoing eating action. This is useful for emergency situations, such as when the bot takes damage or receives a command to stop eating. It utilizes the `cancelEat()` method of the `autoEat` plugin. ```javascript bot.loadPlugin(autoEat) bot.autoEat.enableAuto() bot.on('entityHurt', (entity) => { // Cancel eating if we're being attacked if (entity === bot.entity && bot.autoEat.isEating) { bot.autoEat.cancelEat() console.log('Cancelled eating due to damage!') // Run away or fight back // ... } }) bot.on('chat', (username, message) => { if (message === 'stop' && bot.autoEat.isEating) { bot.autoEat.cancelEat() console.log('Eating cancelled by user command') } }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.