Solved Issues attempting to store floats in a YAML file

Discussion in 'Spigot Plugin Development' started by Vert3x, Apr 17, 2017.

Thread Status:
Not open for further replies.
  1. Hello, I'm making a warping plugin which is supposed to include Yaw and Pitch into stored locations. I'm using a YAML file to store my warps and I have to store yaw and pitch floats too. As many spigot and bukkit users know, getting floats is not possible using bukkit configuration classes, so I tried to accomplish this in two different ways:
    1) Using doubles and casting them to floats:
    Issue: I begun having weird values changes issues, so I thought to go for the second way.
    2) Storing as a String and using Float.parseFloat method to get its value back.
    As setting config values through FileConfiguration#set method doesn't put any quotes at all, this may have caused some issue having the value to be handled as a number; I got back to the same issue of the first way.
    So what's the issue I encountered in both ways? Well, when data is stored negative float values are handled bad and get converted to values much greater than 90 (the maximum value which I get myself set to when I use warps with those wrong numbers stored as yaw or pitch values). There is nothing into my console.
    If you wanna see some code, here it is; perhaps you get something I missed:
    Two methods from WarpStorage.class setting and getting warps.
    Code (Java):

    private static void setWarp(Warp warp) {
        String path = warp.getName();
        storage.set(path + ".location.world", warp.getLocation().getWorld().getName());
        storage.set(path + ".location.x", warp.getLocation().getX());
        storage.set(path + ".location.y", warp.getLocation().getY());
        storage.set(path + ".location.z", warp.getLocation().getZ());
        storage.set(path + ".location.yaw", String.valueOf(warp.getLocation().getYaw()));
        storage.set(path + ".location.pitch", String.valueOf(warp.getLocation().getPitch()));
        storage.set(path + ".id", warp.getId().toString());
        storage.set(path + ".creation-date", warp.getCreationDate().getTime());
    }

    @Nullable
    private static Warp getWarpFromStorage(String name) {
        Warp warp = new Warp(null, null);
        if (storage.get(name + ".location.world") != null &&
                storage.get(name + ".location.x") != null &&
                storage.get(name + ".location.y") != null &&
                storage.get(name + ".location.z") != null &&
                storage.get(name + ".location.yaw") != null &&
                storage.get(name + ".location.pitch") != null &&
                storage.get(name + ".id") != null &&
                storage.get(name + ".creation-date") != null) {
            warp.setName(name);
            warp.setLocation(new Location(
                    Bukkit.getWorld(storage.getString(name + ".location.world")),
                    storage.getDouble(name + ".location.x"),
                    storage.getDouble(name + ".location.y"),
                    storage.getDouble(name + ".location.z"),
                    Float.parseFloat(storage.getString(name + ".location.pitch")),
                    Float.parseFloat(storage.getString(name + ".location.yaw"))
            ));
            warp.setId(UUID.fromString(storage.getString(name + ".id")));
            warp.setCreationDate(new Date(storage.getLong(name + ".creation-date")));
        }
        return warp.getName() == null || warp.getLocation() == null ? null : warp;
    }
     
    In this code you may see the second way I tried.
    So, when I type "/addwarp" (command to create a new warp) and insert a name for the warp, it is working correctly as long as plugin isn't getting disabled and doesn't have to rely on the file storage; but as soon as I disable and enable back the plugin, making its restoring rely on yaml file's contents, it gets wrong values.
    Here is an example of what happens:
    My location and directions are:
    X: -258.497
    Y: 68.00000
    Z: 86.625
    Yaw: -64.7
    Pitch: -28.7
    I type "/addwarp Test" as a Player so that I don't have to type any coordinate and the warp's data are got from me. In-code storage (a simple Array list containing Warp's, pretty easy objects) saves good yaw and pitch (directly from saved location); in-file configuration stores this:
    Code (YAML):

    Test
    :
      location
    :
        world
    : world
        x
    : -258.4965194589647
        y
    : 68.0
        z
    : 86.62512898048469
        yaw
    : 295.2611389160156
        pitch
    : -28.69709014892578
      id
    : d0794278-a5c7-4d7c-beb6-3b93664304e7
      creation-date
    : 1492454029391
     
    Minding this is the String way, the wrong one is yaw; when I was using the double casting way, the wrong one was the pitch, and it's senseless change was potentially the same you can see above.
    I'm pretty confused around this, and this post too is most likely going to be a mess, but I will perhaps get through this some way.. I hope. Thanks in advance for any help!
    Solved edit: There was no actual issue as I stated into the last post of this thread and I was just wronging around my IDE compiling.
     
    #1 Vert3x, Apr 17, 2017
    Last edited: Apr 17, 2017
  2. Im not sure if u noticed but it looks like u got the inverse angle of ur yaw.
    To fix this, while retrieveing your yaw, set retrieved yaw value to:-

    (the yaw value from config) - 360

    That should hopefully give you your correct yaw.
    Hope this helps. :D
     
  3. The problem was completely different. I'm dumb; I kept compiling the wrong plugin every time I was changing the code so I was still trying the double method which mysteriously was wrong. Once I noticed that I didn't even have to subtract 360 to the value, everything was fine and strings were being set with quotes. Sorry for stupidity :D
    Also, @Yugikid1 , thanks anyways :)
     
Thread Status:
Not open for further replies.