package net.minecraft.world.inventory;

import net.minecraft.core.NonNullList;
import net.minecraft.world.item.crafting.CraftingRecipe;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;

public class ResultSlot extends Slot {
    private final CraftingContainer craftSlots;
    private final Player player;
    private int removeCount;
    
    public ResultSlot(final Player bec, final CraftingContainer bgu, final Container amz, final int integer4, final int integer5, final int integer6) {
        super(amz, integer4, integer5, integer6);
        this.player = bec;
        this.craftSlots = bgu;
    }
    
    @Override
    public boolean mayPlace(final ItemStack bki) {
        return false;
    }
    
    @Override
    public ItemStack remove(final int integer) {
        if (this.hasItem()) {
            this.removeCount += Math.min(integer, this.getItem().getCount());
        }
        return super.remove(integer);
    }
    
    @Override
    protected void onQuickCraft(final ItemStack bki, final int integer) {
        this.removeCount += integer;
        this.checkTakeAchievements(bki);
    }
    
    @Override
    protected void onSwapCraft(final int integer) {
        this.removeCount += integer;
    }
    
    @Override
    protected void checkTakeAchievements(final ItemStack bki) {
        if (this.removeCount > 0) {
            bki.onCraftedBy(this.player.level, this.player, this.removeCount);
        }
        if (this.container instanceof RecipeHolder) {
            ((RecipeHolder)this.container).awardUsedRecipes(this.player);
        }
        this.removeCount = 0;
    }
    
    @Override
    public ItemStack onTake(final Player bec, final ItemStack bki) {
        this.checkTakeAchievements(bki);
        final NonNullList<ItemStack> gi4 = bec.level.getRecipeManager().<CraftingContainer, CraftingRecipe>getRemainingItemsFor(RecipeType.CRAFTING, this.craftSlots, bec.level);
        for (int integer5 = 0; integer5 < gi4.size(); ++integer5) {
            ItemStack bki2 = this.craftSlots.getItem(integer5);
            final ItemStack bki3 = gi4.get(integer5);
            if (!bki2.isEmpty()) {
                this.craftSlots.removeItem(integer5, 1);
                bki2 = this.craftSlots.getItem(integer5);
            }
            if (!bki3.isEmpty()) {
                if (bki2.isEmpty()) {
                    this.craftSlots.setItem(integer5, bki3);
                }
                else if (ItemStack.isSame(bki2, bki3) && ItemStack.tagMatches(bki2, bki3)) {
                    bki3.grow(bki2.getCount());
                    this.craftSlots.setItem(integer5, bki3);
                }
                else if (!this.player.inventory.add(bki3)) {
                    this.player.drop(bki3, false);
                }
            }
        }
        return bki;
    }
}
