Jump to content

How to find a block in a world client-side?


DimChig

Recommended Posts

My goal is to every tick scan for a "diamond block" for example in loaded chunks from client-side. I already did something like looping all surrounding blocks in a radius, and taking a block by 

world.getBlockState(new BlockPos(x, y, z)).getBlock() == Blocks.diamond_block

and my loop is like this:

int range = 100;
for (int xi = -range; xi < range; xi++) {
	for (int yi = -3; yi < 3; yi++) {
		for (int zi = -range; zi < range; zi++) {
			double x = Math.floor(xi + player.posX);
			double y = Math.floor(yi + player.posY);
			double z = Math.floor(zi + player.posZ);

			if (world.getBlockState(new BlockPos(x, y, z)).getBlock() == Blocks.diamond_block) {
				//do something
			}
		}
	}
}

It is working, BUT. It is lagging so much (i use it every tick, and i need to use it so often). My fps drops from 200 to 50. I need a faster way of doing this, please help!

Link to comment
Share on other sites

4 minutes ago, Luis_ST said:

what did you try to achieve, since this code is terrible you should not call it each tick

and btw you loop through round about 120k Blocks each tick

Yea, I know, that's why it is lagging. I need it for my custom minimap. I want to show specific blocks on it, but I need to loop through all of them to find them. Also, blocks can be destroyed, so I need to scan them frequently. I was wondering if forge has some methods for finding blocks in rendered world

Edited by DimChig
Link to comment
Share on other sites

 You don't need to update a minimap every 0.05 seconds. I'm pretty sure you are writing an x-ray.🙂

Anyway have a look at BlockPos.findClosestMatch() or one of the other static methods like betweenClosed().

Their main optimisation I guess is using MutableBlockPos instead of creating 120,000 new BlockPos() like you are.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

  • 2 weeks later...
On 7/12/2022 at 11:52 PM, warjort said:

 You don't need to update a minimap every 0.05 seconds. I'm pretty sure you are writing an x-ray.🙂

Anyway have a look at BlockPos.findClosestMatch() or one of the other static methods like betweenClosed().

Their main optimisation I guess is using MutableBlockPos instead of creating 120,000 new BlockPos() like you are.

Okay, i solved it. So i just splitted my "scan zone" into small zones, and scan each zone every tick. So in 1-3 seconds it scans 60 small zones, and i have no lag. So after i find my block in "small scan zone" i add it to an array (and save new Date().getTime()) so than each tick i clear all my blocks from array that have time diffrence more that 3 seconds. And minimap reaction time is fine, around 3 seconds.

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.

Announcements



×
×
  • Create New...

Important Information

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