Compare commits

...

18 Commits

Author SHA1 Message Date
jay
feb0b0927a feat(informer): add villager profession details
Provide names in place of numbers for villager profession.
Implementation uses an enum
2021-05-15 18:37:24 +05:00
jay
8276e68489 feat(informer): add block info based on relative position for players 2021-05-11 17:59:26 +05:00
jay
2b7163bef3 feat(informer): add block info based on other players
Now should work for position of players other than the callee.

Alternative implementation could use `entity` function.
This allows to also work for named entities as a bonus.
2021-05-11 17:54:19 +05:00
jay
a0b4641f37 feat(informer): add entity detail by type and kind for some entities
So far:
- mobs
 - villager
- object
 - drops
2021-05-11 17:42:31 +05:00
jay
186d6ac3d2 feat(informer): 🚸 add more detail to entity info 2021-05-11 17:36:41 +05:00
jay
984c9490c3 fix(informer): 🥅 make player undefined if nul
This allows to fallback to defaults in functions
2021-05-11 17:34:25 +05:00
jay
3379f75ab9 feat(informer): 🚸 make entity search case insensitive 2021-05-11 17:32:38 +05:00
jay
69b1ab0b0b feat(informer): add reverse mcdata lookup by block or item id 2021-05-11 14:44:22 +05:00
jay
3c9d62441f feat(informer): add block info based on player's relative position
Relative position includes simple words like "feet", "standing", "head", etc
2021-05-11 10:39:53 +05:00
jay
665102e19c fix(informer): 🚑 remove extra let cfg declaration left over from refactor 2021-05-11 09:46:07 +05:00
jay
db459f52e6 fix(informer): ✏️ fix typo in isVec3 check function 2021-05-11 09:32:54 +05:00
jay
360eeff02f feat(informer): add item info based on player
Change the parameter order of `item` function to accomodate this
2021-05-11 07:32:05 +05:00
jay
25faac2f4c feat(informer): add command me for block info based on player position 2021-05-10 18:37:48 +05:00
jay
ccf0c598e8 feat(informer): add block info based on player position or manually provided coords
This paves the way to get block info based on positions relative from the player
2021-05-07 16:27:44 +05:00
jay
050e2b3bd9 feat(informer): 🚸 add sub-command for module level quiet 2021-05-07 12:53:24 +05:00
jay
e879d1f4ad refactor(informer): ♻️ reorder config loading 2021-05-07 12:40:17 +05:00
jay
df193da779 feat(command): 🚧 add stub for bot._client events
Not sure how these are different:
- `bot._client.on("chat")`
- `bot.on("message(str)")`
2021-05-06 13:15:38 +05:00
jay
269d258763 feat(command): use a simpler text based system for receiving server commands 2021-05-06 13:11:31 +05:00
2 changed files with 235 additions and 35 deletions

View File

@ -56,7 +56,8 @@ const events = {
}, 15 * 60 * 1000, bot, cfg); }, 15 * 60 * 1000, bot, cfg);
} }
} }
, message: systemMessage // , message: systemMessage
, messagestr: function systemMessageStr(...args) { console.log("cmd msgstr:", ...args) }
, "chat:web": commandWeb , "chat:web": commandWeb
} }
@ -73,12 +74,12 @@ function systemMessage(...args) {
} }
function commandWeb([[mode, username, message]]) { function commandWeb([[mode, username, message]]) {
console.log("web msg:", mode, username, message) // console.log("web msg:", mode, username, message)
message && command(username, message) message && command(username, message)
} }
function _clientSystemMessage(...args) { function _clientSystemMessage(...args) {
console.log("cmd msg:", args[0]?.message, args[0]?.extra?.length, args[0]?.extra, args[0], args.slice(1)) console.log("cmd chat:", args[0]?.message, args[0]?.extra?.length, args[0]?.extra, args[0], args.slice(1))
} }
const events_registered = [] const events_registered = []
@ -544,6 +545,8 @@ const load = (config) => {
bot.on(key, fn) bot.on(key, fn)
) )
} }
// bot._client.on("chat", _clientSystemMessage)
} }
const unload = () => { const unload = () => {
@ -553,6 +556,8 @@ const unload = () => {
events_registered.shift() events_registered.shift()
} }
console.log("events_registered:", bot._eventsCount) console.log("events_registered:", bot._eventsCount)
// bot._client.off("chat", _clientSystemMessage)
} }
module.exports = { load, unload, events_registered, command } module.exports = { load, unload, events_registered, command }

View File

@ -1,39 +1,104 @@
let cfg
let bot let bot
let mcData let mcData
let quiet
// import v from 'vec3' // import v from 'vec3'
const v = require('vec3') const v = require('vec3')
function block(pos) { let cfg = {
const block = pos ? bot.blockAt(v(pos)) : bot.blockAtCursor() info: {
console.log(block, block && block.getProperties()) quiet: quiet,
recentCommand: null,
},
// to satisfy typescript
quiet: null,
bot: bot
}
var RelativePosEnum
(function (RelativePosEnum) {
RelativePosEnum[RelativePosEnum["feet"] = 0] = "feet"
RelativePosEnum[RelativePosEnum["standing"] = 1] = "standing"
RelativePosEnum[RelativePosEnum["head"] = 2] = "head"
RelativePosEnum[RelativePosEnum["looking"] = 3] = "looking"
RelativePosEnum[RelativePosEnum["infront"] = 4] = "infront"
RelativePosEnum[RelativePosEnum["behind"] = 5] = "behind"
})(RelativePosEnum || (RelativePosEnum = {}))
function relPosToBlock(entity, relPos) {
let pos
if (!(relPos in RelativePosEnum)) return console.warn("info: not a relative position:", relPos)
relPos = typeof relPos === "number" ? RelativePosEnum[relPos] : relPos
switch (relPos) {
case "feet":
pos = entity.position;
break
case "standing":
pos = entity.position.offset(0, -1, 0)
break
// todo use CARDINAL directions api from pathfinder
// case "behind":
// entity.yaw
// pos = entity.position
// break
// case "front":
// case "infront":
// pos = entity.position
// break
case "head":
pos = entity.position.offset(0, 1.85, 0)
break
case "looking":
if (entity === bot.entity) {
return bot.blockAtCursor()
// return bot.blockInSight(128, 128)
}
default:
quiet || bot.chat(`info: pos '${relPos}' not implemented`)
break
}
if (pos) {
// nearest block
return bot.blockAt(pos)
}
}
function isVec3(vec) {
return vec?.length === 3 || vec.x && vec.y && vec.z
}
function block(entity = bot.entity, pos = entity?.position?.offset(0, -1, 0)) {
console.assert(pos || entity)
const block = isVec3(pos) ? bot.blockAt(v(pos))
: typeof pos === "string" ? relPosToBlock(entity, RelativePosEnum[pos]) : pos
console.log(block, block?.getProperties && block.getProperties())
if (!block) { if (!block) {
cfg.quiet || bot.chat("empty block") quiet || bot.chat("empty block")
return block return block
} }
bot.lookAt(block?.position)
let info = [block.type, block.name] let info = [block.type, block.name]
if (block.metadata) info.push(Object.entries(block.getProperties())) if (block.metadata) info.push(Object.entries(block.getProperties()))
cfg.quiet || bot.chat(info.join(": ")) quiet || bot.chat(info.join(": "))
} }
function item( function item(
slot, entity = bot.entity,
entity = bot.entity slot = entity.heldItem
) { ) {
const item = slot ? const item = typeof slot === "number" ?
bot.inventory.slots[parseInt(slot) + bot.QUICK_BAR_START] : bot.inventory.slots[slot + bot.QUICK_BAR_START] :
entity.heldItem slot
console.log(item) console.log("info item:", item)
if (!item) { if (!item) {
cfg.quiet || bot.chat("no item") quiet || bot.chat("no item")
return item return item
} }
let info = [item.type, item.name] let info = [item.type, item.name, item.count]
if (item.metadata) info.push("meta: " + item.metadata.length) if (item.metadata) info.push("meta: " + item.metadata.length)
if (item.nbt) { if (item.nbt) {
info.push(compound_value(item.nbt)) info.push(compound_value(item.nbt))
} }
cfg.quiet || bot.chat(info.join("; ")) quiet || bot.chat(info.join("; "))
function compound_value(obj) { function compound_value(obj) {
if (typeof obj.value == "object") { if (typeof obj.value == "object") {
return compound_value(obj.value) return compound_value(obj.value)
@ -51,27 +116,110 @@ function item(
return item return item
} }
function entity(name) { var VillagerProfession
const entity = typeof name === "string" ? bot.nearestEntity((entity) => { (function (VillagerProfession) {
const ename = entity.name || entity.username VillagerProfession[VillagerProfession["Unemployed"] = 0] = "Unemployed"
return name && ename ? ename == name : true VillagerProfession[VillagerProfession["Armourer"] = 1] = "Armourer"
}) : entity VillagerProfession[VillagerProfession["Butcher"] = 2] = "Butcher"
console.log(entity) VillagerProfession[VillagerProfession["Cartographer"] = 3] = "Cartographer"
VillagerProfession[VillagerProfession["Cleric"] = 4] = "Cleric"
VillagerProfession[VillagerProfession["Farmer"] = 5] = "Farmer"
VillagerProfession[VillagerProfession["Fisherman"] = 6] = "Fisherman"
VillagerProfession[VillagerProfession["Fletcher"] = 7] = "Fletcher"
VillagerProfession[VillagerProfession["Leatherworker"] = 8] = "Leatherworker"
VillagerProfession[VillagerProfession["Librarian"] = 9] = "Librarian"
VillagerProfession[VillagerProfession["Mason"] = 10] = "Mason"
VillagerProfession[VillagerProfession["Nitwit"] = 11] = "Nitwit"
VillagerProfession[VillagerProfession["Shepherd"] = 12] = "Shepherd"
VillagerProfession[VillagerProfession["Toolsmith"] = 13] = "Toolsmith"
VillagerProfession[VillagerProfession["Weaponsmith"] = 14] = "Weaponsmith"
})(VillagerProfession || (VillagerProfession = {}))
function entity(name = bot.nearestEntity()) {
const entity = typeof name === "string" ? (name = name.toLowerCase()) && bot.nearestEntity((entity) => {
const enames = [entity.username?.toLowerCase(), entity.name, entity.displayName?.toLowerCase()]
return enames.includes(name)
}) : name
console.log("info entity:", entity)
if (!entity) { if (!entity) {
cfg.quiet || bot.chat("no entity") quiet || bot.chat("no entity")
return entity return entity
} }
let info = [entity.type, entity.username || entity.name] entity?.position && bot.lookAt(entity.position)
let info = [(entity.entityType && entity.entityType + ":" || "") + (entity.type === "object" ? entity.kind : entity.type)]
info.push(entity.username || entity.name)
// TODO various info depending on the type of entity; player, villager, etc // TODO various info depending on the type of entity; player, villager, etc
// TODO refactor and combine with compound_value()
switch (entity.type) {
case "object":
switch (entity.kind) {
case "Drops":
const item = entity.metadata[7];
entity.metadata;
if (item.present) {
info.push("item: " + item.itemId + ": "
+ mcData.items[item.itemId].name
+ `x${item.itemCount}`);
}
else {
console.warn("info entity: metadata expected item, got:", entity.metadata[7]);
}
break;
default:
}
break;
case "mob":
switch (name) {
case 'villager':
const { villagerProfession, ...otherProps } = entity.metadata[17]
info.push(VillagerProfession[villagerProfession])
info.push(Object.entries(otherProps).toString())
break
default:
break
}
entity.metadata[8] && info.push("health:" + entity.metadata[8])
entity.metadata[15] && info.push("baby")
if (entity.heldItem) {
info.push("holding")
item(entity)
}
case "global":
case "orb":
case "player":
break;
case "other":
info.push("kind:" + entity.kind)
default:
}
if (entity.metadata) info.push("len: " + entity.metadata.length) if (entity.metadata) info.push("len: " + entity.metadata.length)
cfg.quiet || bot.chat(info.join("; ")) quiet || bot.chat(info.join("; "))
return entity return entity
} }
function blockOrItemFromId(num, quiet = cfg.info.quiet) {
const block = mcData?.blocks[num]
const item = mcData?.items[num]
// const entity = mcData?.entities[num]
if (block || item) {
quiet || bot.chat(
(block && `block: ${block.name}, ` || "")
+ (item && `item: ${item.name}, ` || "")
// + (entity && `entity: ${entity.name}, ` || "")
)
} else {
quiet || bot.chat("info: nonexistent block or item")
}
return { block, item }
}
function command(message_parts, player) { function command(message_parts, player) {
if (message_parts.length > 0) { if (message_parts.length > 0) {
cfg.info.recentCommand = message_parts cfg.info.recentCommand = message_parts
} }
if (player === null)
player = void 0
switch (message_parts.length) { switch (message_parts.length) {
case 0: case 0:
if (cfg.info.recentCommand) { if (cfg.info.recentCommand) {
@ -79,11 +227,15 @@ function command(message_parts, player) {
} else { } else {
// TODO dispatch on instance of entity, block, etc.. // TODO dispatch on instance of entity, block, etc..
// TODO have the logic inside the function or with a utility function // TODO have the logic inside the function or with a utility function
block(player.position || player?.entity.position || null) block()
} }
break; break;
case 1: case 1:
switch (message_parts[0]) { switch (message_parts[0]) {
case "quiet":
cfg.info.quiet = quiet = !quiet;
quiet || bot.chat(`info: ${quiet ? "" : "not "}being quiet`);
break;
case "i": case "i":
case "item": case "item":
item() item()
@ -92,10 +244,20 @@ function command(message_parts, player) {
case "entity": case "entity":
entity() entity()
break break
case "me":
block(player)
break
case "b": case "b":
case "block": case "block":
default:
block() block()
break
default:
const num = parseInt(message_parts[0])
if (isFinite(num)) {
blockOrItemFromId(num)
} else {
quiet || bot.chat("info usage: `!info [me|i|e|b|<num>|quiet]`")
}
break; break;
} }
@ -105,7 +267,43 @@ function command(message_parts, player) {
switch (message_parts[0]) { switch (message_parts[0]) {
case "i": case "i":
case "item": case "item":
item(message_parts[1]) switch (message_parts[1]) {
case "me":
item(player)
break
default:
const slot = parseInt(message_parts[1])
slot && item(undefined, slot)
break
}
break
case "b":
case "block":
switch (message_parts[1]) {
case "me":
block(player)
break
default:
const aPlayer = bot.players[message_parts[1]]
if (message_parts[1] in RelativePosEnum) {
block(undefined, message_parts[1])
} else if (aPlayer) {
if (aPlayer.entity) {
block(aPlayer.entity)
} else {
quiet || bot.chat(`info: player ${aPlayer.username} too far!`)
}
} else {
// or entity(message_parts[1]).position
console.log(bot.players[message_parts[1]])
quiet || bot.chat("info: not yet implemented")
}
}
break
case "me":
if (message_parts[1] in RelativePosEnum) {
block(player, message_parts[1])
}
break break
case "e": case "e":
case "entity": case "entity":
@ -120,7 +318,7 @@ function command(message_parts, player) {
case "b": case "b":
case "block": case "block":
default: default:
block(message_parts.slice(1)) block(undefined, message_parts.slice(1))
break; break;
} }
@ -133,12 +331,9 @@ function command(message_parts, player) {
} }
const load = (config) => { const load = (config) => {
config.info = cfg.info
cfg = config cfg = config
bot = cfg.bot bot = cfg.bot
cfg.info = {
quiet: cfg.quiet,
recentCommand: null,
}
mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version)) mcData = bot.mcData || (bot.mcData = require('minecraft-data')(bot.version))
} }