Code Driven Recipes in StationAPI can be registered using the conveniently named RecipeRegisterEvent
, if you want to learn how to subscribe to an event you can go to Entrypoints & Events.
This approach has 2 main benefits compared to data driven recipes:
- You can form the recipes dynamically, meaning that you can decide the recipe at the time of registration once all blocks and items are registered
- You can decide to just not register the recipe if some condition is met/not met
Registering a Recipe
To register the recipe you will need to listen to the RecipeRegisterEvent
, but beware, this event is called for each recipe type, so if you just register your recipe there it will get registered 3 times, this is why we need to first get the type of recipe that is being registered and only register our recipes if the type is correct, let’s look at how to do that:
src/main/java/net/danygames2014/examplemod/event/RecipeListener.java
So, we are now listening to the RecipeRegisterEvent
, when the event comes in, we are using the fromType
method to get the type of the recipe and then if that matches our desired type, an if condition will pass and our code will run.
Let’s now look at each of the recipe types and how the method to add them is formatted, the parameters are pretty much the same as when you define it using JSON files, just layed out a bit differently.
Shaped Recipe
To add a shaped recipe we will want to use the CraftingRegistry.addShapedRecipe
method which takes the following parameters: output, pattern and pattern keys. Let’s look at an example and break it down:
We will split it into multiple lines so its a bit more digestable
Okay, this is better, and you might already start seeing how this is formatted but let’s go over it:
output
- the first parameter is always the output ItemStack
pattern
- the pattern is 1 - 3 strings containing the pattern, so you can for example only have 2 strings for a 2x2 crafting pattern
pattern keys
- the pattern keys define what the characters in your pattern stand for, theyre always a char (a single letter in apostrophes ( ’ )), followed by an Item
, ItemStack
or Block
Here is the result of this recipe:
Shapeless Recipe
To add a shapeless recipe we will want to use the CraftingRegistry.addShapelessRecipe
method which takes the output and ingredients parameters. Let’s look at an example and break it down:
We will split it into multiple lines so its a bit more digestable
output
- The first parameter is always the output ItemStack
ingredients
- All the following parameters are ingredients, they can be either Item
, Block
or ItemStack
Here is the result of this recipe:
Smelting Recipe
To add a smelting recipe we will want to use the SmeltingRegistry.addSmeltingRecipe
method which takes the input and output paremeters. Let’s lok at an example and break it down:
We will split it into multiple lines so its a bit more digestable
input
- Unlike with the crafting recipes, the first parameter here is the input ItemStack
output
- The second parameter is the output ItemStack
And here is the result of this recipe:
External Recources
Examples:
NyaWiki Example Mod
StationAPI Test Mod