A high-level, general purpose and modular minecraft bot using hot re-loadable (without restarting the bot!) plugins. Batteries included, launch to run!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

115 lines
3.5 KiB

const pathfinder = require('mineflayer-pathfinder').pathfinder
const { Bot } = require('mineflayer')
const {
gameplay,
MoveTo,
MoveToInteract,
ObtainItem,
// Craft
} = require('prismarine-gameplay')
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)
// const nearbed =
bot.gameplay.solveFor(
new MoveTo((bed.position.range = 2) && bed.position), (err) => {
// new MoveTo(bed.position), (err) => {
// new MoveToInteract(bed.position), (err) => {
if (err) {
!quiet && bot.chat(`can't reach bed: ${err.message}`)
} else {
bot.waitForChunksToLoad(() => {
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 (bed){
} 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 {
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')
}
}
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) // give 2 seconds for multiple events
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: false
}
bot.loadPlugin(pathfinder)
bot.loadPlugin(gameplay)
inv = cfg.plugins["inventory"]
bot.on("time", autoSleep)
}
const unload = () => {
bot.off("time", autoSleep)
}
module.exports = { load, unload, sleep, wake }