[Applause] Mmmm I dropped two up do another monkey monster rattling did you how to add Network must’ve to the game so this is for a block breaker because at the moment our capability which you added last time doesn’t sync with the client when we need it to which is irritating So what we’re going to have to do is going to use a simple I am PL wrapper to create our own Network messages so we’re going to add a new package called Network and inside here we’re going to add messages so I’m going to call the first one we’re going to add 2 today so one’s going to send it to the server saying hey can I have the information about the work done and you’re going to send it back to the client saying the work done so pack it and it’s going to be get work get worker Because I’m going to get the get work done and also the max work done so you’re going to get the worker and he’s going to be a new iMessage like so that’s gonna make us add some methods and there we go we’re going to have a private billing to begin with so private I can spell private and its message valid and we’re going to have is a default constructor that’s when we register it and intercept message valid to false if I can spell today and that’s false so the message isn’t valid and two bytes if it’s not valid we’re going to Return like so so if it’s not valid we’re going to return the message that’s just to make sure that our message is okay and for the front bytes I’m just going to add a try-catch and it’s going to get index out of bounds exception ioe where we’re just going to do a the Logger so utils get logger dot caching and return and this stop message valid equals true so that’s how message will default lis be set up then we’re going to add variables that we’re going to send so what I’m going to send is a block pass Of the tile entity so pause and I’m also going to send an enum facing and it’s just the side so that’s for when we use a capability and because you can actually have it for each individual side has a separate capability so we’re going to add a public packet get worker And inside here we’re going to take our block pause pause and we’re going to take a numb facing side and then what we’re going to do is we’re going to do this dot cars equals cars and this side equals side I’m going to say the message Is actually valid like so so in two bytes we’re going to write all of our data to our buffer and it’s what the buffer is what is actually sent so there’s quite a few things that we can do this so first thing we’re going to need to write a block fast so it’s Actually just free in so you know write an int and it’s passed up get X going to write another int and that’s passed up if I can spell pass cars that get Y and going to write another int and that’s cars get dead and also we’re going to Write the enum facing now face interfacing can’t be written like anything because it’s an object but what inland facing does provide is a method called get name I’m going to use get name to get name to which return to this name but doesn’t override it so we’re Going to write this now it’s actually it’s easy for us but if it’s going to write a string we have to write out the byte data so it can use as byte glove utils is the utility class and what we can do is we’re going to write the String to the buffer and it’s this stuff side don’t get named – and then we’ve wrote all of our data we now need to read all of our data so this dot passed equals and what is going to be is a new block pause and it’s both read int so Those reading the first x value so buffed up reading it again the Y and Buffett read got a dot read in again for the Zed so you have to do it in the order that you wrote it to the buffer and then this dot side equals enum Facing dot by name and it’s just going to be bite both utils dot read utf-8 string from the buffer like so so we’ve transferred all of our data we need to actually handle this message so what we’re going to do is have a new class inside of the packet and I’m just going To call it Handler and it implements a iMessage handler now what we put here is what we’re going to actually receive the message that we’re going to get and that’s packet get worker and that’s what we’re going to reply we’re not going to reply like the way by returning we can Reply a little bit differently and what we can have here is just a method process message and it is going to take in a packet get work a message and the message context CTX like so we’re going to fill that in in a bit so we’re going To do is you’re going to check if the message is invalid and side context side is not equal to side server so it’s not the service side we’re going to return not we don’t want to do anything because it’s on the wrong side it’s not going to do anything Seeing as it’s on the right side now we need to actually process the message now we could to see process message what we’re going to do is we’re actually going to we have to add it to the current thread so going to SML comment and alert instance and it get well Fred From the net hand low we can get from the context net handler I’m going to add a scheduled task we’re going to use them is it Lombardo as and I say and we’re going to use that again and I just going to call process message and it can get Rid of that like so we’ve added this method to other tasks to the server so what we’re going to do here is we’re going to send a message back we’re not going to do this yet because need to actually get the message that we’re going to send back made so Hackett Return worker and like any other message you’re going to implement a message like so we go and what we can do is actually going to write the data so what we’re going to have is the cool down and we’re also going to have a private int for the Max cool down as you will see in the worker over here called a max cool down we’re going to send this information so what we’re going to do is make sure we write the information so it’s a write information first so is this both right now there is actually something that I Forgot because the way we’re going to update the values for us is we’re going to use a bit of Java reflection so we’re going to have two fields in here which we’re going to update with the method but we need to say what the fields are Called so going to have a private string class name like so and we’re going to have a private string and it’s going to be a cool down field to name and another private string and it’s max cool down field name like so and we’re going to Have to take these in so string class forget to write all that data so bite for you to good name and guess what we have to do it on the other message as well public park it get worker and we’re going to take an int for the cool down And However two here get the rest of this from up here put that in there and need to read them right so we write all the data we need to now do our handler so public static class hand alone it handled a client-side message that I message handler the message that we’re Going to get packet return worker and not going to return anything back to the server because this is was going to end here so as implemented methods and in here we’re going to have a process message again this is going to feed packet return worker and that’s message Now we’re not going to use the method context for this one now like the last time we need to check if it’s not valid so if message dot isn’t valid and this context outside is not equal to side client so not the client side we’re going to return null now seeing that We’re on the client side we can actually use client side methods so we can use the minecraft class so if we take a look it is actually client side only this class so minecraft up get minecraft dot and add a scheduled tasks like last time And inside here we’re going to use a bit of land bada again and process the message like so so in here we actually going to update that field so what we can do is use a try catch exception II and what we’re going to do is just use the you tools that get Logger dot catching a in here now what can do is we’re going to make get the class that we are going to set the value in and that’s equal to class dot for name and it’s message dot class name so we’re going to get the class from the Class name we’re going to get the field so this is going to be the cool field and that’s equal to class get declared field by the name of message stopped cooldown field name and we need to import this control shift oh we’re going to import from Java reflect stop Field we’re going to copy and paste this and this is going to be the max cooldown field so just change this to max cooldown field name so we’ve got field now you set the values so we can do cool down field dot set int the first parenthesis is the class and second Parentheses is actually the cool down so that’s just cool down here and do max cool down I put full round great max cool down field starts set in again class and it is message dot max cool down so that’s going to set the variable in the class correctly so now we can Return the values so I’m just going to do the tile and Tiffany totality tenancy te equals context get server handler dot if you get the player entity and we can then get the server world get tile entity at message dot pause I know that seems really long-winded but we’ve got The tile entity at the position now what we need to do is need to check that tile entity is not nothing so if it is nothing we’re going to return you’re not going to do anything else if the tile and see has capability if it doesn’t have the capability mod Capabilities dot and dot capability worker from side message dot side if it doesn’t have it it going to return again we’re not going to do anything and now our Oliver tea has everything we want we can actually return the values so what we can do is we need to actually Register our messages to send them back to the client so in network in the package we’re going to have our package handler and we’re going to have is a private static int which is ID and that’s equal to zero and you can have a private static int called next ID and This is just simply going to an ID plus less so the next ID and I’ll show you what it means and we can just add public static void register messages like so and we’re going to put in here is the channel name so string channel name and Now we need to actually have our channel so is a public static simple network rougher and we’re going to call it instance and in here if that instance equal to network street dot and it’s instance dot new simple tunnel from the channel name so we set up our channel And we’ll get back to this we can now send messages off this channel so if you package handler instance and we can I’ll send messages so this will send message to a player which is what we’re going to use it can send it to everyone on the server or everyone which I don’t Recommend doing everyone around a point everyone around in a dimension and send it to the server what we can do is just do a send to and now we need to create a new message so it’s packet return worker and we’re going to put in the cooldown And all these are message dot bit here so we’re going to pass these on the cooldown is te dot and need to actually get the capability really so I work worker equals T to get capability mod capabilities dot capability worker message dot side like so control flow to import work and what can Do is going to do work dot oh it’s an int excuse me it’s worker dot get work done and work dot get max work so that’s all of the data need to now just put in the player so it’s C TX get server handler dot player entity so that’s the Player who sent it so we’ve now sent it back to the client we’ve updated to field need to now register our messages so I’m just going to do server packet and client packet so you can see which ones are which and what can do is instance don’t register message and We’re going to use a class one the message handler is packet this is going to be sent to deserve another side the handler is on so we put beside two handlers on so I could get worker dot handler dot class the actual message is packet get worker dot class the Discriminator is next not a entity next ID and thus I decide that it’s going to be handled aren’t is the server-side and we can copy and paste all of this for the client one except for we need send to clients here it’s change it to return worker not get worker and that will Register our messages when we call it so we need to now register our messages any proxies in the common proxy and in three in it we’re going to do packet handler dot register messages the channel name is just going to be multi DSO reference mark ID and like so we Registered our messages we now need to sync with the server this is the most important part so in here we’re going to have a public static int and it’s going to either cool down and it’s also the max cool down the values that we’re going to sync Now we’re going to set both of these to zero for now so instead of doing T don’t get capability here we’re going to do and we’re going to show it so cool down add and then I’m going to put a divided and in max cool down like so that’s Going to draw it but these aren’t going to be synced because going to tell it to sync so we’re going to have a public static int sync equals zero every time we draw we’re going to syncs not every time I draw we’re going to increase think so sync plus plus sync And what we’re going to do is we’re going to sync with the server every 10 ticks so that’s every half a second you don’t do every second because going to flood the server with loads and messages and we do not want that and what we can do If the sync is zero then we need to sync a message of value so you can do packet handles instant I instance there go dot send to server and now we’re going to send a server packet get worker the pass is te get pass the side is and this MC Dot player dot get addresses horizontal facing the class name is this class name so the way to get to this class so it’s the package add the actual name of the class which is GUI block breaker just as a note if you had a class inside of a Class you would put a dollar sign say we have the hand alert inside of this class Ruby dollar handler but it’s not the cool down field name is just cooled down and the match score down field name is max cool down and like so hopefully that Will now sync our values with the server so I’m just going to run it in debug mode for now but hopefully every now be synched with the server will have the correct data for a DUI okay so I believe the error might actually be to something from absolutely ages ago I wasn’t Supposed to remove the Super’s from here and does it actually been a really long era that I’ve had for a while the case of game is loaded up so should we have to go our test world and hopefully another Thailand he will be working after a Little error I made earlier so I will have to break the tile aunty of course because otherwise it won’t work so we place you down there we go as well put it on we don’t get error and we do see when it’s on it goes up to 50 when it’s Off it stops perfect and if we get the other block breaker the t1 block breaker we should also see that that goes 200 as you can see Chris using the same view why it’s at least 50 it’ll stay at 50 then you will update which is perfectly Fine and I didn’t realize other thing okay and so obviously one needs are both on these will both go up this will go up and also if I grab some efficiency book efficiency 5 will do as you know efficiency will lower the cap so we can Actually now see what the calf is in the next tutorial we’re going to be adding a visual way of seeing this cap so on so that’s really cool that we can actually now update with the server hopefully this is useful to you so say you have Some server data now the quick fix for this would have been to make three updates a client as well what talents today is should really be handled on the server side so thank you for tuning like comment and subscribe I am out Video Information
This video, titled ‘Minecraft Modding Tutorial | Network Messages (1.10.2,1.11.2)’, was uploaded by CJMinecraft on 2017-05-08 16:30:00. It has garnered 3399 views and 46 likes. The duration of the video is 00:24:33 or 1473 seconds.
Today I teach you how to handle and receive network messages to update the client with server side info!
Please support me if you can – https://www.patreon.com/cjminecraft Discord – https://discord.gg/ZvhUKkd
Don’t forget to like, comment and subscribe!
Music by Ninety9Lives Elektronomia – Imagination Video Link: http://99l.tv/Imagination Channel: http://99l.tv/Subscribe Album Download Link: http://99l.tv/GoldenGun
Links: ►GitHub – https://goo.gl/c4JlXe ►My Website – http://cjminecraft.theclever.me/ ►Modding Tutorial Playlist – https://goo.gl/bq6sgx ►Facebook – https://www.facebook.com/CJMinecraft01/ ►Google Plus – https://goo.gl/3n1umj ►SoundCloud – https://soundcloud.com/speed2-pc/ ►MCAmino – http://goo.gl/LzwZ9W ►Votable – http://www.votable.com/CJMinecraft
For modding help or if you want to contact me, email me at [email protected]
My Schedule: Monday or Thursday: ►Modding Tutorial