Tutorial Converting location to string to store in config

Discussion in 'Resources' started by Hex_27, Dec 24, 2014.

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

    Hex_27

    Some of you may want to store locations as strings. So I see a lot of configs like this:
    Code:
    location:
       x: 1
       y: 5
       z : 10
    And so on. It is actually quite easy to store the location in a single string.


    This is how you save the location.
    Code:
    Location loc = ;//your location
    String name = ;//location name
    String key = loc.getWorld().getName() + " , " +  loc.getX() + " , " + loc.getY() + " , " + loc.getZ() + " , " + loc.getPitch() + " , " + loc.getYaw();
    
    config.set(name , key);
    
    This is how you get it from the config with a simple method:

    Code:
        public Location stringToLocation(String key){
            String[] split = key.split(" , ");
            if(split.length == 6){
            Location loc = new Location(Bukkit.getWorld(split[0]), Double.parseDouble(split[1]), Double.parseDouble(split[2]), Double.parseDouble(split[3]), Float.parseFloat(split[4]), Float.parseFloat(split[5]));
            return loc;
            }else{
                return null;
            }
        
        
        }

    How to use this: For example, when you store a location you name "spawn", to get the location from the config, do
    Code:
    String spawn = config.getString("spawn");
    stringToLocation(spawn);
    Hope this helps :)
     
    Last edited: Dec 24, 2014
  2. Offline

    teej107

    Your method name is not following Java Naming Conventions
     
  3. Offline

    Hex_27

    Okay... I'll change that
     
  4. Offline

    nverdier

    If you want anyone to see your post you have to Tahg or quote them.
     
  5. Offline

    Hex_27

    @nverdier I find it too trival to tag him for that
     
  6. Offline

    nverdier

    Ok fair enough.

    EDIT: Post #300 :p
     
Thread Status:
Not open for further replies.

Share This Page