Solved Yaw/Pitch not saving/loading in a Location

Discussion in 'Spigot Plugin Development' started by Killstreak702, Apr 18, 2017.

Thread Status:
Not open for further replies.
  1. Heyo,

    I'm currently making a core, and for some odd reason the Yaw/Pitch isn't included in the Location that I return in a method of mine.

    I've tried fiddling around with the method a bit (such as including the Yaw/Pitch after the World, X, Y, Z in the Location object) but no matter what it only seems to return the World, X, Y and Z, causing the player to not teleport in the proper direction when they are teleported. I've also tried using player.setPitch/player.setYaw but that doesn't work either.

    Method that returns the lobby location:
    Code (Text):
    public Location getLobbyLocation() {
            ConfigurationSection l = getConfig().getConfigurationSection("settings.lobby.location");
            Location loc = new Location(Bukkit.getWorld(l.getString("world")), l.getDouble("X"), l.getDouble("Y"), l.getDouble("Z"));
            loc.setYaw(l.getFloat("Yaw"));
            loc.setPitch(l.getFloat("Pitch"));
            return loc;
            // NOTE: I have also tried returning a new Location with the Yaw and Pitch at the end but it doesn't work. - return new Location(Bukkit.getWorld(l.getString("world")), l.getDouble("X"), l.getDouble("Y"), l.getDouble("Z"), l.getFloat("Yaw"), l.getFloat("Pitch"));
        }
    Method that teleports the player:
    Code (Text):
    public void teleportToLobby(Player player) {
            ConfigurationSection l = getConfig().getConfigurationSection("settings.lobby.location");
            player.teleport(getLobbyLocation());
            // I've also tried setting the player's pitch and yaw manually but it doesn't work.
            //player.getLocation().setPitch(l.getFloat("Pitch"));
            //player.getLocation().setYaw(l.getFloat("Yaw"));
        }
    Method that saves the location (via a command that uses the player's location):
    Code (Text):
    public void setLobbyLocation(Location loc) {
            ConfigurationSection l = getConfig().getConfigurationSection("settings.lobby.location");
            l.set("world", loc.getWorld().getName());
            l.set("X", loc.getX());
            l.set("Y", loc.getY());
            l.set("Z", loc.getZ());
            l.set("Yaw", (float)loc.getYaw());
            l.set("Pitch", (float)loc.getPitch());
            saveConfig();
            reloadConfig();
        }
    Configuration file snippet:
    Code (Text):
    settings:
      [REDACTED]:
        [REDACTED]: 5
      lobby:
        [REDACTED]: true
        location:
          world: [REDACTED]
          X: 67.55909233759466
          Y: 136.0
          Z: 63.66856310099868
          Yaw: 0.3826672
          Pitch: 90.0
    I'm using teleportToLobby(player) to teleport them, I've also tried player.teleport(getLobbyLocation()) but it does the same thing.

    Notes:
    - I've tried casting the X, Y, Z to float but it still didn't work.
    - I've tried casting Yaw/Pitch to double and float but it didn't work.
    - The yaw/pitch does change in the config.yml when the command is executed.
    - When the plugin/server reloads nothing is changed in the config (since the location stuff in the config is updated as the config is reloaded when the location is set).
    - I've tried using different spigots. I tried using a spigot build and then even tried using the PaperSpigot I use in my plugins (that I know should work with plugins because I've made plugins that have Locations with yaw/pitch in them before and they've worked fine) and it didn't change anything.
    - No errors are being thrown in console.

    I'm pretty stumbled here as to why the yaw/pitch isn't saving, the World, X, Y and Z saves perfectly fine and works, just not the Yaw/Pitch. Probably something stupid I'm doing as to why it's not working, I've tried a lot of things to fix it but nothing has worked which I've decided to post here.

    Help would be greatly appreciated :)

    Thanks,
    - Killstreak/Suggesting
     
  2. Hey,
    what does
    Code (Text):
    System.out.println(String.valueOf(l.get("Pitch")));
    do in the getLobbyLocation Methode? Is this code setting the Yaw and Pitch?
    Code (Text):
    player.teleport(new Location(Bukkit.getWorld("world"), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), 0, 0));
     
  3. Both return 0.0 (I was expecting it to return that though since it wouldn't work) http://prntscr.com/exrvj5

    I tried doing this instead and it still didn't set the Yaw/Pitch correctly (both were the same as before, 0.0):
    Code (Text):
    ConfigurationSection l = getCore().getConfig().getConfigurationSection("settings.lobby.location");
            player.teleport(new Location(Bukkit.getWorld(l.getString("world")), l.getDouble("X"), l.getDouble("Y"), l.getDouble("Z"), l.getFloat("Yaw"), l.getFloat("Pitch")));
    I'm pretty sure it's some how not reading the Yaw and Pitch correctly from the config.yml, but if it was then wouldn't it throw a NullPointerException? Still pretty confused as to why this is happening though, the World, X, Y and Z works fine, just not the Yaw and Pitch...
     
  4. Hey,

    try setting the Yaw and Pitch AFTER the teleportation of the player. This should work.
     
  5. Spigot doesn't even have a getFloat method. Try using getDouble() and cast that to float. If that works, blame whoever tried to add getFloat and failed.
     
  6. Already tried that, didn't work. I just found a fix for this whilst fiddling around more, apparently casting float to getDouble() for the Yaw and Pitch works fine (which is pretty weird because Yaw and Pitch is considered a Float)

    Code (Text):
    ConfigurationSection l = getCore().getConfig().getConfigurationSection("settings.lobby.location");
            player.teleport(new Location(Bukkit.getWorld(l.getString("world")), l.getDouble("X"), l.getDouble("Y"), l.getDouble("Z"), (float)l.getDouble("Yaw"), (float)l.getDouble("Pitch")));
     
  7. Yeah, I completely blame whoever tried to add getFloat() lmao, I mean at least I know now but I was literally wondering why it wouldn't work for ages lmao.
     
Thread Status:
Not open for further replies.