Jump to content

[1.7.10] Correct way to get block under the player?


American2050

Recommended Posts

Would this be a correct way to bring the block the player is standing on?

 

		int playerX = (int) (player.posX);
	int playerY = (int) player.posY;
	int playerZ = (int) (player.posZ);

	if (player.posX<0) { playerX = playerX-1; }
	if (player.posZ<0) { playerZ = playerZ-1; }


	Block getBlock = world.getBlock(playerX, playerY-1, playerZ);

	System.out.println(getBlock.getLocalizedName());

	System.out.println("Doubles - X: "+ player.posX + " - Y: " + player.posY + " - Z: " + player.posZ);

	System.out.println("Integers - X: "+ playerX + " - Y: " + playerY + " - Z: " + playerZ);

 

Without the if statements all blocks on the negative coordinates were returning wrong blocks.

 

I also notice that this is not perfect in case the player is standing on blocks edges, for example when holding sneak. But that wouldn't matter, what I like to know if that fix for when player is on negative coordinates is ok, or it should be done in a different way?

 

Thanks.

Link to comment
Share on other sites

Re if-blocks: this is what Math.floor() is for. Conversion from flay or double to integer it equivalent to Math.truncate() which rounds towards zero. Identical to Math.floor() for positive values, but not for negative.

 

As for sneaking, you are unlikely to solve that problem to your satisfaction, as there are eight possible blocks the player could be standing on, possibly up to 3 of them.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I'm not sure you're properly factoring in the Y-offset of the entity. I basically follow what the vanilla code does for this:

 

It is pretty simple really, but I see this question asked and some people get confused because the block position uses int versus the double used by entity position, plus entities have some y offsets that need to be considered.

 

public Block findBlockUnderEntity(Entity parEntity)

{

    int blockX = MathHelper.floor_double(parEntity.posX);

    int blockY = MathHelper.floor_double(parEntity.posY-0.2D - (double)parEntity.yOffset);

    int blockZ = MathHelper.floor_double(parEntity.posZ);

    return parEntity.worldObj.getBlock(blockX, blockY, blockZ);

}

 

However, for custom entities it is possible that the yOffset isn't properly set.  It may be preferable (suggested by CoolAlias) to use the bounding box instead.  So this version is probably the more trustworthy:

 

public Block findBlockUnderEntity(Entity parEntity)

{

    int blockX = MathHelper.floor_double(parEntity.posX);

    int blockY = MathHelper.floor_double(parEntity.boundingBox.minY)-1;

    int blockZ = MathHelper.floor_double(parEntity.posZ);

    return parEntity.worldObj.getBlock(blockX, blockY, blockZ);

}

 

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

Link to comment
Share on other sites

I'm not sure you're properly factoring in the Y-offset of the entity. I basically follow what the vanilla code does for this:

 

It is pretty simple really, but I see this question asked and some people get confused because the block position uses int versus the double used by entity position, plus entities have some y offsets that need to be considered.

 

public Block findBlockUnderEntity(Entity parEntity)

{

    int blockX = MathHelper.floor_double(parEntity.posX);

    int blockY = MathHelper.floor_double(parEntity.posY-0.2D - (double)parEntity.yOffset);

    int blockZ = MathHelper.floor_double(parEntity.posZ);

    return parEntity.worldObj.getBlock(blockX, blockY, blockZ);

}

 

However, for custom entities it is possible that the yOffset isn't properly set.  It may be preferable (suggested by CoolAlias) to use the bounding box instead.  So this version is probably the more trustworthy:

 

public Block findBlockUnderEntity(Entity parEntity)

{

    int blockX = MathHelper.floor_double(parEntity.posX);

    int blockY = MathHelper.floor_double(parEntity.boundingBox.minY)-1;

    int blockZ = MathHelper.floor_double(parEntity.posZ);

    return parEntity.worldObj.getBlock(blockX, blockY, blockZ);

}

 

Thanks you. I will be mainly used to detect block under the player itself, no mobs or other entities. Haven't consider cases where the player may be jumping, so I'm gonna take a look at that also and see how it goes.

Link to comment
Share on other sites

Thanks you. I will be mainly used to detect block under the player itself, no mobs or other entities. Haven't consider cases where the player may be jumping, so I'm gonna take a look at that also and see how it goes.

 

If you want to consider jumping as well, you might want to try ray-tracing instead. Basically you start at the player and keep checking downwards until you find a block that isn't air.

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

Link to comment
Share on other sites

????

 

No look in the moveEntity() method, that's where vanilla minecraft determines the block below to making walking noises

 

That is the code I already gave him. Copied from the vanilla code. The only issue, like I mentioned, is that for some custom entities you need to make sure you factor in the bottom of the bounding box better.

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

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.