mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-12-12 12:26:45 +01:00
Fix iterator remove() for NPCs
This commit is contained in:
parent
6c5ebcddee
commit
63cc59c833
@ -114,8 +114,9 @@ public class Citizens extends JavaPlugin implements CitizensPlugin {
|
|||||||
NPC npc = itr.next();
|
NPC npc = itr.next();
|
||||||
try {
|
try {
|
||||||
npc.despawn(DespawnReason.REMOVAL);
|
npc.despawn(DespawnReason.REMOVAL);
|
||||||
for (Trait trait : npc.getTraits())
|
for (Trait trait : npc.getTraits()) {
|
||||||
trait.onRemove();
|
trait.onRemove();
|
||||||
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
// ensure that all entities are despawned
|
// ensure that all entities are despawned
|
||||||
|
@ -158,6 +158,12 @@ public class ByIdArray<T> implements Iterable<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void advance() {
|
||||||
|
do {
|
||||||
|
idx++;
|
||||||
|
} while (idx != highest + 1 && elementData[idx] == null);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
if (modCount != expected) {
|
if (modCount != expected) {
|
||||||
@ -176,9 +182,7 @@ public class ByIdArray<T> implements Iterable<T> {
|
|||||||
T next = (T) elementData[idx];
|
T next = (T) elementData[idx];
|
||||||
if (next == null)
|
if (next == null)
|
||||||
throw new NoSuchElementException();
|
throw new NoSuchElementException();
|
||||||
do {
|
advance();
|
||||||
idx++;
|
|
||||||
} while (idx != highest + 1 && elementData[idx] == null);
|
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,8 +190,11 @@ public class ByIdArray<T> implements Iterable<T> {
|
|||||||
public void remove() {
|
public void remove() {
|
||||||
if (modCount != expected)
|
if (modCount != expected)
|
||||||
throw new ConcurrentModificationException();
|
throw new ConcurrentModificationException();
|
||||||
|
if (elementData[idx] == null)
|
||||||
|
throw new NoSuchElementException();
|
||||||
fastRemove(idx);
|
fastRemove(idx);
|
||||||
expected = modCount;
|
expected = modCount;
|
||||||
|
advance();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@ package net.citizensnpcs.util;
|
|||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
@ -45,6 +47,22 @@ public class ByIdArrayTest {
|
|||||||
assertSize(array, 2);
|
assertSize(array, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIteratorRemove() {
|
||||||
|
ByIdArray<Integer> array = ByIdArray.create();
|
||||||
|
array.put(10, 1);
|
||||||
|
array.put(20, 2);
|
||||||
|
array.put(30, 3);
|
||||||
|
Iterator<Integer> itr = array.iterator();
|
||||||
|
itr.next();
|
||||||
|
itr.remove();
|
||||||
|
itr.next();
|
||||||
|
assertSize(array, 2);
|
||||||
|
assertThat(array.contains(20), is(false));
|
||||||
|
assertThat(array.get(10), is(1));
|
||||||
|
assertThat(array.get(30), is(3));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPut() {
|
public void testPut() {
|
||||||
ByIdArray<Integer> array = ByIdArray.create();
|
ByIdArray<Integer> array = ByIdArray.create();
|
||||||
|
Loading…
Reference in New Issue
Block a user