Solved How to save 2 locations for an unlimited amount of objects in a data file to then use right after?

Discussion in 'Spigot Plugin Development' started by BourneDev, Apr 25, 2017.

  1. I need to save 2 locations in my data file for each Region object created with commands provided in my plugin.

    I need to save these locations so I can put the regions back the way they were when the server reloads or restarts, (cause this removes the locations from the hashmap).

    I need to save (In order) the name of the region, and the 2 locations.

    No code needs to be provided because where the 2 locations and the name are seperate.

    The name of the region is a String, and I want the 2 locations to be saved in the file in a way so that I can put all regions exactly the way they were when I start the server, so I need them in order.

    How the heck do I do this?

    Is there a more efficient way to do this? If so I would love to hear it.
  2. i believe you mean just plain saving two locations, if so, you could somewhat 'translate' them into a string and read them back and forth when loading/saving.

    Code (Java):

    public Location translateToLocation(String translatedLocation) {
        String[] split = key.split(", ");
        if (split.length == 6) {
            return new Location(getWorld(split[0]), Double.parseDouble(split[1]), Double.parseDouble(split[2]), Double.parseDouble(split[3]), Float.parseFloat(split[5]), Float.parseFloat(split[4]));
        return null;

    public String translateToString(Location location) {
        return location.getWorld().getName() + ", " + location.getX() + ", " + location.getY() + ", " + location.getZ() + ", " + location.getPitch() + ", " + location.getYaw();
  3. Use ConfigurationSections. For example,
    Code (Text):
        position1: world,5,2,5
        position2: world,5,3,5
        position1: world,6,2,2
        position2: world,3,3,9
  4. Try doing this:

    Code (Text):

    Location loc1 = location;
    Location loc2 = location;
    String regionPostion1 = loc1.getX() + " " + loc1.getY() + " " + loc1.getZ();
    String regionPostion2 = loc2.getX() + " " + loc2.getY() + " " + loc2.getZ();

    getConfig().set("regions.TestRegion.pos1", regionPosition1);
    getConfig().set("regions.TestRegion.pos2", regionPosition2);

    Your config should then look like:
    Code (Text):

          pos1: 123 45 123
          pos2: 555 67 12
    I haven't worked with configs for a little while so correct me if I'm wrong but I think that should work.
    #4 SmokingIsBadMkay, Apr 25, 2017
    Last edited: Apr 25, 2017
  5. Thanks guys! I think I am good for now. I'll try to make this when I get home :)
    • Friendly Friendly x 1
  6. Create a region object. Keep the locations there. Serialize your object. When needed, deserialize it. I normally use JSON for saving data like this, and GSON is a perfect tool for deserialzing and serializing json.

    You have to create your own Location object though, implementing Serializable. Shouldn't be that hard though!

    EDIT: I have a serialized object looking as so, with a location.
  7. I finished coding it!
    I used this general idea, and I coded it based on this general idea. Thanks very much for the help everyone though!
    • Friendly Friendly x 1
  8. That's not really the ideal way though.
  9. It isn't much data being written, there are only ever like 3 sections being written when the server is shutting down. Performance wise my plugin doesn't cause any problems, and for lines of code it only took me about 10 in total to make for the onDisable and onEnable combined. It works just fine for me, but maybe if more then just 1-3 sections get added to the file, then I would consider using your method.