Solved Saving locations to a file and reteriving

Discussion in 'Spigot Plugin Development' started by MimoBot, Jun 28, 2016.

  1. I am making a plugin in which the player can set and point in-game (with a name), I want to be able to save the location and name that the player gave it for further use. How would I do this and how would I go about retrieving that data later?

    This is how I would like to format my command in-game:

    /pf setpoint <Point Name> - Point name being a string that is saved with the location.

    I have been looking at the location class and I believe Ill have to something the with serialization/ deserialization but I am not sure how to implement it. Here is the doc I am referencing:
  2. You don't need serialization for this, i believe you can use spigots FileConfiguration

    And then you would just store the location world, x, y, and z, and check it when necessary.
    • Useful Useful x 1
  3. I have this simple location util I use whenever I need to save a location. This is for saving it in a config though, it's better to use a seperate YML file which I usually just edit that for when I need it.
    • Useful Useful x 1
  4. Well if you don't care about it being readable in your config file, you can just use config.set(pathName, location);
    Then to retrieve it, you would use: Location loc = (Location) config.get(pathName);

    If you want it to be readable in your config file, then you'd have to save each part of the data you need under a slightly different path.
    config.set(pathName + ".X", loc.getX());
    Once you've done that, you would have to recreate the Location object with your saved data like this:
    Location loc = new Location(Bukkit.getWorld(worldName, x, y, z));

    Location API:
  5. Is there a way to save it a file other then the config file? A new file, just to keep it separate?
    • Optimistic Optimistic x 1
  6. Absolutely! I have my own Config API that I use to make it easier for me to manage multiple config/data storage files. With some quick googling, I'm sure you can find something. If not, make another thread for multiple configs and people will help.
  7. I reported him three times 0_0
  8. Yes, you could create another YAML file.

    Good thing we're recruiting more forum staff

    EDIT: annnddd, he's gone :)
    • Like Like x 1
  9. Also, sorry I might not be following this correctly, is there anything I have to do when using the config.set? Is that automatically implemented or do I have do something else?
  10. using set just stores the object to a given path.

    For example:
    Code (Java):
    myConfig.set("users.locations." + locID, location);
    The "set" method is already inherited by FileConfiguration, so you do not have to implement anything in your class.
  11. You have to save the file after you make a change to it also locations are serializable which means you can just read and write them to files without saving each part of a location object every time

  12. Ok, so I got this far with it, I have it where I thought the code would work but each time I try the command it returns "An internal error has occurred while attempting to preform this command."

    This is the code I have so far:
    This is at the top of my class, just to make it easier to call later on, instead of typing out this.getConfig all the time:
    Code (Java):

    FileConfiguration file = this.getConfig();

    In my onEnable() method:
    Code (Java):

    file.addDefault(name, location);

    The code for actually setting the point (bare in mind that there will be a checker for used names, I just haven't gotten to that yet):
    Code (Java):

    else if(args[0].equalsIgnoreCase("setpoint")) //Sets a point at the player's current location with a name
                                String name = args[1];
                                Location location = player.getLocation();
                                file.set("users.locations." + name, location);
                                ((Plugin) file).saveConfig();
                                player.sendMessage(ChatColor.AQUA + name + " was successfully saved.");
    I can't figure out whats going wrong, I am currently trying to use the default config file as I think it will be easier for now. The code compiles fine but it gives up once in in-game. Its really bugging me cause I know Im really close to getting it.