我不知道为什么会有这个错误:错误信息
这是第107行(for循环):
for (Pair<UUID, UUID> pair : timeStopList) {
if (pair.getA() == playerUUID) {
cancelTimeStop(pair.getB());
}
}我不想问这样一个简单的解决方案,但我真的不知道发生了什么!所以如果你知道答案,我会很感激你能告诉我的。谢谢!
cancelTimeStop代码:
public static void cancelTimeStop(UUID entityUUID) {
Entity unknownEntity = Bukkit.getEntity(entityUUID);
if (unknownEntity == null || unknownEntity.isDead()) return;
LivingEntity entity = (LivingEntity) unknownEntity;
entity.removePotionEffect(PotionEffectType.SLOW);
entity.removePotionEffect(PotionEffectType.BLINDNESS);
entity.setGravity(true);
entity.setAI(true);
entity.setCanPickupItems(true);
entity.setSilent(false);
removeEntityFromTimeStop(entityUUID);
}removeEntityFromTimeStop()是:
private static void removeEntityFromTimeStop(UUID entityUUID) {
if (timeStopList == null) return;
timeStopList.removeIf(pair -> pair.getB() == entityUUID);
}发布于 2021-09-12 19:00:05
您可以稍微修改您的for loop以避免此异常:
Pair<UUID, UUID> pairToRemove = null;
for (Pair<UUID, UUID> pair : timeStopList) {
if (pair.getA() == playerUUID) {
pairToRemove = pair; // assign the found pair to pairToRemove variable
break;
}
}
if (pairToRemove != null) {
cancelTimeStop(pairToRemove.getB());
}更新
我假设一旦找到一个pair.getA() == playerUUID,就不需要继续循环了。如果此If语句计算结果为true的可能性很大,则可以将代码调整为:
List<Pair<UUID, UUID>> pairToRemoveList = new ArrayList();
for (Pair<UUID, UUID> pair : timeStopList) {
if (pair.getA() == playerUUID) {
pairToRemoveList.add(pair);
}
}
if (!pairToRemoveList.isEmpty()) {
for (Pair<UUID, UUID> pair : pairToRemoveList) {
cancelTimeStop(pair.getB());
}
}发布于 2021-09-12 19:13:40
迭代集合时抛出并发修改异常,并从集合中移除项。在这种情况下,Pair<UUID, UUID>将从timeStopList中删除。
timeStopList.removeIf(pair -> pair.getB() == entityUUID);解决方案是在迭代时删除列表时使用Iterator,并且不需要对逻辑进行其他修改:
Iterator<Pair<UUID, UUID>> iter = cancelTimeStop.listIterator();
while(iter.hasNext()){
if(...){
iter.remove();
}
}https://stackoverflow.com/questions/69154188
复制相似问题