package net.minecraft.world.item.crafting;

import java.util.AbstractList;
import net.minecraft.world.Container;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.Items;
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 FireworkRocketRecipe extends CustomRecipe {
    private static final Ingredient PAPER_INGREDIENT;
    private static final Ingredient GUNPOWDER_INGREDIENT;
    private static final Ingredient STAR_INGREDIENT;
    
    public FireworkRocketRecipe(final ResourceLocation uh) {
        super(uh);
    }
    
    @Override
    public boolean matches(final CraftingContainer bgu, final Level bqb) {
        boolean boolean4 = false;
        int integer5 = 0;
        for (int integer6 = 0; integer6 < bgu.getContainerSize(); ++integer6) {
            final ItemStack bki7 = bgu.getItem(integer6);
            if (!bki7.isEmpty()) {
                if (FireworkRocketRecipe.PAPER_INGREDIENT.test(bki7)) {
                    if (boolean4) {
                        return false;
                    }
                    boolean4 = true;
                }
                else if (FireworkRocketRecipe.GUNPOWDER_INGREDIENT.test(bki7)) {
                    if (++integer5 > 3) {
                        return false;
                    }
                }
                else if (!FireworkRocketRecipe.STAR_INGREDIENT.test(bki7)) {
                    return false;
                }
            }
        }
        return boolean4 && integer5 >= 1;
    }
    
    @Override
    public ItemStack assemble(final CraftingContainer bgu) {
        final ItemStack bki3 = new ItemStack(Items.FIREWORK_ROCKET, 3);
        final CompoundTag le4 = bki3.getOrCreateTagElement("Fireworks");
        final ListTag lk5 = new ListTag();
        int integer6 = 0;
        for (int integer7 = 0; integer7 < bgu.getContainerSize(); ++integer7) {
            final ItemStack bki4 = bgu.getItem(integer7);
            if (!bki4.isEmpty()) {
                if (FireworkRocketRecipe.GUNPOWDER_INGREDIENT.test(bki4)) {
                    ++integer6;
                }
                else if (FireworkRocketRecipe.STAR_INGREDIENT.test(bki4)) {
                    final CompoundTag le5 = bki4.getTagElement("Explosion");
                    if (le5 != null) {
                        ((AbstractList<CompoundTag>)lk5).add(le5);
                    }
                }
            }
        }
        le4.putByte("Flight", (byte)integer6);
        if (!lk5.isEmpty()) {
            le4.put("Explosions", lk5);
        }
        return bki3;
    }
    
    @Override
    public boolean canCraftInDimensions(final int integer1, final int integer2) {
        return integer1 * integer2 >= 2;
    }
    
    @Override
    public ItemStack getResultItem() {
        return new ItemStack(Items.FIREWORK_ROCKET);
    }
    
    @Override
    public RecipeSerializer<?> getSerializer() {
        return RecipeSerializer.FIREWORK_ROCKET;
    }
    
    static {
        PAPER_INGREDIENT = Ingredient.of(Items.PAPER);
        GUNPOWDER_INGREDIENT = Ingredient.of(Items.GUNPOWDER);
        STAR_INGREDIENT = Ingredient.of(Items.FIREWORK_STAR);
    }
}
