Better saving and retrieving location from config

Discussion in 'Resources' started by hawkfalcon, Jul 2, 2013.

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

    hawkfalcon

    finalblade1234 recently posted a way that he sets and gets locations from a config, but it didn't support yaw or pitch. This is the way I do it:
    Save:
    Code:java
    1. public void saveLoc(Location loc) {
    2. String location = loc.getWorld().getName() + "," + loc.getX() + "," + loc.getY() + "," + loc.getZ() + "," + loc.getYaw() + "," + loc.getPitch();
    3. plugin.getConfig().set("location", location);
    4. plugin.saveConfig();
    5. }


    Get:
    Code:java
    1. public Location getLocation() {
    2. String[] loc = plugin.getConfig().getString("location").split("\\,");
    3. World w = Bukkit.getWorld(loc[0]);
    4. Double x = Double.parseDouble(loc[1]);
    5. Double y = Double.parseDouble(loc[2]);
    6. Double z = Double.parseDouble(loc[3]);
    7. float yaw = Float.parseFloat(loc[4]);
    8. float pitch = Float.parseFloat(loc[5]);
    9. Location location = new Location(w, x, y, z, yaw, pitch);
    10. return location;
    11. }
     
  2. Offline

    AstramG

    Thanks man, this will be useful for my plugin! :)
     
    hawkfalcon likes this.
  3. Offline

    lol768

    Make sure to check that the world isn't null and that all the values you're parsing are actually what you expect.

    Also check the length of that string array.
     
  4. Offline

    hawkfalcon

    If you store it, it will never be null.
     
  5. Offline

    lol768

    Sure it can. What if stupid user decides to delete the world in between it being stored and being retrieved?

    Assume users will mess up the config and change your stored values, too.
     
    hawkfalcon likes this.
  6. Offline

    foodyling

    lol768
    Code:
        public void setLocation(ConfigurationSection section, String path, Location location) {
            section.set(path, String.format("%s,%s,%s,%s,%s,%s",
                    location.getWorld(), location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()
                ));
        }
       
        public Location getLocation(ConfigurationSection section, String path) {
            if (section.contains(path) && section.isString(path)) {
                try {
                    String[] location = section.getString(path).split(",");
                    if (location.length == 6 && Bukkit.getWorld(location[0]) != null) {
                        return new Location(
                                Bukkit.getWorld(location[0]),
                                Double.parseDouble(location[1]),
                                Double.parseDouble(location[2]),
                                Double.parseDouble(location[3]),
                                Float.parseFloat(location[4]),
                                Float.parseFloat(location[5])
                            );
                    }
                } catch (Exception error) {
                   
                }
            }
            return null;
        }
    Something probably like this?
     
    hawkfalcon likes this.
  7. Offline

    lol768

    That'd work. My only concern is what characters can be included in a world name? If they can put commas in the name they'll screw up your string splitting. Maybe something like this would work better.
    {"world" : "hungergames", "x" : 5, "y": 65, "z": 255, "yaw": 1.748, "pitch": 1.489}
     
  8. Offline

    foodyling

    Maybe using '|' instead of a ','? It's pretty much impossible to set a file containing '|' in it.
     
  9. Offline

    chasechocolate

    I know it's not as compact, but you could also just use a configuration section for each location and have world, x, y, z, pitch, yaw sub-values in it.
     
    lol768 likes this.
  10. Offline

    hawkfalcon

    like location:
    x: 0
    y: 0...
    Yeah, I like this due to it only taking up one line.
    Sample output:
    1. lobby_spawn: world,922.0388790007195,84.0,779.5382056660741,0,0
     
    foodyling likes this.
  11. Offline

    foodyling

    You might be able to just round the coordinates off to the third digit, since there isn't a huge noticeable difference with a .0001 change
     
  12. Offline

    hawkfalcon

    I do.
     
    Jozeth likes this.
Thread Status:
Not open for further replies.

Share This Page