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

package com.hypixel.hytale.builtin.buildertools;

import com.hypixel.hytale.component.RemoveReason;
import java.util.Iterator;
import java.util.List;
import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
import com.hypixel.hytale.server.core.inventory.Inventory;
import java.util.Map;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import com.hypixel.hytale.server.core.asset.type.item.config.BuilderToolItemReferenceAsset;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.component.query.Query;
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.HolderSystem;

public class BuilderToolsSystems
{
    public static class EnsureBuilderTools extends HolderSystem<EntityStore>
    {
        @Nonnull
        private static final ComponentType<EntityStore, Player> PLAYER_COMPONENT_TYPE;
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return EnsureBuilderTools.PLAYER_COMPONENT_TYPE;
        }
        
        @Override
        public void onEntityAdd(@Nonnull final Holder<EntityStore> holder, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store) {
            final Player playerComponent = holder.getComponent(EnsureBuilderTools.PLAYER_COMPONENT_TYPE);
            assert playerComponent != null;
            final Map<String, BuilderToolItemReferenceAsset> builderTools = BuilderToolItemReferenceAsset.getAssetMap().getAssetMap();
            final Inventory playerInventory = playerComponent.getInventory();
            final ItemContainer playerTools = playerInventory.getTools();
            playerTools.clear();
            final List<ItemStack> toolsToAdd = new ObjectArrayList<ItemStack>();
            for (final BuilderToolItemReferenceAsset builderTool : builderTools.values()) {
                final String[] items;
                final String[] builderToolItems = items = builderTool.getItems();
                for (final String builderToolItem : items) {
                    toolsToAdd.add(new ItemStack(builderToolItem));
                }
            }
            if (!playerTools.addItemStacks(toolsToAdd).succeeded()) {
                throw new IllegalArgumentException("Could not add items to the Tools container");
            }
        }
        
        @Override
        public void onEntityRemoved(@Nonnull final Holder<EntityStore> holder, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store) {
        }
        
        static {
            PLAYER_COMPONENT_TYPE = Player.getComponentType();
        }
    }
}
