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

package com.hypixel.hytale.server.core.universe.world;

import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.server.core.asset.type.model.config.Model;
import com.hypixel.hytale.server.core.modules.entity.component.ModelComponent;
import com.hypixel.hytale.server.core.cosmetics.CosmeticsModule;
import com.hypixel.hytale.server.core.modules.entity.player.PlayerSkinComponent;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.server.core.entity.entities.player.HiddenPlayersManager;
import java.util.Iterator;
import com.hypixel.hytale.server.core.Message;
import java.util.UUID;
import javax.annotation.Nullable;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.modules.entity.tracker.EntityTrackerSystems;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.function.consumer.TriConsumer;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;

public class PlayerUtil
{
    static final /* synthetic */ boolean $assertionsDisabled;
    
    public static void forEachPlayerThatCanSeeEntity(@Nonnull final Ref<EntityStore> ref, @Nonnull final TriConsumer<Ref<EntityStore>, PlayerRef, ComponentAccessor<EntityStore>> consumer, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        final Store<EntityStore> store = componentAccessor.getExternalData().getStore();
        final ComponentType<EntityStore, PlayerRef> playerRefComponentType = PlayerRef.getComponentType();
        store.forEachChunk(playerRefComponentType, (archetypeChunk, commandBuffer) -> {
            for (int index = 0; index < archetypeChunk.size(); ++index) {
                final EntityTrackerSystems.EntityViewer entityViewerComponent = archetypeChunk.getComponent(index, EntityTrackerSystems.EntityViewer.getComponentType());
                if (entityViewerComponent != null && entityViewerComponent.visible.contains(ref)) {
                    final Ref<EntityStore> targetPlayerRef = archetypeChunk.getReferenceTo(index);
                    final PlayerRef targetPlayerRefComponent = archetypeChunk.getComponent(index, playerRefComponentType);
                    if (!PlayerUtil.$assertionsDisabled && targetPlayerRefComponent == null) {
                        throw new AssertionError();
                    }
                    else {
                        consumer.accept(targetPlayerRef, targetPlayerRefComponent, commandBuffer);
                    }
                }
            }
        });
    }
    
    public static void forEachPlayerThatCanSeeEntity(@Nonnull final Ref<EntityStore> ref, @Nonnull final TriConsumer<Ref<EntityStore>, PlayerRef, ComponentAccessor<EntityStore>> consumer, @Nullable final Ref<EntityStore> ignoredPlayerRef, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        final Store<EntityStore> store = componentAccessor.getExternalData().getStore();
        final ComponentType<EntityStore, PlayerRef> playerRefComponentType = PlayerRef.getComponentType();
        store.forEachChunk(playerRefComponentType, (archetypeChunk, commandBuffer) -> {
            for (int index = 0; index < archetypeChunk.size(); ++index) {
                final EntityTrackerSystems.EntityViewer entityViewerComponent = archetypeChunk.getComponent(index, EntityTrackerSystems.EntityViewer.getComponentType());
                if (entityViewerComponent != null && entityViewerComponent.visible.contains(ref)) {
                    final Ref<EntityStore> targetRef = archetypeChunk.getReferenceTo(index);
                    if (!targetRef.equals(ignoredPlayerRef)) {
                        final PlayerRef targetPlayerRefComponent = archetypeChunk.getComponent(index, playerRefComponentType);
                        if (!PlayerUtil.$assertionsDisabled && targetPlayerRefComponent == null) {
                            throw new AssertionError();
                        }
                        else {
                            consumer.accept(targetRef, targetPlayerRefComponent, commandBuffer);
                        }
                    }
                }
            }
        });
    }
    
    public static void broadcastMessageToPlayers(@Nullable final UUID sourcePlayerUuid, @Nonnull final Message message, @Nonnull final Store<EntityStore> store) {
        final World world = store.getExternalData().getWorld();
        for (final PlayerRef targetPlayerRef : world.getPlayerRefs()) {
            final HiddenPlayersManager targetHiddenPlayersManager = targetPlayerRef.getHiddenPlayersManager();
            if (sourcePlayerUuid == null || !targetHiddenPlayersManager.isPlayerHidden(sourcePlayerUuid)) {
                targetPlayerRef.sendMessage(message);
            }
        }
    }
    
    public static void broadcastPacketToPlayers(@Nonnull final ComponentAccessor<EntityStore> componentAccessor, @Nonnull final Packet packet) {
        final World world = componentAccessor.getExternalData().getWorld();
        for (final PlayerRef targetPlayerRef : world.getPlayerRefs()) {
            targetPlayerRef.getPacketHandler().write(packet);
        }
    }
    
    public static void broadcastPacketToPlayersNoCache(@Nonnull final ComponentAccessor<EntityStore> componentAccessor, @Nonnull final Packet packet) {
        final World world = componentAccessor.getExternalData().getWorld();
        for (final PlayerRef targetPlayerRef : world.getPlayerRefs()) {
            targetPlayerRef.getPacketHandler().writeNoCache(packet);
        }
    }
    
    public static void broadcastPacketToPlayers(@Nonnull final ComponentAccessor<EntityStore> componentAccessor, @Nonnull final Packet... packets) {
        final World world = componentAccessor.getExternalData().getWorld();
        for (final PlayerRef targetPlayerRef : world.getPlayerRefs()) {
            targetPlayerRef.getPacketHandler().write(packets);
        }
    }
    
    @Deprecated(forRemoval = true)
    public static void resetPlayerModel(@Nonnull final Ref<EntityStore> ref, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        final PlayerSkinComponent playerSkinComponent = componentAccessor.getComponent(ref, PlayerSkinComponent.getComponentType());
        if (playerSkinComponent == null) {
            return;
        }
        playerSkinComponent.setNetworkOutdated();
        final Model newModel = CosmeticsModule.get().createModel(playerSkinComponent.getPlayerSkin());
        if (newModel != null) {
            componentAccessor.putComponent(ref, ModelComponent.getComponentType(), new ModelComponent(newModel));
        }
    }
}
