Jump to content

How to check what block the player is looking at?


martiblq

Recommended Posts

The version is 1.15. I want something to happen when the block the player is looking at is the same as whatever block. So it would be somethis like this

if(the block the player is looking at == Blocks.a block)

{

}

I just don't know how to check.

Link to comment
Share on other sites

Entity#pick may be the method you want to use; it emits a ray that collides with blocks in the world. The first parameter is the range, the second is the "partial ticks" (you can just put a value of 1), and the third is if it should work on fluids.

The returned RayTraceResult will tell you what kind of thing it hit, as well as its position. From there, you can get the block at that position.

I'm eager to learn and am prone to mistakes. Don't hesitate to tell me how I can improve.

Link to comment
Share on other sites

What are you even trying to do?

If you're trying to interact with a block you can just override onBlockActivated in your block class.

Read the vanilla code for the crafting table.

I'm eager to learn and am prone to mistakes. Don't hesitate to tell me how I can improve.

Link to comment
Share on other sites

I want  to get an item in my inventory when i rightclick a specific block with a specigic item. I did the item part with an if in an event class. Can i put another if inside the first one that checks if the block is the right one?

Edited by martiblq
Link to comment
Share on other sites

8 minutes ago, martiblq said:

I want  to get an item in my inventory when i rightclick a specific block with a specigic item. I did the item part with an if in an event class. Can i put another if inside the first one that checks if the block is the right one?

If at least one of those is something you added raytracing is redundant.

11 minutes ago, imacatlolol said:

If you're trying to interact with a block you can just override onBlockActivated in your block class.

 

Edited by Novârch

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Link to comment
Share on other sites

10 minutes ago, martiblq said:

I want  to get an item in my inventory when i rightclick a specific block with a specigic item. I did the item part with an if in an event class. Can i put another if inside the first one that checks if the block is the right one?

If the block you're trying use is your own block, just override onBlockActivated as I mentioned and put in the code to check the item in your hand.

If the item you're using is your own item, override the item's onItemUse function.

If neither of them are your own, use the PlayerInteractEvent$RightClickBlock event.

I'm eager to learn and am prone to mistakes. Don't hesitate to tell me how I can improve.

Link to comment
Share on other sites

Spoiler

if(event.getPlayer().getHeldItem(Hand.MAIN_HAND).getItem() == Items.NETHER_STAR)
        {
            
        }

this is the code for the held item. i can't just paste it in the block

Edited by martiblq
Link to comment
Share on other sites

4 minutes ago, martiblq said:
  Reveal hidden contents

if(event.getPlayer().getHeldItem(Hand.MAIN_HAND).getItem() == Items.NETHER_STAR)
        {
            
        }

this is the code for the held item. i can't just paste it in the block

Of course you can't, 

 

39 minutes ago, imacatlolol said:

use the PlayerInteractEvent$RightClickBlock event.

 

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

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.