package net.minecraft.world.item.crafting;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.ItemLike;
import net.minecraft.core.Registry;
import net.minecraft.world.item.Item;
import com.google.gson.JsonElement;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonObject;
import net.minecraft.core.NonNullList;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.Container;

public abstract class SingleItemRecipe implements Recipe<Container> {
    protected final Ingredient ingredient;
    protected final ItemStack result;
    private final RecipeType<?> type;
    private final RecipeSerializer<?> serializer;
    protected final ResourceLocation id;
    protected final String group;
    
    public SingleItemRecipe(final RecipeType<?> bmx, final RecipeSerializer<?> bmw, final ResourceLocation uh, final String string, final Ingredient bmr, final ItemStack bki) {
        this.type = bmx;
        this.serializer = bmw;
        this.id = uh;
        this.group = string;
        this.ingredient = bmr;
        this.result = bki;
    }
    
    @Override
    public RecipeType<?> getType() {
        return this.type;
    }
    
    @Override
    public RecipeSerializer<?> getSerializer() {
        return this.serializer;
    }
    
    @Override
    public ResourceLocation getId() {
        return this.id;
    }
    
    @Override
    public String getGroup() {
        return this.group;
    }
    
    @Override
    public ItemStack getResultItem() {
        return this.result;
    }
    
    @Override
    public NonNullList<Ingredient> getIngredients() {
        final NonNullList<Ingredient> gi2 = NonNullList.<Ingredient>create();
        gi2.add(this.ingredient);
        return gi2;
    }
    
    @Override
    public boolean canCraftInDimensions(final int integer1, final int integer2) {
        return true;
    }
    
    @Override
    public ItemStack assemble(final Container amz) {
        return this.result.copy();
    }
    
    public static class Serializer<T extends SingleItemRecipe> implements RecipeSerializer<T> {
        final SingleItemMaker<T> factory;
        
        protected Serializer(final SingleItemMaker<T> a) {
            this.factory = a;
        }
        
        @Override
        public T fromJson(final ResourceLocation uh, final JsonObject jsonObject) {
            final String string4 = GsonHelper.getAsString(jsonObject, "group", "");
            Ingredient bmr5;
            if (GsonHelper.isArrayNode(jsonObject, "ingredient")) {
                bmr5 = Ingredient.fromJson(GsonHelper.getAsJsonArray(jsonObject, "ingredient"));
            }
            else {
                bmr5 = Ingredient.fromJson(GsonHelper.getAsJsonObject(jsonObject, "ingredient"));
            }
            final String string5 = GsonHelper.getAsString(jsonObject, "result");
            final int integer7 = GsonHelper.getAsInt(jsonObject, "count");
            final ItemStack bki8 = new ItemStack(Registry.ITEM.get(new ResourceLocation(string5)), integer7);
            return this.factory.create(uh, string4, bmr5, bki8);
        }
        
        @Override
        public T fromNetwork(final ResourceLocation uh, final FriendlyByteBuf mg) {
            final String string4 = mg.readUtf(32767);
            final Ingredient bmr5 = Ingredient.fromNetwork(mg);
            final ItemStack bki6 = mg.readItem();
            return this.factory.create(uh, string4, bmr5, bki6);
        }
        
        @Override
        public void toNetwork(final FriendlyByteBuf mg, final T bnf) {
            mg.writeUtf(bnf.group);
            bnf.ingredient.toNetwork(mg);
            mg.writeItem(bnf.result);
        }
        
        interface SingleItemMaker<T extends SingleItemRecipe> {
            T create(final ResourceLocation uh, final String string, final Ingredient bmr, final ItemStack bki);
        }
    }
}
