package net.minecraft.world.item.crafting;

import net.minecraft.world.Container;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.BannerItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.resources.ResourceLocation;

public class ShieldDecorationRecipe extends CustomRecipe {
    public ShieldDecorationRecipe(final ResourceLocation sm) {
        super(sm);
    }
    
    @Override
    public boolean matches(final CraftingContainer baz, final Level bjw) {
        ItemStack ben4 = ItemStack.EMPTY;
        ItemStack ben5 = ItemStack.EMPTY;
        for (int integer6 = 0; integer6 < baz.getContainerSize(); ++integer6) {
            final ItemStack ben6 = baz.getItem(integer6);
            if (!ben6.isEmpty()) {
                if (ben6.getItem() instanceof BannerItem) {
                    if (!ben5.isEmpty()) {
                        return false;
                    }
                    ben5 = ben6;
                }
                else {
                    if (ben6.getItem() != Items.SHIELD) {
                        return false;
                    }
                    if (!ben4.isEmpty()) {
                        return false;
                    }
                    if (ben6.getTagElement("BlockEntityTag") != null) {
                        return false;
                    }
                    ben4 = ben6;
                }
            }
        }
        return !ben4.isEmpty() && !ben5.isEmpty();
    }
    
    @Override
    public ItemStack assemble(final CraftingContainer baz) {
        ItemStack ben3 = ItemStack.EMPTY;
        ItemStack ben4 = ItemStack.EMPTY;
        for (int integer5 = 0; integer5 < baz.getContainerSize(); ++integer5) {
            final ItemStack ben5 = baz.getItem(integer5);
            if (!ben5.isEmpty()) {
                if (ben5.getItem() instanceof BannerItem) {
                    ben3 = ben5;
                }
                else if (ben5.getItem() == Items.SHIELD) {
                    ben4 = ben5.copy();
                }
            }
        }
        if (ben4.isEmpty()) {
            return ben4;
        }
        final CompoundTag jt5 = ben3.getTagElement("BlockEntityTag");
        final CompoundTag jt6 = (jt5 == null) ? new CompoundTag() : jt5.copy();
        jt6.putInt("Base", ((BannerItem)ben3.getItem()).getColor().getId());
        ben4.addTagElement("BlockEntityTag", jt6);
        return ben4;
    }
    
    @Override
    public boolean canCraftInDimensions(final int integer1, final int integer2) {
        return integer1 * integer2 >= 2;
    }
    
    @Override
    public RecipeSerializer<?> getSerializer() {
        return RecipeSerializer.SHIELD_DECORATION;
    }
}
