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

how to get the side of the block the player is currently looking at?


Recommended Posts

I've been exploring code for the last 4 hours and I can't find a way to make that work. 


Also tried the search on the forums but couldn't come up with better keywords, I'm afraid.


Is it possible to get the "NORTH; EAST; WEST; UP; DOWN; SOUTH" side  of a block corresponding to the player crosshair?


I want to call it inside of onItemUse, or onItemUseFirst.

Link to comment
Share on other sites

Pulled this from my code, use/edit freely


 * If [4] is -1 , there was no block within reach.
 * @return [0]= BlockId, [1]= BlockX, [2]= BlockY, [3]= BlockZ, [4]= SideHit, [5]= BlockMeta
public static int[] getBlockInfront(World theWorld, EntityPlayer thePlayer, double reach)
	int[] blockInfo = { 0, 0, 0, 0, -1, 0 };

	MovingObjectPosition movingobjectposition = getMovingObjectPositionFromPlayer(theWorld, thePlayer, true, reach);
	if (movingobjectposition != null) {
		if (movingobjectposition.typeOfHit == EnumMovingObjectType.TILE) {
			blockInfo[1] = movingobjectposition.blockX;
			blockInfo[2] = movingobjectposition.blockY;
			blockInfo[3] = movingobjectposition.blockZ;
			blockInfo[4] = movingobjectposition.sideHit;
			blockInfo[5] = theWorld.getBlockMetadata(blockInfo[1], blockInfo[2], blockInfo[3]);
			blockInfo[0] = theWorld.getBlockId(blockInfo[1], blockInfo[2], blockInfo[3]);
	return blockInfo;

private static MovingObjectPosition getMovingObjectPositionFromPlayer(World world, EntityPlayer entityplayer, boolean flag, double reach)
	float f = 1.0F;
	float playerPitch = entityplayer.prevRotationPitch + (entityplayer.rotationPitch - entityplayer.prevRotationPitch) * f;
	float playerYaw = entityplayer.prevRotationYaw + (entityplayer.rotationYaw - entityplayer.prevRotationYaw) * f;
	double playerPosX = entityplayer.prevPosX + (entityplayer.posX - entityplayer.prevPosX) * f;
	double playerPosY = (entityplayer.prevPosY + (entityplayer.posY - entityplayer.prevPosY) * f + 1.6200000000000001D) - entityplayer.yOffset;
	double playerPosZ = entityplayer.prevPosZ + (entityplayer.posZ - entityplayer.prevPosZ) * f;
	Vec3 vecPlayer = Vec3.createVectorHelper(playerPosX, playerPosY, playerPosZ);
	float cosYaw = MathHelper.cos(-playerYaw * 0.01745329F - 3.141593F);
	float sinYaw = MathHelper.sin(-playerYaw * 0.01745329F - 3.141593F);
	float cosPitch = -MathHelper.cos(-playerPitch * 0.01745329F);
	float sinPitch = MathHelper.sin(-playerPitch * 0.01745329F);
	float pointX = sinYaw * cosPitch;
	float pointY = sinPitch;
	float pointZ = cosYaw * cosPitch;
	Vec3 vecPoint = vecPlayer.addVector(pointX * reach, pointY * reach, pointZ * reach);
	MovingObjectPosition movingobjectposition = world.rayTraceBlocks_do_do(vecPlayer, vecPoint, flag, !flag);
	return movingobjectposition;

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.


  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • I have installed a forge server, and tried launching it. It launches as a vanilla server if I run the file called "minecraft_server.1.16.5.jar" If i run the file called "forge-1.16.5-36.2.23.jar", nothing at all happens, it just creates a log with these 2 lines: [19Jan2022 16:08:29.340] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--gameDir, ., --launchTarget, fmlserver, --fml.forgeVersion, 36.2.23, --fml.mcpVersion, 20210115.111550, --fml.mcVersion, 1.16.5, --fml.forgeGroup, net.minecraftforge] [19Jan2022 16:08:29.345] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 8.0.9+86+master.3cf110c starting: java version 17.0.1 by Oracle Corporation I have also tried running both files (since I don't know which one I'm supposed to use) using the command line like this: java -Xmx6000M -Xms6000M -jar {serverName}.jar nogui pause If I run the the server file, i get a vanilla server as well. If i run the forge file, the log file looks the same as before, and I get this result in the terminal: D:\MCS\VH>java -Xmx6000M -Xms6000M -jar forge-1.16.5-36.2.23.jar nogui 2022-01-19 16:16:32,476 main WARN Advanced terminal features are not available in this environment [16:16:32] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher running: args [--gameDir, ., --launchTarget, fmlserver, --fml.forgeVersion, 36.2.23, --fml.mcpVersion, 20210115.111550, --fml.mcVersion, 1.16.5, --fml.forgeGroup, net.minecraftforge, nogui] [16:16:32] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher 8.0.9+86+master.3cf110c starting: java version 17.0.1 by Oracle Corporation Exception in thread "main" java.lang.IllegalAccessError: class cpw.mods.modlauncher.SecureJarHandler (in unnamed module @0xadd0edd) cannot access class sun.security.util.ManifestEntryVerifier (in module java.base) because module java.base does not export sun.security.util to unnamed module @0xadd0edd at cpw.mods.modlauncher.SecureJarHandler.lambda$static$1(SecureJarHandler.java:41) at cpw.mods.modlauncher.api.LamdbaExceptionUtils.uncheck(LamdbaExceptionUtils.java:95) at cpw.mods.modlauncher.SecureJarHandler.<clinit>(SecureJarHandler.java:41) at cpw.mods.modlauncher.Launcher.lambda$new$6(Launcher.java:55) at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1708) at cpw.mods.modlauncher.api.TypesafeMap.computeIfAbsent(TypesafeMap.java:52) at cpw.mods.modlauncher.api.TypesafeMap.computeIfAbsent(TypesafeMap.java:47) at cpw.mods.modlauncher.Environment.computePropertyIfAbsent(Environment.java:62) at cpw.mods.modlauncher.Launcher.<init>(Launcher.java:55) at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) at net.minecraftforge.server.ServerMain$Runner.runLauncher(ServerMain.java:63) at net.minecraftforge.server.ServerMain$Runner.access$100(ServerMain.java:60) at net.minecraftforge.server.ServerMain.main(ServerMain.java:57) D:\MCS\VH>pause Press any key to continue . . . I have also tried launching old servers that I had on SkyFactory 3 that worked fine before, but they have stopped working too. When I launch them I get this result: Starting Sky Factory 3 Server... A problem occurred running the Server launcher.java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at net.minecraftforge.fml.relauncher.ServerLaunchWrapper.run(ServerLaunchWrapper.java:62) at net.minecraftforge.fml.relauncher.ServerLaunchWrapper.main(ServerLaunchWrapper.java:31) Caused by: java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap') at net.minecraft.launchwrapper.Launch.<init>(Launch.java:34) at net.minecraft.launchwrapper.Launch.main(Launch.java:28) ... 6 more Press any key to continue . . . I assume this means that something has changed on my pc, causing forge servers not to run correctly, but I cannot imagine what that could be. I hope this is enough information to be able to help
    • Check it out, you won't regret https://shrinke.me/I0iwHJ3
    • These should be static initializers and the fields should be final. That is the point of DeferredRegister. Your code looks fine and should work, however you can use EnchantmentHelper.getItemEnchantmentLevel if you just want the level for one Enchantment. This call will be more efficient as it does not need to build the whole enchantment map.
  • Topics

  • Who's Online (See full list)

  • Create New...

Important Information

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