refactor(sleeper): ♻️ simplify and make sleeping code more robust

This commit is contained in:
jay 2020-12-27 01:58:17 +05:00
parent ba7c53be0c
commit 112eb04a8d

View File

@ -7,8 +7,7 @@ let bot = {}
let inv let inv
// cfg.autosleep = false // cfg.autosleep = false
function sleep(quiet) { function sleep(quiet = cfg.sleep.quiet) {
quiet = quiet !== undefined ? quiet : cfg.sleep.quiet
if(bot.game.dimension !== "minecraft:overworld" || cfg.sleep.force){ if(bot.game.dimension !== "minecraft:overworld" || cfg.sleep.force){
!quiet && bot.chat("can't sleep, not in overworld now") !quiet && bot.chat("can't sleep, not in overworld now")
return return
@ -16,18 +15,18 @@ function sleep(quiet) {
let bed = bot.findBlock({ let bed = bot.findBlock({
matching: block => bot.isABed(block) matching: block => bot.isABed(block)
}) })
let bedstatus = bed && bot.parseBedMetadata(bed).occupied ? "n unoccupied" : "" let bed_occupied = bed && bot.parseBedMetadata(bed).occupied
if(bed && bedstatus == "n unoccupied"){ if (bed && !bed_occupied) {
bot.lookAt(bed.position) bot.lookAt(bed.position)
bed = bot.findBlock({ bed = bot.findBlock({
matching: block => bot.isABed(block) && !bot.parseBedMetadata(block).occupied matching: block => bot.isABed(block) && !bot.parseBedMetadata(block).occupied
}) || bed }) || bed
bedstatus = bot.parseBedMetadata(bed).occupied ? "n unoccupied" : "" bed_occupied = bot.parseBedMetadata(bed).occupied
} }
if (bed && bedstatus == "") { if (bed && !bed_occupied) {
bot.lookAt(bed.position) bot.lookAt(bed.position)
bot.waitForChunksToLoad(() => { bot.waitForChunksToLoad(() => {
cfg.plugins.mover.moveNear(bed.position) cfg.plugins.mover && cfg.plugins.mover.moveNear(bed.position)
bot.sleep(bed, (err) => { bot.sleep(bed, (err) => {
if (err) { if (err) {
!quiet && bot.chat(`can't sleep: ${err.message}`) !quiet && bot.chat(`can't sleep: ${err.message}`)
@ -35,8 +34,8 @@ function sleep(quiet) {
!quiet && bot.chat("zzz") !quiet && bot.chat("zzz")
console.log("sleeping? ", bot.isSleeping) console.log("sleeping? ", bot.isSleeping)
// hack until this is fixed // hack until this is fixed
// bot.isSleeping = bot.isSleeping ? bot.isSleeping : true // TODO confirm this is fixed
bot.isSleeping = true // bot.isSleeping = true
} }
}) })
}) })
@ -49,7 +48,7 @@ function sleep(quiet) {
// bot.gameplay.solveFor( // bot.gameplay.solveFor(
// new ObtainItem("bed"), (err) => { // new ObtainItem("bed"), (err) => {
// if (err) { // if (err) {
// !quiet && bot.chat(`need a${bedstatus} bed: may not see if just placed`) !quiet && bot.chat(`need a${bed_occupied ? "n unoccupied" : ""} bed: may not see if just placed`)
// } // }
// } // }
// ) // )
@ -72,7 +71,7 @@ function autoSleep() {
if (!bot.time.isDay && !cfg.sleep.timeoutFn && cfg.sleep.auto && !bot.isSleeping) { if (!bot.time.isDay && !cfg.sleep.timeoutFn && cfg.sleep.auto && !bot.isSleeping) {
sleep() sleep()
cfg.sleep.timeoutFn = setTimeout(() => { cfg.sleep.timeoutFn = null }, cfg.sleep.timeout) cfg.sleep.timeoutFn = setTimeout(() => { cfg.sleep.timeoutFn = null }, cfg.sleep.timeout)
console.log("sleeping?", bot.isSleeping, bot.time) console.log("sleeping?", bot.isSleeping, bot.time.isDay, bot.time.timeOfDay)
} }
} }