Hello and welcome back to another video today we’re going to be covering network and packets so in the last episode we created keybinds but when we click the button it just sends something to the console however this only happens on the client so if we want to tell the server Something we’re going to need to send a packet so in order to do that we first need to create a network and the kind of network we’re going to be creating is a simple channel network so let’s right click on call and create a new package called Dot network and then let’s create a class and i’m going to call this tutorial network now in here we’re going to create a public static final string and this is going to be our network version and i’m just going to set this to 0.1.0 every time we want to add a new packet To the network we should increase this so that servers know that the network has changed so now we can create a public static final simple channel and we’re going to call this channel and we’re going to set it equal to network registry dot new simple channel and now We need to give it a resource location so let’s do a new resource location then we need to give it the name space so let’s do tutorial mod dot mod id and then the id which we can just set to network next we need three suppliers the network protocol version the client accepted Versions and the server accepted versions so for the first one we’re just going to supply our network version and for this we’re going to check if the version equals our network version like so and then we can do the same thing for the server one Not just the client and there we go now we’ve created our simple channel next we can create a public static void called init and in here we’re going to register all the packets that we create but first let’s register the tutorial network class so under here let’s create a public void Call this common setup and we’re going to pass in a final fml common setup event let’s use ctrl shift up to import and let’s call this event we’re going to call this event and we’re going to press ctrl shift o to import fml common setup event and all We need to do in here is to call tutorial network dot initialize now we need to call this common setup function and to do that up here we can do bus dot add listener and then we can call this colon colon common setup now we need to create a packet to register In the tutorial network so under our network package we’re going to create a new package called message and this is where we’re going to keep all our packets i’m just going to call this input message and this is the packets that we’re going to use to get the data from input events like The key and the action into the server so first of all let’s just make an empty constructor so we can initialize this without any data so here if our key is pressed we can also pass in some data into the input message so i’m just going to pass in the key Index even though we already know that just as an example but if you’re just making a keybind thing you don’t need to do this so to pass in the int key value we need to make another constructor with an int key let’s actually give this a properties let’s do public int Key and then in here we can do this dot key is equal to key next we need our encode decode and handle function so let’s do public static void encode and this is going to take an input message message and a packet buffer buffer so let’s do buffer dot write Int and then we can do message dot key and we need to make sure we memorize the order in which we write the stuff here so let’s just copy this and paste it and rename this to decode and instead we’re going to return an input message and we don’t need to Even pass in a packet buffer and let’s make sure this is static i’m going to return a new input message and we’re going to pass in buffer dot read int as the key and we want to make sure we read stuff in the same order as we write to it so If i were to have like another int here and say this wasn’t the key this was like a value or something we want to first read the key then read the value in the same order that we write here so finally let’s create a public static Void handle and this is also going to take the input message message but it’s also going to take a supplier of network event dot context and we’re going to call this context so let’s open some brackets and import supplier from java.util.function now let’s get the context so let’s do networkevent.context Context but we can’t have this to be the same name so let’s just do con text supplier is equal to context supplier dot get and we’re going to do context dot in q work and then we need to pass in a runnable so to do that we can Do a supplier with a function inside it let’s put a semicolon at the end of here and then afterwards we can do context dot set packet handled to true and now that we’ve done that anything that we want to happen when our event is cooled this time on the server with our client Data for example the key we can do that here so the way we get stuff from the server is by getting the server player entity and this is going to be our player that’s going to be equal to context dot get sender and with this server Player entity we can get the world by doing player dot world and this is already going to be the server world so this allows us to do anything inside the server so for this example i’m going to first give the player a diamond so we’re going to do add item stacked Inventory and we’re going to create a new item stack with items dot diamond so now when we press our key we’re going to give an item to the player’s inventory another thing i’m going to do is set a block so we’re going to do world world is equal to player dot get World and we’re going to get the entity world let’s use ctrl shift o to import world and now let’s set a block so let’s do world dot set block state and first we need to get the position so i’m just going to set it at the player’s position so let’s do player Dot get position however this is going to set the block at the player’s feet so let’s do dot down to set the block the player is standing on and then we’re going to set the block the player is standing on to a diamond block so let’s do blocks dot Diamond block dot get default state and there we go so now when we click our key we’re going to give a diamond to the player’s inventory and we’re going to set a block state so next we actually need to register so in our tutorial network let’s go to the init And let’s do channel dot register message then we need to pass in the index so let’s just do zero then we need to pass four things from the input message so let’s do input message dot class for the first one and then we need to do input message dot encode Input message colon colon decode and input message colon colon handle and there we go if we were to have another message we could just copy this paste it and change this zero to a one because that’s the index of which we’re registering it so we need to have A unique one for each one so we can just increment it each time however i’m going to delete this because i don’t want to register the same packet twice and now we need to cool the packet so back in our input event instead of calling the key pressed we’re Going to create our packet so to do that we’re going to call tutorialnetwork.channel.send to server and then we need to pass in the message so let’s create a new input message and then we just need to pass on the key so i’m just gonna pass in key As i said the key is actually useless so let’s do something with that key in our input message we can do dot system.out.print nine and let’s just print the key so we can do message dot key so now we’re going to give the player a diamond set a diamond block at their feet And print something from the server with the key that we pressed so now we can actually start the game and there we go so now if i go into the game and press g you can see i get a diamond the block below me gets set to a diamond And in the console we can see that the input message has said something to the chat thank you for watching if you need any help join the discord and i’ll see you in the next episode You Video Information
This video, titled ‘Minecraft Modding Tutorial 1.16 | 14.0 – Packets’, was uploaded by Cy4’s Modding on 2021-04-08 13:22:33. It has garnered 4902 views and 112 likes. The duration of the video is 00:08:29 or 509 seconds.
note: this is only client to server. ill cover the server to client some other time
(ɔ◔‿◔)ɔ ♥ ~ expand me
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/14.0-Packets All Episodes: https://github.com/Cy4Shot/ModdingTutorial1.16/