1.14.4 Storing player-specific data

Discussion in 'Spigot Plugin Development' started by DavidMa, Feb 25, 2020.

  1. Hi.
    I would like to store data/stats of a player, such as death counts, kill counts, or any other
    arbitrary values (i.e. mana). I would like the data to retain after the player logs out.
    What is the best way to do this?
     
  2. Personally, I just use one .txt file by player, but you can perfectly put these informations in a database with MySql, or use SQLite to use SQL request in a file.
     
    • Like Like x 1
  3. Or do what Essentials does and create a <uuid>.yml file for each player that joins, you should be able to work with those easily since there's a parser built in for you.
     
    • Like Like x 2
    • Like Like x 1
  4. FrostedSnowman

    Resource Staff

    You could alternatively use Gson for easy serialization & deserialization of POJOs. The library is already included in Spigot.

    Example:
    Code (Java):
    public class Statistics {
         private int kills;
         private int deaths;
         private int respaws;
    }
    To serialize this, you'd use a Writer to write to a file using Gson. This will write the data to a target file:
    Code (Java):
    try (Writer writer = new FileWriter(file)) {
       gson.toJson(statistics, writer);
    } catch (IOException exception) {
       exception.printStackTrace();
    }
    Very similarly, you'd deserialize using a Reader:
    Code (Java):
    try (Reader reader = new FileReader(file)) {
       Statistics statistics = gson.fromJson(reader, Statistics.class);
    } catch (IOException exception) {
       exception.printStackTrace();
    }
    View more about Gson here.
     
    #5 FrostedSnowman, Feb 25, 2020
    Last edited: Feb 25, 2020
    • Like Like x 1
  5. or also for saving in yml files use ConfigurationSerializable by Spigot.
     
  6. Would it be considered bad if you stored all of the player's data in one .yml file? like:

    Code (YAML):
    Players:
        <Player1UUID>
    :
            Random-Key
    : Value
            Random-Key-2
    : Value
        <Player2UUID>
    :
            Random-Key
    : Value
            Random-Key-2
    : Value

    #And so on.
     
  7. in most cases, yes. The larger the file gets, the longer the loading. So if you are not on a whitelisted server for example, I would not recommend it.
     
    • Useful Useful x 1
  8. Thank you, I have been wondering about it for a while. Looks like I have some fixing to do :D

    EDIT:

    I am going to start using SQLite for handling player information and whatnot, I will just use .yml files for smaller things, such as storing shop inventories, or arena locations. Things that do not change nearly as often
     
    #9 FyrenGaming, Feb 25, 2020
    Last edited: Feb 25, 2020
  9. Strahan

    Benefactor

    I'd make it configurable to use SQL or individual player YML files.
     
    • Agree Agree x 1