Jump to content

[SOLVED] How to teleport EntityPlayer to X Y Z without lagback with RayCast.


Jaycen1000

Recommended Posts

Hello, I'm making an item in my mod that when you right click, it will teleport you to where you are facing.

Now, I know that since I am using RayCast, it will run on client side only, how do I fix this so I can use teleportation. (I don't want to use /tp command since it clutters logs (Maybe a method is to fix this?)).

CODE:

@Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
    {
    	RayTraceResult mop = Minecraft.getMinecraft().getRenderViewEntity().rayTrace(200, 1.0F);
    	if(mop != null)
    	{
    		EnumFacing blockHitSide = mop.sideHit;
    		Block blkresult = worldIn.getBlockState(new BlockPos(new BlockPos(mop.getBlockPos().getX(), mop.getBlockPos().getY(), mop.getBlockPos().getZ()))).getBlock();
    	}

    	
    	System.out.println(mop.getBlockPos().getX());
    	System.out.println(mop.getBlockPos().getY());
    	System.out.println(mop.getBlockPos().getZ());
    	
    	MinecraftServer s = FMLCommonHandler.instance().getMinecraftServerInstance();
		
    	
		//s.getCommandManager().executeCommand( s, "/tp " + playerIn.getName() + " " + (mop.getBlockPos().getX() + " " + mop.getBlockPos().getY() + " " + mop.getBlockPos().getZ()));

    	
    	
    	return new ActionResult<ItemStack>(EnumActionResult.PASS, playerIn.getHeldItem(handIn));
    }

 

Edited by Jaycen1000
Solved (Adding in title)
Link to comment
Share on other sites

1 hour ago, diesieben07 said:

Indeed it is protected. But the code you showed above shows your doing this from inside an item class. Please learn basic Java (aka how protected works).

I am doing this from a class that extends Item, but it is saying that it is not visible from my ide, also, would I be able to use reflection to get it to work (like, would it be efficient)? Also, what do you mean by I’m coding it in an item class? And I looked it up and protected meant what I thought. And when I run it and right click, the game crashes (because of the visibility error).

Link to comment
Share on other sites

1 hour ago, Jaycen1000 said:

RayTraceResult mop = playerIn.getHeldItemMainhand().getItem().rayTrace(worldIn, playerIn, false);

 

Hint:

this.rayTrace(blah)

 

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

I can’t believe I didn’t do that, I DID THAT (a bit but enough to remember) when making Swing apps, my mind must have blanked, thanks. Yeah, I know since it extends Item, the class is an item.

 

EDIT:

After a min, I did @Override to change the distance (It was just how far your character could reach) and here it is.

	@Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
    {
    	
    	
		RayTraceResult mop = this.rayTrace(worldIn, playerIn, false);
    	Item i = null;
    	
    	
    	if(mop != null)
    	{
    		EnumFacing blockHitSide = mop.sideHit;
    		Block blkresult = worldIn.getBlockState(new BlockPos(new BlockPos(mop.getBlockPos().getX(), mop.getBlockPos().getY(), mop.getBlockPos().getZ()))).getBlock();
    	

    	
    	System.out.println(mop.getBlockPos().getX());
    	System.out.println(mop.getBlockPos().getY());
    	System.out.println(mop.getBlockPos().getZ());
    	
    	MinecraftServer s = FMLCommonHandler.instance().getMinecraftServerInstance();
		
    	playerIn.setPosition(mop.getBlockPos().getX(), mop.getBlockPos().getY(),mop.getBlockPos().getZ());
		//s.getCommandManager().executeCommand( s, "/tp " + playerIn.getName() + " " + (mop.getBlockPos().getX() + " " + mop.getBlockPos().getY() + " " + mop.getBlockPos().getZ()));
    	}

    	
    	
    	return new ActionResult<ItemStack>(EnumActionResult.PASS, playerIn.getHeldItem(handIn));
    }

	@Override
	protected RayTraceResult rayTrace(World worldIn, EntityPlayer playerIn, boolean useLiquids)
    {
        float f = playerIn.rotationPitch;
        float f1 = playerIn.rotationYaw;
        double d0 = playerIn.posX;
        double d1 = playerIn.posY + (double)playerIn.getEyeHeight();
        double d2 = playerIn.posZ;
        Vec3d vec3d = new Vec3d(d0, d1, d2);
        float f2 = MathHelper.cos(-f1 * 0.017453292F - (float)Math.PI);
        float f3 = MathHelper.sin(-f1 * 0.017453292F - (float)Math.PI);
        float f4 = -MathHelper.cos(-f * 0.017453292F);
        float f5 = MathHelper.sin(-f * 0.017453292F);
        float f6 = f3 * f4;
        float f7 = f2 * f4;
        double d3 = 200;
        Vec3d vec3d1 = vec3d.addVector((double)f6 * d3, (double)f5 * d3, (double)f7 * d3);
        return worldIn.rayTraceBlocks(vec3d, vec3d1, useLiquids, !useLiquids, false);
    }

 

Edited by Jaycen1000
Full fix
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.