Jump to content

Recommended Posts

Posted (edited)

Hello!
RayTracing is still a very new concept to me so this code may be very bad.

I am making a mod that adds an enchantment that is supposed to teleport the player where the are looking when holding a certain item, enderdust.

Currently it works, however the teleportation is very janky. I get teleported backards, side to side, into blocks, and almost never directly where I am looking.

Any help would be appreciated.

 

Code for the teleportation event:

public class TeleBootEvent {

    @SubscribeEvent
    public void teleBoot(PlayerInteractEvent.RightClickItem event) {
        PlayerEntity player = event.getPlayer();
        Vector3d eyePos = player.getEyePosition(1.0F);
        BlockRayTraceResult result = player.getEntityWorld().rayTraceBlocks(new RayTraceContext(eyePos, player.getLookVec(), RayTraceContext.BlockMode.COLLIDER, RayTraceContext.FluidMode.NONE, player));
        double x = result.getPos().getX();
        double y = result.getPos().getY();
        double z = result.getPos().getZ();

        ItemStack armor = player.getItemStackFromSlot(EquipmentSlotType.FEET);
        ItemStack enderdust = new ItemStack(ObjectHolderStorage.ENDERDUST);

        if (EnchantmentHelper.getEnchantmentLevel(ObjectHolderStorage.TELEBLINK, armor) > 0) {
            if (result.getType() == RayTraceResult.Type.BLOCK) {
                if (player.getHeldItemMainhand().isItemEqual(enderdust)) {
                    player.setPosition(x, y + 0.5, z);
                }
            }
        }
    }
}

 

Edited by Giga_5
Posted

1st: In 1.15 the 2nd parameter of RayTraceContext#new was not a direction, but rather another position. If that is still the case, then in your case you would have to do something like:

new RayTraceContext(eyePos, eyePos.add(player.getLookVec().scale(RANGE)), RayTraceContext.BlockMode.COLLIDER, RayTraceContext.FluidMode.NONE, player)

 

2nd: Out of curiosity: Why are you using ItemStack#isItemEqual instead of just comparing the items? Is NBT important here?

Posted

Ahh it worked! thank you for the help.

5 hours ago, CAS_ual_TY said:

2nd: Out of curiosity: Why are you using ItemStack#isItemEqual instead of just comparing the items? Is NBT important here?

I plan on adding different "types" of enderdust soon to teleport different lengths, I just havent done the NBT stuff yet

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.