Hello and welcome back to another tutorial today we’re going to be covering recipe serializers which allows us to create our own jason recipe type a couple of episodes ago we created an event that gives us a diamond if we throw something when we stand on an example block today i’m going to be Making it so that we can create a recipe which tells us what block we have to stand on and what item we have to throw and then give us a different output as a result so let’s begin in our common package let’s create another new package called Dot recipe and in here let’s create a new class called example recipe and this is going to implement i recipe of i inventory let’s press ctrl shift o to import i inventory and then we need to add some methods into this class to remove this error however first inside here let’s create a Private static class is going to be called serializer and this is going to extend a forward registry entry of any eye recipe serializer and it’s going to implement an eye recipe serializer of our example recipe so next let’s create a constructor like so and we actually don’t need this to be Public and in here we can set the registry name with this dot set registry name and then we can pass in the mod id and the name so let’s do tutorial mod dot mod id and then as the name we can have example underscore recipe next after the Constructor we need to add a read method so first let’s read using a json object and we also need a method for reading with the packet buffer and a method for writing to the packet buffer and we can fill these methods out later so at the top let’s create a public static Final serializer and we can call this serializer we can set it to a new serializer then we need the fields for our recipes let’s create a private final ingredient input a private final item stack output a private final block which is going to be the block that we stand on and a private Final resource location which is going to be the id then let’s press ctrl shift o to import all of these and make sure we select net.minecraft.block and now we need to initialize these so let’s create a example recipe constructor and this is going to take a resource location id An ingredient input an item stack output and a block block and then all we need to do in this constructor is set this dot id equal to the id that’s passed in and so on for all the other values and there we go now that we’ve removed all these errors we Can fill in these functions down here so first let’s create a final json element and we’re going to call this input element and we’re going to set it equal to jsonutils.json array and then we can pass in the json object and then the json here is going to be Input and if it is we’re going to get the json array so utils.get json array of json and input and otherwise we need to get the json objects so jsonutils.get json object of json and input then let’s press ctrl shift o to import json element and then we can get the final ingredient Input and we’re going to set it equal to ingredient dot deserialize the input element next we need to get the output let’s do final item stack output is equal to shaped recipe dot d serialize item then we need to get the json object so we can do json Utils dot get json object and then we can pass in just json and this time instead of input is going to be output next we need to get the block let’s do final block block is equal to forge registries dot blocks dot get value and then we Need to pass in the resource location key which is going to be a new resource location and then we need to pass in the value from the json object so we can do jsonutils.getstring of json and block next we need to check if the block actually exists so we’re going To say if block is equal to null let’s throw a new illegal state exception saying block plus block dot to string does not exist and finally we can return a new example recipe using the constructor made above so let’s pass in the recipe id input output and Block let’s just change this to block does not exist because we’re only going to output this error if the block is null so next let’s do a final ingredient input and we’re going to set this equal to ingredient dot read buffer then we can do a final item stack Stack i’m going to set that equal to buffer dot read item stack then a final block block which is going to be equal to forge registries dot blocks dot get value of buffer dot read resource location then once again we can copy all of this down here next in Right we can do recipe dot input dot right to the buffer then we can do buffer dot write item stack of recipe dot output and then buffer dot write resource location of recipe dot block dot get registry name and we want to make sure that we do this in the same Order as we read and that’s it for our serializer now we can add some methods into our class so first let’s do dot matches and we can return this dot input dot test of inventory dot get stack and slot zero then we can get the crafting result and we want to do This dot output dot copy and this is important because if we just use the output it would use up the item stack in this class so if we create a copy then this item stack will remain here if we just want the item stack to be used from the recipe We can do get recipe output and then just return this dot output then let’s override two more functions get id we’re going to return this dot id and get serializer we’re going to return this dot serializer next let’s override get type and we’re going to return Recipe init which is a class we haven’t made yet dot example recipe and for the recipe icon i’m just going to return a new item stack of our item init dot example item dot get finally let’s create a public boolean called is valid and we’re going to pass in An item stack input and a block block and then let’s return this dot input dot test input and block is equal to this dot block finally we just need to override can fit and we’re just going to return true since this isn’t in a crafting table next let’s create Another class so let’s create a new class called example recipe type and this is going to implement i recipe type of example recipe and let’s press ctrl shift o to import that then let’s override to string and all we’re going to do is return tutorial mod dot mod Id plus colon and then we want to put example underscore recipe which is the same thing as we put over here so let’s save that and that’s it for this class now let’s make our recipe in it class so let’s create a public static final i recipe type of example Recipe let’s call this example underscore recipe let’s set it equal to a new example recipe type so now if we go back here and import all the errors in this class should be fixed now let’s create a public static void called register recipes and this is going to take a Register of i recipe serializers of anything called event and let’s create a private static void and then we’re just going to call this register recipe and this is also going to take the event but also an i recipe type of any recipe and an i recipe serializer of any recipe And we’re going to do registry dot register i’m going to do registry dot recipe types then i’m going to create a new resource location of type dot to string and then we can just pass in the type then we can do event dot get registry dot register And then just pass in the serializer now all we have to do is for every recipe type up here we can call register recipes with the event example recipe and example recipe dot serializer finally all we need to do is call this register recipes function in our main class so let’s do bus Dot add generic listener to i recipe serializer dot class and then we can pass in recipe init colon colon register recipe then let’s import and there we go however to use this in code we’re going to have to create another method in our recipe in it so let’s create a public Map of a resource location to i recipe and we’re going to call this get recipes then let’s press ctrl shift o to import java.util.map and in here we’re going to pass in a couple of values so we’re going to pass in the i recipe type type and the recipe manager Manager then let’s create another final map of i recipe type any recipe to a another map of resource locations to i recipe of any recipe and we’re going to call this recipes i’m going to set it equal to opposification reflection helper dot get private value of recipe manager.class manager and then We need the id of the field so if we go into recipe manager.class the private value we need to get is this recipes so we can go into my discord server and do estimation mark mcp recipe manager dot recipes and here is our recipes so the value that we’re going to need is Field 199 522d so let’s copy this go into here and write this as a string and now we can get that value and finally we can just return recipes dot get type so now in our player events we have the player the state and let’s get the item Thrown let’s do item item is equal to event dot get entity item dot get item dot get item then let’s import item from net.minecraft.item then in here let’s loop over all the recipes in our registry and we want to make sure this function is static So we can get it from this class so that in here we can do recipeinit.getrecipes for recipe init dot example recipe and then world dot get recipe manager then we can go out of bracket and do dot value like so so now for each recipe let’s convert it into a example Recipe example recipe is equal to example recipe of recipe let’s change this item to an item stack so we can just do that by changing it to an item stack and removing the last get item and then we can check if example recipe dot is valid then we pass in the Item and then state dot get block then let’s do item handler helper dot give item to player and then we pass in the player and then we just need to do recipe dot get recipe output dot copy then we need to destroy the item entity because obviously we don’t want it to Remain there so we can do event dot gent entity dot remove and that’s it for the java now let’s have a look at creating a recipe so let’s create a new file in our recipes package and we’re going to call this example recipe 1 dot json and Just like any other recipe we need to define the type and this is going to be tutorial mod colon example recipe then let’s pass in the block so let’s do minecraft stone then we need the input so let’s open brackets and then we need to give it an Item so let’s say we’re going to throw a example item then we need an output and let’s say i’m going to give myself diamonds so let’s do minecraft colon diamond and then let’s give us three diamonds so let’s do count three and that’s it for our recipe So now let’s run the game so now if we go into a game and stand on stone and hold an example item and throw it we are given three diamonds and there we go now we have a working example recipe type thank you for watching if you need any help join the discord And in the next episode we’ll be covering the build.gradle file You Video Information
This video, titled ‘Minecraft Modding Tutorial 1.16 | 15.0 – Recipe Types’, was uploaded by Cy4’s Modding on 2021-05-01 13:44:02. It has garnered 2437 views and 82 likes. The duration of the video is 00:13:43 or 823 seconds.
this was a nightmare to record for some reason
(ɔ◔‿◔)ɔ ♥ ~ expand me
C://Sources/ Thank you Darkhax for a good example! 😀
C://Follow_Me/ Subscribe: https://www.youtube.com/channel/UCJIDXtGpf4wv1ybDzdTA_vQ/ Website: https://mcmodding.club/tutorials/
C://Help/ Discord: https://discord.gg/x9Mj63m4QG Or comment on this video!
C://Source_Code/ This Episode: https://github.com/Cy4Shot/ModdingTutorial1.16/tree/main/15.0-RecipeTypes All Episodes: https://github.com/Cy4Shot/ModdingTutorial1.16/