Posted March 18, 20214 yr 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 March 18, 20214 yr by NoClipMonster
March 18, 20214 yr Find existing usages of RayTraceResult. 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.
March 18, 20214 yr Author Just now, Draco18s said: Find existing usages of RayTraceResult. I didn't find any ready-made examples for 1.16 And the examples for older versions don't work in 1.16 . Maybe I'm looking wrong, which is why I wrote here.
March 18, 20214 yr 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? Check out the port of the BetterEnd fabric mod (WIP): https://www.curseforge.com/minecraft/mc-mods/betterend-forge-port
March 18, 20214 yr Author 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
March 18, 20214 yr Author 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.
March 19, 20214 yr 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 Edited March 19, 20214 yr by Beethoven92 Check out the port of the BetterEnd fabric mod (WIP): https://www.curseforge.com/minecraft/mc-mods/betterend-forge-port
March 19, 20214 yr Author 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 March 19, 20214 yr by NoClipMonster
March 19, 20214 yr Author 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 March 19, 20214 yr by NoClipMonster
March 19, 20214 yr 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.
March 19, 20214 yr Author 8 minutes ago, Draco18s said: Also, this code is still client side due to the call to Minecraft.getInstance() How to do it correctly?
March 19, 20214 yr 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) Check out the port of the BetterEnd fabric mod (WIP): https://www.curseforge.com/minecraft/mc-mods/betterend-forge-port
March 20, 20214 yr Author 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🙂
March 20, 20214 yr Author 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()); } }
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.