This repository was archived by the owner on May 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Packet API
jaime29010 edited this page Nov 13, 2016
·
4 revisions
- Create a packet listener
- Insert your own packet
//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.
//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 packetCreate 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);
}
}
}