Custom Recipes

From Forge Community Wiki
Revision as of 22:41, 2 August 2021 by ChampionAsh5357 (talk | contribs) (Update to 1.17 and fix all the wrong information, probably should come back later to finish at some point)

Custom Recipe Serializers

Want to make a custom recipe type for a machine, or some mechanic your mod has? Great! It means users can participate in the datapack system for recipes, so you get automatic syncing, updating, and ease of maintenance on servers. Here's what you need to get started:

Classes/Interfaces for the inventories and serialization

RecipeSerializer

The thing that actually serializes recipe files. It implements the vanilla recipe serializer interface and provides means of reading from JSON and performing read/write operations on network buffer objects. One should look into using Codecs for this class, as using them simplifies the logic significantly.

public class MyRecipeSerializer 
  extends ForgeRegistryEntry<RecipeSerializer<?>> 
  implements RecipeSerializer<MyRecipe> {
  
  // your serializer code here

}

RecipeType

Links the recipe type to its implementation class(es). This is a very lightweight class that vanilla uses to determine what recipes are associated with what implementors (e.g. CRAFTING is for SHAPED_RECIPE, SHAPELESS_RECIPE, etc.).

Container

Vanilla's inventory system used in recipes to grab references to implementors to see if the associated recipe matches the current container data.

Recipe

Your actual recipe class or an abstraction to use your own recipe matching system. Takes the Container from above and matches it. Your recipe class should return the RecipeType from registration in #getType and the serializer in #getSerializer.

Setup

Once you have the above implementations, here's how to wire everything up:

  1. Register your recipe serializer to the RECIPE_SERIALIZERS Forge registry.
  2. Register the recipe type during the common setup event or statically. See below.
  3. Place a JSON file in data/recipes/my_recipe.json and ensure it has "type": "mymod:my_recipe_serializer_registry_name" in the JSON structure.
RecipeType.register("mymod:my_recipe_type");

This will load the recipe using the recipe serialization system, and wire it back to the serializer via matching the registry name of the serializer to the value you specified while registering the serializer in step 1.