Skip to content

Listening for Packets

gcflames5 edited this page Aug 24, 2014 · 3 revisions

ServerInterconnect uses my Event Framework to handle its events. When a packet is received, a PacketRecievedEvent will be called.

To listen for a packet, we must first create a event handler:

@EventHandler
public void onPacketRecieve(PacketRecievedEvent e){
    Packet recievedPacket = e.getPacket():
    Connection connection = e.getConnection();
    //Handle packet (check if it is an instanceof the desired packet type and handle accordingly)
}

Then, place that event handler inside of a Listener:

public class MessagePacketListener implements Listener{

    @EventHandler
    public void onPacketRecieve(PacketRecievedEvent e){
        Packet recievedPacket = e.getPacket():
        Connection connection = e.getConnection();
        //Handle packet (check if it is an instanceof the desired packet type and handle accordingly)
    }

}

Finally, we need to register that listener:

Event.addListener(new MessagePacketListener());

Please keep in mind that there is no limit to the number of event handlers that you can have in one listener, and that you can integrate these methods into classes that have other functionality

Clone this wiki locally