// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.spawning.spawnmarkers;

import com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.npc.role.Role;
import it.unimi.dsi.fastutil.objects.ObjectList;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.modules.time.WorldTimeResource;
import com.hypixel.hytale.server.core.entity.reference.InvalidatablePersistentRef;
import com.hypixel.hytale.server.npc.components.SpawnMarkerReference;
import com.hypixel.hytale.protocol.AnimationSlot;
import com.hypixel.hytale.server.core.modules.entity.damage.DeathComponent;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.dependency.OrderPriority;
import com.hypixel.hytale.server.core.modules.entity.system.PlayerSpatialSystem;
import com.hypixel.hytale.component.spatial.SpatialResource;
import com.hypixel.hytale.component.ResourceType;
import com.hypixel.hytale.server.core.modules.entity.component.ModelComponent;
import com.hypixel.hytale.server.core.modules.entity.component.HeadRotation;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.entity.reference.PersistentRefCount;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;
import javax.annotation.Nullable;
import com.hypixel.hytale.component.SystemGroup;
import com.hypixel.hytale.server.flock.StoredFlock;
import java.time.Duration;
import java.time.Instant;
import com.hypixel.hytale.server.core.modules.entity.component.FromWorldGen;
import com.hypixel.hytale.server.core.modules.entity.component.FromPrefab;
import com.hypixel.hytale.server.core.prefab.PrefabCopyableComponent;
import java.util.List;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import it.unimi.dsi.fastutil.Pair;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import com.hypixel.hytale.component.dependency.SystemDependency;
import com.hypixel.hytale.component.dependency.Order;
import java.util.logging.Level;
import com.hypixel.hytale.server.spawning.assets.spawnmarker.config.SpawnMarker;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.system.RefSystem;
import com.hypixel.hytale.server.core.modules.entity.component.Intangible;
import com.hypixel.hytale.server.core.modules.entity.tracker.NetworkId;
import com.hypixel.hytale.component.system.HolderSystem;
import com.hypixel.hytale.component.dependency.RootDependency;
import com.hypixel.hytale.component.dependency.Dependency;
import java.util.Set;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.component.Archetype;
import java.util.Map;
import org.bson.BsonValue;
import com.hypixel.hytale.codec.ExtraInfo;
import com.hypixel.hytale.server.core.modules.entity.component.WorldGenId;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.server.core.modules.entity.component.HiddenFromAdventurePlayers;
import java.util.UUID;
import com.hypixel.hytale.server.core.entity.Entity;
import com.hypixel.hytale.server.core.asset.type.model.config.Model;
import org.bson.BsonDocument;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.server.core.modules.entity.AllLegacyEntityTypesQuery;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.data.unknown.UnknownComponents;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.server.core.entity.nameplate.Nameplate;
import com.hypixel.hytale.server.core.modules.entity.component.PersistentModel;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.modules.entity.EntityModule;
import javax.annotation.Nonnull;
import com.hypixel.hytale.logger.HytaleLogger;

public class SpawnMarkerSystems
{
    @Nonnull
    private static final HytaleLogger LOGGER;
    
    static {
        LOGGER = HytaleLogger.forEnclosingClass();
    }
    
    @Deprecated(forRemoval = true)
    public static class LegacyEntityMigration extends EntityModule.MigrationSystem
    {
        @Nonnull
        private final ComponentType<EntityStore, PersistentModel> persistentModelComponentType;
        @Nonnull
        private final ComponentType<EntityStore, Nameplate> nameplateComponentType;
        @Nonnull
        private final ComponentType<EntityStore, UUIDComponent> uuidComponentType;
        @Nonnull
        private final ComponentType<EntityStore, UnknownComponents<EntityStore>> unknownComponentsComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public LegacyEntityMigration() {
            this.persistentModelComponentType = PersistentModel.getComponentType();
            this.nameplateComponentType = Nameplate.getComponentType();
            this.uuidComponentType = UUIDComponent.getComponentType();
            this.unknownComponentsComponentType = EntityStore.REGISTRY.getUnknownComponentType();
            this.query = (Query<EntityStore>)Query.and(this.unknownComponentsComponentType, Query.not((Query<Object>)AllLegacyEntityTypesQuery.INSTANCE));
        }
        
        @Override
        public void onEntityAdd(@Nonnull final Holder<EntityStore> holder, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store) {
            final UnknownComponents<EntityStore> unknownComponentsComponent = holder.getComponent(this.unknownComponentsComponentType);
            assert unknownComponentsComponent != null;
            final Map<String, BsonDocument> unknownComponents = unknownComponentsComponent.getUnknownComponents();
            final BsonDocument spawnMarker = unknownComponents.remove("SpawnMarker");
            if (spawnMarker == null) {
                return;
            }
            final Archetype<EntityStore> archetype = holder.getArchetype();
            assert archetype != null;
            if (!archetype.contains(this.persistentModelComponentType)) {
                final Model.ModelReference modelReference = Entity.MODEL.get(spawnMarker).get();
                holder.addComponent(this.persistentModelComponentType, new PersistentModel(modelReference));
            }
            if (!archetype.contains(this.nameplateComponentType)) {
                holder.addComponent(this.nameplateComponentType, new Nameplate(Entity.DISPLAY_NAME.get(spawnMarker).get()));
            }
            if (!archetype.contains(this.uuidComponentType)) {
                holder.addComponent(this.uuidComponentType, new UUIDComponent(Entity.UUID.get(spawnMarker).get()));
            }
            holder.ensureComponent(HiddenFromAdventurePlayers.getComponentType());
            final int worldGenId = Codec.INTEGER.decode(spawnMarker.get("WorldgenId"));
            if (worldGenId != 0) {
                holder.addComponent(WorldGenId.getComponentType(), new WorldGenId(worldGenId));
            }
            final SpawnMarkerEntity marker = SpawnMarkerEntity.CODEC.decode(spawnMarker, new ExtraInfo(5));
            holder.addComponent(SpawnMarkerEntity.getComponentType(), marker);
        }
        
        @Override
        public void onEntityRemoved(@Nonnull final Holder<EntityStore> holder, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store) {
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Nonnull
        @Override
        public Set<Dependency<EntityStore>> getDependencies() {
            return RootDependency.firstSet();
        }
    }
    
    public static class EnsureNetworkSendable extends HolderSystem<EntityStore>
    {
        @Nonnull
        private final Query<EntityStore> query;
        
        public EnsureNetworkSendable() {
            this.query = SpawnMarkerEntity.getComponentType();
        }
        
        @Override
        public void onEntityAdd(@Nonnull final Holder<EntityStore> holder, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store) {
            final Archetype<EntityStore> archetype = holder.getArchetype();
            assert archetype != null;
            final ComponentType<EntityStore, NetworkId> networkIdComponentType = NetworkId.getComponentType();
            if (!archetype.contains(networkIdComponentType)) {
                holder.addComponent(networkIdComponentType, new NetworkId(store.getExternalData().takeNextNetworkId()));
            }
            holder.ensureComponent(Intangible.getComponentType());
        }
        
        @Override
        public void onEntityRemoved(@Nonnull final Holder<EntityStore> holder, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store) {
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
    }
    
    public static class CacheMarker extends RefSystem<EntityStore>
    {
        private final ComponentType<EntityStore, SpawnMarkerEntity> spawnMarkerComponentType;
        
        public CacheMarker(@Nonnull final ComponentType<EntityStore, SpawnMarkerEntity> spawnMarkerComponentType) {
            this.spawnMarkerComponentType = spawnMarkerComponentType;
        }
        
        @Override
        public Query<EntityStore> getQuery() {
            return this.spawnMarkerComponentType;
        }
        
        @Override
        public void onEntityAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final SpawnMarkerEntity spawnMarkerEntityComponent = store.getComponent(ref, this.spawnMarkerComponentType);
            assert spawnMarkerEntityComponent != null;
            final SpawnMarker spawnMarker = SpawnMarker.getAssetMap().getAsset(spawnMarkerEntityComponent.getSpawnMarkerId());
            if (spawnMarker == null) {
                SpawnMarkerSystems.LOGGER.at(Level.SEVERE).log("Marker %s removed due to missing spawn marker type: %s", ref, spawnMarkerEntityComponent.getSpawnMarkerId());
                commandBuffer.removeEntity(ref, RemoveReason.REMOVE);
                return;
            }
            spawnMarkerEntityComponent.setCachedMarker(spawnMarker);
        }
        
        @Override
        public void onEntityRemove(@Nonnull final Ref<EntityStore> ref, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
    }
    
    public static class EntityAdded extends RefSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, SpawnMarkerEntity> spawnMarkerEntityComponentType;
        @Nonnull
        private final ComponentType<EntityStore, UUIDComponent> uuidComponentType;
        @Nonnull
        private final Set<Dependency<EntityStore>> dependencies;
        @Nonnull
        private final Query<EntityStore> query;
        
        public EntityAdded(@Nonnull final ComponentType<EntityStore, SpawnMarkerEntity> spawnMarkerEntityComponentType) {
            this.spawnMarkerEntityComponentType = spawnMarkerEntityComponentType;
            this.uuidComponentType = UUIDComponent.getComponentType();
            this.dependencies = (Set<Dependency<EntityStore>>)Set.of(new SystemDependency(Order.AFTER, CacheMarker.class));
            this.query = (Query<EntityStore>)Query.and(spawnMarkerEntityComponentType, this.uuidComponentType);
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Nonnull
        @Override
        public Set<Dependency<EntityStore>> getDependencies() {
            return this.dependencies;
        }
        
        @Override
        public void onEntityAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final SpawnMarkerEntity spawnMarkerEntityComponent = store.getComponent(ref, this.spawnMarkerEntityComponentType);
            assert spawnMarkerEntityComponent != null;
            final HytaleLogger.Api context = SpawnMarkerSystems.LOGGER.at(Level.FINE);
            if (context.isEnabled()) {
                context.log("Loaded marker %s", store.getComponent(ref, this.uuidComponentType));
            }
            if (spawnMarkerEntityComponent.getStoredFlock() != null) {
                spawnMarkerEntityComponent.setTempStorageList(new ObjectArrayList<Pair<Ref<EntityStore>, NPCEntity>>());
            }
            if (spawnMarkerEntityComponent.getSpawnCount() != 0) {
                spawnMarkerEntityComponent.refreshTimeout();
            }
            commandBuffer.ensureComponent(ref, PrefabCopyableComponent.getComponentType());
        }
        
        @Override
        public void onEntityRemove(@Nonnull final Ref<EntityStore> ref, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
    }
    
    public static class EntityAddedFromExternal extends RefSystem<EntityStore>
    {
        @Nonnull
        private final Query<EntityStore> query;
        @Nonnull
        private final ComponentType<EntityStore, SpawnMarkerEntity> spawnMarkerEntityComponentType;
        @Nonnull
        private final Set<Dependency<EntityStore>> dependencies;
        
        public EntityAddedFromExternal(@Nonnull final ComponentType<EntityStore, SpawnMarkerEntity> spawnMarkerEntityComponentType) {
            this.query = (Query<EntityStore>)Query.and(spawnMarkerEntityComponentType, Query.or(FromPrefab.getComponentType(), FromWorldGen.getComponentType()));
            this.spawnMarkerEntityComponentType = spawnMarkerEntityComponentType;
            this.dependencies = (Set<Dependency<EntityStore>>)Set.of(new SystemDependency(Order.BEFORE, EntityAdded.class), new SystemDependency(Order.AFTER, CacheMarker.class));
        }
        
        @Override
        public void onEntityAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final SpawnMarkerEntity spawnMarkerEntityComponent = store.getComponent(ref, this.spawnMarkerEntityComponentType);
            assert spawnMarkerEntityComponent != null;
            spawnMarkerEntityComponent.setSpawnCount(0);
            spawnMarkerEntityComponent.setRespawnCounter(0.0);
            spawnMarkerEntityComponent.setSpawnAfter(null);
            spawnMarkerEntityComponent.setGameTimeRespawn(null);
            if (spawnMarkerEntityComponent.getCachedMarker().getDeactivationDistance() > 0.0) {
                spawnMarkerEntityComponent.setStoredFlock(new StoredFlock());
            }
        }
        
        @Override
        public void onEntityRemove(@Nonnull final Ref<EntityStore> ref, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
        
        @Nonnull
        @Override
        public Set<Dependency<EntityStore>> getDependencies() {
            return this.dependencies;
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Nullable
        @Override
        public SystemGroup<EntityStore> getGroup() {
            return EntityModule.get().getPreClearMarkersGroup();
        }
    }
    
    public static class AddedFromWorldGen extends HolderSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, SpawnMarkerEntity> componentType;
        @Nonnull
        private final ComponentType<EntityStore, WorldGenId> worldGenIdComponentType;
        @Nonnull
        private final ComponentType<EntityStore, FromWorldGen> fromWorldGenComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public AddedFromWorldGen() {
            this.componentType = SpawnMarkerEntity.getComponentType();
            this.worldGenIdComponentType = WorldGenId.getComponentType();
            this.fromWorldGenComponentType = FromWorldGen.getComponentType();
            this.query = (Query<EntityStore>)Query.and(this.componentType, this.fromWorldGenComponentType);
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Nullable
        @Override
        public SystemGroup<EntityStore> getGroup() {
            return EntityModule.get().getPreClearMarkersGroup();
        }
        
        @Override
        public void onEntityAdd(@Nonnull final Holder<EntityStore> holder, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store) {
            final FromWorldGen fromWorldGenComponent = holder.getComponent(this.fromWorldGenComponentType);
            assert fromWorldGenComponent != null;
            holder.putComponent(this.worldGenIdComponentType, new WorldGenId(fromWorldGenComponent.getWorldGenId()));
        }
        
        @Override
        public void onEntityRemoved(@Nonnull final Holder<EntityStore> holder, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store) {
        }
    }
    
    public static class Ticking extends EntityTickingSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, SpawnMarkerEntity> spawnMarkerEntityComponentType;
        @Nullable
        private final ComponentType<EntityStore, NPCEntity> npcComponentType;
        @Nonnull
        private final ComponentType<EntityStore, PersistentRefCount> referenceIdComponentType;
        @Nonnull
        private final ComponentType<EntityStore, TransformComponent> transformComponentType;
        @Nonnull
        private final ComponentType<EntityStore, HeadRotation> headRotationComponentType;
        @Nonnull
        private final ComponentType<EntityStore, ModelComponent> modelComponentType;
        @Nonnull
        private final ResourceType<EntityStore, SpatialResource<Ref<EntityStore>, EntityStore>> playerSpatialComponent;
        @Nonnull
        private final Set<Dependency<EntityStore>> dependencies;
        @Nonnull
        private final Query<EntityStore> query;
        static final /* synthetic */ boolean $assertionsDisabled;
        
        public Ticking(@Nonnull final ComponentType<EntityStore, SpawnMarkerEntity> spawnMarkerEntityComponentType, @Nonnull final ResourceType<EntityStore, SpatialResource<Ref<EntityStore>, EntityStore>> playerSpatialComponent) {
            this.transformComponentType = TransformComponent.getComponentType();
            this.headRotationComponentType = HeadRotation.getComponentType();
            this.modelComponentType = ModelComponent.getComponentType();
            this.spawnMarkerEntityComponentType = spawnMarkerEntityComponentType;
            this.npcComponentType = NPCEntity.getComponentType();
            this.referenceIdComponentType = PersistentRefCount.getComponentType();
            this.playerSpatialComponent = playerSpatialComponent;
            this.dependencies = (Set<Dependency<EntityStore>>)Set.of(new SystemDependency(Order.AFTER, PlayerSpatialSystem.class, OrderPriority.CLOSEST));
            this.query = (Query<EntityStore>)Archetype.of(spawnMarkerEntityComponentType, this.transformComponentType);
        }
        
        @Nonnull
        @Override
        public Set<Dependency<EntityStore>> getDependencies() {
            return this.dependencies;
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Override
        public boolean isParallel(final int archetypeChunkSize, final int taskCount) {
            return EntityTickingSystem.maybeUseParallel(archetypeChunkSize, taskCount);
        }
        
        @Override
        public void tick(final float dt, final int index, @Nonnull final ArchetypeChunk<EntityStore> archetypeChunk, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final SpawnMarkerEntity spawnMarkerEntityComponent = archetypeChunk.getComponent(index, this.spawnMarkerEntityComponentType);
            assert spawnMarkerEntityComponent != null;
            final TransformComponent transformComponent = archetypeChunk.getComponent(index, this.transformComponentType);
            assert transformComponent != null;
            final World world = store.getExternalData().getWorld();
            final SpawnMarker cachedMarker = spawnMarkerEntityComponent.getCachedMarker();
            Ref<EntityStore> ref = null;
            if (spawnMarkerEntityComponent.getSpawnCount() > 0) {
                final StoredFlock storedFlock = spawnMarkerEntityComponent.getStoredFlock();
                if (storedFlock != null) {
                    final SpatialResource<Ref<EntityStore>, EntityStore> spatialResource = store.getResource(this.playerSpatialComponent);
                    final ObjectList<Ref<EntityStore>> results = SpatialResource.getThreadLocalReferenceList();
                    spatialResource.getSpatialStructure().collect(transformComponent.getPosition(), cachedMarker.getDeactivationDistance(), results);
                    final boolean hasPlayersInRange = !results.isEmpty();
                    if (!hasPlayersInRange) {
                        if (!storedFlock.hasStoredNPCs() && spawnMarkerEntityComponent.tickTimeToDeactivation(dt)) {
                            final InvalidatablePersistentRef[] npcReferences = spawnMarkerEntityComponent.getNpcReferences();
                            if (npcReferences == null) {
                                return;
                            }
                            if (!spawnMarkerEntityComponent.isDespawnStarted()) {
                                final List<Pair<Ref<EntityStore>, NPCEntity>> tempStorageList = spawnMarkerEntityComponent.getTempStorageList();
                                for (final InvalidatablePersistentRef reference : npcReferences) {
                                    final Ref<EntityStore> npcRef = reference.getEntity(commandBuffer);
                                    if (npcRef != null) {
                                        final NPCEntity npcComponent = commandBuffer.getComponent(npcRef, this.npcComponentType);
                                        assert npcComponent != null;
                                        tempStorageList.add(Pair.of(npcRef, npcComponent));
                                        final boolean isDead = commandBuffer.getArchetype(npcRef).contains(DeathComponent.getComponentType());
                                        if (isDead || npcComponent.getRole().getStateSupport().isInBusyState()) {
                                            spawnMarkerEntityComponent.setTimeToDeactivation(cachedMarker.getDeactivationTime());
                                            tempStorageList.clear();
                                            return;
                                        }
                                    }
                                }
                                for (int i = 0; i < tempStorageList.size(); ++i) {
                                    final Pair<Ref<EntityStore>, NPCEntity> npcPair = tempStorageList.get(i);
                                    final Ref<EntityStore> npcRef2 = npcPair.first();
                                    final NPCEntity npcComponent2 = npcPair.second();
                                    final ModelComponent modelComponent = commandBuffer.getComponent(npcRef2, this.modelComponentType);
                                    if (modelComponent != null && modelComponent.getModel().getAnimationSetMap().containsKey("Despawn")) {
                                        final Role role = npcComponent2.getRole();
                                        assert role != null;
                                        final double despawnAnimationTime = role.getDespawnAnimationTime();
                                        if (despawnAnimationTime > spawnMarkerEntityComponent.getTimeToDeactivation()) {
                                            spawnMarkerEntityComponent.setTimeToDeactivation(despawnAnimationTime);
                                        }
                                        npcComponent2.playAnimation(npcRef2, AnimationSlot.Status, "Despawn", commandBuffer);
                                    }
                                }
                                spawnMarkerEntityComponent.setDespawnStarted(true);
                                tempStorageList.clear();
                                return;
                            }
                            final PersistentRefCount refId = archetypeChunk.getComponent(index, this.referenceIdComponentType);
                            if (refId != null) {
                                refId.increment();
                            }
                            ref = archetypeChunk.getReferenceTo(index);
                            commandBuffer.run(_store -> {
                                final ObjectList<Ref<EntityStore>> tempStorageList2 = SpatialResource.getThreadLocalReferenceList();
                                for (final InvalidatablePersistentRef reference2 : npcReferences) {
                                    final Ref<EntityStore> npcRef3 = reference2.getEntity(_store);
                                    if (npcRef3 == null || !npcRef3.isValid()) {
                                        SpawnMarkerSystems.LOGGER.atWarning().log("Connection with NPC from marker at %s lost due to being invalid/already unloaded", transformComponent.getPosition());
                                    }
                                    else {
                                        final SpawnMarkerReference spawnMarkerReference = _store.ensureAndGetComponent(npcRef3, SpawnMarkerReference.getComponentType());
                                        spawnMarkerReference.getReference().setEntity(ref, store);
                                        tempStorageList2.add(npcRef3);
                                    }
                                }
                                storedFlock.storeNPCs(tempStorageList2, _store);
                                spawnMarkerEntityComponent.setNpcReferences(null);
                            });
                        }
                        return;
                    }
                    if (storedFlock.hasStoredNPCs()) {
                        commandBuffer.run(_store -> {
                            final ObjectList<Ref<EntityStore>> tempStorageList3 = SpatialResource.getThreadLocalReferenceList();
                            storedFlock.restoreNPCs(tempStorageList3, _store);
                            spawnMarkerEntityComponent.setSpawnCount(tempStorageList3.size());
                            final Vector3d position = spawnMarkerEntityComponent.getSpawnPosition();
                            final Vector3f rotation = transformComponent.getRotation();
                            final InvalidatablePersistentRef[] npcReferences2 = new InvalidatablePersistentRef[tempStorageList3.size()];
                            int k = 0;
                            final int bound = tempStorageList3.size();
                            while (k < bound) {
                                final Ref<EntityStore> ref4 = tempStorageList3.get(k);
                                final NPCEntity npcComponent3 = _store.getComponent(ref4, this.npcComponentType);
                                if (!Ticking.$assertionsDisabled && npcComponent3 == null) {
                                    throw new AssertionError();
                                }
                                else {
                                    final TransformComponent npcTransform = _store.getComponent(ref4, this.transformComponentType);
                                    if (!Ticking.$assertionsDisabled && npcTransform == null) {
                                        throw new AssertionError();
                                    }
                                    else {
                                        final HeadRotation npcHeadRotation = _store.getComponent(ref4, this.headRotationComponentType);
                                        if (!Ticking.$assertionsDisabled && npcHeadRotation == null) {
                                            throw new AssertionError();
                                        }
                                        else {
                                            final InvalidatablePersistentRef reference3 = new InvalidatablePersistentRef();
                                            reference3.setEntity(ref4, _store);
                                            npcReferences2[k] = reference3;
                                            npcTransform.getPosition().assign(position);
                                            npcTransform.getRotation().assign(rotation);
                                            npcHeadRotation.setRotation(rotation);
                                            npcComponent3.playAnimation(ref4, AnimationSlot.Status, null, commandBuffer);
                                            ++k;
                                        }
                                    }
                                }
                            }
                            spawnMarkerEntityComponent.setNpcReferences(npcReferences2);
                            spawnMarkerEntityComponent.setDespawnStarted(false);
                            spawnMarkerEntityComponent.setTimeToDeactivation(cachedMarker.getDeactivationTime());
                            return;
                        });
                    }
                }
                if (spawnMarkerEntityComponent.tickSpawnLostTimeout(dt)) {
                    final PersistentRefCount refId2 = archetypeChunk.getComponent(index, this.referenceIdComponentType);
                    if (refId2 != null) {
                        refId2.increment();
                        SpawnMarkerSystems.LOGGER.at(Level.FINE).log("Marker lost spawned NPC and changed reference ID to %s", refId2.get());
                    }
                    final Ref<EntityStore> ref2 = archetypeChunk.getReferenceTo(index);
                    commandBuffer.run(_store -> spawnMarkerEntityComponent.spawnNPC(ref, cachedMarker, _store));
                }
                return;
            }
            if (!world.getWorldConfig().isSpawnMarkersEnabled() || cachedMarker.isManualTrigger() || (spawnMarkerEntityComponent.getSuppressedBy() != null && !spawnMarkerEntityComponent.getSuppressedBy().isEmpty())) {
                return;
            }
            final Ref<EntityStore> ref3 = archetypeChunk.getReferenceTo(index);
            final WorldTimeResource worldTimeResource = commandBuffer.getResource(WorldTimeResource.getResourceType());
            if (cachedMarker.isRealtimeRespawn()) {
                if (spawnMarkerEntityComponent.tickRespawnTimer(dt)) {
                    commandBuffer.run(_store -> spawnMarkerEntityComponent.spawnNPC(ref, cachedMarker, _store));
                }
            }
            else if (spawnMarkerEntityComponent.getSpawnAfter() == null || worldTimeResource.getGameTime().isAfter(spawnMarkerEntityComponent.getSpawnAfter())) {
                commandBuffer.run(_store -> spawnMarkerEntityComponent.spawnNPC(ref, cachedMarker, _store));
            }
        }
    }
}
