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

package com.hypixel.hytale.server.core.modules.entity.system;

import java.util.Iterator;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.protocol.GameMode;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ArchetypeChunk;
import javax.annotation.Nullable;
import com.hypixel.hytale.component.SystemGroup;
import java.util.Collections;
import com.hypixel.hytale.component.dependency.SystemDependency;
import com.hypixel.hytale.component.dependency.Order;
import com.hypixel.hytale.server.core.modules.entity.EntityModule;
import com.hypixel.hytale.component.dependency.Dependency;
import java.util.Set;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.core.modules.entity.player.PlayerSettings;
import com.hypixel.hytale.server.core.modules.entity.component.HiddenFromAdventurePlayers;
import com.hypixel.hytale.server.core.entity.entities.Player;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.modules.entity.tracker.EntityTrackerSystems;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;

public class HideEntitySystems
{
    public static class AdventurePlayerSystem extends EntityTickingSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, EntityTrackerSystems.EntityViewer> entityViewerComponentType;
        @Nonnull
        private final ComponentType<EntityStore, Player> playerComponentType;
        @Nonnull
        private final ComponentType<EntityStore, HiddenFromAdventurePlayers> hiddenFromAdventurePlayersComponentType;
        @Nonnull
        private final ComponentType<EntityStore, PlayerSettings> playerSettingsComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        @Nonnull
        private final Set<Dependency<EntityStore>> dependencies;
        
        public AdventurePlayerSystem() {
            this.entityViewerComponentType = EntityTrackerSystems.EntityViewer.getComponentType();
            this.playerComponentType = Player.getComponentType();
            this.hiddenFromAdventurePlayersComponentType = HiddenFromAdventurePlayers.getComponentType();
            this.playerSettingsComponentType = EntityModule.get().getPlayerSettingsComponentType();
            this.query = (Query<EntityStore>)Query.and(this.entityViewerComponentType, this.playerComponentType, this.playerSettingsComponentType);
            this.dependencies = (Set<Dependency<EntityStore>>)Collections.singleton(new SystemDependency(Order.AFTER, EntityTrackerSystems.CollectVisible.class));
        }
        
        @Nullable
        @Override
        public SystemGroup<EntityStore> getGroup() {
            return EntityTrackerSystems.FIND_VISIBLE_ENTITIES_GROUP;
        }
        
        @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 EntityTrackerSystems.EntityViewer entityViewerComponent = archetypeChunk.getComponent(index, this.entityViewerComponentType);
            assert entityViewerComponent != null;
            final PlayerSettings playerSettingsComponent = archetypeChunk.getComponent(index, this.playerSettingsComponentType);
            assert playerSettingsComponent != null;
            final Player playerComponent = archetypeChunk.getComponent(index, this.playerComponentType);
            assert playerComponent != null;
            if (playerComponent.getGameMode() != GameMode.Adventure && playerSettingsComponent.showEntityMarkers()) {
                return;
            }
            final Iterator<Ref<EntityStore>> iterator = entityViewerComponent.visible.iterator();
            while (iterator.hasNext()) {
                final Ref<EntityStore> ref = iterator.next();
                if (commandBuffer.getArchetype(ref).contains(this.hiddenFromAdventurePlayersComponentType)) {
                    final EntityTrackerSystems.EntityViewer entityViewer = entityViewerComponent;
                    ++entityViewer.hiddenCount;
                    iterator.remove();
                }
            }
        }
    }
}
