package net.minecraft.world.inventory;

import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.stats.Stats;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.Container;
import net.minecraft.world.item.trading.Merchant;
import net.minecraft.world.entity.player.Player;

public class MerchantResultSlot extends Slot {
    private final MerchantContainer slots;
    private final Player player;
    private int removeCount;
    private final Merchant merchant;
    
    public MerchantResultSlot(final Player ayj, final Merchant biy, final MerchantContainer bbp, final int integer4, final int integer5, final int integer6) {
        super(bbp, integer4, integer5, integer6);
        this.player = ayj;
        this.merchant = biy;
        this.slots = bbp;
    }
    
    @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 checkTakeAchievements(final ItemStack ben) {
        ben.onCraftedBy(this.player.level, this.player, this.removeCount);
        this.removeCount = 0;
    }
    
    @Override
    public ItemStack onTake(final Player ayj, final ItemStack ben) {
        this.checkTakeAchievements(ben);
        final MerchantOffer biz4 = this.slots.getActiveOffer();
        if (biz4 != null) {
            final ItemStack ben2 = this.slots.getItem(0);
            final ItemStack ben3 = this.slots.getItem(1);
            if (biz4.take(ben2, ben3) || biz4.take(ben3, ben2)) {
                this.merchant.notifyTrade(biz4);
                ayj.awardStat(Stats.TRADED_WITH_VILLAGER);
                this.slots.setItem(0, ben2);
                this.slots.setItem(1, ben3);
            }
            this.merchant.overrideXp(this.merchant.getVillagerXp() + biz4.getXp());
        }
        return ben;
    }
}
