Remove Player Error?

Discussion in 'Plugin Development' started by MrTwiggy, Jul 13, 2012.

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

    MrTwiggy

    Hey there! I'm working on an arena-esque plugin right now, but I've run into a snag with a large error. Here is the code:

    Code:
    //This is inside the onCommand method
    else if (args[0].equalsIgnoreCase("leave"))
                {
                    sender.sendMessage("You have left your team, " + sender.getName() + "!");
                    testMatch.RemovePlayer(sender.getName());
                }
     
     
    //This is the RemovePlayer() function
    public void RemovePlayer(String playerName)
        {
                for (PlayerState playerState : players)
                {
                    if (playerState.playerObj.getName().equalsIgnoreCase(playerName))
                    {
                        //players.remove(playerState);
                        players.remove(playerState);
                    }
                   
                    playerState.PlayerObj().sendMessage(playerName + " was checked against " + playerState.PlayerObj().getName());
                }
        }
    This is the error message in console: http://i.imgur.com/ImhGa.png

    And in game, it sends the player the message:
    "[YourName] was checked against [YourName]"
    "An internal error occurred while attempting to perform this command"


    It seems like it works in removing the 'PlayerState' from the 'players' list, however I get these errors, and it doesn't complete the 'sender.sendMessage("You have left your team...")'. It appear the entire error is caused by the 'players.remove(playerState);' line, because when I comment it out, the errors don't occur.

    Any ideas?
     
  2. Offline

    gjossep

    Don't save the Player object to a hash map or list, do it with player.getname(), and save it to a list with strings.
    To get the player again. you can do Bukkit.getPlayer(NAME) and you will get the player object back.


    But your error is that you edit the list while you are in a for loop, you can't do that, so you have to use a ilartor.
    Like this:
    Code:java
    1.  
    2.  
    3. for(int g = 0; g < itemList.size(); g++){
    4. Iterator<Entry<String>> it = itemList.entrySet().iterator();
    5. while (it.hasNext()) {
    6.  
    7. //getting player object.
    8. Bukkit.getPlayer(it.next);
    9.  
    10. //do what you want here.
    11.  
    12. //remmoving it from the list.
    13. it.remove();
    14. }
    15. }
    16.  
    17.  


    Also this is a example. and i wrote it without a IDE,

    Gjosse
     
  3. Offline

    ferrybig

    ou editing the player list, while you looping over it, this throws an currentmodifedexception in the best affort base to prevent bugs, try to use an copyonwritelist if you dont want this from happen, or design other code strategy
     
Thread Status:
Not open for further replies.

Share This Page