Save a location in config and get it

Discussion in 'Plugin Development' started by ArthurHoeke, Apr 19, 2014.

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

    ArthurHoeke

    Hello
    How to save a location, And get the location back from the config?
     
  2. Offline

    JBoss925

    If you're just looking for one location, you can do:
    Code:java
    1. this.getConfig().setInt("x", p.getLocation().getBlockX());
    2. this.getConfig().setInt("y", p.getLocation().getBlockY());
    3. this.getConfig().setInt("z", p.getLocation().getBlockZ());

    then to get it do:
    Code:java
    1. this.getConfig().getInt("x");
    2. this.getConfig().getInt("y");
    3. this.getConfig().getInt("z");
     
  3. Offline

    ArthurHoeke

    JBoss925 And in my onenable? I saw i need some code there
    And how to make a location from it when you get it?
     
  4. Offline

    JBoss925


    Code:java
    1. new Location(p.getWorld, this.getConfig().getInt("x"), this.getConfig().getInt("y"), this.getConfig().getInt("z"));
     
  5. Offline

    ArthurHoeke

    JBoss925 But i need to put some code in the onenable to load up the config i saw
     
  6. Offline

    1Rogue

    Just save the vector:

    Code:java
    1. Location loc;//location to serialize
    2. FileConfiguration yourConfig;//should reference whatever configuration file you're using
    3. yourConfig.set("some-location", loc.getVector());
    4. Vector v = (Vector) yourConfig.get("some-location");
    5. Location configLocation = v.toLocation(/*pass a world object*/);


    You'll need to either keep track of the world yourself (serialize the world's UUID) or supply it to the Vector#toLocation, but that's the gist of it.
     
    TigerHix likes this.
  7. Offline

    coasterman10

    This is a strong indicator that you are looking to be spoon-fed. Never program from the perspective of what code you need to put, but instead what you need to do, and then from there extrapolate the necessary code. There is no such thing as magic code or a specific bunch of code that magically makes things work.

    Anyways, we can help you with parts of loading the config, but we are not going to spoon-feed you the code if you don't understand what it does and instead intend to blindly copy+paste it.
     
    ever_x and TigerHix like this.
  8. Offline

    ArthurHoeke

    coasterman10 I I do not need a whole sermon about this.... Its only a question...
     
  9. Offline

    coasterman10

    ArthurHoeke In that case, your question appears to already have been answered.
     
    TigerHix likes this.
  10. Offline

    TigerHix

    @1Rogue's answer is correct and simple, but if you still don't understand, add following methods into your class (Better create a new Utils class).

    Code:java
    1. public static Location stringToLocation(String string) {
    2. if (string.split(",").length != 5 && string.split(",").length != 3) return null;
    3. return string == null ? null : string.split(",").length == 5
    4. ? new Location(Bukkit.getWorld(string.split("@")[0]), Double.parseDouble(string.split("@")[1].split(",")[0]), Double.parseDouble(string.split(",")[1]), Double.parseDouble(string.split(",")[2]), Float.parseFloat(string.split(",")[3]), Float.parseFloat(string.split(",")[4]))
    5. : new Location(Bukkit.getWorld(string.split("@")[0]), Double.parseDouble(string.split("@")[1].split(",")[0]), Double.parseDouble(string.split(",")[1]), Double.parseDouble(string.split(",")[2]));
    6. }
    7.  
    8. public static String locationToString(Location location) {
    9. return (location.getYaw() == 0 && location.getPitch() == 0)
    10. ? location.getWorld().getName() + "@" + location.getX() + "," + location.getY() + "," + location.getZ()
    11. : location.getWorld().getName() + "@" + location.getX() + "," + location.getY() + "," + location.getZ() + "," + location.getYaw() + "," + location.getPitch();
    12. }


    To save a location into a string, use

    Code:java
    1. Location location = new Location(Bukkit.getWorld("world"), 1.0, 2.0, 3.0, 0, 0);
    2. config.set("location", locationToString(location));


    You should get following things in your config (of course! you need to saveConfig(); first.)

    location: [email protected],2.0,3.0,0.0,0.0

    To load a string into a location, use

    Code:java
    1. Location location = stringToLocation(config.getString("location"));


    I have to admit that this is kind-of spoon-fed, so please learn the code yourself and try to understand how it works. If you have question you can ask me.
     
Thread Status:
Not open for further replies.

Share This Page