Ir para conteúdo
  • 0

[Resolvido] [Ajuda] ConcurrentModificationExeption


LuizB

Pergunta

Bem, por algum motivo o loop está parando, só ta levando só 1 player

Código:

 

for (String s : Main.participantes) {
			if (Bukkit.getServer().getPlayer(s).getLocation().subtract(0, 1, 0).getBlock().getType() == Material.WOOL
					&& Bukkit.getServer().getPlayer(s).getLocation().subtract(0, 1, 0).getBlock().getData() == 5
					&& answer.equalsIgnoreCase("Verdadeira")) {
			} else if (Bukkit.getServer().getPlayer(s).getLocation().subtract(0, 1, 0).getBlock()
					.getType() == Material.WOOL
					&& Bukkit.getServer().getPlayer(s).getLocation().subtract(0, 1, 0).getBlock().getData() == 14
					&& answer.equalsIgnoreCase("Falsa")) {
			} else {
				// *** --- ***
				// *** --- ***
				// *** --- ***
				// *** ***
				// *** ***
				Bukkit.getServer().getPlayer(s).sendMessage(plugin.errou.replace("&", "§"));
				World world = Bukkit
						.getWorld((String) plugin.cp.getConfig().getString("Eventolocal." + "Saida" + ".Mundo"));
				double x = plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".X");
				double y = plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".Y");
				double z = plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".Z");
				float yy = (float) plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".Yaw");
				float pp = (float) plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".Pitch");
				Location wap = new Location(world, (double) x, (double) y, (double) z, (float) yy, (float) pp);
				Bukkit.getServer().getPlayer(s).teleport(wap);
				Main.participantes.remove(s);
			}
			// End of loop
		}


Erro:

 

[18:28:07 WARN]: Exception in thread "Craft Scheduler Thread - 3" 
[18:28:07 WARN]: org.apache.commons.lang.UnhandledException: Plugin CuthQuiz v1.0 generated an exception while executing task 15
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:56)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
        at java.util.ArrayList$Itr.next(ArrayList.java:851)
        at me.cuthmf.quiz.Eventolib.verificar(Eventolib.java:389)
        at me.cuthmf.quiz.Eventolib$4.run(Eventolib.java:304)
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
        ... 3 more
Link para o comentário
Compartilhar em outros sites

8 respostass a esta questão

Posts Recomendados

 

Bem, por algum motivo o loop está parando, só ta levando só 1 player

 

Código:

 

 

for (String s : Main.participantes) {
			if (Bukkit.getServer().getPlayer(s).getLocation().subtract(0, 1, 0).getBlock().getType() == Material.WOOL
					&& Bukkit.getServer().getPlayer(s).getLocation().subtract(0, 1, 0).getBlock().getData() == 5
					&& answer.equalsIgnoreCase("Verdadeira")) {
			} else if (Bukkit.getServer().getPlayer(s).getLocation().subtract(0, 1, 0).getBlock()
					.getType() == Material.WOOL
					&& Bukkit.getServer().getPlayer(s).getLocation().subtract(0, 1, 0).getBlock().getData() == 14
					&& answer.equalsIgnoreCase("Falsa")) {
			} else {
				// *** --- ***
				// *** --- ***
				// *** --- ***
				// *** ***
				// *** ***
				Bukkit.getServer().getPlayer(s).sendMessage(plugin.errou.replace("&", "§"));
				World world = Bukkit
						.getWorld((String) plugin.cp.getConfig().getString("Eventolocal." + "Saida" + ".Mundo"));
				double x = plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".X");
				double y = plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".Y");
				double z = plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".Z");
				float yy = (float) plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".Yaw");
				float pp = (float) plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".Pitch");
				Location wap = new Location(world, (double) x, (double) y, (double) z, (float) yy, (float) pp);
				Bukkit.getServer().getPlayer(s).teleport(wap);
				Main.participantes.remove(s);
			}
			// End of loop
		}

Erro:

 

 

[18:28:07 WARN]: Exception in thread "Craft Scheduler Thread - 3" 
[18:28:07 WARN]: org.apache.commons.lang.UnhandledException: Plugin CuthQuiz v1.0 generated an exception while executing task 15
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:56)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
        at java.util.ArrayList$Itr.next(ArrayList.java:851)
        at me.cuthmf.quiz.Eventolib.verificar(Eventolib.java:389)
        at me.cuthmf.quiz.Eventolib$4.run(Eventolib.java:304)
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftAsyncTask.run(CraftAsyncTask.java:53)
        ... 3 more

 

Se não pode remover algo da coleção enquanto estiver fazendo um foreach.

 

Use Iterator... Exemplo

List<String> foo = new ArrayList<>(Arrays.asList("foo", "bar", "qux", "nux"));

System.out.println(foo); // [foo, bar, qux, nux]

Iterator<String> it = foo.iterator();

while (it.hasNext()) {
  String current = it.next();
  if (current.equals("qux"))
    it.remove();
}

System.out.println(foo); // [foo, bar, nux]
Link para o comentário
Compartilhar em outros sites

 

 

Se não pode remover algo da coleção enquanto estiver fazendo um foreach.

 

Use Iterator... Exemplo

List<String> foo = new ArrayList<>(Arrays.asList("foo", "bar", "qux", "nux"));

System.out.println(foo); // [foo, bar, qux, nux]

Iterator<String> it = foo.iterator();

while (it.hasNext()) {
  String current = it.next();
  if (current.equals("qux"))
    it.remove();
}

System.out.println(foo); // [foo, bar, nux]

 

Leo, não tem como fazer usando outra forma, se u usar essa irei ter que fazer umas gambiarras loucas

Link para o comentário
Compartilhar em outros sites

Leo, não tem como fazer usando outra forma, se u usar essa irei ter que fazer umas gambiarras loucas

 

Pq gambiarra?

 

...

Iterator<String> it = Main.participantes.iterator();

while (it.hasNext()) {
  String s = it.next();
  if (Bukkit.getServer().getPlayer(s).getLocation().subtract(0, 1, 0).getBlock().getType() == Material.WOOL
      && Bukkit.getServer().getPlayer(s).getLocation().subtract(0, 1, 0).getBlock().getData() == 5
      && answer.equalsIgnoreCase("Verdadeira")) {
  } else if (Bukkit.getServer().getPlayer(s).getLocation().subtract(0, 1, 0).getBlock()
      .getType() == Material.WOOL
      && Bukkit.getServer().getPlayer(s).getLocation().subtract(0, 1, 0).getBlock().getData() == 14
      && answer.equalsIgnoreCase("Falsa")) {
  } else {
    // *** --- ***
    // *** --- ***
    // *** --- ***
    // *** ***
    // *** ***
    Bukkit.getServer().getPlayer(s).sendMessage(plugin.errou.replace("&", "§"));
    World world = Bukkit
        .getWorld((String) plugin.cp.getConfig().getString("Eventolocal." + "Saida" + ".Mundo"));
    double x = plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".X");
    double y = plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".Y");
    double z = plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".Z");
    float yy = (float) plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".Yaw");
    float pp = (float) plugin.cp.getConfig().getDouble("Eventolocal." + "Saida" + ".Pitch");
    Location wap = new Location(world, (double) x, (double) y, (double) z, (float) yy, (float) pp);
    Bukkit.getServer().getPlayer(s).teleport(wap);
    it.remove();
  }
  // End of loop
}

Diferença: https://www.diffchecker.com/pAQqQXkK

Link para o comentário
Compartilhar em outros sites

ConcurrentModificationException não acontece quando há uma alteração em coleção de forma assíncrona (concorrente)? Você não está executando esse bloco de código em alguma task assíncrona não?

Sim, estou usando async

 

Porque algumas coisas não são permitidas com Async ?

Editado por Cuthmf
Link para o comentário
Compartilhar em outros sites

Não vou saber te explicar o motivo de não poder usar async em algumas collections mas esse post da Caelum explica bem detalhadamente:

http://blog.caelum.com.br/concurrentmodificationexception-e-os-fail-fast-iterators/

 

Existe também a possibilidade de você usar a Concurrent Collections da java api se o erro estiver ocorrendo na manipulação de alguma collection sua, se o error ocorrer com manipulação de collection do minecraft ou do bukkit não vai dar certo...

 

Exemplo se você estiver usando o HashMap existe o ConcurrentHashMap que é mais lento mas é thread safe.

 

http://blog.caelum.com.br/concurrentmodificationexception-e-os-fail-fast-iterators/

Link para o comentário
Compartilhar em outros sites

Sim, estou usando async

 

Porque algumas coisas não são permitidas com Async ?

 

Pq, como eu disse, tu não pode mudar a coleção enquanto estiver iterando, e se tu tivesse um código async que modificasse a coleção, e esse código removesse algo na coleção enquanto você estivesse iterando ela, ia dar esse erro.

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novos posts.
×
×
  • Criar Novo...