package net.minecraft.world.level.entity; import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMaps; import it.unimi.dsi.fastutil.ints.Int2ObjectMap.Entry; import java.util.function.Consumer; import net.minecraft.world.entity.Entity; import org.jetbrains.annotations.Nullable; public class EntityTickList { private Int2ObjectMap active = new Int2ObjectLinkedOpenHashMap<>(); private Int2ObjectMap passive = new Int2ObjectLinkedOpenHashMap<>(); @Nullable private Int2ObjectMap iterated; private void ensureActiveIsNotIterated() { if (this.iterated == this.active) { this.passive.clear(); for (Entry entry : Int2ObjectMaps.fastIterable(this.active)) { this.passive.put(entry.getIntKey(), (Entity)entry.getValue()); } Int2ObjectMap int2ObjectMap = this.active; this.active = this.passive; this.passive = int2ObjectMap; } } public void add(Entity entity) { this.ensureActiveIsNotIterated(); this.active.put(entity.getId(), entity); } public void remove(Entity entity) { this.ensureActiveIsNotIterated(); this.active.remove(entity.getId()); } public boolean contains(Entity entity) { return this.active.containsKey(entity.getId()); } public void forEach(Consumer entity) { if (this.iterated != null) { throw new UnsupportedOperationException("Only one concurrent iteration supported"); } else { this.iterated = this.active; try { for (Entity entity2 : this.active.values()) { entity.accept(entity2); } } finally { this.iterated = null; } } } }