angram/lib/plugins/sleeper.js

99 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-12-22 06:31:06 +00:00
let pathfinder
//TODO replace with simple pathfinder motions
let cfg = {}
let bot = {}
let inv
// cfg.autosleep = false
function sleep(quiet = cfg.sleep.quiet) {
if(bot.game.dimension !== "minecraft:overworld" || cfg.sleep.force){
!quiet && bot.chat("can't sleep, not in overworld now")
return
}
let bed = bot.findBlock({
matching: block => bot.isABed(block)
})
let bed_occupied = bed && bot.parseBedMetadata(bed).occupied
if (bed && !bed_occupied) {
bot.lookAt(bed.position)
bed = bot.findBlock({
matching: block => bot.isABed(block) && !bot.parseBedMetadata(block).occupied
}) || bed
bed_occupied = bot.parseBedMetadata(bed).occupied
}
if (bed && !bed_occupied) {
bot.lookAt(bed.position)
bot.waitForChunksToLoad(() => {
cfg.plugins.mover && cfg.plugins.mover.moveNear(bed.position)
bot.sleep(bed, (err) => {
if (err) {
!quiet && bot.chat(`can't sleep: ${err.message}`)
} else {
!quiet && bot.chat("zzz")
console.log("sleeping? ", bot.isSleeping)
// hack until this is fixed
// TODO confirm this is fixed
// bot.isSleeping = true
}
})
})
} else if (inv && inv.equipItem("red_bed", "hand", true)) {
// doesn't work fortunately
// FIXME: DONT IMPLEMENT until it is detected as NOT NETHER
// bot.placeBlock()
} else {
// TODO: use mover
// bot.gameplay.solveFor(
// new ObtainItem("bed"), (err) => {
// if (err) {
!quiet && bot.chat(`need a${bed_occupied ? "n unoccupied" : ""} bed: may not see if just placed`)
// }
// }
// )
// bot.chat('/afk')
}
2020-12-22 06:31:06 +00:00
bot.pathfinder.movements
}
function wake() {
bot.wake((err) => {
if (err) {
bot.chat(`can't wake up: ${err.message}`)
} else {
bot.chat('woke up')
}
})
}
function autoSleep() {
if (!bot.time.isDay && !cfg.sleep.timeoutFn && cfg.sleep.auto && !bot.isSleeping) {
sleep()
2020-12-22 06:31:06 +00:00
cfg.sleep.timeoutFn = setTimeout(() => { cfg.sleep.timeoutFn = null }, cfg.sleep.timeout)
console.log("sleeping?", bot.isSleeping, bot.time.isDay, bot.time.timeOfDay)
}
}
const load = (config) => {
cfg = config
bot = cfg.bot
cfg.sleep = {
auto: true,
// timeout: 30 * 1000,
timeout: 2 * 60 * 1000,
2020-12-22 06:31:06 +00:00
quiet: !!cfg.quiet
}
2020-12-22 06:31:06 +00:00
pathfinder = bot.pathfinder || require('mineflayer-pathfinder').pathfinder
// bot.loadPlugin(pathfinder)
inv = cfg.plugins["inventory"]
bot.on("time", autoSleep)
}
const unload = () => {
bot.off("time", autoSleep)
}
module.exports = { load, unload, sleep, wake }