angram/lib/plugins/informer.js

128 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2020-12-25 02:17:54 +00:00
let cfg
let bot
let mcData
const v = require('vec3')
2020-12-25 02:17:54 +00:00
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(Object.entries(block.getProperties()))
cfg.quiet || bot.chat(info.join(": "))
2020-12-25 02:17:54 +00:00
}
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()
break;
}
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;
}
}
2020-12-25 02:17:54 +00:00
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, command, block, item, entity }