Skip to content

Automatic Packet Handlers

gcflames5 edited this page Aug 26, 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 may 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));
            }
});

The packet and event variables are set for you in Response automagically.

Clone this wiki locally