1.15.2 Teleport player in front of Player after being hooked

Discussion in 'Spigot Plugin Development' started by Xsm0deus, Jan 26, 2020.

  1. Hi, so I've written this code:


    Code (Text):
    @EventHandler
        public void fishingHookPlayer(PlayerFishEvent e) {
            Player p = e.getPlayer();
            if(e.getCaught() instanceof Player) {
                Location eyeL = p.getEyeLocation();
                Vector vec = p.getLocation().getDirection();
                Location frontLocation = eyeL.add(vec);
                frontLocation.setDirection(frontLocation.getDirection().multiply(-1));
               
                Player caught = (Player) e.getCaught();
                caught.teleport(frontLocation);
            }
        }
    It works most of the time but if you're like at an angle when you fish someone it teleports them behind you or moves them behind you rather than directly in front of you. Any idea how to fix? I just want the location in front of the player, but not above or below them
     
  2. Probably because Location#getDirection() also takes into account the pitch of the player...
    You can calculate the 2D-rotation using this formula:
    Code (Java):
    double yaw = location.getYaw();
    double x = Math.sin(Math.toRadians(yaw));
    double z = Math.cos(Math.toRadians(yaw));
    Vector inFront = new Vector(x, 0, z);
    Be careful thou; any of these numbers might have to be multiplied with -1 because of the implementation of yaw & pitch...
     
  3. So I wrote this but it's not giving the desired tp right in front of someone, people still end up behind me

    Code (Text):
    double yaw = p.getEyeLocation().getYaw();
                double x = Math.sin(Math.toRadians(yaw));
                double z = Math.cos(Math.toRadians(yaw));
                Vector inFront = new Vector(x, 0, z);
                Location frontLocation = p.getEyeLocation().add(inFront.multiply(-1));
               
                //frontLocation.setDirection(frontLocation.getDirection().multiply(-1));
               
                Player caught = (Player) e.getCaught();
                caught.teleport(frontLocation);