Drink up and let’s add a player capability to save data on the player oh right we found this back into each once more and in this tutorial we’re going to be talking about a player capabilities basically saving our custom player thirst right our thirst data onto the Player now how will that work well for this we’re actually going to need a new package in the tutorial more package and it’s going to be called thirst and inside of there we’re going to make two new java classes the first one will be the player thirst class and the second One will be the player thirst a provider class now first of all let’s go through the player thirst class this is the one that actually saves the data and you know does a few basic things around the data while the player thirst provider is going to be the actual capability Provider so this will have a private end called thirst should be fairly self-explanatory and we’ll also have a private final end and that’s just going to be called the max thirst and that’s going to be 10. now we can also add another one which is called a Min thirst and that’s just going to be zero in this case so we just have this available to us if we wanted to so we now we need a few different methods first of all public ind get thirst should be fairly self-explanatory just returning the first level at the moment The next method we’re going to need is a public avoid add thirst where we basically are going to add a particular you know number of you know thirst points or you know number of water points to the thirst and this is going to be this dot thirst equals then we’re saying math min Thirst plus add or max thirst now why we’re using this well the general idea is that if we add anything that is you know thirst plus nine for example but thirst is already eight right then this is going to be over max thirst and we basically want to Bound it by max thirst now the same thing is going to happen if we sub thirst right here we’re going to say sub thirst and here we’re subtracting here we’re subtracting the sub from it and here we actually need to use a max the max method of course and here we want to Use the min thirst in this case so we basically want to say hey if we’re subtracting the thirst right if we have five thirds but we’re subtracting nine from it right then we get into the negative values but we in this case we actually want to bound it by zero we Also want a copy from method over here that’s just taking in a player thirst over here which is going to be called source and then we’re just going to say this dot thirst equals source dot thirst and then we need two boilerplate methods roughly speaking the one is called save Nbt data with a compound nbt tag or entity and this will just do nbt.put int and we’re going to call it thirst and we’re going to just save the thirst field there and then we’re going to have a public void load nbt data which is once again also going To take in a compound nbt and this is just going to say thirst equals nbt.getint first and now everything should be saved on this player thirst this is also all that we need for this particular class and now we can make this available through the player thirst provider by implementing two new Interfaces we’re going to implement the i capability provider interface as well as the inbt serializable interface with a compound tag over here let’s just hover over this implement the method so you can see there’s three methods that we have to implement and then we should Be fine now we’re going to need a few things here as well and that’s going to be number one the public static capability of player thirst so there’s going to be a capability of our custom class over here this is going to be the player underscore thirst and that’s Equal to the capability manager dot get a new capability token of type player thirst and what we’re going to do is we’re not going to overwrite anything we’re just going to keep it like this so it is an anonymous class however you can see we’re not overriding anything in this anonymous Class and i’m going to say private player thirst all thirst which is equal to null in the beginning here and then we’ll also have a private final lazy optional of player thirst and this is going to be the optional equal to a lazy optional dot of and then just this colon colon create Player thirst which is a method we’ve not yet created but we’re going to create in just a moment let’s just hover over this create this method and you can see actually we don’t even need to do this as a t we can just say player Thirst over here and we can just say if this dot thirst is null then what we want to do is we want to set the thirst to a new player thirst we just want to create a new one otherwise we’ll just return this stuff third and then afterwards we’ll just return this that Thirst right over here we just want to in the get capability one we just want to say if cap equals player thirst so if there is a player thirst capability present player thirst there you go then we’ll just return optional.cast otherwise we’ll return lazy optional dot empty serialize nvt is Actually also fairly straightforward you can just say compound tag nbt equals a new compound tag then we’re going to create playerthirstdata.savenbt passing in the mbt and then just returning it of course we’re using the create player thirst in this case because it’s just going to return the already existing one if none Other basically exists that’s the reason why we’re basically always using the create player first same with the load nvt data here where we just pass in the nbt right there and that is actually all that we need for the custom capability provider now comes the interesting part Where we actually have to register it make sure that it you know stays on the player and all of that craziness now this is mostly if i recall correctly mostly boilerplate code which is going to be placed in the mod events class over here so i will do the following We’re just going to copy over method by method the first one is the unattached capability player so you can see this is an attached capabilities event for an entity so we’re just making sure that this is a player and if it is a player that they don’t have the player first Capability just yet and if they don’t then we’re just going to add this as a property you can see with this particular capability next we also want to make sure that if a player basically will die so we can see event is was death then what we’re going to do is We’re going to get the original capability and we’re going to put it onto the newer player so that is all that is happening here once again of course all of the code is available to you in the description below get up repository individual just as well and What we’ll then also have is we’ll have the on register capabilities one right here so register capabilities event where we just register the actual player thirst class as a capability and those are the three things that we definitely need to well basically have this now What i will also have is the following i will also have a player tick method over here which i’m going to just put in here you can see so basically this is just going to say hey every time you know we basically have on average every 10 Seconds we’re just going to subtract one thirst from the player now the reason why i have this is because there’s no other way at the moment that we basically subtract anything in terms of thirst from the player this is why i basically wanted to have this Going on and just to also be sure i think that what we should also do is we should say event dot player and then just send a system message we’re just going to say component literal just like sub subtract it first just so that we know you know things have happened over Here and that should be pretty much fine now this once again is the reason why i said you know the system isn’t like fully functional yet or there’s you know a few things that are kind of weird because just you know subtracting every 10 seconds on average some thirst is Definitely not the way that you would want a system like this to work and now we have the entire player thirst system implemented except for synchronizing to the client we’re gonna look at that in the next tutorial but for the time being we can now change the drink water packet Over here to well basically also include increase the water level over here as well so the way we’re gonna do this is we’re going to say player dot get capability with the player thirst provider dot player thirst and then if present we’re gonna make a thirst handler over here and that’s Going to be the player thirst first dot add thirst so we’re just going to add thirst if we are around it and then well basically that is pretty much all that we need to do oh this is thirst of course so there you go and then we Can also output the current level of this then outputting the current level of this is just going to be the same as this one we can just say that of a translatable we’re just going to make a literal over here and this is going to be current thirst Just so that we know what it is we’re just going to say thirst there’s not get thirst there you go and we’re gonna make this aqua now we can do the same thing right here in when there’s no water around and instead of increasing it we’re just outputting the actual Thirst in this case and this is pretty much all that we need to do now we actually have the thirst saved on the player but once again there is going to be a mismatch between the server and the client at the moment now this is not an issue for us right now we Wouldn’t actually notice this we will notice this however once we add the hud in the next tutorial but for the time being let’s look at what we’ve made right now and see if it works all right finds us in minecraft again and let’s just get a little bit of well drinking Here you can see current thirst one two three four five six and then we have at 10. now nowhere nearby and we actually already seen the subtracted thirst you can see now it’s subtracted enough i look at it we have nine thirds now so you can actually see everything well Pretty much working exactly how you would expect it to current thirst is decreasing you know like i said roughly every 10 seconds that’s the rough average it’s you know it’s not going to be like extremely accurate you know because it is just random so sometimes it’s going to be a little bit longer Sometimes it’s going to be a little bit shorter but overall you can see it is increasing and decreasing and we can also you know get this and then you can see this if we actually go outside of the world and go back in in theory this Should all be saved so let’s just take a look and we have seven thirds there you go and now six thirds so the thirst is definitely being saved via the world but like i said it is currently not actually synchronized via the client and i will show you this basically in the next Tutorial when we add the custom hud but that’s going to be it for this tutorial right here hope you found this useful and you’ll learn something new i’ll see you all in the next tutorial so yeah Video Information
This video, titled ‘Minecraft 1.19 Forge Modding Tutorial | PLAYER CAPABILITY | #16’, was uploaded by Modding by Kaupenjoe on 2022-08-02 13:59:53. It has garnered 15280 views and 201 likes. The duration of the video is 00:10:29 or 629 seconds.
In this Minecraft Modding Tutorial, we are adding a custom PLAYER CAPABILITY to Minecraft 1.19 using Forge!
== MINECRAFT COURSES == ▶️ Learn Forge Modding with Minecraft 1.18: https://url.kaupenjoe.net/CourseForge118 * ▶️ Learn Fabric Modding with Minecraft 1.18: https://url.kaupenjoe.net/CourseFabric118 *
== ASSETS & DOWNLOAD LINKS == GitHub Repo: https://github.com/Tutorials-By-Kaupenjoe/Forge-Tutorial-1.19/tree/16-playerCapability Gist: https://url.kaupenjoe.net/yt380/gist
== TIMESTAMPS == 0:00 Intro 0:20 Creating the PlayerThirst Class 3:10 Creating the PlayerThirstProvider Class 5:37 Registering the Custom Capability 6:47 Decreasing the Thirst From the Player 8:00 Changing the Thirst in the Drink Water Packet 9:19 Demonstration 10:20 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 *
== SUPPORT ME ON PATREON == ▶️ https://www.patreon.com/Kaupenjoe
== 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
== 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