Jump to content

1.20.1 Whats the proper way for entity finding certain blocks?


Gabuly

Recommended Posts

I am trying to add the goal to animals which let them constantly search for water block and approach it. 

I have tried using the iterator, which however not always returns the closest water block. The following code can function properly but I think this may very performance consuming? I want to know how people usually deal with this kind of tasks and if there is any good project I can learn from? :)

 

public void start() {
       // LOGGER.info("Start!!");
        BlockPos entityPos = mob.blockPosition();
        // Define the search range

        double nearestDistanceSq = Double.MAX_VALUE; // Start with the highest distance possible
        for (int dx = -chunkRange; dx <= chunkRange; dx++) {
            for (int dy = -10; dy <= 10; dy++) { // Assuming you want to search 4 blocks up and down
                for (int dz = -chunkRange; dz <= chunkRange; dz++) {
                    BlockPos searchPos = entityPos.offset(dx, dy, dz);
                    if (this.mob.level().getFluidState(searchPos).is(FluidTags.WATER)) {
                        double distanceSq = entityPos.distSqr(searchPos);
                        if (distanceSq < nearestDistanceSq) {
                            nearestDistanceSq = distanceSq;
                            movePos = searchPos;
                        }
                    }
                }
            }
        }

 

Here is the complete file:

https://github.com/gabuly/Livebywater/blob/master/src/main/java/com/github/gabuly/livebyrivermod/RandomTryFindWaterGoal.java

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.