Jump to content

[SOLVED] How to get the block the player's looking at


xCrazyFeline

Recommended Posts

I have tried several different things in order to try to get where the player is looking at such as player.getLookAngle() (which now I realize doesn't have anything to do with that) and a few other methods that I can't remember right now, but the only thing I've found that works is getPlayerPOVHitResult, and that only works in children of the Item class.

Currently, I'm trying to get an entity to spawn where the player is looking at 100 blocks in the air, and I have an item mostly set up to do so, but getPlayerPOVHitResult is not working in this situation.

Here is exactly what I'm doing, if you know how to make this work properly your assistance would be highly appreciated:

        BlockHitResult povHitResult = getPlayerPOVHitResult(level, player, ClipContext.Fluid.ANY);

	if(!level.isClientSide && hand == InteractionHand.MAIN_HAND && !player.isCrouching() && mode == 6 && mode6) {
            DescendingShellEntity descendingShell = new DescendingShellEntity(TMEntityTypes.DESCENDING_SHELL.get(), level);
            descendingShell.setPos(povHitResult.getBlockPos().getX(), povHitResult.getBlockPos().getY() + 100, povHitResult.getBlockPos().getZ());
            level.addFreshEntity(descendingShell);

Here's the GitHub repo if it helps: https://github.com/xCrazyFeline/Turtle-Magic-1.19.2-FORGE-

Edited by xCrazyFeline
Adding GitHub repo link

Sanity is for losers. -Felix

Link to comment
Share on other sites

I used the following code:

	public static @NotNull Vec3 clipWithDistance(@NotNull Player player, @NotNull Level level, double clipDistance) {
		double vecX = Math.sin(-player.getYRot() * (Math.PI / 180.0) - Math.PI) * -Math.cos(-player.getXRot() * (Math.PI / 180.0));
		double vecY = Math.sin(-player.getXRot() * (Math.PI / 180.0));
		double vecZ = Math.cos(-player.getYRot() * (Math.PI / 180.0) - Math.PI) * -Math.cos(-player.getXRot() * (Math.PI / 180.0));
		return level.clip(new ClipContext(player.getEyePosition(), player.getEyePosition().add(vecX * clipDistance, vecY * clipDistance, vecZ * clipDistance), ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, player)).getLocation();
	}

Note if you also want to recognise Fluid as a block, you have to change ClipContext.Fluid.

Link to comment
Share on other sites

That works! Thank you! I'll add your name to the mod's credits for your assistance.
(I really can not thank you enough for this, I've been working on it for weeks, and it also helped me with something else I was going to have to fix)

Edited by xCrazyFeline

Sanity is for losers. -Felix

Link to comment
Share on other sites

  • xCrazyFeline changed the title to [SOLVED] How to get the block the player's looking at

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.