# Trying to get closest block

## Recommended Posts

Im trying to get the closest block to player, like the closest wheat and i really have no idea what im doing hear, im pretty stumped, can some one help

##### Share on other sites

Well it first of all, you need code that checks all the block positions starting from the player position. However, note that as the distance gets farther the number of blocks required grows very large, so you will have to limit the range for checking to about 10 blocks for performance reasons.

For the actually checking, you just loop through all the block locations using "for" loops for each of the x, y, and z dimensions and look for the block. Note though you need to check the distance of each of the target type blocks you find to see if you've actually found the closest.

So the code would be something like this (I didn't check it, and it might need some modification, but you should get the general idea)

```Block targetBlock = Blocks.WHEAT;
BlockPos closestPos = null;
BlockPos checkPos = player.getPos();

for (int x = player.getX()-10; x < player.getX()+10; x++)
{
for (int y = player.getY()-10; x < player.getY()+10; y++)
{
for (int z = player.getZ()-10; z < player.getZ()+10; z++)
{
checkPos = new BlockPos(x, y, z);
if (world.getBlockState(checkPos).getBlock == targetBlock}
{
// check if it is closer than any previously found position
if (closestPos == null ||
player.getDistanceSq(player.getX() - checkPos.getX(),
player.getY() - checkPos.getY(),
player.getZ() - checkPos.getZ())
< player.getDistanceSq(player.getX() - closestPos.getX(),
player.getY() - closestPos.getY(),
player.getZ() - closestPos.getZ()))
{
closestPos = checkPos;
}
}
}
}
}```

As you can see, it loops through all the combinations of x, y, and z within 10 blocks radius and checks if the block at that position is the type you're looking for. If it is, it checks to see if it is closer than anything else you've already found.

There are actually cleverer ways of improving the performance, such as spiraling outwards and stopping at the first block found, but this is logically one of the most standard was to cover an area with a search.

Edited by jabelar
• 1
• 1

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

## 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.

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

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

• ### Posts

• version: 1.19.2 launcher: Forge (43.2.14) Log: https://paste.ee/p/H3MCd#section0 getting an access violation error complaining about the create millstone, game will run fine for a little while, i'll load in, play about for 10-15 minutes and then it will crash without warning (launcher gives error code -1 if that helps) i'm stumped does anyone know what the issue is? if possible a solution that doesn't involve removing the create mod. thanks in advance.
• Hi everyone, I am trying to start a modded server with the mods I also have in my client. The server works perfeclty normal without any mods in it, however when I put the mods in I get a lot of missing mods.toml files or something like that. I don't really know how this works, I'll add the server log below. Does anyone know what i'm doing wrong? https://paste.ee/p/F3a5E
• Have you registered the DeferredRegister with the mod event bus? You can link your GitHub repo.
• First, the solution: use the mdk and don't touch anything in it before checking that it works. You seemed to have changed something that broke it. You're also targeting 1.20.0 - *don't use this!* - it is a known buggy and abandoned version that people dropped in favour of 1.20.1 which came out shortly after. Next, a couple of things about your post: 1) you tagged it with "broken mod", which is commonly used to indicate that your issue has been solved and that the problem was that one of the mod's you installed was broken 2) please use code embeds. It's hard to interpret an arrow pointing to the issue in your error when the arrow doesn't line up properly

• ### Who's Online (See full list)

×

• #### Activity

×
• Create New...