Storing player data

Discussion in 'Plugin Development' started by Permeer, Sep 13, 2015.

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

    Permeer

    So, what would be the "best way" to save individual player data?

    1. When the server start up load all the data from a unique YAML file with all players data and save it in a HashMap and when the data is updated save it in the hashmap and when the server stops save it in the YAML file or

    2. Each player have a individual file that are created when the player join in the server and is all the data is saved in a HashMap and when the player left the server all the data is saved in the player YAML and removed from the HashMap

    Sorry for my english
     
  2. Offline

    SkyleTyler1337

    could you show us what you already have? @Permeer
     
  3. Offline

    Halmerson

    @SkyleTyler1337
    I'm not very good in storing data, but shouldn't he have hashmap, then create an event onplayerjoin, then somehow call the hashmap on join?
    Again, I'm not gold at this but that's what I might assume.
     
  4. Offline

    Googlelover1234

    @Permeer
    One thing to keep in mind is if you store it in one file, it'll be harder to find a player's data, after a while. Using multiple files let's you easily find that file and look at it.
     
  5. Offline

    Permeer

    @Googlelover1234 Yeah , i know, but if i store it in one file the file will be loaded/used only when the server start up and will load every player data thats saved

    @SkyleTyler1337 Thats what i have, its simple but works
    and works like that

    - Player join, loads from YAML and save data in a hashmap
    - Player quit, save data in the YAML and remove from the hashmap

    Code:
    @EventHandler
        public void PlayerJoin(PlayerJoinEvent e) {
        
    
            Player p = e.getPlayer();
            UUID uuid = p.getUniqueId();
    
            int kill= 0;
            int death = 0;
            int killstreak = 0;
            int money = 0;
        
            if (Config.existPlayerFile(uuid)) {
                Config playerfile = Config.getPlayerFile(uuid);
            
                if(playerfile.contains("kill"))
                kill = playerfile.getInteger("kill");
            
                if(playerfile.contains("death"))
                death = playerfile.getInteger("death");
            
                if(playerfile.contains("killstreak"))
                killstreak = playerfile.getInteger("killstreak");
            
                if(playerfile.contains("money"))
                money = playerfile.getInteger("money");
            }
        
            Main.Main_().kill.put(uuid, kill);
            Main.Main_().death.put(uuid, death);
            Main.Main_().killstreak.put(uuid, killstreak);
            Main.Main_().money.put(uuid, money);
        
            Main.Main_().setScoreboard(p);
        }
    
        @EventHandler
        public void PlayerQuit(PlayerQuitEvent e){
            Player p = e.getPlayer();
            UUID uuid = p.getUniqueId();
        
            int kill = Main.Main_().kill.get(uuid);
            int death = Main.Main_().death.get(uuid);
            int killstreak = Main.Main_().killstreak.get(uuid);
            int money = Main.Main_().money.get(uuid);
        
            Main.Main_().kill.remove(uuid);
            Main.Main_().death.remove(uuid);
            Main.Main_().killstreak.remove(uuid);
            Main.Main_().money.remove(uuid);
        
            Config playerfile = Config.getPlayerFile(uuid);
        
            boolean kbol = true;
            boolean dbol = true;
            boolean ksbol = true;
            boolean mbol = true;
        
            if(playerfile.contains("kill") && playerfile.getInteger("kill") == kill)
                kbol = false;
            if(playerfile.contains("death") && playerfile.getInteger("death") == kill)
                dbol = false;
            if(playerfile.contains("killstreak") && playerfile.getInteger("killstreak") == kill)
                ksbol = false;
            if(playerfile.contains("money") && playerfile.getInteger("money") == kill)
                mbol = false;
        
            if(kbol)
            playerfile.set("kill", kill);
        
            if(dbol)
            playerfile.set("death", death);
        
            if(ksbol)
            playerfile.set("killstreak", killstreak);
        
            if(mbol)
            playerfile.set("money", money);
            playerfile.save();
        }
    Double post ;v sorry

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
Thread Status:
Not open for further replies.

Share This Page