Compare commits

...

18 Commits

Author SHA1 Message Date
jay
1d361e04a6 build: ⬆️ update deps 2021-01-18 01:54:05 +05:00
jay
7597620626 ci: 🔧 add vscode conventional commits scopes 2021-01-18 01:47:32 +05:00
jay
e5faa6f022 feat(informer): add more detail to item info
Gives detail of traversing the item's nbt data
2021-01-18 01:24:47 +05:00
jay
cc18ac5c2e refactor(mover): 🚚 move commands inside mover plugin itself
This reduces the code inside the command plugin.
Most of the logic was only relevant to moving anyway.
Command aliases like `come` and `follow` still remain in command plugin.
2021-01-17 23:59:39 +05:00
jay
7050a1621b fix(informer): 🐛 add missing name for entity info when entity is a player
Uses `username` when `entity.name` is missing; should check other cases
2021-01-17 18:32:05 +05:00
jay
7cbfa16476 feat(informer): add case for when sub command is passed a single param
Currently returns:
- Item at given slot number
- Entity that matches given name
2021-01-17 16:20:20 +05:00
jay
63849e0729 fix(informer): 🥅 catch and report when objects are missing 2021-01-17 16:16:41 +05:00
jay
4e7f8d59fd feat(informer): add more detailed block metadata info
Uses `block.getProperties()`. Thanks to a [comment][1]

[1]: https://github.com/PrismarineJS/mineflayer-pathfinder/pull/84/files#r541196424
2021-01-17 15:54:31 +05:00
jay
7b2b936f81 feat(mover): implement moveY (vertical move up or down)
Doesn't appear to be working properly, bot assumes XZ is goal reached
2021-01-17 13:56:18 +05:00
jay
8a39596b1d feat(informer): add info for block at given position 2021-01-17 13:02:05 +05:00
jay
2601b7cfb1 fix(informer): 🥅 fix crash for block info when no block or an empty block is found 2021-01-17 12:20:49 +05:00
jay
67932b2f6a fix(sleeper): 🥅 catch sleeping edge case errors
Happens when trying to sleep while previously unable to move.
Or maybe when trying to sleep during dawn.
2021-01-16 16:39:42 +05:00
jay
9a6e684b11 feat(informer): add info about nearest entities 2021-01-16 16:17:42 +05:00
jay
3488a94233 feat(informer): info about held item(s) 2021-01-16 14:51:49 +05:00
jay
4d21327086 fix(mover): 🚸 better messages 2021-01-16 13:57:21 +05:00
jay
e74d796124 fix(command): 🐛 make follow command work again without params 2021-01-16 13:50:35 +05:00
jay
3d5ffe38cd feat(mover): inform when goal reached 2021-01-16 13:25:26 +05:00
jay
b519913355 feat(mover): implement moving to X Z goal (without y) 2021-01-15 00:36:17 +05:00
6 changed files with 275 additions and 119 deletions

8
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,8 @@
{
"conventionalCommits.scopes": [
"command",
"mover",
"sleeper",
"informer"
]
}

View File

@ -193,7 +193,15 @@ function command(username, message) {
break;
case "follow":
subcommand("go follow " + message_parts.slice(1).join(" "))
switch (message_parts.length) {
case 1:
subcommand("go follow me")
break;
default:
subcommand("go " + message)
break;
}
break;
case "come":
switch (message_parts[1]) {
@ -201,106 +209,17 @@ function command(username, message) {
case "closer":
subcommand("go follow close")
break
case "up":
case "down":
cfg.plugins.mover.moveY(player.position)
break
default:
subcommand("go follow once")
}
break;
case "move":
case "go":
// TODO move most of the subcommands into mover.js?
const message_parts2 = message_parts.slice(2)
switch (message_parts[1]) {
case "init":
cfg.plugins.mover.initMoves()
break
case "near":
// message_parts2 = message_parts.slice(2)
switch (message_parts2.length) {
case 0:
cfg.plugins.mover.moveNear(bot.nearestEntity().position)
break
case 1:
switch (message_parts2[0]) {
case "me":
if (player) {
cfg.plugins.mover.moveNear(player.position)
} else {
cfg.quiet || bot.chat("can't see you")
}
break;
default:
const aPlayer = bot.players[message_parts[2]] ? bot.players[message_parts[2]].entity : null
if (aPlayer) {
cfg.plugins.mover.moveNear(aPlayer.position)
} else {
cfg.quiet || bot.chat(`can't see ${message_parts[2]}`)
}
break;
}
break
case 2:
todo()
// bot.lookAt({}) goalxz?
break
case 3:
//TODO more checks
cfg.plugins.mover.moveNear(message_parts2)
break
default:
break
}
break
case "follow":
// message_parts2 = message_parts.slice(2)
switch (message_parts2.length) {
case 0:
cfg.plugins.mover.follow(bot.nearestEntity())
break
case 1:
let dist = 3
switch (message_parts2[0]) {
case "close":
dist = 1
case "me":
case "once":
if (player) {
cfg.plugins.mover.follow(player, message_parts2[0] === "me", dist)
} else {
cfg.quiet || bot.chat("can't see you")
}
break;
default:
const aPlayer = bot.players[message_parts[2]] ? bot.players[message_parts[2]].entity : null
if (aPlayer) {
cfg.plugins.mover.follow(aPlayer)
} else {
cfg.quiet || bot.chat(`can't see ${message_parts[2]}`)
}
break;
}
break
// case 2:
// bot.lookAt({}) goalxz?
// break
// case 3:
//TODO more checks
// cfg.plugins.mover.moveNear(message_parts2)
// break
default:
todo()
break
}
break
case "stop":
cfg.plugins.mover.stop()
break
default:
return todo()
break;
}
cfg.plugins.mover.command(message_parts.slice(1), player)
break;
case "attack":

View File

@ -1,20 +1,84 @@
let cfg
let bot
let mcData
const v = require('vec3')
function block() {
const block = bot.blockAtCursor()
console.log(block)
function block(pos) {
const block = pos ? bot.blockAt(v(pos)) : bot.blockAtCursor()
console.log(block, block && block.getProperties())
if (!block) {
cfg.quiet || bot.chat("empty block")
return block
}
let info = [block.type, block.name]
if (block.metadata) info.push(block.metadata)
if (block.metadata) info.push(Object.entries(block.getProperties()))
cfg.quiet || bot.chat(info.join(": "))
}
function item(
slot,
entity = bot.entity
) {
const item = slot ?
bot.inventory.slots[parseInt(slot) + bot.QUICK_BAR_START] :
entity.heldItem
console.log(item)
if (!item) {
cfg.quiet || bot.chat("no item")
return item
}
let info = [item.type, item.name]
if (item.metadata) info.push("meta: " + item.metadata.length)
if (item.nbt) {
info.push(compound_value(item.nbt))
}
cfg.quiet || bot.chat(info.join("; "))
function compound_value(obj) {
if (typeof obj.value == "object") {
return compound_value(obj.value)
} else if (obj.value) {
return obj.value
} else if (typeof obj == "object") {
const keys = Object.keys(obj)
return keys.map(key => {
return `${key}: ${compound_value(obj[key])}`
});
} else {
return obj
}
}
return item
}
function entity(name) {
const entity = bot.nearestEntity((entity) => {
const ename = entity.name || entity.username
return name && ename ? ename == name : true
})
console.log(entity)
if (!entity) {
cfg.quiet || bot.chat("no entity")
return entity
}
let info = [entity.type, entity.name || entity.username]
if (entity.metadata) info.push("len: " + entity.metadata.length)
cfg.quiet || bot.chat(info.join("; "))
}
function command(message_parts) {
switch (message_parts.length) {
case 0:
// TODO most recent command?
block()
break;
case 1:
switch (message_parts[0]) {
case "item":
item()
break
case "entity":
entity()
break
case "block":
default:
block()
@ -23,6 +87,28 @@ function command(message_parts) {
break;
case 2:
switch (message_parts[0]) {
case "item":
item(message_parts[1])
break
case "entity":
default:
entity(message_parts[1])
break;
}
break
case 4:
switch (message_parts[0]) {
case "block":
default:
block(message_parts.slice(1))
break;
}
break;
default:
break;
}
@ -39,4 +125,4 @@ const load = (config) => {
const unload = () => {}
module.exports = { load, unload, command, block }
module.exports = { load, unload, command, block, item, entity }

View File

@ -30,13 +30,39 @@ function initMoves(bot = bot, mcData = bot.mcData) {
function moveNear(pos, distance = 3) {
const { GoalNear } = require('mineflayer-pathfinder').goals
cfg.quiet || bot.chat(`moving to ${pos}`)
pos = v(pos)
cfg.quiet || bot.chat(`moving to ${pos.floored()}`)
bot.pathfinder.setMovements(movements.defaultMove)
bot.pathfinder.setGoal(new GoalNear(pos.x, pos.y, pos.z, distance))
}
function moveXZ(pos) {
const { GoalXZ } = require('mineflayer-pathfinder').goals
if (Array.isArray(pos) && pos.length == 2) {
pos = v(pos[0], 0, pos[1])
}
pos = v(pos)
console.log(pos)
cfg.quiet || bot.chat(`moving to ${pos.floored()}`)
bot.pathfinder.setMovements(movements.defaultMove)
bot.pathfinder.setGoal(new GoalXZ(pos.x, pos.z))
}
function moveY(pos) {
const { GoalY } = require('mineflayer-pathfinder').goals
if (Array.isArray(pos) && pos.length == 1) {
pos = v(null, pos[0], null)
}
pos = v(pos)
console.log(pos)
cfg.quiet || bot.chat(`moving to ${pos.floored()}`)
bot.pathfinder.setMovements(movements.defaultMove)
bot.pathfinder.setGoal(new GoalY(pos.y))
}
function follow(entity, dynamic = true, distance = 3) {
console.assert(entity)
const { GoalFollow } = require('mineflayer-pathfinder').goals
@ -60,11 +86,123 @@ function hit(blockOrEntity) {
bot.chat(`hitting ${entity.name || entity.type}`)
}
function goalReached(goal) {
console.log(goal)
const entity = goal?.entity
let entityInfo = ""
if (entity) {
entityInfo += entity.type + ": "
switch (entity.type) {
case "player":
entityInfo += entity.username
break;
default:
break;
}
}
cfg.quiet || bot.chat(`goal reached: ${entityInfo}; pos: [x:${goal?.x}, y:${goal?.y}, z:${goal?.z}]`)
}
function stop() {
bot.pathfinder.setGoal(null)
bot.stopDigging()
}
function command(message_parts, player) {
const message_parts2 = message_parts.slice(1)
switch (message_parts[0]) {
case "init":
initMoves()
break
case "near":
switch (message_parts2.length) {
case 0:
moveNear(bot.nearestEntity().position)
break
case 1:
switch (message_parts2[0]) {
case "me":
if (player) {
moveNear(player.position)
} else {
cfg.quiet || bot.chat("can't see you")
}
break;
default:
const aPlayer = bot.players[message_parts2[0]] ? bot.players[message_parts2[0]].entity : null
if (aPlayer) {
moveNear(aPlayer.position)
} else {
cfg.quiet || bot.chat(`can't see ${message_parts2[0]}`)
}
break;
}
break
case 2:
//TODO this isn't near
moveXZ(message_parts2)
break
case 3:
//TODO more checks
moveNear(message_parts2)
break
default:
break
}
break
case "follow":
// message_parts2 = message_parts.slice(2)
switch (message_parts2.length) {
case 0:
follow(bot.nearestEntity())
break
case 1:
let dist = 3
switch (message_parts2[0]) {
case "close":
dist = 1
case "me":
case "once":
if (player) {
follow(player, message_parts2[0] === "me", dist)
} else {
cfg.quiet || bot.chat("can't see you")
}
break;
default:
const aPlayer = bot.players[message_parts2[0]] ? bot.players[message_parts2[0]].entity : null
if (aPlayer) {
follow(aPlayer)
} else {
cfg.quiet || bot.chat(`can't see ${message_parts2[0]}`)
}
break;
}
break
// case 2:
// bot.lookAt({}) goalxz?
// break
// case 3:
//TODO more checks
// moveNear(message_parts2)
// break
default:
cfg.quiet || bot.chat("unknown or bad command")
break
}
break
case "stop":
stop()
break
default:
return cfg.quiet || bot.chat(`unknown command ${message_parts[0]}`)
break;
}
}
const load = (config) => {
cfg = config
bot = cfg.bot
@ -81,11 +219,13 @@ const load = (config) => {
// initMoves(bot, mcData)
setTimeout(initMoves, 500, bot, mcData)
bot.on('goal_reached', goalReached)
}
const unload = () => {
stop()
bot.off('goal_reached', goalReached)
}
module.exports = { load, unload, stop, initMoves, moveNear, follow }
module.exports = { load, unload, command, stop, initMoves, moveNear, moveXZ, moveY, follow }

View File

@ -33,17 +33,20 @@ function sleep(quiet = cfg.sleep.quiet) {
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
}
})
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)
}
})
})
} else if (bed = bot.inventory.items().filter(bot.isABed)[0]) {

View File

@ -35,16 +35,16 @@
},
"dependencies": {
"dotenv-packed": "^1.2.1",
"minecraft-data": "^2.70.2",
"mineflayer": "^2.39.2",
"mineflayer-armor-manager": "^1.3.0",
"mineflayer-pathfinder": "^1.2.3",
"minecraft-data": "^2.73.1",
"mineflayer": "^2.40.1",
"mineflayer-armor-manager": "^1.4.0",
"mineflayer-pathfinder": "^1.3.6",
"mineflayer-pvp": "^1.0.2",
"prismarine-block": "^1.7.2",
"prismarine-block": "^1.7.3",
"prismarine-chat": "^1.0.3",
"prismarine-entity": "^1.1.0",
"prismarine-item": "^1.5.0",
"prismarine-nbt": "^1.3.0",
"prismarine-nbt": "^1.4.0",
"prismarine-recipe": "^1.1.0",
"typescript": "^4.1.3",
"vec3": "^0.1.7"