2020-12-21 16:08:38 +00:00
|
|
|
|
2020-12-22 06:31:06 +00:00
|
|
|
let pathfinder
|
|
|
|
//TODO replace with simple pathfinder motions
|
2020-12-21 16:08:38 +00:00
|
|
|
|
|
|
|
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)
|
2020-12-24 05:35:01 +00:00
|
|
|
bot.waitForChunksToLoad(() => {
|
|
|
|
cfg.plugins.moveNear(bed.position)
|
|
|
|
bot.sleep(bed, (err) => {
|
2020-12-21 16:08:38 +00:00
|
|
|
if (err) {
|
2020-12-24 05:35:01 +00:00
|
|
|
!quiet && bot.chat(`can't sleep: ${err.message}`)
|
2020-12-21 16:08:38 +00:00
|
|
|
} else {
|
2020-12-24 05:35:01 +00:00
|
|
|
!quiet && bot.chat("zzz")
|
|
|
|
console.log("sleeping? ", bot.isSleeping)
|
|
|
|
// hack until this is fixed
|
|
|
|
// bot.isSleeping = bot.isSleeping ? bot.isSleeping : true
|
|
|
|
bot.isSleeping = true
|
2020-12-21 16:08:38 +00:00
|
|
|
}
|
|
|
|
})
|
2020-12-24 05:35:01 +00:00
|
|
|
})
|
2020-12-21 16:08:38 +00:00
|
|
|
} else if (inv && inv.equipItem("red_bed", "hand", true)) {
|
|
|
|
// doesn't work fortunately
|
|
|
|
// FIXME: DONT IMPLEMENT until it is detected as NOT NETHER
|
2020-12-24 05:35:01 +00:00
|
|
|
// bot.placeBlock()
|
2020-12-21 16:08:38 +00:00
|
|
|
} else {
|
2020-12-24 05:35:01 +00:00
|
|
|
// 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`)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// )
|
2020-12-21 16:08:38 +00:00
|
|
|
// bot.chat('/afk')
|
|
|
|
}
|
2020-12-22 06:31:06 +00:00
|
|
|
bot.pathfinder.movements
|
2020-12-21 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-12-21 16:08:38 +00:00
|
|
|
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-21 16:08:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 06:31:06 +00:00
|
|
|
pathfinder = bot.pathfinder || require('mineflayer-pathfinder').pathfinder
|
|
|
|
// bot.loadPlugin(pathfinder)
|
2020-12-21 16:08:38 +00:00
|
|
|
inv = cfg.plugins["inventory"]
|
|
|
|
bot.on("time", autoSleep)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const unload = () => {
|
|
|
|
bot.off("time", autoSleep)
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { load, unload, sleep, wake }
|