angram/lib/plugins/sleeper.js
jay 8e719d5ccf feat(sleeper): add and adjust functionality to properly sleep
Can now use beds in inventory but with a hacky block placing workaround.
`findBlock` returns a null position.
So it uses the closest adjacent block and assumes it'll work.

Bot also is more robust at sleeping,
2020-12-27 05:50:16 +05:00

126 lines
4.1 KiB
JavaScript

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
}
if (bot.isSleeping && !cfg.sleep.force) {
!quiet && bot.chat("already in bed!")
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, 2)
bot.once('goal_reached', (goal) => {
console.info(goal)
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 (bed = bot.inventory.items().filter(bot.isABed)[0]) {
const v = require('vec3')
bot.equip(bed, "hand", (err) => { if (err) console.error(err) })
bot.waitForChunksToLoad(() => {
let refBlock =
// FIXME hack to get around findBlock returning null
bot.blockAt(bot.entity.position.offset(1, 0, 1), false)
// bot.findBlock({
// matching: (block) => {
// // if (block && block.type !== 0 && block.position) {
// if (block && block.position) {
// console.info("found", block)
// const blockAbove = bot.blockAt(block.position.offset(0, 1, 0))
// return !blockAbove || blockAbove.type === 0
// }
// // console.info("not found", block)
// return false
// }
// , maxDistance: 10
// })
console.log(refBlock)
bot.placeBlock(refBlock, new v.Vec3(0, 1, 0), console.error)
setTimeout(sleep, 3000, true)
})
} 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')
}
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()
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,
quiet: !!cfg.quiet
}
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 }