Bukkit插件的严重问题[意外行为]

时间:2013-08-24 05:15:25

标签: java arraylist concurrentmodification bukkit

你好再次stackoverflow!出于某种原因,我正在玩这个插件最难的时候。以下是问题。所以它有一个带有方法的游戏类,我会发布它,但它似乎不能正常工作它根本不会发送调试消息给玩家!然后我注意到列表中有一个ConcurrentModificationException来保存类游戏。老实说,我不确定是什么导致了ConcurrentModificationException,因为我浏览了我的代码,而且我似乎没有在迭代中修改列表。我并不想展示我的所有代码,但我觉得我需要展示一下它。如果您可以查看这些细分并试图阐明它,我将非常感激!如果有人可以指出导致ConcurrentModificationException的原因很棒,并且有人可以向我解释为什么p.sendMessage(“test!”);似乎根本没有发生。

ConcurrentModificationException已修复!

来自班级主要的细分:

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();
        }
    }
}

}

来自命令处理程序的段:新游戏命令上发生ConcurrentModificationException错误

@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;
            }

来自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();
        }
}

1 个答案:

答案 0 :(得分:0)

现在你的ConcurrentModificationException已经修复了吗?

在您提供的代码段中,我无法看到您在游戏的恐怖分子列表中添加玩家的任何位置。因此,即使游戏正在滴答作响,也没有人将消息发送给。

此外,通过向您的播放器或游戏的创建者发送硬编码消息进行调试可能会有所帮助。这将显示游戏是否正确注册玩家。

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

for (Player p: terrorists) {
    p.sendMessage("test! You are a terrorist!");
}