Teleporting player one block foward?

Discussion in 'Plugin Development' started by Anonomoose, Mar 27, 2014.

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

    Anonomoose

    Hi everyone,

    Is it possible to teleport the player 1 block forward in the direction they're facing when a certain event happens? I've got the event all set up, I just can't figure out how to teleport them forward.

    Thanks!
     
  2. Offline

    Serilum

    Just get the player location, add 1 to the X/Z (depending on which you want to change) and teleport the player to that location.

    Edit: To teleport to the direction the player is facing, you'd need to get the facing direction. This'll be with the numbers 0-3, get the lines if(int.equals(0)) { //code } etc. And add the X/Z
     
  3. Offline

    2MBKindiegames

    First you get the direction with this:
    Code:java
    1. public static String getCardinalDirection(Player player) {
    2. double rotation = (player.getLocation().getYaw() - 90) % 360;
    3. if (rotation < 0) {
    4. rotation += 360.0;
    5. }
    6. if (0 <= rotation && rotation < 22.5) {
    7. return "N";
    8. } else if (22.5 <= rotation && rotation < 67.5) {
    9. return "NE";
    10. } else if (67.5 <= rotation && rotation < 112.5) {
    11. return "E";
    12. } else if (112.5 <= rotation && rotation < 157.5) {
    13. return "SE";
    14. } else if (157.5 <= rotation && rotation < 202.5) {
    15. return "S";
    16. } else if (202.5 <= rotation && rotation < 247.5) {
    17. return "SW";
    18. } else if (247.5 <= rotation && rotation < 292.5) {
    19. return "W";
    20. } else if (292.5 <= rotation && rotation < 337.5) {
    21. return "NW";
    22. } else if (337.5 <= rotation && rotation < 360.0) {
    23. return "N";
    24. } else {
    25. return null;
    26. }
    27. }


    and then you use that to get the blocks like this:
    Code:java
    1. Player player;
    2. Location loc = player.getLocation();
    3. switch (getCardinalDirection(Player player)) {
    4. case ("N") :
    5. loc.add(0, 0, -1);
    6. break;
    7. case ("E") :
    8. loc.add(1, 0, 0);
    9. break;
    10. case ("S") :
    11. loc.add(0, 0, 1);
    12. break;
    13. case ("W") :
    14. loc.add(-1, 0, 0);
    15. break;
    16. case ("NE") :
    17. loc.add(1, 0, -1);
    18. break;
    19. case ("SE") :
    20. loc.add(1, 0, 1);
    21. break;
    22. case ("NW") :
    23. loc.add(-1, 0, -1);
    24. break;
    25. case ("S") :
    26. loc.add(-1, 0, 1);
    27. break;
    28. }
    29. player.setLocation(loc);
     
Thread Status:
Not open for further replies.

Share This Page