let pathfinder
//TODO replace with simple pathfinder motions
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 )
bot . waitForChunksToLoad ( ( ) => {
cfg . plugins . moveNear ( bed . position )
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
// bot.isSleeping = bot.isSleeping ? bot.isSleeping : true
bot . isSleeping = true
}
} )
} )
} else if ( inv && inv . equipItem ( "red_bed" , "hand" , true ) ) {
// doesn't work fortunately
// FIXME: DONT IMPLEMENT until it is detected as NOT NETHER
// bot.placeBlock()
} else {
// 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`)
// }
// }
// )
// 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 )
}
}
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 }