(Minigame) ArrayList duplicating same values?

Discussion in 'Plugin Development' started by DrMedia, Nov 16, 2013.

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

    DrMedia

    Hey guys. I'm working on a minigame for a freind... Whenever I load arenas, even thought they all have different names, the same arena is added. For example: If I had arena test1, test2 and test3, the ArrayList would contain 3 test3 arenas.. I have been pulling my hair out of my head for weeks trying to fix this, it's probably really simple to fix. Anyway, here's the code:
    Code:java
    1. public static HashSet<Arena> arenas = new HashSet<Arena>();
    2.  
    3. public static void loadArenas() {
    4. FileConfiguration fc = Main.getPlugin().getConfig();
    5. for(String keys : fc.getConfigurationSection("Arenas").getKeys(false)){
    6. Arena arena = new Arena(keys);
    7. arenas.add(arena);
    8. for(Player player : Bukkit.getOnlinePlayers()){
    9. for(Arena arena2 : ArenaManager.arenas) player.sendMessage(Bukkit.getServer().getPluginManager().getPlugin("PresidentAssassination").getConfig().getString("Prefix").replaceAll("(&([a-f0-9]))", "\u00A7$2") + ChatColor.RESET + " " + arena2.getName());
    10. }
    11. }
    12. }
     
  2. I think it would have to do with the adding code, so where you add the arena to the arryalist. If you could show us that, that might help.
     
  3. Offline

    DrMedia

    arenas.add(arena); is adding the arena directly into the arena HashSet. (the HashSet .add() method)
     
  4. Yeah, but you are loading that from the config file, I am talking about the piece of code you use for adding the arena to the config. So probably your command handler.
     
  5. Offline

    DrMedia

    Ahh, that isn't the problem as all the arenas have different names.

    Code:java
    1. Main.getPlugin().getConfig().createSection("Arenas."+name);
    2. ConfigurationSection s = Main.getPlugin().getConfig().createSection("Arenas."+args[1]+".spawn");
    3. s.set("world", player.getWorld().getName());
    4. s.set("x", player.getLocation().getX());
    5. s.set("y", player.getLocation().getY());
    6. s.set("z", player.getLocation().getZ());
    7. s.set("pitch", player.getLocation().getPitch());
    8. s.set("yaw", player.getLocation().getYaw());
    9. Main.getPlugin().getConfig().set("Arenas."+args[1]+".spawn", s);


    PHP:
    Arenas:
      
    test:
        
    spawn:
          
    worldworld
          x
    : -230.10313918986492
          y
    107.0
          z
    26.61788232957443
          pitch
    10.572506
          yaw
    : -194.86748
      test2
    :
        
    spawn:
          
    worldworld
          x
    : -229.78936402591967
          y
    106.0
          z
    27.8187870924245
          pitch
    10.572506
          yaw
    : -194.86748
      test3
    :
        
    spawn:
          
    worldworld
          x
    : -247.38852254280613
          y
    132.0
          z
    : -20.59685805661127
          pitch
    10.572506
          yaw
    : -194.86748
     
  6. Try moving that second for loop out of the first one, might have something to do with it, dunno.
     
  7. Offline

    DrMedia

  8. Offline

    mrkirby153

    DrMedia

    Try changing arenas.add(arena) to
    Code:java
    1. if (!arenas.contains(arena)){
    2. arenas.add(arena);
    3. }
     
  9. Offline

    DrMedia

    Tried that long time ago.. didn't work :p
    The problem was that I was using static variables..
     
Thread Status:
Not open for further replies.

Share This Page