Let’s save some data on items by using nbt all right we find ourselves back intellij once more and in this tutorial we’re going to be talking about nbt data and this is well a way to save data on item stacks now first of all you’re going to say wait Item stacks items what is the difference well what is a block state to a block is an item stack to an item so simply put if you have two different diamond swords in your inventory and you’re going to use one to kill a lot of monsters then Of course what’s going to happen is the durability of that one is going to decrease the second one is not going to decrease because they’re two different sorts right but they’re both of the same diamond sword class so to speak or they’re both the same diamond sword field once again they’re different item Stacks that’s all that there is to it and what we’re going to do is when we right click with the dowsing rod we’re going to save this on a data tablet item so in the custom package right click new java class called the data tablet item and this will Of course extends the item class right here when i hover over this create constructor matching super and then we just need a few things so what we’re going to do is we’re going to basically make this a display the last or that was found including the actual position and Then what we’re also going to do is we’ll make gonna make this glint so when data is saved on this tablet it’s going to have the glint effect and then when we right click this item stack in this case then we’re going to erase the data So we’re going to first of all do the right clicking this is the use method here and what we’re just going to say is if user dot get stack in hand and as nbt so this is the first method that we need this simply asks whether or not The specific item stack so this returns an item stack as you can see has nvt data associated with it and then we’re just going to say user dot get back in hand with the hand passed in and we’re going to say set nvt so this sets nvt And then we’re just going to put in a new compound nvt compound this in this case would be empty therefore we’re going to overwrite it and basically well empty it out so to speak then we’re going to overwrite at the has glint method this is basically the shine that An item stack has when well it for example is enchanted and we’re just going to return as nvt here so it’s going to shine when there is nbt data associated on this item stack and then we’re also going to use the append tooltip method which is pretty cool but What we’re going to do here is we’re going to say if the stack has nbt so once again we’re asking hey does this stack have nbt data on it and if it does we’re just going to say a string current or is equal to the stack dot Get nvt this time and then you can see what we can basically do here we can first of all add stuff so add basically you need to call the put methods right here so you can see a bunch of different things that we can put here and then There’s also get methods we’re going to get a string with a particular key so this is tutorialmod dot last underscore or the way that nvt data is saved basically is a very similar to a map so we basically always map a certain key so a key value pair and the key is always Going to be a string if this may be a little bit confusing then i highly recommend going back to some of the basics for example in the java introduction i talk a little bit about key value pairs and the maps for example i will link this in the top right corner So that you can take a look at that and otherwise we want to add the tooltip current or right here there you go and then actually the entire data tablet item class is already done now this is of course not quite right because we need this to be a new literal text There you go but then of course it works there you go and then this is pretty much done nothing else to be done here and what we want is now in the dowsing rod item when we actually have a data tablet in our inventory then we want to Change some stuff let’s actually first of all register the data tablet so we’re going to say this is the data underscore tablet and then of course the name as well a data tablet there you go and this is a data tablet item now what’s kind of Important here is that the max count is one and the rest is going to be fine now what we’re going to need is we’re actually going to need a util class i’m immediately going to copy this over it’s gonna just make our life a little bit Easier all of the code is available to you in the description below get a repository an individual just as well this is just as you can see it has player stack and inventory so we’re just gonna pass in a player and an item and we’re gonna return true if this player Has the stack in its inventory and then we also have get first inventory index pretty much the same thing if the item is in there we’re just gonna return the first index that it appears in so nothing too crazy right so what are we gonna need here i will actually also Copy over this method but i will explain so this is the add nbt to data tablet method and you can see we’re first of all making a what we’re getting the item stack from the player’s inventory by just saying hey give me the first inventory index here with this item so This player we’re going to take a look at that inventory and give me the first index where this item appears so that’s all that we really do here this is always going to be working so usually you can see this returns negative one if you know this is not the case but we’re Gonna check if the item is actually in the inventory in just a moment but first of all here we then have okay we’re making a new nbt compound so some nbt data we’re putting a string on it with tutorial mod dot last underscore or this is going to be The key and then this is the value so we’re just once again going to get the string name we’re going to say at the position x y and z and there you go then going to be saved as you can see and then we’re going to Say data tablet dot set nbt and then this nvt data so that’s actually all of the magic here and there’s one thing that we need right here so this is between the true and the break but once we found something we’re just going to say hey does the player have this stack In its inventory so does it have a data tablet item if it does then we’re going to add the data to it and that is pretty much all that there is to it nothing else now of course we still need some translation for the data tablet as well As an item model and a texture let’s add the translation first of all as of course an item here they go with the data tablet should be fairly self-explanatory and the actual item model is also just a normal item model so you can see it just points to the Data tablet texture so nothing too crazy here let’s also copy over the texture and there you go and that is pretty much all that we need to do so the real interesting thing for this is the well adding of the nbt data making a new nvt Compound and with that you can then save data on particular item stack so for completion sake let’s see if it works all right we found ourselves back in minecraft as you can see the data tablet has been added so let’s see i found some color you can see it’s already glinting And if i hover over this found call or at this particular position and if i right click then you can see i’ve deleted the data now one important thing is that because we’re returning the first index we’re always going to overwrite this one so you can see even Though i now found the iron ore we’re not saving it on the second one we’re saving it on this one so this is something that you could of course also try for yourself changing around the methods a little bit so that it saves it on the first available Item stack instead of just on the first one that is something to think about but overall this is how easy it is to add some well custom nbt data and some cool functionality associated with it through minecraft right once again all of the code is available to you in the description Below get a repository in individual gists as well but otherwise this would be it for this tutorial right here i hope you found this useful and you learned something new if you did i would very much appreciate like and don’t forget to subscribe for more tutorials Just like this one i also want to thank all of my lovely patreon supporters for supporting me and this channel it is very much appreciated and special golden thanks go out to mc arctic for actually supporting me with the gold block tier and i’ll see you in the next tutorial so yeah Video Information
This video, titled ‘Minecraft 1.18.1 Fabric Modding | NBT DATA EXPLAINED’, was uploaded by Modding by Kaupenjoe on 2022-03-02 14:59:48. It has garnered 5742 views and 114 likes. The duration of the video is 00:08:35 or 515 seconds.
In this Minecraft Modding Tutorial, I explain NBT Data and show an interesting application of the NBT Data.
== ASSETS & DOWNLOAD LINKS == GitHub Repo: https://github.com/Tutorials-By-Kaupenjoe/Fabric-Tutorial-1.18.1/tree/24-nbt Gist: https://url.kaupenjoe.net/yt228/gist Data Tablet Zipped: https://url.kaupenjoe.net/yt228/texture
== TIMESTAMPS == 0:00 Intro 0:14 What are ItemStacks? 1:02 Creating the DataTabletItem Class 4:14 Registering the Item 4:40 Making a Util Class 5:07 Changing the DowsingRodItem Class 6:38 Adding the JSON Files 7:13 Demonstration 8:08 Outro
== TAKE A LOOK AT MY COURSES WITH COUPON CODES == ▶️ NEW Forge Modding with Minecraft 1.20.X: https://url.kaupenjoe.net/CourseForge120X ▶️ Learn Forge Modding with Minecraft 1.18: https://url.kaupenjoe.net/CourseForge118 * ▶️ Learn Fabric Modding with Minecraft 1.18: https://url.kaupenjoe.net/CourseFabric118 * ▶️ Complete and Roblox Lua Game Development: https://url.kaupenjoe.net/RobloxCoupon *
== SOCIALS == Discord: https://discord.com/invite/yqxykanpWf Personal Twitter: https://twitter.com/Kaupenjoe
Instagram: https://url.kaupenjoe.net/tutorials/instagram Facebook: https://url.kaupenjoe.net/tutorials/facebook Twitter: https://url.kaupenjoe.net/tutorials/twitter TikTok: https://url.kaupenjoe.net/tutorials/tiktok Written Tutorials: https://url.kaupenjoe.net/tutorials/blog
== LICENSE == Source Code is distributed under the MIT License. Additional Licenses for other assets can be seen below or in the accompanying CREDITS.txt on download.
== ADDITIONAL CREDITS == Outro Musik by Kevin MacLeod: “That’s a Wrap” Kevin MacLeod (incompetech.com) Licensed under Creative Commons: By Attribution 3.0 http://creativecommons.org/licenses/by/3.0
== AMAZON AFFILIATE LINKS == Amazon Referral [US]: https://url.kaupenjoe.net/amazonref/us * Amazon Referral [UK]: https://url.kaupenjoe.net/amazonref/uk * Amazon Referral [DE]: https://url.kaupenjoe.net/amazonref/de *
== AFFILIATE DISCLAIMER == * Some of the links and other products that appear in the video description are from companies which I will earn an affiliate commission or referral bonus from or are my own products. This means that if you click on one of the product links, I’ll receive a small commission or additional kickback without any additional cost for you. This helps support the channel and allows me to continue to make videos. Thank you for the support!
== HASHTAGS == #Minecraft #MinecraftModding #MinecraftTutorial #Kaupenjoe