Writing to the config too many times

Discussion in 'Spigot Plugin Development' started by TheGamerPlayz, May 23, 2016.

  1. So the plugin should test if the block under a player is blue/red wool, and if so it adds them to the config and if not it removes them, but it keeps adding a player continously and when i checked it i saw this:
    [​IMG]

    Anyone see why it would keep adding it to the config?

    Relevant code:
    Code (Text):

    public class GameStart implements Listener {
        Main pl;
        public GameStart(Main pl){
            this.pl = pl;
        }
        List<String> red = new ArrayList<String>();
        List<String> blue = new ArrayList<String>();
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent event){
            TeamData td = new TeamData(pl);
            FileConfiguration teams = td.getData();
            Player p = event.getPlayer();
            blue.addAll(teams.getStringList("Teams.Blue"));
            red.addAll(teams.getStringList("Teams.Red"));
            if(p.getWorld().getName().equals("world_nether")){
                Block b = p.getLocation().getBlock().getRelative(BlockFace.DOWN);
                if(b.getState().getData() instanceof Wool){
                    Wool wool = (Wool) b.getState().getData();
                    DyeColor dc = wool.getColor();
                    if(dc.toString() == "BLUE"){
                        if(!teams.contains("Teams.Blue." + p.getName())){
                            blue.add(p.getName());
                            if(teams.getStringList("Teams.Blue").contains(p.getName()))
                                teams.set("Teams.Blue", blue);
                            td.saveData();
                        }
                    }else if(dc.toString() == "RED"){
                        if(!teams.contains("Teams.Red." + p.getName())){
                            red.add(p.getName());
                            if(teams.getStringList("Teams.Red").contains(p.getName()))
                                teams.set("Teams.Red", red);
                            td.saveData();
                        }
                    }
               }
        }
    }
     
  2. Because on each iteration you use td.saveData()?
     
  3. You also add the player to the arraylists without considering if they are already in it before
     
    • Agree Agree x 2
  4. use if statements to check their name's existence in the list. :D goodLuck looking forward.
     
  5. Just realised this was in your code. Remove the "Teams.Red" part, because you only store the player name in your list