Saving X,Y,and Z coordinates.

Discussion in 'Plugin Development' started by auhsoj_, Oct 26, 2012.

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

    auhsoj_

    I know. You looked at the topic and said "Ha! That's so easy!". Well, you probably did. If your good. But, unfortunately for me, I am not exactly the most experienced programmer you'll ever mthateet, so this is unknown to me. So, how could I have it so that is someone does /savecoordinate or something like that so that it saves it to a file that can be accessed in another session?
     
  2. Offline

    lokpique

    You could always just use essentials and create a warp point for the coordinates. It actually saves them as a text file too, per warp.

    What are you trying to do with these coordinates when you have them saved?
     
  3. Offline

    WoodMKII

    If you want to do this in a plugin, you can store them in a string (or even in your own custom object) then you can just append the string to a text file. :)
    i.e.

    String locationToString(Location loc){
    return loc.getWorld().getName() + "," + loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ();
    }

    Location stringToLocation(String loc){
    String pieces[] = loc.split(",");
    if(pieces.length != 4)
    return null;

    World w = Bukkit.getWorld(pieces[0]);

    if(w == null)
    return null;

    int x = 0, y = 0, z = 0;

    try{
    x = Integer.parseInt(pieces[1]);
    y = Integer.parseInt(pieces[2]);
    z = Integer.parseInt(pieces[3]);
    }catch(NumberFormatException e){
    return null;
    }

    return new Location(w, x, y, z);
    }

    Although if you used this, when you turn the String to a Location, you'll have to do a null check, as you can't use a location that equals null. (You could also add in values for float and yaw)
     
    fireblast709 and auhsoj_ like this.
  4. Offline

    auhsoj_

    Hmm... I like it, but how would I store multiples? Would I make a separate text file for each? Or should I skip YAML altogether and go toMySQL? Forgive me if im asking too much, but you seem to know a lot :)
     
  5. Offline

    WoodMKII

    If you want to, you can store each location with a key, like a waypoint. I suggest that you load the file into a hashmap on the plugin enable, and save them on disable. You don't have to save each location in a new file, you can simply have each location on a new line. So, for example, if you wanted a command like /savecoordinate (Although perhaps /wp new or /wp create might be easier) you could have the argument /savecoordinate [Location Name]

    for example:
    Code:
    HashMap<String, String> locations = new HashMap<String, String>();
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
     
    if(!(sender instanceof Player){
    sender.sendMessage("That command is only available ingame");
    return true;
    }
     
    Player player = (Player) sender;
     
    if(commandLabel.equalsIgnoreCase("savecoordinate"){
    if(args.length != 1){
    player.sendMessage("Usage: /savecoordinate [Location Name] ");
    return true;
    }
    String key = args[0].toLowerCase();
    Location playerLocation = player.getLocation();
    String location = locationToString(playerLocation); //From above example
    locations.put(key, location);
    player.sendMessage("Your location has been saved");
    return true;
    }
     
    //You may also want a command to return to the location saved before
    if(commandLabel.equalsIgnoreCase("loadcoordinate"){
    if(args.length != 1){
    player.sendMessage("Usage: /loadcoordinate [Location Name] ");
    return true;
    }
     
    String key = args[0].toLowerCase();
     
    if(!locations.containsKey(key)){
    player.sendMessage("Location not found");
    return true;
    }
     
    Location loc = stringToLocation(locations.get(key)); //Also from last post
     
    if(loc == null){
    player.sendMessage("Error with saved location, check savefile on plugin disable");
    return true;
    }
     
    player.sendMessage("Sending you to location " + key);
    player.teleport(location);
    return true;
    }
     
    }
    Although you probably didn't need all that :p

    Anyway, in onEnable you can load your text file with a method like so:

    Code:
    public void loadLocations(){
    File file = new File(path); //Where path is the path to the text file containing the locations, ie "locations.txt"
    if(!file.exists()){
    Bukkit.getLogger().info("Failed to load locations file.");
    return;
    }
     
    try{
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;
    while((line = br.readLine()) != null){
    String pieces[] = line.split(":");
    if(pieces.length != 2){ //If the line does not follow the format key:location it won't be loaded
    continue;
    }
    locations.put(pieces[0], pieces[1]); //locations is the HashMap from the above example
    }
    Bukkit.getLogger().info("Locations loaded.");
    }catch(Exception e){
    Bukkit.getLogger().severe("Failed to read locations file: " + e.getMessage());
    }
    }
    also in onDisable, or whenever you want to save your locations you could use something like:

    Code:
    public void saveLocations(){
    try{
    BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path), true)); //Where path is once again the path to the locations file
    for(String key : locations.keySet()){
    writer.write(key + ":" + locations.get(key));
    writer.flush();
    }
    writer.close();
    Bukkit.getLogger().info("Locations saved.");
    }catch(Exception e){
    Bukkit.getLogger().severe("Failed to write to locations file: " + e.getMessage());
    }
    }
    Hope this helps :) if you want to do something like having different players have different locations, you'll have to make up a class to store them in.

    Also, i haven't done much on permissions, so i'll leave them to you ;)
     
    auhsoj_ likes this.
  6. Offline

    auhsoj_

    Dude, I swear. You are fricking awsome!
     
  7. Offline

    auhsoj_

    Hey, Wood, I tried it out, and in runs pretty smoothly for 1 saved location, but It doesn't save more than one (More than 1 save results in them saving next to each other, which causes errors. I tried using "/n", but it doesn't work. Do you have any suggestions?
     
  8. Offline

    WoodMKII

    lol, sorry i forgot. Try changing the line
    writer.write(key + ":" + locations.get(key));
    to:
    writer.append(key + ":" + locations.get(key) + "\n");
    or:
    writer.append(key + ":" + locations.get(key) + "\n\r");
    although i think either will work.
    :)

    EDIT: Also, i wasn't watching this post, if you quote me, or tag me I'll get an alert :)
     
    auhsoj_ likes this.
  9. Offline

    auhsoj_

    Well, its odd.( i used /n/r) The file dosen't end up printing on a new line, but it works. Thanks, man.

    I have another question about something else related to this. (Btw, your code is amazing. Thanks) If I wanted to turn warps (thats not what they'll actually be, but its a good example)on and off, I would I keep track of on and offs? I assume you would use hashmapping, but im not very good at that .:) So how would you do it?​

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  10. Offline

    Butkicker12

    auhsoj_ Moved to correct forum
     
  11. Offline

    fireblast709

    WoodMKII why not using YamlConfiguration instead of a own defined HashMap?
    auhsoj_ use an ArrayList to store the warpnames that are 'on'
    Code:java
    1. // Define the ArrayList somewhere
    2. public static ArrayList<String> enabledWarps = new ArrayList<String>();
    3. // To enable one, the following line
    4. enabledWarps.add("warpname");
    5. // And to disable one
    6. if(enabledWarps.contains("warpname"))
    7. {
    8. enabledWarps.remove("warpname");
    9. }
    10. // To check if it is enabled
    11. if(enabledWarps.contains("warpname"))
    12.  
     
    auhsoj_ likes this.
  12. Offline

    auhsoj_

    Thanks. I should have thought of that :)
     
  13. Offline

    JJSfluffyduck

    Just if you need more help check out "thebcbroz" on YouTube and his plugin tutorial 43 it all about this stuff
     
  14. Offline

    WoodMKII

    lol, I think your problem might be that you are using forward slash, "/", instead of backslash "\" :)
     
  15. Offline

    auhsoj_

    Oh. Yeah, your right :oops:
     
Thread Status:
Not open for further replies.

Share This Page