你好再次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;
}
游戏类的片段:
//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();
}
}