#EntityJS - How can I make a mob look for and break a specific blok?

17 messages · Page 1 of 1 (latest)

keen widget
#

I want kubejs:light_seeker to find the nearest blocks with the kubejs:light_source tag and make it drop as an item.
I made this, and it can start the game, but it doesn't work properly.
( Sorry, I don‘t really understand this at all... I pretty much did it with ChatGPT. )

heavy auroraBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

royal birchBOT
#

Paste version of destroy_light_source.js from @keen widget

whole merlin
#

@twilit mulch

royal birchBOT
#

Never say 'it crashed', 'it errored', or 'it didn't work' without providing the full scripts and log/crash report!

twilit mulch
#

also i dont believe Infinity is a good thing to go off of in terms of it being smaller, just set it to the max size of your bounding box which looks like 40

keen widget
#

let nearestDistance = 20

keen widget
opal osprey
#

I faced the same problem, have no idea how to implement.
I don't think that looping over all blocks is a good idea - it scales really bad when search area grows (even for your example it's 64k iterations worst case) and block search should run repeatedly to make sure that lightsource isn't gone.
Maybe it's worth to track all placements of light blocks by player and ignore natureal lightsources (if it's enough)

keen widget
#

still don't really get how to do it. I only know how to make this mob look for nearby entities, but I have no clue when it comes to block tags. It'd be much easier if there were some examples.

twilit mulch
#

alright so i've spent some time revising it so it will use minecraft's built in BlockPos iteration logic, we're also instancing a lot of the logic in persistentData so it's multiplayer friendly and wont conflict with other mobs who have the same goals ect, we're also only checking once every second for a torch instead of checking every single tick to avoid unnecessary entity ticking which will save resources, im only sending the custom goal code because of discord's message limit though it should be copy and paste in

#
mob => {
    if (mob.age % 20 == 0) {
        let aabb = mob.boundingBox.inflate(20);
        let Searching = mob.persistentData.get('Search')
        if (!Searching) Searching = 0
        if (Searching == 0) {
            Searching = 1
            for (let pos of BlockPos.betweenClosed(
                Math.floor(aabb.minX), Math.floor(aabb.minY), Math.floor(aabb.minZ),
                Math.floor(aabb.maxX), Math.floor(aabb.maxY), Math.floor(aabb.maxZ)
            )) {
                let block = mob.level.getBlock(pos);
                if (block.hasTag('kubejs:light_source')) {
                    mob.persistentData.NLP = { x: pos.getX(), y: pos.getY(), z: pos.getZ() };
                    Searching = 2
                    break;
                }
            }
            if (Searching == 2 && mob.persistentData.NLP) {
                mob.getNavigation().moveTo(mob.persistentData.NLP.x, mob.persistentData.NLP.y, mob.persistentData.NLP.z, 0.7);
            } else {
                Searching = 0
            }
        }
        let targetPos = mob.persistentData.get('NLP');
        if (targetPos) {
            let dist = mob.blockPosition().distManhattan(new BlockPos(targetPos.x, targetPos.y, targetPos.z))
            if (dist <= 3) {
                mob.level.destroyBlock(new BlockPos(targetPos.x, targetPos.y, targetPos.z), true);
                mob.persistentData.remove('NLP');
                Searching = 0
            }
        }
    }
}```
#

let me know if you have any other problems and we'll work them out

keen widget
#

It’s running perfectly! I think this is exactly what I wanted!

#

Thanks for your help! 😊

royal birchBOT
#

Please close your ticket (with </ticket close:1054771505520717835> or the button atop this thread) once you resolved your issue!
This also helps others that would like to help out, as they don't have to look into this thread to check if it has been resolved by now.

Do you have any other questions regarding your issue? Feel free to ask!
Note: You should create a new post for unrelated issues.