# Need help finding X Y Z of bounding box!

## Recommended Posts

I want a code that I can use to change a block that you are looking at into an item for test purposes. I do not know how to find the block x y and z based on the bounding bow you are looking at

If anyone has the coding for the philosophers stone so I could look at that, that would be AMAZING!

Thanks,

Kore

The Korecraft Mod

##### Share on other sites

in an Item

```

/**
* Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
*/
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
{
float var4 = 1.0F;
float var5 = player.prevRotationPitch + (player.rotationPitch - player.prevRotationPitch) * var4;
float var6 = player.prevRotationYaw + (player.rotationYaw - player.prevRotationYaw) * var4;
double var7 = player.prevPosX + (player.posX - player.prevPosX) * var4;
double var9 = player.prevPosY + (player.posY - player.prevPosY) * var4 + 1.62D - player.yOffset;
double var11 = player.prevPosZ + (player.posZ - player.prevPosZ) * var4;
Vec3 var13 = Vec3.getVec3Pool().getVecFromPool(var7, var9, var11);
float var14 = MathHelper.cos(-var6 * 0.017453292F - (float)Math.PI);
float var15 = MathHelper.sin(-var6 * 0.017453292F - (float)Math.PI);
float var16 = -MathHelper.cos(-var5 * 0.017453292F);
float var17 = MathHelper.sin(-var5 * 0.017453292F);
float var18 = var15 * var16;
float var20 = var14 * var16;
double var21 = 5.0D;
Vec3 var23 = var13.addVector(var18 * var21, var17 * var21, var20 * var21);
MovingObjectPosition var24 = world.rayTraceBlocks_do(var13, var23, true);
if (var24 == null)
{
return stack;
}
else
{
Vec3 var25 = player.getLook(var4);
boolean var26 = false;
float var27 = 1.0F;
List var28 = world.getEntitiesWithinAABBExcludingEntity(player, player.boundingBox.addCoord(var25.xCoord * var21, var25.yCoord * var21, var25.zCoord * var21).expand(var27, var27, var27));
Iterator var29 = var28.iterator();
while (var29.hasNext())
{
Entity var30 = (Entity)var29.next();

if (var30.canBeCollidedWith())
{
float var31 = var30.getCollisionBorderSize();
AxisAlignedBB var32 = var30.boundingBox.expand(var31, var31, var31);

if (var32.isVecInside(var13))
{
var26 = true;
}
}
}

if (var26)
{
return stack;
}
else
{
if (var24.typeOfHit == EnumMovingObjectType.TILE)
{

int var42 = var24.blockX;
int var43 = var24.blockY;
int var44 = var24.blockZ;

System.out.println("hitX: " +var42+" hitY: "+var43+" hitZ: "+var44);
System.out.println("hitXMax: " +(var42+1)+" hitYMax: "+(var43+1)+" hitZMax: "+(var44+1));

}
return stack;
}
}
}

```

That code in an item will return the min/max bounds of the block you clicked on (if a block was present)..I.E. the block bounded by the block-pick-outline.  Wont' work if an entity is in the way, or the block has an onBlockActivated method.  But gets you basic x,y,z coords of the block clicked otherwise.

Really..you can use similar code anywhere to get the block the player is looking at, not just in an item...I have it there because I was using the item to print meta-data for whatever block I clicked on (just modified the output for your purposes).

You could then call world.setBlockWithNotify(x,y,z,blockID); to update the block clicked to whatever you want.

Hope that helps

##### Share on other sites

is the code just finding what direction you are facing and then determining the block based on the way that you are looking?

or am I just completely screwing how it works up?

The Korecraft Mod

##### Share on other sites

Yes, it pretty much gets the direction you are looking, does some vector math, then grabs a MobileObjectPosition to do a proper ray-trace.  This ray trace will get the block that is hit.

The code I  posted wasn't cleaned up very much, and was pretty much taken out of one of the vanilla items....a boat I think?

I guess the important lines are:

float var4 = 1.0F;

float var5 = player.prevRotationPitch + (player.rotationPitch - player.prevRotationPitch) * var4;

float var6 = player.prevRotationYaw + (player.rotationYaw - player.prevRotationYaw) * var4;

double var7 = player.prevPosX + (player.posX - player.prevPosX) * var4;

double var9 = player.prevPosY + (player.posY - player.prevPosY) * var4 + 1.62D - player.yOffset;

double var11 = player.prevPosZ + (player.posZ - player.prevPosZ) * var4;

Vec3 var13 = Vec3.getVec3Pool().getVecFromPool(var7, var9, var11);

float var14 = MathHelper.cos(-var6 * 0.017453292F - (float)Math.PI);

float var15 = MathHelper.sin(-var6 * 0.017453292F - (float)Math.PI);

float var16 = -MathHelper.cos(-var5 * 0.017453292F);

float var17 = MathHelper.sin(-var5 * 0.017453292F);

float var18 = var15 * var16;

float var20 = var14 * var16;

double var21 = 5.0D;

Vec3 var23 = var13.addVector(var18 * var21, var17 * var21, var20 * var21);

MovingObjectPosition var24 = world.rayTraceBlocks_do(var13, var23, true);///this right here holds hit values.  if it is not null, there was a hit.  it will contain info for what type of hit (entity/tile), as /////well as exact coordinates for the hit.

////

if(var24!=null)

{

if(var24.typeOfHit == EnumMovingObjectType.TILE)

{

////then var24.blockHitX~Z will contain the coordinates of the block hit

}

}

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

×

### Announcements

×

• #### Activity

×
• Create New...