angram/lib/plugins/sleeper.js

100 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) {
quiet = quiet !== undefined ? 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 bedstatus = bed && bot.parseBedMetadata(bed).occupied ? "n unoccupied" : ""
if(bed && bedstatus == "n unoccupied"){
bot.lookAt(bed.position)
bed = bot.findBlock({
matching: block => bot.isABed(block) && !bot.parseBedMetadata(block).occupied
}) || bed
bedstatus = bot.parseBedMetadata(bed).occupied ? "n unoccupied" : ""
}
if (bed && bedstatus == "") {
bot.lookAt(bed.position)
bot.waitForChunksToLoad(() => {
2020-12-24 06:23:16 +00:00
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
// bot.isSleeping = bot.isSleeping ? bot.isSleeping : true
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${bedstatus} 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)
}
}
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 }