2,577 bytes added
, 03:58, 27 July 2021
= 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/1.16]] for this class, as using them simplifies the logic significantly.
<syntaxhighlight lang="java">
public class MyRecipeSerializer
extends ForgeRegistryEntry<IRecipeSerializer<?>>
implements IRecipeSerializer<MyRecipeType> {
// your serializer code here
}
</syntaxhighlight>
==== IRecipeType ====
Links the recipe type to its implementation class. This is a very lightweight class that vanilla uses to make a recipe file's <code>type</code> line to which serializer should be used to deserialize a recipe into an instance.
==== IInventory ====
Wraps vanilla's <code>IInventory</code> system. [https://gist.github.com/robotgryphon/344e5815e27f28fef7a837c4f98ade40 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 <code>IInventory</code> from above and matches it. Your recipe class should return the <code>IRecipeType</code> from registration in the getType override.
== Setup ==
Once you have the above implementations, here's how to wire everything up:
# Register your recipe serializer to the <code>RECIPE_SERIALIZERS</code> Forge registry.
# Register the recipe type during the registration (constructor) phase. See below.
# Place a JSON file in <code>data/recipes/my_recipe.json</code> and ensure it has <code>"type": "mymod:my_recipe_type"</code> in the JSON structure.
<syntaxhighlight lang="java">
Registry.register(Registry.RECIPE_TYPE, new ResLoc('mymod:my_recipe_type'), MyRecipeType.class);
</syntaxhighlight>
This will load the recipe using the recipe serialization system, and wire it back to the serializer via matching the registry name in <code>type</code> to the value you specified while registering the serializer in step 1.