Skip to content
This repository was archived by the owner on May 18, 2020. It is now read-only.

Packet API

jaime29010 edited this page Nov 13, 2016 · 4 revisions
  1. Create a packet listener
  2. Insert your own packet

Create a packet listener

//You can listen to a specific packet by its class name or by using the general Packet class
PacketLib.addHandler(new PacketHandler<PacketPlayOutChat>() {
   @Override
   public void handle(PacketHandleEvent<PacketPlayOutChat> e) {
      //You can cancel the event 
      e.setCanceled(true);
   }
});

WARNING: At the moment you can only register a Listener after the class PacketHandler has successfully loaded. This bug will be fixed in the future. If you register the Listener in the onEnable method you must register the listener delayed by one second.

Insert your own packet

//First argument is the Protocol. It can be HANDSHAKE, LOGIN, GAME, PING.
//Second argument is the target of the packet. Possibilities: TO_SERVER, TO_CLIENT
//Third argument is the packet id. List of all packets ids can you find here: http://wiki.vg/Protocol
//Fourth argument is the packet serializer class.
Packet.registerPacket(Protocol.TO_CLIENT, Direction, 1, <PacketClass>.class); //Register the serializer for the packet

Create the serializer class for the packet 0x1D (PacketPlayOutEntityEffect)

import dev.wolveringer.BungeeUtil.ClientVersion.BigClientVersion;
import dev.wolveringer.BungeeUtil.packets.Abstract.PacketPlayOut;
import dev.wolveringer.packet.PacketDataSerializer;

public class PacketPlayOutEntityEffect extends Packet {
	int entity;
	int effect;
	int amplifier;
	int duration;
	boolean hidden = false; //Define the variable who will read/write to the packet

        //Constructor with the parameters of your custom packet
	public PacketPlayOutEntityEffect(int entity, int effect, int amplifier, int duration, boolean hidden) {	          this.entity = entity;
		this.effect = effect;
		this.amplifier = amplifier;
		this.duration = duration;
		this.hidden = hidden;
	}

	public PacketPlayOutEntityEffect() { //But this is not needed
		super(); //Default constructor. This constructor is executed when a packet is not created by an API/Lib
	}

	@Override //Read the packet data. List of packets and the data can you find here: http://wiki.vg/Protocol
	public void read(PacketDataSerializer s) {
		entity = s.readVarInt();
		effect = s.readByte();
		amplifier = s.readByte();
		duration = s.readVarInt();
		hidden = s.readBoolean();
	}
	@Override //Write the packet data (version dependent)
	public void write(PacketDataSerializer s) {
		if(getVersion().getBigVersion() == BigClientVersion.v1_8){ //Data for the Version 1.8
			s.writeVarInt(entity);
			s.writeByte(effect);
			s.writeByte(amplifier);
			s.writeVarInt(duration);
			s.writeBoolean(hidden);
		} else if(getVersion().getBigVersion() == BigClientVersion.v1_7){ //Data for the Version 1.7
			s.writeInt(entity);
			s.writeByte(effect);
			s.writeByte(amplifier);
			s.writeShort(duration);
		}
	}
}

Clone this wiki locally