How to save a location

Discussion in 'Plugin Development' started by Emphytos, Dec 1, 2011.

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

    Emphytos

    Hi every body,
    I want to create a jail plugin for my server and I know how to get Location to set the spawn of the jail but with a restart or a reload the spawn point it's not save.
    How can I save the location in .txt or .yml file ?
    Thanks
    Emphytos
     
  2. Offline

    halley

    I would form the string this way:

    Code:
    Location loc = player.getLocation(); // or whatever other method
    String recorded = loc.getWorld().getName() + "/" +
       loc.getX() + "/" +
       loc.getY() + "/" +
       loc.getZ();
    
    Then you could break the string and make a location later:

    Code:
    String recorded = getConfig().get("location", null); // or whatever other method
    String[] parts = recorded.split("/");
    Location loc = null;
    try
    {
        World world = getServer().getWorld(parts[0]);
        loc = world.getBlockAt( Double.valueOf(parts[1]),
                                Double.valueOf(parts[2]),
                                Double.valueOf(parts[3]) );
    }
    catch (Exception e)
    {
        // what to do if you could not figure out what location they meant
    }
    
    (Untested code, I probably forgot an argument or misspelled some method somewhere.)
     
  3. Offline

    Drakonix

    You can use HashMaps
    Code:JAVA
    1.  
    2. Map<String,Location> locs = new HashMap<String,Location>();
    3. //To put value in hashmap.
    4. locs.put("NameOfJail", player.getLocation());
    5.  
    6. //to Check if jail is actually registred
    7. if(!locs.containsKey("Jailname"){
    8. }else{
    9. //This prison is already registred.
    10. }
    11. //To remove from hashmap
    12. locs.remove("NameOfJail")
    13.  
    14.  


    You can load/save hashmaps to txt files using SLAPI(thanks to Tomsik68)

    Which you can find http://wiki.bukkit.org/Plugin_Tutorial
     
  4. Offline

    halley

    ObjectOutputStream is not for human-readable/editable text content. You should not use that function to put things into files that admins may want to edit as text content.

    You also shouldn't just make a hash table just to achieve the goal of writing a single location to a file. If you want to save one Location to a binary file, then you can directly use the "SLAPI" technique (shared on the wiki as Drakonix suggested) for any individual serializable object.
     
    Drakonix likes this.
  5. You can just store the location through a Vector and the world name through the "new" configuration API.
     
  6. Offline

    Drakonix

    It would be hard if you want more than 1 location.
     
  7. Offline

    Emphytos

    Yes but in your code I don't see where is the .txt or .yml save..
     
  8. Offline

    Sagacious_Zed Bukkit Docs

  9. Offline

    Emphytos

    Okay I will try it.
     
Thread Status:
Not open for further replies.

Share This Page