Custom Recipes

From Forge Community Wiki
Revision as of 17:32, 5 June 2021 by RobotGryphon (talk | contribs) (Created page with "= 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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

IRecipeSerializer

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<IRecipeSerializer<?>> 
  implements IRecipeSerializer<MyRecipeType> {
  
  // your serializer code here

}

IRecipeType

Links the recipe type to its implementation class. This is a very lightweight class that vanilla uses to make a recipe file's type line to which serializer should be used to deserialize a recipe into an instance.

IInventory

Wraps vanilla's IInventory system. Here's a reference. Note this should not be used directly; you should make a fake inventory class and handle the actual match implementation with your own methods and logic.

IRecipe

Your actual recipe class or an abstraction to cheat vanilla's system and use your own recipe matching system. Takes the IInventory from above and matches it. Your recipe class should return the IRecipeType from registration in the getType override.

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 registration (constructor) phase. See below.
  3. Place a JSON file in data/recipes/my_recipe.json and ensure it has "type": "mymod:my_recipe_type" in the JSON structure.
Registry.register(Registry.RECIPE_TYPE, new ResLoc('mymod:my_recipe_type'), MyRecipeType.class);

This will load the recipe using the recipe serialization system, and wire it back to the serializer via type matching the value you specified during the RECIPE_TYPE registration above, in step 2.