-1

Hello once again stackoverflow! I am having the hardest time with this plugin for some reason. Here are the issues. So it have a game class with a method tick i will post it, but it dose not seem to work right it will not send the debug message to the player at all! And then i noticed that there was a ConcurrentModificationException on the list that holds the class game. I'm honestly not sure what is causing the ConcurrentModificationException because i glanced over my code and i do not appear to be modifying the list from within an iteration. I didnt really want to show all my code but i feel like i will need to show a bit of it. If you could look over these segments and try to shed some light on it i would be very grateful! If some one could point out what is causing the ConcurrentModificationException that would be awesome and if some one could explain to me why p.sendMessage("test!"); dosent seem to occur at all.

the ConcurrentModificationException is fixed!

Segment From Class Main:

public class Main extends JavaPlugin implements Runnable {
//Holds all active games
public static ArrayList<Game> games = new ArrayList<Game>();
private CommandHandler cmdHandler = new CommandHandler(this);
private Thread thread = new Thread(this);
private boolean serverActive = true;

public void onEnable(){
    //Command stuff here 
    thread.start();
}

public void onDisable(){
    serverActive = false;
}

@Override
public void run() {
    //Loop through games.
    while(serverActive == true){
        for (Game game: games){
            game.Tick();
        }
    }
}

}

Segment from command Handler: The ConcurrentModificationException Error happens on the newgame command

@Override
public boolean onCommand(CommandSender sender, Command arg, String cmd, String[] args){
    if (cmd.equalsIgnoreCase("newgame")){
        if (args.length == 0){
            sender.sendMessage(ChatColor.RED + "Please enter a game name.");
            return true;
        }else{
        Player player = sender.getServer().getPlayer(sender.getName());
                //Loop and Check
                Game game;
                for (int i = 0; i < Main.games.size(); i++){
                    game = Main.games.get(i);
                    if (game.getName().equalsIgnoreCase(args[0])){
                        //Tells that a game already exists with that name.
                        player.sendMessage(ChatColor.RED + "Can not create game with the name of " + args[0]);
                        return true;
                    }
                }   
                // We have reached the end of the loop so we now know that none of the 
                // games in the list match. Now we can return.
                Main.games.add(new Game(args[0],sender.getServer().getPlayer(sender.getName())));
                player.sendMessage(ChatColor.GREEN + "Game Created. To join Type " + ChatColor.ITALIC + "/join " + args[0]);
                return true;
            }

Segment from Game class:

//Teams
public ArrayList<Player> terrorists = new ArrayList<Player>();
private ArrayList<Player> traitors = new ArrayList<Player>();

public Game(String gameName, Player gameOwner){
    this.gameName = gameName;
    this.gameOwner = gameOwner;
    state = State.Idle;
}

public void Tick(){
        if (state == State.Active){
            //Perform while Active Game.


        }else if (state == State.Idle){
            //Perform while Idle Game.
            for (Player p: terrorists){
                p.sendMessage("test!");
            }
        }else{
            //Clean up
            terrorists.clear();
            traitors.clear();
        }
}
Devin Wall
  • 180
  • 1
  • 16
  • You are adding new games to the list while iterating the list of games at the same time. That is where the ConcurrentModificationException is coming from. – Tyler Aug 27 '13 at 16:26
  • Yes i do this all the time in other games i have made from scratch not once have i had a problem? the .add is not occurring in the actual iteration?? Is it odd that this happens now or is it due to the threading? i mean i know you cant do for (Game game: games){ games.add //ConcurrentModificationException} but adding externally? i mean removing while iterating externally can be done for(Game game: remove){ games.remove(game)} – Devin Wall Aug 28 '13 at 23:59

1 Answers1

0

Do you still have this problem now that your ConcurrentModificationException is fixed?

In the code segments you've provided, I can't see anywhere where you're adding players to the game's terrorists List. So even if the game is ticking, then there's no-one to send the message to.

Further, perhaps debugging by sending a hard-coded message to your Player or the game's creator would help. That would show if the game is registering players correctly.

myWorld.getPlayer(*yournickname*).sendMessage("Game tick!");

for (Player p: terrorists) {
    p.sendMessage("test! You are a terrorist!");
}
Slate
  • 3,189
  • 1
  • 31
  • 32