UNPKG

mineflayer-utility-bot

Version:

A utility package for Mineflayer bots, including automatic eating and totem handling.

100 lines (86 loc) 2.69 kB
const fs = require('fs'); // Food class to handle automatic eating functionality class Food { constructor(bot) { this.bot = bot; this.foodList = [ 'minecraft:cooked_beef', 'minecraft:cooked_chicken', 'minecraft:bread', ]; // Default food list this.bot.once('spawn', () => { console.log('Food module initialized. Loaded food list:', this.foodList); this.startAutoEat(); }); } normalizeFoodName(foodName) { return foodName.startsWith('minecraft:') ? foodName : `minecraft:${foodName}`; } startAutoEat() { setInterval(() => { const hunger = this.bot.food; if (hunger === undefined || hunger >= 20) return; this.tryToEat(); }, 2000); } async tryToEat() { const foodItem = this.findFoodInInventory(); if (foodItem) { try { await this.bot.equip(foodItem, 'hand'); await this.bot.consume(); console.log(`Consumed: ${foodItem.name}`); } catch (error) { console.error('Error consuming food:', error); } } else { console.log('No food found in inventory.'); } } findFoodInInventory() { const items = this.bot.inventory.items(); return items.find((item) => this.foodList.includes(this.normalizeFoodName(item.name)) ); } } class AutoTotem { constructor(bot) { this.bot = bot; this.lowHealthThreshold = 10; // Trigger health threshold } start() { // Listen for health updates this.bot.on('health', () => this.handleAutoTotem()); // Run periodic checks as a fallback setInterval(() => this.handleAutoTotem(), 100); } async handleAutoTotem() { if (this.bot.health < this.lowHealthThreshold) { await this.equipTotem(); } } async equipTotem() { const totemItem = this.findTotemInInventory(); if (totemItem) { try { await this.bot.equip(totemItem, 'off-hand'); // Equip the totem in the offhand slot console.log(`Equipped Totem of Undying: ${totemItem.name}`); } catch (err) { console.error('Error equipping totem:', err); } } else { console.log('No totem found in inventory to equip.'); } } findTotemInInventory() { const items = this.bot.inventory.items(); // Retrieve all items in the inventory for (const item of items) { if (item.name === 'totem_of_undying') { // Match exact name without "minecraft:" prefix return item; // Return the first totem found } } return null; // Return null if no totem is found } } module.exports = { Food, AutoTotem };