refactor(sleeper): ♻️ simplify and make sleeping code more robust
This commit is contained in:
		| @@ -7,8 +7,7 @@ let bot = {} | ||||
| let inv | ||||
| // cfg.autosleep = false | ||||
|  | ||||
| function sleep(quiet) { | ||||
|     quiet = quiet !== undefined ? quiet : cfg.sleep.quiet | ||||
| 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 | ||||
| @@ -16,18 +15,18 @@ function sleep(quiet) { | ||||
|     let bed = bot.findBlock({ | ||||
|         matching: block => bot.isABed(block) | ||||
|     }) | ||||
|     let bedstatus = bed && bot.parseBedMetadata(bed).occupied ? "n unoccupied" : "" | ||||
|     if(bed && bedstatus == "n unoccupied"){ | ||||
|     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 | ||||
|         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.waitForChunksToLoad(() => { | ||||
|             cfg.plugins.mover.moveNear(bed.position) | ||||
|             cfg.plugins.mover && cfg.plugins.mover.moveNear(bed.position) | ||||
|             bot.sleep(bed, (err) => { | ||||
|                 if (err) { | ||||
|                     !quiet && bot.chat(`can't sleep: ${err.message}`) | ||||
| @@ -35,8 +34,8 @@ function sleep(quiet) { | ||||
|                     !quiet && bot.chat("zzz") | ||||
|                     console.log("sleeping? ", bot.isSleeping) | ||||
|                     // hack until this is fixed | ||||
|                     // bot.isSleeping = bot.isSleeping ? bot.isSleeping : true | ||||
|                     bot.isSleeping = true | ||||
|                     // TODO confirm this is fixed | ||||
|                     // bot.isSleeping = true | ||||
|                 } | ||||
|             }) | ||||
|         }) | ||||
| @@ -49,7 +48,7 @@ function sleep(quiet) { | ||||
|         // bot.gameplay.solveFor( | ||||
|         //     new ObtainItem("bed"), (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) { | ||||
|         sleep() | ||||
|         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) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user