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

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

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

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

##### Share on other sites

Thanks guys, gonna try those fix

Yes really not worried so far with the cases where they are sneaking at the edge of some blocks, will start with the basic for now

Thanks.

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

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

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

##### Share on other sites

?

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

"you seem to be THE best modder I've seen imo."

~spynathan

ლ(́◉◞౪◟◉‵ლ

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

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

• As a seasoned investor, I’ve learned that the financial world can be exciting and treacherous. My experiences with investments have taught me the importance of diligence and thorough research, particularly when dealing with newer and high-risk assets like Bitcoin. Unfortunately, my journey with Bitcoin was marred by a painful lesson in the form of a scam, which I hope to share to help others avoid similar pitfalls. My introduction to Bitcoin investing seemed promising. The allure of high returns and the buzz surrounding cryptocurrency were captivating. I was drawn to several Bitcoin investment platforms that promised substantial profits. These platforms presented themselves with professional websites, attractive promotions, and genuine testimonials. I was led to believe that these sites were reliable and that I was making a sound investment. However, this trust proved to be misplaced. The platform I initially invested in showcased fake success stories and substantial profits to entice investors. They used these fabricated examples to build credibility and persuade new investors like me to commit more funds. Their tactics were sophisticated; they knew exactly how to create an illusion of success and security. I, too, was lured by their promises and gradually invested a considerable amount of money, totaling 68,000 USD.The initial investments went smoothly. My account appeared to grow with impressive returns, and I felt a sense of validation in my investment strategy. But things took a drastic turn when I decided to make a more significant investment, believing that the returns would only get better. Once I deposited a substantial sum, the platform’s behavior changed abruptly. My account was frozen without warning, and I was faced with a barrage of demands for additional payments before I could access my funds or the supposed profits. The situation was both distressing and bewildering. I was met with excuses and obstructions at every turn. It became clear that I was dealing with a fraudulent company that had no intention of honoring its commitments. The realization that I had been deceived was crushing. The emotional and financial toll of the situation was overwhelming. Determined to recover my funds, I reached out to Trust Geeks Hack Expert, a service recommended by a trusted friend who had faced a similar ordeal. My initial skepticism was tempered by desperation and hope. Trust Geeks Hack Expert took immediate action. Their team worked tirelessly to investigate the fraudulent platform and recover my lost funds. Their dedication and expertise were evident throughout the process. In just a few weeks, Trust Geeks Hack Expert managed to successfully retrieve the full amount of 68,000 USD. Their assistance was thorough and professional, and they kept me informed every step of the way. Their efforts not only resulted in the recovery of my funds but also provided me with invaluable insights into how these scams operate. This. This experience has been a harsh lesson in the importance of conducting thorough research before investing in any platform, particularly in the cryptocurrency space. The allure of high returns can be overwhelming, but it’s crucial to approach such investments with caution. Verify the legitimacy of the platform, seek out reviews from reliable sources, and ensure that any investment opportunity has a track record of transparency and reliability. To anyone who finds themselves in a similar predicament, I cannot recommend Trust Geeks Hack Expert  enough. Their professionalism and commitment to recovering my funds were exemplary:: E>mail: trustgeekshackexpert {@} fastservice{.}com -----> Tele>gram : Trustgeekshackexpert, And also  What's>App   + 1-7-1-9-4-9-2-2-6-9-3
• As a seasoned investor, I’ve learned that the financial world can be exciting and treacherous. My experiences with investments have taught me the importance of diligence and thorough research, particularly when dealing with newer and high-risk assets like Bitcoin. Unfortunately, my journey with Bitcoin was marred by a painful lesson in the form of a scam, which I hope to share to help others avoid similar pitfalls. My introduction to Bitcoin investing seemed promising. The allure of high returns and the buzz surrounding cryptocurrency were captivating. I was drawn to several Bitcoin investment platforms that promised substantial profits. These platforms presented themselves with professional websites, attractive promotions, and genuine testimonials. I was led to believe that these sites were reliable and that I was making a sound investment. However, this trust proved to be misplaced. The platform I initially invested in showcased fake success stories and substantial profits to entice investors. They used these fabricated examples to build credibility and persuade new investors like me to commit more funds. Their tactics were sophisticated; they knew exactly how to create an illusion of success and security. I, too, was lured by their promises and gradually invested a considerable amount of money, totaling 68,000 USD.The initial investments went smoothly. My account appeared to grow with impressive returns, and I felt a sense of validation in my investment strategy. But things took a drastic turn when I decided to make a more significant investment, believing that the returns would only get better. Once I deposited a substantial sum, the platform’s behavior changed abruptly. My account was frozen without warning, and I was faced with a barrage of demands for additional payments before I could access my funds or the supposed profits. The situation was both distressing and bewildering. I was met with excuses and obstructions at every turn. It became clear that I was dealing with a fraudulent company that had no intention of honoring its commitments. The realization that I had been deceived was crushing. The emotional and financial toll of the situation was overwhelming. Determined to recover my funds, I reached out to Trust Geeks Hack Expert, a service recommended by a trusted friend who had faced a similar ordeal. My initial skepticism was tempered by desperation and hope. Trust Geeks Hack Expert took immediate action. Their team worked tirelessly to investigate the fraudulent platform and recover my lost funds. Their dedication and expertise were evident throughout the process. In just a few weeks, Trust Geeks Hack Expert managed to successfully retrieve the full amount of 68,000 USD. Their assistance was thorough and professional, and they kept me informed every step of the way. Their efforts not only resulted in the recovery of my funds but also provided me with invaluable insights into how these scams operate. This. This experience has been a harsh lesson in the importance of conducting thorough research before investing in any platform, particularly in the cryptocurrency space. The allure of high returns can be overwhelming, but it’s crucial to approach such investments with caution. Verify the legitimacy of the platform, seek out reviews from reliable sources, and ensure that any investment opportunity has a track record of transparency and reliability. To anyone who finds themselves in a similar predicament, I cannot recommend Trust Geeks Hack Expert  enough. Their professionalism and commitment to recovering my funds were exemplary:: E>mail: trustgeekshackexpert{@}fastservice{.}com -----> Tele>gram : Trustgeekshackexpert, And also  What's>App   + 1-7-1-9-4-9-2-2-6-9-3
• I've been playing it for only 1 day and it doesn't want to go into the game anymore. I've tried reinstalling the game and repairing the game files (on curseforge) but it still doesn't work. Whenever i try to click on the Singleplayer icon, it just flickers into the world creation page and flickers back
• https://gist.github.com/RealMangoBot/03ce10d60ce10f126dcf2c033c3a4f46
• Sure, please notify, 1.20.1 preferred.

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

×

• #### Activity

×
• Create New...