ddevault / Craft.Net

(Unmaintained, see TrueCraft) Minecraft server, client, and etc for .NET
MIT License
228 stars 65 forks source link

Registering packet handlers? #217

Closed Plazmaz closed 10 years ago

Plazmaz commented 10 years ago

Looking into client-side packet handlers, it becomes extremely unclear as to how this is executed. In you examples you appear to access raw streams or use existing packet handlers, how is MinecraftClient.PacketHandler instantiated to point to a function?

ddevault commented 10 years ago
    public delegate void PacketHandler(MinecraftClient client, IPacket packet);
    public void RegisterPacketHandler(Type packetType, PacketHandler handler)

You're given the client receiving the packet, and the packet itself.

You specify the packet type you want to handle when you call RegisterPacketHandler.

Plazmaz commented 10 years ago

Right, but where can I specify the callback? Would I just create a new delegate void and then reference that? (sorry, I'm fairly new to C#)

ddevault commented 10 years ago

You pass in the name of a method that has the same signature as the PacketHandler delegate. Or you use a lambda. Or a delegate object. Research delegates in C#.