Solved Need help with per player data storage

Discussion in 'Spigot Plugin Development' started by Sanfeanso, Mar 25, 2020.

  1. Hello! I'm making a statistics plugin. I want to make yml files for each player which stores their data, but I'm not sure how I could do this. Also, I need to be able to read the yml files so I can store them in a variable. Thanks! (please don't suggest databases since this will be a private plugin for my small server)
     
  2. Code (Text):
    public abstract class Data {
        private FileConfiguration data = null;
        private File dataFile = null;

        protected File getFile() {
            return dataFile;
        }

        protected void setFile(File file) {
            dataFile = file;
        }

        public FileConfiguration getData() {
            if (data == null) {
                loadData();
            }
            return data;
        }

        public void loadData() {
                try {
                    data.load(dataFile);
                } catch (IOException | InvalidConfigurationException e) {
                    e.printStackTrace();
                    // No error because this will happen for all new players
                    data = new YamlConfiguration();
                }
        }

        public void saveData() {
            try {
                getData().save(dataFile);
            } catch (IOException ex) {
                Basics.getInstance().getLogger().log(Level.SEVERE, "Could not save data to " + dataFile, ex);
            }
        }

    }
     
    Code (Text):
    public class BasicPlayer extends Data {
        private Basics plugin;
        private UUID uuid = null;

        public BasicPlayer(UUID uuid) {
            plugin = Basics.getInstance();
            this.uuid = uuid;
            setFile(new File(plugin.getPlayerDir(), this.uuid + ".yml"));
            Player player = plugin.getServer().getPlayer(uuid);
            if (getData().contains("name")) {
                if (player != null) {
                    if (!player.getName().equals(getData().getString("name"))) {
                        getData().set("previousName", getData().getString("name"));
                        getData().set("name", player.getName());
                    }
                }
            } else {
                if(player != null)
                    getData().set("name", player.getName());
                else {
                    OfflinePlayer offlinePlayer = plugin.getServer().getOfflinePlayer(uuid);
                    if(offlinePlayer == null) {
                        plugin.getLogger().info("Failed to initilize data for player " + uuid + "!");
                        getData().set("name", "Unknown");
                    }
                    else
                        getData().set("name", offlinePlayer.getName());
                }
            }
        }

    }
     
    I call saveData() in the BasicPlayer() when they log off or when something really important needs to be saved (saves cause lag so it has to be really important)