mineflayer-totem
Version:
Auto totem for mineflayer
48 lines (41 loc) • 1.39 kB
JavaScript
// index.js
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 = AutoTotem; // Export the class for use in other files