Jump to content

[1.16.5]Determine the block that the player is looking at.


NoClipMonster

Recommended Posts

Hello, I am a moder novice and I can't figure out how to get the name and position of the block that the player is looking at.
I want to make a bot that will dig for resources itself.
I tried to do this with the help of Ray Trace Result, but nothing happened, I can't find the normal documentation for 1.16.5.
Please help me solve it.

Edited by NoClipMonster
Link to comment
Share on other sites

Find existing usages of RayTraceResult.

  • Like 2

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.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

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

Link to comment
Share on other sites

3 minutes ago, Beethoven92 said:

The F3 debug screen has a similar function to what you are searching for. If you think about it, on the right it shows you the the name of the block you are looking at. Maybe try looking how vanilla implements that?

Okay I'll try, thanks for the hint

Link to comment
Share on other sites

1 hour ago, Beethoven92 said:

The F3 debug screen has a similar function to what you are searching for. If you think about it, on the right it shows you the the name of the block you are looking at. Maybe try looking how vanilla implements that?

OK, due to my lack of experience, I didn't find any information that could even push me in the right direction.

Link to comment
Share on other sites

32 minutes ago, Beethoven92 said:

The class i pointed you toward is called DebugOverlayGui. Did you find it? There you can find an example of what you are trying to achieve

This will be useful for me, thank you, but I managed to do it using RayTraceResult.

Thank you so much for your help.

I'll leave the code for people like me) 

 

public Block LookingAt(){
    RayTraceResult rt = Minecraft.getInstance().hitResult;

    double x = (rt.getLocation().x);
    double y = (rt.getLocation().y);
    double z = (rt.getLocation().z);

    double xla = Minecraft.getInstance().player.getLookAngle().x;
    double yla = Minecraft.getInstance().player.getLookAngle().y;
    double zla = Minecraft.getInstance().player.getLookAngle().z;

    if ((x%1==0)&&(xla<0))x-=0.01;
    if ((y%1==0)&&(yla<0))y-=0.01;
    if ((z%1==0)&&(zla<0))z-=0.01;

    BlockPos ps = new BlockPos(x,y,z);
    BlockState bl = Minecraft.getInstance().level.getBlockState(ps);

    return bl.getBlock();
}

If you have any ideas for improving the code, please write.

Edited by NoClipMonster
Link to comment
Share on other sites

Found a better solution 

ArrayList<String> list = new ArrayList<String>();

Minecraft minecraft = Minecraft.getInstance();
ClientPlayerEntity player= minecraft.player;
ClientWorld level = minecraft.level;

RayTraceResult block =  player.pick(20.0D, 0.0F, false);
RayTraceResult fluid =  player.pick(20.0D, 0.0F, true);

\\BLOCK
if(block.getType() == RayTraceResult.Type.BLOCK)
{
    BlockPos blockpos = ((BlockRayTraceResult)block).getBlockPos();
    BlockState blockstate = level.getBlockState(blockpos);
    LOGGER.info("Looking at: "+blockstate.getBlock()+"\nPosition= "+ blockpos.getX() + ", " + blockpos.getY() + ", " + blockpos.getZ());
}
\\FLUID
if(fluid.getType() == RayTraceResult.Type.BLOCK)
{
    BlockPos blockpos = ((BlockRayTraceResult)fluid).getBlockPos();
    BlockState blockstate = level.getBlockState(blockpos);
    LOGGER.info("Looking at: "+blockstate.getBlock()+"\nPosition= "+ blockpos.getX() + ", " + blockpos.getY() + ", " + blockpos.getZ());
}

What do the first two variables do?

player.pick(20.0D, 0.0F, false);

Edited by NoClipMonster
  • Like 1
Link to comment
Share on other sites

Go look at the function?
Also, this code is still client side due to the call to Minecraft.getInstance()

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.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

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

Link to comment
Share on other sites

It is not necessarily incorrect the way you are doing it...just you will only be able to use the above code client side..if it is enough for what you are trying to achieve then its good..but if you intend to call this portion of code somewhere else that is not the client side then it won't work due to the presence of client side only classes (like the Minecraft class)

  • Like 1

Check out the port of the BetterEnd fabric mod (WIP): https://www.curseforge.com/minecraft/mc-mods/betterend-forge-port

Link to comment
Share on other sites

13 hours ago, Beethoven92 said:

It is not necessarily incorrect the way you are doing it...just you will only be able to use the above code client side..if it is enough for what you are trying to achieve then its good..but if you intend to call this portion of code somewhere else that is not the client side then it won't work due to the presence of client side only classes (like the Minecraft class)

Yes, I guessed it when I went to bed 😅😅
Thank you🙂

Link to comment
Share on other sites

New version:

public void LookingAt(PlayerEntity player, boolean isFluid){
    ArrayList<String> list = new ArrayList<String>();

    RayTraceResult block =  player.pick(20.0D, 0.0F, isFluid);

     if(block.getType() == RayTraceResult.Type.BLOCK)
      {
        BlockPos blockpos = ((BlockRayTraceResult)block).getBlockPos();
        BlockState blockstate = player.level.getBlockState(blockpos);
        LOGGER.info("Looking at: "+blockstate.getBlock()+"\nIs Fluid: "+isFluid+"\nPosition= "+ blockpos.getX() + ", " + blockpos.getY() + ", " + blockpos.getZ());
      }
}
  • Like 1
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.