PacketListenerApi

PacketListenerApi

Source: https://www.spigotmc.org/resources/api-packetlistenerapi.2930/

This API provides an easy way to listen for when a Packet is sent or received and includes functions to modify Fields of the Packet.

Code (Text):
PacketListenerAPI.addPacketHandler(new PacketHandler() {

@Override
public void onSend(SentPacket packet) {
}

@Override
public void onReceive(ReceivedPacket packet) {
}

});

The processed packets can also be limited by adding the @PacketOptions annotation.

If you are interested in manipulating packets directly on your BungeeCord server, check out the BungeeCord version of this API.

Examples

Checking a Packet

1.) Not version save
Code (Java):
PacketListenerAPI.addPacketHandler(new PacketHandler() {

@Override
public void onSend(SentPacket packet) {
if (packet.getPacket() instanceof PacketPlayOutSpawnEntity) {// instanceof requires an import of the Packet, therefore not Version save
// Currently spawning an entity.
}
}

@Override
public void onReceive(ReceivedPacket packet) {
}

});

2.) version save (recommended)

Code (Java):
PacketListenerAPI.addPacketHandler(new PacketHandler() {

@Override
public void onSend(SentPacket packet) {
if (packet.getPacketName().equals(“PacketPlayOutSpawnEntity”)) {
// Currently spawning an entity.
}
}

@Override
public void onReceive(ReceivedPacket packet) {
}

});

1.) Not version save
Code (Text):
    @EventHandler
public void onPacketSend(PacketSendEvent e) {
if (e.getPacket() instanceof PacketPlayOutSpawnEntity) {// instanceof requires an import of the Packet, therefore not Version save
// Currently spawning an entity.
}
}

2.) version save (recommended)

Code (Text):
@EventHandler
public void onPacketSend(PacketSendEvent e) {
if(e.getPacketName().equals(“PacketPlayOutSpawnEntity”)) {
// Currently spawning an entity.
}
}

Modifying Field values

Code (Java):
PacketListenerAPI.addPacketHandler(new PacketHandler() {

@Override
public void onSend(SentPacket packet) {
if(packet.getPacketName().equals(“PacketPlayOutSpawnEntity”)) {
packet.setPacketValue(“f”, <EntityID>);//f is the Field in the packet which defines the Entity-type as an Integer
}
}

@Override
public void onReceive(ReceivedPacket packet) {
}

});

Maven
[​IMG]

APIManager
This API is compatible with APIManger.
Click here for information on how to implement it.

Links

 

Please leave a rating if you like this resource.
Donations to support me or this resource are also very much appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *

eighteen − 6 =

Skip to toolbar