You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
2.9 KiB
109 lines
2.9 KiB
let cfg = {} |
|
let bot = {} |
|
let isEating = false |
|
|
|
function eat(callback = e => e && console.error(e)) { |
|
isEating = true |
|
|
|
const foodNames = require('minecraft-data')(bot.version).foodsArray.map((item) => item.name) |
|
|
|
let available_food = bot.inventory |
|
.items() |
|
.filter((item) => foodNames.includes(item.name)) |
|
|
|
if (available_food.length === 0 || !available_food) { |
|
isEating = false |
|
return callback(new Error('No food found.')) |
|
} |
|
|
|
if (cfg.eat.bannedFood.length > 0) { |
|
available_food = available_food.filter( |
|
(item) => !cfg.eat.bannedFood.includes(item.name) |
|
) |
|
} |
|
let priority = cfg.eat.priority |
|
let best_food = available_food.reduce((prev, current) => (prev[priority] > current[priority]) ? prev : current) |
|
|
|
if (!best_food) { |
|
isEating = false |
|
return callback(new Error('No best food has been found.')) |
|
} |
|
|
|
bot.emit('eat_start') |
|
|
|
bot.equip(best_food, 'hand', function (error) { |
|
if (error) { |
|
console.warn(error, best_food) |
|
isEating = false |
|
bot.emit('eat_stop') |
|
} else { |
|
try { |
|
bot.consume().catch(error => { |
|
if (error.message === "Food is full") { |
|
console.warn(error, best_food) |
|
} else { |
|
return callback({ error, best_food }) |
|
} |
|
}).finally(() => { |
|
isEating = false |
|
bot.emit('eat_stop') |
|
}) |
|
} catch { } |
|
if (bot.food !== 20) eat(callback) |
|
} |
|
}) |
|
} |
|
|
|
function checkFood() { |
|
console.info("eater: " |
|
// , " status: ", !isEating |
|
, cfg.eat.auto && "auto" |
|
, bot.food < cfg.eat.startAt && "hungry" |
|
, "hunger:", bot.food |
|
, "at:", cfg.eat.startAt) |
|
if ( |
|
!isEating |
|
&& cfg.eat.auto |
|
&& bot.food < cfg.eat.startAt |
|
) { |
|
if ( |
|
(bot.pathfinder |
|
&& !(bot.pathfinder.isMining() || bot.pathfinder.isBuilding()) |
|
// TODO implement better idle state |
|
) || true // idle most likely |
|
) { |
|
eat() |
|
} |
|
} |
|
} |
|
|
|
function resetEat(value) { |
|
// to prevent the plugin from breaking if the bot gets killed while eating btw |
|
isEating = !!value // false |
|
} |
|
|
|
const load = (config) => { |
|
cfg = config |
|
bot = cfg.bot |
|
cfg.eat = { |
|
priority: 'saturation', //'foodPoints', // |
|
// startAt: 19, //anarchy |
|
// startAt: 18, |
|
startAt: 14, |
|
bannedFood: [ |
|
"enchanted_golden_apple", "golden_apple", "pufferfish", "chorus_fruit" |
|
], |
|
auto: true |
|
} |
|
|
|
bot.on('health', checkFood) |
|
bot.on('spawn', resetEat) |
|
|
|
} |
|
|
|
const unload = () => { |
|
bot.off('health', checkFood) |
|
bot.off('spawn', resetEat) |
|
} |
|
|
|
module.exports = { load, unload, eat, resetEat } |