Save Location MySQL

Discussion in 'Spigot Plugin Development' started by BlockFighter, Jul 15, 2017.

  1. I just want to save the x, y, z, pitch.yaw coordinates to the player
     
    • Informative Informative x 1
  2. Just create multiple columns:
    • varchar(36) - Player UUID
    • TEXT - World name
    • double - x
    • double - y
    • double - z
    • double - yaw
    • double - pitch
     
  3. I do not understand
    I want to do it if you quit the player to save it
    Code (Text):
    public void playerQuit(PlayerQuitEvent e) {
    }
     
  4. Do you have a single bit of knowledge of MySQL at all?
     
  5. Just save it to a yml file
     
    • Funny Funny x 1
  6. No, that would be the first thing
    I've done it already, but I need to be in mysql
     
    • Funny Funny x 2
  7. Then get some basic knowledge of MySQL first...
     
  8. just use Gson....
    Code (Text):
    Gson gson = new Gson();
            String data = gson.toJson(location.serialize(), new TypeToken<HashMap<String, Object>>(){}.getType());
            //data is now location in json so store that
           
            location = Location.deserialize(gson.fromJson(data, new TypeToken<HashMap<String, Object>>(){}.getType()));
            //now you have the same location as before
    Just store player UUID, and a string(this string would be the location in json)
     
    • Agree Agree x 1
  9. That'd ruin the entire purpose of using a relational database.
     
    • Agree Agree x 1
  10. This is no different from just joining the different location components together with a delimiter. They're equally bad if stored in an SQL table
     
    • Agree Agree x 2
  11. Learn SQL, then learn jdbc, then look back at the answers given in this thread.
     
    • Agree Agree x 1
  12. Hold up, because this was the most important line in the thread potentially.

    Are you wanting to save a reference of a player (e.g. their uuid) to their location? If you need the raw/current value, you'd use a Map or more ideally Player#getLocation.

    Explain what you're trying to achieve, not how you're trying to solve it. http://xyproblem.info/
     
  13. Strahan

    Benefactor

    It's very scary to see how many people seriously think using a database with the concatenated string/split when retrieved method is a good idea.
     
  14. That, and "how do I store a list in a database??"
     
  15. Relational databases contain lists of data. They have relations. E.g. one Player has a list of entities. Each entity can only have one player.

    PlayerId 1 ----> 1...N entity
     
  16. SQL : INSERT INTO table (location) VALUES (loc);

    loc = world;x;y;z;pitch;yaw
     
  17. As said before, don't do that. (I already edited my post).
    I haven't worked with SQL for very long, and I've never gotten a case where I had to filter by a certain location value, so it never occured to me why it wouldn't be a good idea, hence my post. I edited it.
    I in fact sometimes struggle with storing a collection in a database. For example, I have a 'lock' object with a list of UUIDs, these UUIDs are the player who can open the lock. What's would be the best way to store this? (I currently have a way, but now I'm curious to see what is the so called "proper" way and why). Just trying to learn. :)
     
  18. You'd have a lock table, and a 'key' table (for a lack of a better name), where your lock table holds lock data, and the key table holds all (lock_id, uuid) tuples of UUIDs which can open locks.
     

  19. An example which isn't the best one. The location should be a composite primary key instead of having a separate auto incremented key.


    Bildschirmfoto 2017-07-17 um 17.42.08.png


    EDIT: I guess your lock is a lock of a chest or something?!
     
  20. That's in fact the way I've been doing it. Great to hear it's good practice.
    That's how I've been storing the players but not the location, I'll change it. Thanks. :) (It's for all containers and doors etc)
    EDIT: What's that program?
     

Share This Page