How do I add a minecraft crafting recipe with nbt data in fabric mod?
- User
How do I add a minecraft crafting recipe with nbt data in fabric mod?
Assistant
Adding a crafting recipe with NBT data in a Fabric mod involves several steps. Here's a general outline of the process:
- Create a new class for your crafting recipe that extends the
SimpleCraftingRecipeclass or one of its subclasses. This class should define the inputs and outputs of the recipe, as well as any NBT data that should be applied to the output. - Define the inputs and outputs of the recipe using
IngredientandItemStackobjects, respectively. For example, you might create anIngredientthat matches any wooden planks and anItemStackthat represents a stick with a specific NBT tag. - Apply the NBT data to the output
ItemStackusing theNBTCompoundclass. This class allows you to create and modify NBT data, which can then be added to anItemStackusing thesetTagmethod. - Register the recipe with the game using the
CraftingRecipeRegistryclass. This class provides several methods for registering different types of recipes, including shaped and shapeless recipes. You can use theregistermethod to register your recipe, passing in a unique identifier and an instance of your recipe class.
Here's some example code to get you started:
import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.nbt.NBTCompound; import net.minecraft.nbt.NBTUtil; import net.minecraft.network.PacketByteBuf; import net.minecraft.recipe.Ingredient; import net.minecraft.recipe.RecipeSerializer; import net.minecraft.recipe.RecipeType; import net.minecraft.util.Identifier; import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.registry.Registry; import net.minecraft.world.World; import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.registries.RegistryObject; public class NBTCraftingRecipe extends SimpleCraftingRecipe { private final Ingredient input; private final ItemStack output; public NBTCraftingRecipe(Identifier id, Ingredient input, ItemStack output) { super(id, DefaultedList.ofSize(9, Ingredient.EMPTY), output); this.input = input; this.output = output; } @Override public boolean matches(CraftingInventory inv, World world) { return input.test(inv.getStack(0)); } @Override public ItemStack craft(CraftingInventory inv) { ItemStack stack = output.copy(); stack.setCount(1); if (stack.hasNbt()) { NBTCompound nbt = stack.getNbt(); NBTUtil.merge(nbt, input.getMatchingStacks()[0].getNbt()); } else { stack.setNbt(input.getMatchingStacks()[0].getNbt().copy()); } return stack; } @Override public boolean fits(int width, int height) { return width * height >= 1; } @Override public RecipeSerializer<?> getSerializer() { return ModRecipeSerializers.NBT_CRAFTING_RECIPE; } public static class Serializer implements RecipeSerializer<NBTCraftingRecipe> { @Override public NBTCraftingRecipe read(Identifier id, PacketByteBuf buf) { Ingredient input = Ingredient.fromPacket(buf); ItemStack output = buf.readItemStack(); NBTCompound nbt = buf.readNbt(); if (nbt != null) { output.setNbt(nbt); } return new NBTCraftingRecipe(id, input, output); } @Override public NBTCraftingRecipe read(Identifier id, JsonObject json) { Ingredient input = Ingredient.- Create a new class for your crafting recipe that extends the
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.