Stats file help

Discussion in 'Spigot Plugin Development' started by ElbestHD, Jul 24, 2018.

  1. Hello, good people of spigot, I need help from you, if you can help me solve this error that I have
    Code:
    Code (Text):
    import java.util.UUID;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import com.theedwardvz.buildffa.Main;
    import com.theedwardvz.buildffa.utils.StatsFile;



    public class PlayerListener
        implements Listener {
       
        private Main plugin;
        public PlayerListener(Main plugin) {
            this.plugin = plugin;
        }

       
        @EventHandler
        public void onPlayerjoin(PlayerJoinEvent e) {
           
            Player p = e.getPlayer();
            StatsFile sta = StatsFile.getConfig();
            String uuid = p.getUniqueId().toString();
            if (!sta.contains("Players." + uuid )) {
                sta.set("Players." + p.getName(), uuid);
                sta.set("Players." + ".Kills", 0);
                sta.set("Players." + ".Deaths", 0);
                sta.save();
            }
        }
       
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
            StatsFile sta = StatsFile.getConfig();
           
            Player p = e.getEntity();
            if (p.getKiller() instanceof Player) {
                Player k = p.getKiller();
                UUID pUUID = p.getUniqueId();
                UUID kUUID = k.getUniqueId();
                int kills = sta.getInt("Players." + kUUID + ".Kills");
                int deaths = sta.getInt("Players." + pUUID + ".Deaths");
               
                sta.set("Players." + kUUID + ".Kills", kills + 1);
                sta.set("Players." + pUUID + ".Deaths", deaths + 1);
                sta.save();
            }
        }
       
    }
     
    So it has to appear in the config:
    Code (Text):
    Players:
      TheEdwardVnz: ed8b6ab3-f008-4f59-9ecd-87158dd1cbbb
      Kills: 1
      Deaths: 0
      ColdWeapons_: bf7f3056-0747-4417-b9ba-f19b2df90123
      Kills: 0
      Deaths: 1
    This is the error that I have:
    Code (Text):
    Players:
      TheEdwardVnz: ed8b6ab3-f008-4f59-9ecd-87158dd1cbbb
      Kills: 0
      Deaths: 0
      ColdWeapons_: bf7f3056-0747-4417-b9ba-f19b2df90123
      ed8b6ab3-f008-4f59-9ecd-87158dd1cbbb:
        Kills: 1
      bf7f3056-0747-4417-b9ba-f19b2df90123:
        Deaths: 1
     
  2. Heyo,

    So I see a few things wrong with this code.
    First off, you're approaching the .yml file storage in the wrong way.
    The way you want to store player data is to make ConfigurationSections for each player. The way ConfigurationSection works is a bit confusing, especially since FileConfiguration extends ConfigurationSection. Don't let that confuse you. (I'll call ConfigurationSection CS) Think of a CS is a group of a key and all the keys and values underneath it. FileConfiguration is the master group, meaning the entire file.
    The way you retrieve a section of the file is quite simple. You take your FileConfiguration class, and you use fileConfig.getConfigurationSection(KEY); This will return a new CS instance for use.
    The idea is that you store a list of UUIDs under your Players: key, and all the information for that player is stored under their UUID, including their username.

    Code (Java):
        public ConfigurationSection load(Player player) {
        //If the "players" key doesn't contain a sub-key that matches the player's UUID, then it's their first time logging in and we need to create a new section.
        if (!configFile.isSet("players." + player.getUniqueId.toString())) {
            ConfigurationSection section = configFile.createSection("players." + player.getUniqueId.toString(), defaults);
            //Defaults can be setup as a static Map<String, Object>, then add default entries in, such as map.put("Kills", 0);
            section.set("username", player.getName());
            //Manually storing their username.
            saveConfig(); //Essential to save your configuration file upon editing.
            return section;
        } else {
            return configFile.getConfigurationSection("players." + uuid.toString());
        }
    }
     
    Here I have a simple method which retrieves the CS for a player. You'll want to store that CS for later use. Directly from the CS class you can both read and write info. To pull, say, their username, from the CS, simply use the "getString" method. (CS comes with a variety of different "get" methods, including "getItemStack") Setting information is also very simple, but make sure you save the file whenever you edit it, or info could be lost. Just use the "set" method to store an object to a key.
    Example:
    Code (Java):
        public String getUsername() {
            return section.getString("username");
        }

        public void setUsername(String username) {
            section.set("username", username);
            saveConfig();
        }
    As long as you're using the CS that you got earlier which is under the player's UUID, you'll be storing information in a nice and neat tree in your .yml file that's easy to read/edit.

    If you have any more questions, please don't hesitate to PM me or friend me on discord (JacobRuby#5859)!
    Hope I could help!
     
    • Like Like x 1
    • Winner Winner x 1