Jump to content

[1.14.3] Get distant block player is looking at


fishsaucey

Recommended Posts

1. Please post your code.

2. Where are you trying to call rayTrace?

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

 
 
0
 Advanced issues found
 
 
1
19 minutes ago, DavidM said:
1. Please post your code.

			RayTraceResult raytraceresult = this.rayTrace(worldIn, playerIn, RayTraceContext.FluidMode.ANY);
			double posx = raytraceresult.getHitVec().x;
			double posy = raytraceresult.getHitVec().y;
			double posz = raytraceresult.getHitVec().z;
			
			System.out.println(posx);
			System.out.println(posy);
			System.out.println(posz);

2. Where are you trying to call rayTrace?

In an onItemRightClick event

 

Edited by fishsaucey
Link to comment
Share on other sites

21 minutes ago, DavidM said:

1. Please post your code.

2. Where are you trying to call rayTrace?

 

1: 

			RayTraceResult raytraceresult = this.rayTrace(worldIn, playerIn, RayTraceContext.FluidMode.ANY);
			double posx = raytraceresult.getHitVec().x;
			double posy = raytraceresult.getHitVec().y;
			double posz = raytraceresult.getHitVec().z;
			
			System.out.println(posx);
			System.out.println(posy);
			System.out.println(posz);

 

2: In an onItemRightClick event

Link to comment
Share on other sites

The raytrace returns the position where the ray hits the block, not the position of the block.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

2 hours ago, fishsaucey said:

I dont really think that matters in my case

2 hours ago, DavidM said:

The raytrace returns the position where the ray hits the block, not the position of the block.

This is why your raytrace does not return the position of the block.

 

Get the player's looking direction and apply some trigonometry to extend the range in the direction of the retrace.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Link to comment
Share on other sites

  • 1 year later...

Maybe this will work as replacement for default rayTrace method:

import net.minecraft.entity.effect.LightningBoltEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.*;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;

public class CustomItem extends Item {
    public CustomItem() {
        super(new Item.Properties().group(ItemGroup.TOOLS));
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        if (worldIn instanceof ServerWorld) {
            ServerWorld world = (ServerWorld) worldIn;

            RayTraceResult raytraceresult = rayTrace(worldIn, playerIn, RayTraceContext.FluidMode.ANY);
            double posX = raytraceresult.getHitVec().x;
            double posY = Math.floor(raytraceresult.getHitVec().y);
            double posZ = raytraceresult.getHitVec().z;

            System.out.println(posX);
            System.out.println(posY);
            System.out.println(posZ);

            BlockPos pos = new BlockPos(posX, posY, posZ);

            LightningBoltEntity l = new LightningBoltEntity(worldIn, posX, posY, posZ, false);
            world.addLightningBolt(l);
        }
        
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    protected static RayTraceResult rayTrace(World worldIn, PlayerEntity player, RayTraceContext.FluidMode fluidMode) {
        float f = player.rotationPitch;
        float f1 = player.rotationYaw;
        Vec3d vec3d = player.getEyePosition(1.0F);
        float f2 = MathHelper.cos(-f1 * ((float)Math.PI / 180F) - (float)Math.PI);
        float f3 = MathHelper.sin(-f1 * ((float)Math.PI / 180F) - (float)Math.PI);
        float f4 = -MathHelper.cos(-f * ((float)Math.PI / 180F));
        float f5 = MathHelper.sin(-f * ((float)Math.PI / 180F));
        float f6 = f3 * f4;
        float f7 = f2 * f4;
        double d0 = 8192;
        Vec3d vec3d1 = vec3d.add((double)f6 * d0, (double)f5 * d0, (double)f7 * d0);
        return worldIn.rayTraceBlocks(new RayTraceContext(vec3d, vec3d1, RayTraceContext.BlockMode.OUTLINE, fluidMode, player));
    }
}

I just got it from Item.rayTrace

and changed this line:

    double d0 = double d0 = player.getAttribute(PlayerEntity.REACH_DISTANCE).getValue();;

to:

    double d0 = 8192d;

You can change the value if you want.

Edited by Qplay123
Removed unused import
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

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