Solved Config Creating Duplicate entrys

Discussion in 'Plugin Development' started by hubeb, Mar 27, 2014.

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

    hubeb

    Hello,

    Im trying to add a player to a config. Here is what the config looks like:
    PHP:
    Leaderboard:
      
    '0':
        
    NameMin3CraftDud1
        Wins
    2
        Loses
    2
      
    '1':
        
    NameL0rdOfTheCraft
        Wins
    0
        Loses
    1
    But now when I join the game (triggers method) it make duplicate entries and I cannot figure out how to get it to stop.
    PHP:
    Leaderboard:
      
    '0':
        
    NameMin3CraftDud1
        Wins
    2
        Loses
    2
      
    '1':
        
    NameL0rdOfTheCraft
        Wins
    0
        Loses
    1
      
    '2':
        
    NameMin3CraftDud3
        Wins
    0
        Loses
    0
      
    '3':
        
    NameMin3CraftDud3
        Wins
    0
        Loses
    0
    Here is the code:

    Code:java
    1. @EventHandler
    2. public void onLogin(PlayerLoginEvent e){
    3. Player p = e.getPlayer();
    4. try{Leaderboard.addToFile(p);}catch(Exception i){i.printStackTrace();}
    5. }
    6. public static void addToFile(Player p) throws IOException{
    7. File lbFile = new File(plugin.getDataFolder() + File.separator + "Leaderboard.yml");
    8. if(lbFile.exists()){
    9. FileConfiguration fc = YamlConfiguration.loadConfiguration(lbFile);
    10. for(int j=0;j<fc.getConfigurationSection("Leaderboard").getKeys(false).size();j++){
    11. String s = fc.getString("Leaderboard."+j+".Name");
    12. if(s.equals(p.getName()) || fc.getConfigurationSection("Leaderboard").contains(p.getName())){
    13. return;
    14. }else{
    15. for(int i=0;i<5000;i++){
    16. if(!fc.contains("Leaderboard."+i)){
    17. fc.set("Leaderboard."+i+".Name", p.getName());
    18. fc.set("Leaderboard."+i+".Wins",0);
    19. fc.set("Leaderboard."+i+".Loses",0);
    20. fc.save(lbFile);
    21. break;
    22. }
    23. }
    24. }
    25. }
    26.  
    27. }
    28. }


    Thanks for the help.
     
  2. Offline

    2MBKindiegames

    Maybe this will work,
    Change line 12 into:
     
  3. Offline

    hubeb

    2MBKindiegames Umm both of your posts are the same, and my line 12 and your suggestion are exactly the same...
    Code:java
    1. //My Line 12:
    2. if(s.equals(p.getName()) || fc.getConfigurationSection("Leaderboard").contains(p.getName())){
    3. //Your line 12:
    4. if(s.equals(p.getName()) || fc.getConfigurationSection("Leaderboard").contains(p.getName())){
     
  4. Offline

    2MBKindiegames

    Sorry, that's me messing up (slow internet, double clicking :) )

    What I ment to say was:
     
  5. Offline

    hubeb

    2MBKindiegames Nope same thing. Its really ticking me off, this is the only thing thats keeping me from releasing my plugin.
     
  6. hubeb the config will never contain the name of the person on itself, as the path inside the config to a person is leaderboards.<number>.<name> not leaderboards.<name>

    I would suggest using this
    Code:java
    1. public void configContainsPlayer(FileConfiguration fc, String player){
    2. ConfigurationSection cSection = fc.getConfigurationSection("LeaderBoard");
    3. for(String key : cSection.getKeys(false)){
    4. String name = cSection.getString(key + ".name");
    5. if(player.equals(name))
    6. return true;
    7. }
    8. return false;
    9. }
    10.  
     
  7. Offline

    hubeb

    Sgt_Tailor Nope did not work, but I got it working. I had the other for loop nested. Here is working code:
    Code:java
    1. public static void addToFile(Player p) throws IOException{
    2. File lbFile = new File(plugin.getDataFolder() + File.separator + "Leaderboard.yml");
    3. if(lbFile.exists()){
    4. FileConfiguration fc = YamlConfiguration.loadConfiguration(lbFile);
    5. for(int j=0;j<fc.getConfigurationSection("Leaderboard").getKeys(false).size();j++){
    6. String s = fc.getString("Leaderboard."+j+".Name");
    7. if(s.equals(p.getName())){
    8. return;
    9. }
    10. }
    11. for(int i=0;i<5000;i++){
    12. if(!fc.contains("Leaderboard."+i)){
    13. fc.set("Leaderboard."+i+".Name", p.getName());
    14. fc.set("Leaderboard."+i+".Wins",0);
    15. fc.set("Leaderboard."+i+".Loses",0);
    16. fc.save(lbFile);
    17. break;
    18. }
    19. }
    20. }
    21. }
     
Thread Status:
Not open for further replies.

Share This Page