Preserving player look direction after teleporting

Discussion in 'Plugin Development' started by supersonicsauce, Feb 18, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    supersonicsauce

    Hey guys,
    I'm trying to make a plugin that teleports a player to the block they're looking at. Problem is, no matter what direction they're facing, they always face south after being teleported. Is there any way to preserve the direction they're look towards after they teleport?
    Also, my code only lets them teleport forward about 5 blocks. How can I increase this?

    Code:java
    1. }else if(commandLabel.equalsIgnoreCase("blink")){
    2. @SuppressWarnings("deprecation")
    3. Block b = player.getTargetBlock(null, 5);
    4. Location look = b.getLocation();
    5. player.teleport(look);
    6. player.sendMessage("You blink.");
    7. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 40, 1000));
    8.  
    9. }
    10.  
     
  2. Offline

    NathanWolf

    Just set the pitch/yaw before teleporting, like:

    Code:
    Location look = b.getLocation();
    // ...
    look.setYaw(player.getLocation().getYaw());
    look.setPitch(player.getLocation().getPitch());
    // ...
    player.teleport(look);
     
    _Yooxa_ likes this.
  3. Offline

    Jaker232

    You shouldn't use deprecated methods, they may be removed later.
    Anyways, like NathanWolf said, save the player's location before teleport in a variable, then after teleporting, set the player's pitch and yaw.
     
  4. Offline

    supersonicsauce

    Jaker232
    Is there an alternative to that method that exists already that I can use?
     
  5. Offline

    Jaker232

    I have no idea what line is deprecated.

    After a quick implementation, the method .getTargetBlock(HashSet<Byte>, int) appears to be deprecated. You can try a BlockIterator as stated in this thread.
     
  6. Offline

    kenkojuko

    Does NathanWolf's code work for forge mods? Because I can't find anything about that.
     
  7. Offline

    MOMOTHEREAL

  8. Offline

    NathanWolf


    Yeah you probably ought to not necrobump old threads to ask non-Bukkit questions.

    That said, this is all very standard API stuff, so it should work in any implementation of the Bukkit API, including MCPC... if you're talking about Forge client mods or whatever, then yeah way wrong forums and I have no idea.
     
Thread Status:
Not open for further replies.

Share This Page