Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

How to get the coordinates of a block the player is looking at


Recommended Posts

In case you also want to include liquids in your search, this would be one way to do it:

*  This method is intended to be called right after an item was right-clicked, and no <i>'blockHit'</i> was found. <br>
*  It will search for the first occurrence of a block made of specific material in the direction where the player <br>
*  is looking, limiting the search with the specified range of item's reach.
*  @param player EntityPlayer using the item <b>(unchecked)</b>
*  @param world Instance of the world the player and his item are in <b>(unchecked)</b>
*  @param material The material type to check if item is used on 
*  @param itemReach User defined reach of the item being used <i>(should be > 1)</i>
*  @return True if the block made from designated material has been found 
*  @see EntityPlayer#rayTrace(double, float)
*  @throws java.lang.NullPointerException if EntityPlayer or World instances are <code>null</code>
public static boolean willItemTouchMaterialOnUse(final EntityPlayer player, final World world, Material material, int itemReach)
final Vec3 vec3 = player.getPositionEyes(1.0F);
final Vec3 vec31 = player.getLook(1.0F); 
for (int i = 1; i <= itemReach; i++)  // Manually traverse the vector
	Vec3 vec32 = vec3.addVector(vec31.xCoord * i, vec31.yCoord * i, vec31.zCoord * i);
	BlockPos blockpos = new BlockPos(vec32.xCoord, vec32.yCoord, vec32.zCoord);
	IBlockState iblockstate = world.getBlockState(blockpos);
	if (iblockstate != null && iblockstate.getBlock().getMaterial() == material)
	    return true;

return false;

I still haven't published a mod because I can never get that in-dev version just right to warrant a public release. And yes, after two years of mod development I am still learning to speak Java.


Follow me on GitHub: https://github.com/yooksi

Contact me on Twitter: https://twitter.com/yooksi

Read my Minecraft blog: https://yooksidoesminecraft.blogspot.de/

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.

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.


  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.