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 sm) {
        super(sm);
    }
    
    @Override
    public boolean matches(final CraftingContainer baz, final Level bjw) {
        boolean boolean4 = false;
        int integer5 = 0;
        for (int integer6 = 0; integer6 < baz.getContainerSize(); ++integer6) {
            final ItemStack ben7 = baz.getItem(integer6);
            if (!ben7.isEmpty()) {
                if (FireworkRocketRecipe.PAPER_INGREDIENT.test(ben7)) {
                    if (boolean4) {
                        return false;
                    }
                    boolean4 = true;
                }
                else if (FireworkRocketRecipe.GUNPOWDER_INGREDIENT.test(ben7)) {
                    if (++integer5 > 3) {
                        return false;
                    }
                }
                else if (!FireworkRocketRecipe.STAR_INGREDIENT.test(ben7)) {
                    return false;
                }
            }
        }
        return boolean4 && integer5 >= 1;
    }
    
    @Override
    public ItemStack assemble(final CraftingContainer baz) {
        final ItemStack ben3 = new ItemStack(Items.FIREWORK_ROCKET, 3);
        final CompoundTag jt4 = ben3.getOrCreateTagElement("Fireworks");
        final ListTag jz5 = new ListTag();
        int integer6 = 0;
        for (int integer7 = 0; integer7 < baz.getContainerSize(); ++integer7) {
            final ItemStack ben4 = baz.getItem(integer7);
            if (!ben4.isEmpty()) {
                if (FireworkRocketRecipe.GUNPOWDER_INGREDIENT.test(ben4)) {
                    ++integer6;
                }
                else if (FireworkRocketRecipe.STAR_INGREDIENT.test(ben4)) {
                    final CompoundTag jt5 = ben4.getTagElement("Explosion");
                    if (jt5 != null) {
                        ((AbstractList<CompoundTag>)jz5).add(jt5);
                    }
                }
            }
        }
        jt4.putByte("Flight", (byte)integer6);
        if (!jz5.isEmpty()) {
            jt4.put("Explosions", jz5);
        }
        return ben3;
    }
    
    @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);
    }
}
