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 ayj, final CraftingContainer baz, final Container aje, final int integer4, final int integer5, final int integer6) {
        super(aje, integer4, integer5, integer6);
        this.player = ayj;
        this.craftSlots = baz;
    }
    
    @Override
    public boolean mayPlace(final ItemStack ben) {
        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 ben, final int integer) {
        this.removeCount += integer;
        this.checkTakeAchievements(ben);
    }
    
    @Override
    protected void onSwapCraft(final int integer) {
        this.removeCount += integer;
    }
    
    @Override
    protected void checkTakeAchievements(final ItemStack ben) {
        if (this.removeCount > 0) {
            ben.onCraftedBy(this.player.level, this.player, this.removeCount);
        }
        if (this.container instanceof RecipeHolder) {
            ((RecipeHolder)this.container).awardAndReset(this.player);
        }
        this.removeCount = 0;
    }
    
    @Override
    public ItemStack onTake(final Player ayj, final ItemStack ben) {
        this.checkTakeAchievements(ben);
        final NonNullList<ItemStack> fy4 = ayj.level.getRecipeManager().<CraftingContainer, CraftingRecipe>getRemainingItemsFor(RecipeType.CRAFTING, this.craftSlots, ayj.level);
        for (int integer5 = 0; integer5 < fy4.size(); ++integer5) {
            ItemStack ben2 = this.craftSlots.getItem(integer5);
            final ItemStack ben3 = fy4.get(integer5);
            if (!ben2.isEmpty()) {
                this.craftSlots.removeItem(integer5, 1);
                ben2 = this.craftSlots.getItem(integer5);
            }
            if (!ben3.isEmpty()) {
                if (ben2.isEmpty()) {
                    this.craftSlots.setItem(integer5, ben3);
                }
                else if (ItemStack.isSame(ben2, ben3) && ItemStack.tagMatches(ben2, ben3)) {
                    ben3.grow(ben2.getCount());
                    this.craftSlots.setItem(integer5, ben3);
                }
                else if (!this.player.inventory.add(ben3)) {
                    this.player.drop(ben3, false);
                }
            }
        }
        return ben;
    }
}
