How to create any packet class to use
This is a general guide to creating packet classes to use. I wrote this originally on someones question but I am changing it a bit and adding it here too so someone new can find it with ease. Let me know if I am not supposed to add guides like this here and I will remove it but I dont use public git repos too much (I know it makes me sound stupid as a developer) and so im not sure if people do this.
The first step to creating a packet is knowing what the packet looks like, you can get and idea of this here on these docs, most devs that deal with packets use this as it has lots of useful information. This is how I learn what I need to do when I am adding a packet. If you want to know the packet ID for an older version check out this website. Now ill give you an example for implementing a Held Item Change packet.
Keep in mind this is pretty general so you can apply the strategies to making any packet.
So first you want to define the packet class. Here I named it HeldItemChangePacket and it is an extension of the Packet class. Its an extension of the Packet class so it can have properties that packets have, I assume you understand that much if you are a developer. If you don't its fine but parts of this guide will be a bit confusing. This part will look like this:
class HeldItemChangePacket(Packet):
# Rest of packet will go here
Next you want to return the packet ID. Im going to keep it simple and I will return the latest packet ID. If you want an example of working with all packet versions check out a stock packet which you should know where to find (should be in the same file as you are adding this packet, if its not you might be adding your packet class to the wrong file). You get the ID of a packet and return it in a get_id() function with the argument context. This is for returning the ID of the packet so that when the client sends it the server will know what you are talking about, kind of like how we use packet names the computer uses packet ID's. The context argument has the version that you are connecting to the server in it so you can use that to see what packet ID you have to use (packet ID's change over versions). Im keeping it simple so I wont use that argument in my code but you can see how it is used in other packet classes. (Also ignore the staticmethod part, add that in but it isnt critical to know what it does for now)
class HeldItemChangePacket(Packet):
@staticmethod
def get_id(context):
# Here I am just returning the latest packet ID for this class (assuming you use 1.16)
return 0x23
Next you have to add the definition of the packet. This is so that the packet knows what arguments to send to the server. Your code will look like this when added:
class HeldItemChangePacket(Packet):
@staticmethod
def get_id(context):
return 0x23
packet_name = "held item change"
get_definition = staticmethod(lambda context: [
{'slot': Short}
])
Here we are adding the packet name which is just for human readability purposes as far as I know (I may be wrong correct me if someone who knows for a fact sees this). After the packet name we add the definition which just returns some data that looks like a Java Script object (or JSON if you know that better). The definition for this packet only consists of a slot because thats all the server needs in order to see what slot you want to switch to.
Here you have the whole packet created and all you have to do is call it. I wont explain in depth how to call because it is really simple but it goes something like this:
packet = serverbound.play.HeldItemChangePacket() # Creates an instance of the packet class
packet.slot = 2 # Changes the slot argument to slot 3, any number from 0-8
connection.write_packet(packet) # Writes the packet to the server
Everything I wrote here can be changed to make other packets I just wrote it with an example packet here.
that's good, but pull request to the wiki is better.