I'm working on replacing some of the default behavior of mobs by adding my own AI Tasks, then iterating and removing the original versions, when the onEntityJoinWorld event fires. As expected, this occasionally results in a ConcurrentModificationException when some other method is iterating over the taskEntries as I remove.
Is there any way around this or some method I'm missing to take care of this for me?
Code:
@ForgeSubscribe
public void onEntityJoinedWorld (EntityJoinWorldEvent event)
{
if (event.entity instanceof EntityAnimal)
animal = (EntityAnimal)event.entity;
if (animal != null)
{
//System.out.println("Adding replacement tasks for animals");
animal.tasks.addTask(9, new EntityAIWanderAnimal(animal, 0.3F));
// Remove EntityAIWander task
Iterator i = animal.tasks.taskEntries.iterator();
while (i.hasNext())
{
try
{
AITask = (EntityAITaskEntry)i.next();
}
catch (java.util.ConcurrentModificationException e)
{
System.out.println("Exception caught.. continuing..");
break;
}
finally
{ }
if (AITask.action instanceof EntityAIWander) {
//System.out.println("Found match. Removing AIWander");
i.remove();
}
}
}
}