Skip to content

Automatic Packet Handlers

gcflames5 edited this page Aug 24, 2014 · 6 revisions

If you want your project to respond to a particular packet in a very similar way each time it receives that packet, then the AutoResponder class prove very useful. This class provides an easy way to register a listener that will listen for an indicated packet type and will call the given Response when the specified packet is received.

Here's an example that sends back a JsonMessagePacket as a response to any JsonMessagePacket that it receives.

ResponseUtil.getDefaultResponder().respondTo(JsonMessagePacket.class, new Response() {
            @Override
            public void run() {
                event.getConnection().sendPacket(new JsonMessagePacket("Thanks for the message!").setResponse(packet);
            }
});

You're probably wondering where the event and packet variables came from. These variables are part of the Response class and are automatically populated for you. event is an instance of PacketRecievedEvent that accompanied the packet and packet is an instance of the packet itself.

Clone this wiki locally