Solved Setting yaw and pitch

Discussion in 'Plugin Development' started by Markyroson, May 26, 2014.

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

    Markyroson

    Hello, I am trying to set the yaw and pitch of a player for a plugin I am developing. I am trying to make it so if someone does /sethub it sets the hub and if someone does /hub it teleports them to the location set via /sethub. I have managed to do it with out the yaw/pitch/F (whatever that is) but that means taht they teleport facing a random direction. I want them to teleport and face the same direction as when they set the location (did /sethub).

    Can anyone please help?
     
  2. Offline

    flaaghara

  3. Offline

    Markyroson


    I see it but I don't know how I'd incorporated it. Could you give some example code? Or would you like part of mine in which it would be implemented?
     
  4. Offline

    xMrPoi

    Get the player's pitch and yaw when they do /sethub. Save that along with the X Y and Z. When you're teleporting them to the spawn, make a new location including pitch and yaw.

    Code:java
    1. Location loc = new Location(world, x, y, z, pitch, yaw);
     
  5. Offline

    Markyroson

    Code:java
    1. public class sethub implements CommandExecutor{
    2. public static Main plugin;
    3.  
    4. public sethub(Main i)
    5. {
    6. plugin = i;
    7. }
    8. @Override
    9. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    10. if(sender instanceof Player)
    11. if (label.equalsIgnoreCase("sethub"))
    12. {
    13. Player p = (Player)sender;
    14. if (p.hasPermission("ServerTP.sethub"))
    15. {
    16. plugin.getConfig().set("hub_tp_location.world", p.getLocation().getWorld().getName());
    17. plugin.getConfig().set("hub_tp_location.x", Integer.valueOf(p.getLocation().getBlockX()));
    18. plugin.getConfig().set("hub_tp_location.y", Integer.valueOf(p.getLocation().getBlockY()));
    19. plugin.getConfig().set("hub_tp_location.z", Integer.valueOf(p.getLocation().getBlockZ()));
    20. plugin.saveConfig();
    21. p.sendMessage("§6You set the hub.");
    22. }
    23. else
    24. {
    25. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("no_perms_message")));
    26. }
    27. }
    28.  
    29. return false;
    30. }
    31. }
    32.  
    33. The code relating to the class for /sethub is above, do you know how I would implement it in there?
    34.  
    35. Oh, and for actually teleporting to the hub the code is
    36.  
    37.  
    38. [syntax=java]if(sender instanceof Player)
    39. if (label.equalsIgnoreCase("hub"))
    40. {
    41. Player p1 = (Player)sender;
    42. {
    43. String w = plugin.getConfig().getString("hub_tp_location.world");
    44. int x = plugin.getConfig().getInt("hub_tp_location.x");
    45. int y = plugin.getConfig().getInt("hub_tp_location.y");
    46. int z = plugin.getConfig().getInt("hub_tp_location.z");
    47. p1.teleport(new Location(Bukkit.getWorld(w), x, y, z));
    48. p1.sendMessage("§6Teleporting to hub.");
    49. }
    50. }
    51.  
    52.  
    53.  
    54. return false;
    55.  
    56. }
    57.  
    58. }[/syntax]
    59.  
    60. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  6. Markyroson Location has a getYaw() and a getPitch() and, as mentioned, Location has a constructor that accepts yaw and pitch as well. You should be able to figure it out from that.
     
  7. Offline

    xMrPoi

    Markyroson but remember that pitch and yaw are floats so you'll have to make to to set and get them as that.
     
  8. Offline

    Markyroson

    xMrPoi unfortunately I am new to java coding. Could you possibly help me with this and possibly provide an example that would work with the above listed code (or if you have a better way to code it etc)?
     
  9. Offline

    xMrPoi

    Markyroson the getYaw() method returns a float.

    Code:java
    1. float yaw = player.getLocation().getYaw();
    2. float pitch = player.getLocation().getPitch();
     
  10. Offline

    Markyroson

    xMrPoi Where would I put that in my code? Nested in around here? If so where?

    I apologize, I know other languages (html, css, etc) but I am just so new to java.

    Code:java
    1. String w = plugin.getConfig().getString("hub_tp_location.world");
    2. int x = plugin.getConfig().getInt("hub_tp_location.x");
    3. int y = plugin.getConfig().getInt("hub_tp_location.y");
    4. int z = plugin.getConfig().getInt("hub_tp_location.z");
    5. p1.teleport(new Location(Bukkit.getWorld(w), x, y, z));
    6. p1.sendMessage("§6Teleporting to hub.");
     
  11. Offline

    flaaghara

    When you get the player's pitch and yaw using aforementioned methods, you need to cast them to doubles first before saving using a configuration's methods. When loading those doubles, you need to cast them back to flats before constructing the Location again.
     
  12. Offline

    Markyroson

    flaaghara sorry to sound like a clueless person, but i am brand new to this. could I please just have my code modified to add in the pitch and yaw (in the ways as mentioned above) so i can learn from it visually?
     
  13. Offline

    flaaghara

    Code:java
    1. if (p.hasPermission("ServerTP.sethub"))
    2. {
    3. plugin.getConfig().set("hub_tp_location.world", p.getLocation().getWorld().getName());
    4. plugin.getConfig().set("hub_tp_location.x", Integer.valueOf(p.getLocation().getBlockX()));
    5. plugin.getConfig().set("hub_tp_location.y", Integer.valueOf(p.getLocation().getBlockY()));
    6. plugin.getConfig().set("hub_tp_location.z", Integer.valueOf(p.getLocation().getBlockZ()));
    7. plugin.getConfig().set("hub_tp_location.pitch", Double.valueOf((double)p.getLocation().getPitch()));
    8. plugin.getConfig().set("hub_tp_location.yaw", Double.valueOf((double)p.getLocation().getYaw()));
    9. plugin.saveConfig();
    10. p.sendMessage("§6You set the hub.");
    11. }


    Code:java
    1. String w = plugin.getConfig().getString("hub_tp_location.world");
    2. int x = plugin.getConfig().getInt("hub_tp_location.x");
    3. int y = plugin.getConfig().getInt("hub_tp_location.y");
    4. int z = plugin.getConfig().getInt("hub_tp_location.z");
    5. float pitch = (float) plugin.getConfig().getDouble("hub_tp_location.pitch");
    6. float yaw = (float) plugin.getConfig().getDouble("hub_tp_location.yaw");
    7. p1.teleport(new Location(Bukkit.getWorld(w), x, y, z, yaw, pitch));
    8. p1.sendMessage("§6Teleporting to hub.");
     
  14. Offline

    Markyroson

    flaaghara Thank you so much! The code worked and now when someone does /hub it has them face the way you intended it to when you set the hub via /sethub

    xMrPoi Thank you for your help as well.
     
  15. Offline

    rsod

    just... why?
     
  16. Offline

    Markyroson

    rsod why what?

    Mods! This thread is SOLVED. I can't seem to mark it as such myself, could you please possibly?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page