Resource Save & Get Locations [File Configuration]

Discussion in 'Spigot Plugin Development' started by Nosmakos, Feb 27, 2018.

  1. Hello Spigot.

    I want to help some people who are doing the same basic mistakes as everyone on how they save & get a Location from a configuration file.

    I am not going to lie but I used to do the same mistake as everyone.

    Most of you people trying to save a Location by setting it's methods.

    #1 Example - Saving a Location with a really wrong way.

    Code (Text):
    getConfig().set("world", location.getWorld().getName());
    getConfig().set("x", location.getX());
    getConfig().set("y", location.getY());
    getConfig().set("z", location.getZ());
    getConfig().set("pitch", location.getPitch());
    getConfig().set("yaw", location.getYaw());

    And then, to get this Location, you are getting each one of the values you saved and returning a new Location.

    Thankfully, there is a way to avoid all of this and it takes no time.

    To save a player's location:

    #2 Example - Saving a Location properly.

    Code (Text):
    getConfig().set("path", location);
    #3 Example - Getting a Location properly.

    Code (Text):
    Location location = (Location) getConfig().get("path");
    Since getConfig().get("path") returns an object, we must cast it as a Location.

    Thank you, i hope I can help some people who are still doing this and improve their code.
    #1 Nosmakos, Feb 27, 2018
    Last edited: Jun 14, 2018
    • Useful Useful x 8
    • Winner Winner x 3
    • Like Like x 2
  2. Amazing! Thanks for this thread, will make good use of this in the future!
    • Friendly Friendly x 1
  3. Thank you Spimy!
  4. A really nice post, I'm sure many people will appreciate it! +1
    • Friendly Friendly x 1
  5. Thank you for your kindness! That's why I made it. I am sure many people doing the same thing, thats why it was better for me to share it.
  6. Okay I got a question it turns out I'm one of the ones who saves it wrong way. I save a bunch of a locations in a custom fyl file in the format of "name: location", so I could just use this simple method as saving with the name and location and just get the name (name would be path such as Home: home location) and the home location would be the player location at that time.
  7. Yes, you can work around it as like you want. It returns the path, that means you can return any path you want it to return.
    • Like Like x 1
  8. Alright thanks, this will definitely let me clean up some code.
    • Friendly Friendly x 1
    • Friendly Friendly x 1
  9. Nice edit on that ^
    • Funny Funny x 2