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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.