Make org.bukkit.Location serializable

Discussion in 'Bukkit Discussion' started by SergeantH, Jan 25, 2012.

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


    The title is pretty self explanatory really. Simply put, I have many times come across the problem of having to store some locations in a file, and I have had to store all the data in a text file or create my own object just to be able to store the data in a file that can be read later on.

    As it would be so much easier to serialize the object, I was just thinking:
    "Why doesn't the implementation of Bukkit not have this as is?".
    I assume that the answer is "Because we didn't think of it."

    Hence this post :)

    If there is some reason why this has not been done then could someone please explain it to me? :p

    But yeah, that's about it. Do let me know if anyone thinks of anything to add.
  2. Offline


    The location holds the world object afaik, and you can't serialize the world :p
    It could be changed to hold a string object with the name of the world, but it could potentially break things...
  3. Offline


    I would also appreciate this being done is possible.
  4. Offline


    AHHHHH! :p That makes sense! :D

    Maybe there can just be a SerializableLocation class that has a couple of static methods that allow you to obtain an instance of the class that has the same function as the Location object but just holds the world as a string.
    Then when you want the Location object back, use the other static method and pass in the world object or just give it a list of worlds on the server and have it choose the one with the same name.
  5. Offline


    That's a good idea. Except the Location would have to extend SerializableLocation... :p
  6. Offline


    Moved to Bukkit Project and Community Feedback.
  7. Offline


  8. Offline


    That is almost an exact copy of what I did as a my workaround to the problem! :p
    I just named the return function toLocation(World[]) and just had it search through the worlds on the server and throw an Exception if it failed.
    I had no idea that you could use Bukkit.getWorld(String) :)

    Personally, I wouldn't mind if SerializableLocation was just added into the API for ease of use so that the likes of us don't have to keep typing it every time. Granted, I know you could just copy the class over each time, or even have your own jar of 'tools' but "ease of use" for the API would certainly benefit -- particularly for those who are less experienced programmers in Java.
  9. Offline


    So what would actually break if Location were to implement ConfigurationSerializable, and the following two methods were added:
    public Map<String,Objectserialize() {
    Map<String,Object= new HashMap<String,Object>();
    public static 
    Location deserialize(Map<String,Objectm) {
    World w Bukkit.getServer().getWorld((String) m.get("world"));
      if (
    == null) {
        throw new 
    IllegalArgumentException("non-existent world");
      return new 
    Location(w, (Double) m.get("x"), (Double) m.get("y"), (Double) m.get("z"));
    Maybe I'm missing something very basic here? Client code would need to catch an IllegalArgumentException when deserializing, but that would be the case regardless of what mechanism is used.
  10. Offline


    yep, that should work! :D
  11. Offline


    Exactly. And yet Bukkit doesn't do this for some reason. Adding this to my ever growing list of things that Bukkit should and could do, but doesn't.
    SergeantH likes this.
  12. Offline


    Perhaps one should put this into a PR?
  13. Offline


    Forgive me for being ignorant, but what is 'PR'? :)

    Can you elaborate? :p

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


    It's a Pull Request.
  15. Offline


    Couldn't you just serialize a string with Location,toString?
  16. Offline


    You mean calling Location.toString() and parsing the string you get to retrieve the information about that object?

    If so, while that would work, it is an ugly way of doing it, which would take a lot of code.
    It would be far better for the API to be able to serialize the objects and store the data that way, rather than having to read all the data out of an object, and into a file.

    The former could be achieved in only a few lines of code.

    Hope that helps.

    Not sure I understand (having looked up the term). Could you elaborate? :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 23, 2016
    pyraetos and billofbong like this.
  17. Offline


    I would suggest making the World
    and then implementing the mysterious private methods invoked during serialization, if found to exist in a class:

    1. /**
    2.   * Method invoked by the JVM during serialization to properly save world data.
    3.   *
    4.   * @param os An ObjectOutputStream passed during serialization of the Location.
    5.   */
    6. private void writeObject(ObjectOutputStream os){
    7. try{
    8. os.defaultWriteObject();
    9. os.writeUTF(world.getName());
    10. }catch(IOException e){
    11. e.printStackTrace();
    12. }
    13. }
    15. /**
    16.   * Method invoked by the JVM during deserialization to properly read world data.
    17.   *
    18.   * @param is An ObjectInputStream passed during deserialization of the Location.
    19.   */
    20. private void readObject(ObjectInputStream is){
    21. try{
    22. is.defaultReadObject();
    23. String name = is.readUTF();
    24. = Bukkit.getWorld(name);
    25. }catch(Exception e){
    26. e.printStackTrace();
    27. }
    28. }
    Jacek likes this.
  18. Offline


    A pull request is basically sending a piece of code to the Bukkit project on Github, and then waiting until it gets accepted or rejected in the project. If it's accepted, your code will be in the new .jar :)
  19. Offline


    AH! I see. :D

    In that case, "I APPROVE!" :p
  20. Offline


    Don't worlds have a UUID for just such a use case?
  21. Offline


    I just use Vector (which is serializable to yaml config) and add my own string for world name.
    Why not use your own location object ;s
  22. Offline


  23. Offline


  24. Offline


    In my opinion it's exactly as complex as it needs to be. Example:
    SerializableLocation sl = new SerializableLocation(player.getLocation());
    Or the other way round:
    Location l = sl.toLocation();
  25. Offline


    I meant the implementation :p
  26. Offline


    When you compare the SerializableLocation class with the org.bukkit.Location class, you will notice that its implementation is far less complex because of its immutability.
  27. Offline


    Exact-a-mundo! Good Job, inspired me to write my own, came out rather similar... Great Minds and All
  28. Offline


    Thank you very much kind Sir.
  29. Offline


    Would it be possible to serialize org.Bukkit itself?
  30. Offline


    What's org.Bukkit? Are you referring to the class org.bukkit.Bukkit?
Thread Status:
Not open for further replies.

Share This Page