Compare commits

..

5 Commits

Author SHA1 Message Date
jay
288b7045b6 feat: add informer plugin to show info
Data and information plugin.
Expose methods in mcData with acceptable ux.
Also central place for all kinds of debug features.
2020-12-24 21:39:50 +05:00
jay
94574a4296 refactor: ♻️ reorder plugin loading
Load in order of simplicity and dependance.

TODO: Use `bot.waitForChunksToLoad`:
Split plugins based on whether it requires blocks to be loaded or no.
2020-12-24 21:32:08 +05:00
jay
68e60921b1 refactor(mover): 🔥 remove unused code + comments, minor fixes
Refactoring and fixing code.
 No major functionality change.
 Probably will have less bugs.
 - carry over how mcData is loaded
 - stop bot on unload
 - fix wrong magma block name
2020-12-24 19:57:45 +05:00
jay
f2281a7cb3 feat(command): temp. move inventory chat handling to command
This is temporary, and only the `toss` function.
Old functionality is still intact.
New code will eventually move back as a subcommand to replace the old.
2020-12-24 19:41:52 +05:00
jay
e9f2080556 refactor(command): ♻️ refactor how mcData is loaded
`mcData` is now directly put on bot and loaded once.
It can be accessed from anywhere `bot` is accessible.
2020-12-24 17:51:44 +05:00
5 changed files with 82 additions and 48 deletions

View File

@ -95,13 +95,14 @@ cfg.quiet = true
bot.once("spawn", () => {
plugins = {
command: require('./plugins/command'),
eater: require('./plugins/eater'),
inventory: require('./plugins/inventory'),
informer: require('./plugins/informer'),
finder: require('./plugins/finder'),
sleeper: require('./plugins/sleeper'),
armor: require('./plugins/armor'),
mover: require('./plugins/mover'),
guard: require('./plugins/guard'),
inventory: require('./plugins/inventory'),
eater: require('./plugins/eater'),
finder: require('./plugins/finder'),
// miner: require('./plugins/miner.js'),
// statemachine: require('./plugins/statemachine'),
}

View File

@ -1,5 +1,5 @@
const v = require('vec3')
let mcData = require('minecraft-data')
let mcData
let cfg = {}
let bot = {}
@ -426,29 +426,51 @@ function command(username, message) {
// case "take":
// // TODO take only what's requested, then throw all the rest
// // TODO take all
// case "toss":
// case "drop":
// if (!message_parts[1]) { return false } // FIXME, works but ugly
// if (!checkItemExists(message_parts[1])) { return false }
// switch (message_parts.length) {
// case 2:
// bot.toss(mcData.blocksByName[message_parts[1]].id)
// break
// case 3:
// bot.tossStack(
// mcData.itemsByName[message_parts[1]].id,
// (err) => {
// if (err) {
// console.log(err)
// bot.chat(err)
// }
// }
// )
// break
// default:
// break
// }
// break;
// TODO move subcommands to cfg.plugins.inventory.itemByName
case "toss":
case "drop":
if (!message_parts[1]) { return false } // FIXME, works but ugly
// TODO use cfg.plugins.inventory.itemByName
const item = cfg.plugins.inventory.itemByName(message_parts[1])
if (!mcData.findItemOrBlockByName(message_parts[1])) {
console.log("doesn't exist:", message_parts[1])
cfg.quiet || bot.chat(`item doesn't exist: ${message_parts[1]}`)
return false
} else if (!item) {
console.log("don't have:", message_parts[1])
cfg.quiet || bot.chat(`don't have item: ${message_parts[1]}`)
}
switch (message_parts.length) {
case 2:
bot.tossStack(
item,
(err) => {
if (err) {
console.error(err)
cfg.quiet || bot.chat(err.message)
}
}
)
break
case 3:
const amount = parseInt(message_parts[2])
bot.toss(
item.type,
null, //metadata
amount,
(err) => {
if (err) {
console.error(err)
cfg.quiet || bot.chat(err.message)
}
}
)
break
default:
break
}
break;
case "location":
// TODO put in /lib/location
switch (message_parts[1]) {
@ -547,13 +569,12 @@ const load = (config) => {
cfg = config
bot = cfg.bot
mcData = mcData(bot.version)
mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version))
for (const [key, fn] of Object.entries(events)) {
events_registered.push(
bot.on(key, fn)
)
}
mcData = require('minecraft-data')(bot.version)
}
const unload = () => {

22
lib/plugins/informer.js Normal file
View File

@ -0,0 +1,22 @@
let cfg
let bot
let mcData
function info() {
const block = bot.blockAtCursor()
console.log(block)
cfg.quiet || bot.chat([block.id, block.name])
}
const load = (config) => {
cfg = config
bot = cfg.bot
cfg.info = {
quiet: cfg.quiet,
}
mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version))
}
const unload = () => {}
module.exports = { load, unload, info }

View File

@ -225,4 +225,4 @@ const unload = () => {
bot.off('chat', inventory)
}
module.exports = { load, unload, equipItem, craftItem }
module.exports = { load, unload, equipItem, craftItem, itemByName }

View File

@ -1,31 +1,26 @@
// import { EntityFilters } from "mineflayer-statemachine"
// import v from "vec3"
// import { Movements } from "mineflayer-pathfinder"
// const mineflayer = require('mineflayer')
const { Movements } = require('mineflayer-pathfinder')
// const { GoalBLah } = require('mineflayer-pathfinder').goals
const v = require('vec3')
let cfg = {}
let bot = {}
// let moving
let pathfinder
let mcData
let movements = []
function initMoves(bot = bot, mcData = require('minecraft-data')(bot.version)) {
function initMoves(bot = bot, mcData = bot.mcData) {
console.info(movements)
if (movements.length > 0) {
bot.pathfinder.setMovements(defaultMove)
bot.pathfinder.setMovements(movements.defaultMove)
return console.warn("movements already initialized!")
}
let defaultMove = new Movements(bot, mcData)
defaultMove.canDig = false
defaultMove.scafoldingBlocks.push(mcData.blocksByName.slime_block.id)
// defaultMove.blocksCantBreak.add(mcData.blocksByName.glass.id)
// defaultMove.blocksToAvoid.add(mcData.blocksByName.magma.id)
defaultMove.blocksCantBreak.add(mcData.blocksByName.glass.id)
defaultMove.blocksToAvoid.add(mcData.blocksByName.magma_block.id)
movements.push(defaultMove)
movements.defaultMove = movements[0]
@ -46,8 +41,6 @@ function follow(entity, dynamic = true) {
console.assert(entity)
const { GoalFollow } = require('mineflayer-pathfinder').goals
cfg.quiet && console.log(entity)
|| bot.chat(
`following ${entity.type
@ -83,19 +76,16 @@ const load = (config) => {
movements: []
}
mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version))
pathfinder = bot.pathfinder || bot.loadPlugin(require('mineflayer-pathfinder').pathfinder)
// initMoves(bot, mcData)
setTimeout(initMoves, 500, bot)
// bot.loadPlugin(pathfinder)
// bot.on('time', hello)
setTimeout(initMoves, 500, bot, mcData)
}
const unload = () => {
// TODO stop pathfinding maybe?
stop()
}
module.exports = { load, unload, stop, initMoves, moveNear, follow }