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

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

import com.hypixel.hytale.server.core.inventory.Inventory;
import com.hypixel.hytale.protocol.Packet;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.entity.entities.Player;
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 PlayerSendInventorySystem extends EntityTickingSystem<EntityStore>
{
    @Nonnull
    private final ComponentType<EntityStore, Player> componentType;
    @Nonnull
    private final ComponentType<EntityStore, PlayerRef> refComponentType;
    @Nonnull
    private final Query<EntityStore> query;
    
    public PlayerSendInventorySystem(final ComponentType<EntityStore, Player> componentType) {
        this.refComponentType = PlayerRef.getComponentType();
        this.componentType = componentType;
        this.query = (Query<EntityStore>)Query.and(componentType, this.refComponentType);
    }
    
    @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 Player playerComponent = archetypeChunk.getComponent(index, this.componentType);
        assert playerComponent != null;
        final Inventory inventory = playerComponent.getInventory();
        if (inventory.consumeIsDirty()) {
            final PlayerRef playerRefComponent = archetypeChunk.getComponent(index, this.refComponentType);
            assert playerRefComponent != null;
            playerRefComponent.getPacketHandler().write(inventory.toPacket());
        }
        playerComponent.getWindowManager().updateWindows();
    }
}
