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
|
|
|
|
|
2020-12-26 20:58:17 +00:00
|
|
|
function sleep(quiet = cfg.sleep.quiet) {
|
2020-12-21 16:08:38 +00:00
|
|
|
if(bot.game.dimension !== "minecraft:overworld" || cfg.sleep.force){
|
|
|
|
!quiet && bot.chat("can't sleep, not in overworld now")
|
|
|
|
return
|
|
|
|
}
|
2020-12-27 00:50:16 +00:00
|
|
|
if (bot.isSleeping && !cfg.sleep.force) {
|
|
|
|
!quiet && bot.chat("already in bed!")
|
|
|
|
return
|
|
|
|
}
|
2020-12-21 16:08:38 +00:00
|
|
|
let bed = bot.findBlock({
|
|
|
|
matching: block => bot.isABed(block)
|
|
|
|
})
|
2020-12-26 20:58:17 +00:00
|
|
|
let bed_occupied = bed && bot.parseBedMetadata(bed).occupied
|
2020-12-27 00:50:16 +00:00
|
|
|
if (bed && bed_occupied) {
|
2020-12-21 16:08:38 +00:00
|
|
|
bot.lookAt(bed.position)
|
|
|
|
bed = bot.findBlock({
|
|
|
|
matching: block => bot.isABed(block) && !bot.parseBedMetadata(block).occupied
|
|
|
|
}) || bed
|
2020-12-26 20:58:17 +00:00
|
|
|
bed_occupied = bot.parseBedMetadata(bed).occupied
|
2020-12-21 16:08:38 +00:00
|
|
|
}
|
2020-12-26 20:58:17 +00:00
|
|
|
if (bed && !bed_occupied) {
|
2020-12-21 16:08:38 +00:00
|
|
|
bot.lookAt(bed.position)
|
2020-12-24 05:35:01 +00:00
|
|
|
bot.waitForChunksToLoad(() => {
|
2020-12-27 00:50:16 +00:00
|
|
|
cfg.plugins.mover && cfg.plugins.mover.moveNear(bed.position, 2)
|
|
|
|
bot.once('goal_reached', (goal) => {
|
|
|
|
console.info(goal)
|
2021-01-16 11:39:42 +00:00
|
|
|
try {
|
|
|
|
bot.sleep(bed, (err) => {
|
|
|
|
if (err) {
|
|
|
|
!quiet && bot.chat(`can't sleep: ${err.message}`)
|
|
|
|
} else {
|
|
|
|
!quiet && bot.chat("zzz")
|
|
|
|
// apparently, `bot.isSleeping = true` takes a while
|
|
|
|
// maybe it's async
|
|
|
|
console.log("sleeping? ", bot.isSleeping)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
}
|
2020-12-21 16:08:38 +00:00
|
|
|
})
|
2020-12-24 05:35:01 +00:00
|
|
|
})
|
2020-12-27 00:50:16 +00:00
|
|
|
} 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)
|
|
|
|
})
|
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) {
|
2020-12-26 20:58:17 +00:00
|
|
|
!quiet && bot.chat(`need a${bed_occupied ? "n unoccupied" : ""} bed: may not see if just placed`)
|
2020-12-24 05:35:01 +00:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// )
|
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-26 20:58:17 +00:00
|
|
|
console.log("sleeping?", bot.isSleeping, bot.time.isDay, bot.time.timeOfDay)
|
2020-12-21 16:08:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }
|