PrismarineJS / mineflayer

Create Minecraft bots with a powerful, stable, and high level JavaScript API.
https://prismarinejs.github.io/mineflayer/
MIT License
4.96k stars 904 forks source link

useExtraInfo in findBlock don't provide sign text #1489

Closed magicaltoast closed 3 years ago

magicaltoast commented 3 years ago

Versions

Detailed description of a problem

The 'findBlock' methods with 'useExtraInfo' set to true don't provide a sign text field, but the type suggests that it is

What did you try yet?

I tried to use 'findBlock' methods with 'useExtraInfo', to match every sign and console.log it to check if sign block contain desired field

Your current code


bot.findBlocks({
            count: 3,
            maxDistance: 10,
            useExtraInfo: true,
            matching: (block) => {
                if (block.type == 155) {
                    console.log(block)
                }
                return false

            }
        })

Expected behavior

Be able to access text on the sign when 'useExtraInfo' is set to true

Karang commented 3 years ago

Indeed there is a mistake in findBlocks. This line https://github.com/PrismarineJS/mineflayer/blob/master/lib/plugins/blocks.js#L177 should be:

if (useExtraInfo || isBlockInSection(section, matcher)) {

Alternatively, you can use findBlocks without extraInfos to match all signs, then iterate on the returned position list to find the sign with the text you need.

magicaltoast commented 3 years ago

@Karang technically yes I could, but I am using signs as 'navigation points' so there are a lot of them with different content. I really encourage you to make a pull request to fix that :) have a nice day

Karang commented 3 years ago

Yeah someone can make a PR for that. But if you are worried about efficiency, you should try the alternative method because it is more optimized. If you use only findBlocks, extraInfos + matching function will be run for every block within the search radius, and the search will not be able to use the skip section optimization (each section skipped is 4096 less blocks to check). Iterating over the results of a simpler search will be faster than the slow search.

flowl commented 3 years ago

I noticed this too and used an extra boot.blockAt() as a workaround but I am still not sure if there is another bug in my own code because I don't find signs when they are less than the given maxDistance away but in another chunk. (I don't have time at the moment to debug but when I get back to that and still have a problem I will open a detailed issue.)

getClosestSign(text: string) {
        const options = {
            matching: (block: Block) => {
                return /sign$/.test(block.name)
            },
            maxDistance: 32,
            useExtraInfo: true
        }

        let blocks = this.bot.findBlocks(options)
        let target, blockAt
        let regex = new RegExp(text, 'ig')

        blocks.forEach((block) => {
            // @ts-ignore
            blockAt = this.bot.blockAt(block)
            if (regex.test(blockAt.signText || '')) {
                target = blockAt
            }
        })
        return target || null
    }
rom1504 commented 3 years ago

is this fixed after https://github.com/PrismarineJS/mineflayer/pull/1491 ?

TheDudeFromCI commented 3 years ago

@rom1504 Yes, this issue is fixed.