Home›Java›Mods›[API] PacketListenerAPI
ModsJava

[API] PacketListenerAPI

Java mod listed for Minecraft 1.16–1.17. Download on the author's site.

⬇ Download on Spigot
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.

Versions after 3.8 require Java 11+

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

Spoiler: Checking packet for Entity spawn
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 ) {
            }

        } ) ;
 

Spoiler: Old version
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

Spoiler: Modify the Entity-Type of an Entity-spawn packet
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 ) {
            }

        } ) ;
 
Spoiler: Old version
1.) version save
Code (Text):
    @EventHandler
    public void onPacketSend(PacketSendEvent e) {
        if(e.getPacketName().equals("PacketPlayOutSpawnEntity")) {
            e.setPacketValue("f", <EntityID>);//f is the Field in the packet which defines the Entity-type as an Integer
        }
    }



Maven
Code (Text):
    <dependency>
        <groupId>org.inventivetalent.packetlistenerapi</groupId>
        <artifactId>api</artifactId>
        <version>VERSION-SNAPSHOT</version>
    </dependency>
https://jitpack.io/#org.inventivetalent/packetlistenerapi

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.
​
Quick facts

Install steps are the general flow for this file type — How to install Minecraft Java mods & modpacks walks through it step by step.

[API] PacketListenerAPI is a free Minecraft Java mod. Compatible with Minecraft 1.16, 1.17. Downloaded 160,943 times (via Spigot). Download it and open it directly in the game.

Explore more