Packet Listening
To listen to packets with PacketEventsSK you can use a single syntax element: On Packet. Let's break it down.
Packet Processing
Packets in Minecraft are always processed on what is called the netty threads. This is a group of worker threads that handle all network communication for the server. This means that if you want to intercept a packet, your code must be processed on the same threads (note that you can still read the packets on the other threads). The problem with this is that most of paper's API is not thread safe and can only be accessed on what is called the main thread (or sync thread).
Netty Processing
This processing type should be used for the interception of packets.
# "netty processed" ensures the packet is processed on the netty threads.
# This is the default process type for the event.
on any packet netty processed:
if event-packet is clientbound entity metadata packet:
# This packet won't be sent
cancel packet
else if event-packet is serverbound chat message packet:
# This packet won't be received
cancel packet
else if event-packet is clientbound entity metadata packet:
# this is a clone of the packet's meta
set {_meta} to packet meta of event-packet
set meta glowing state of {_meta} to true
# Sets to the clone
set packet meta of event-packet to {_meta}DANGER
Minecraft is not thread safe, you can not access most of paper's API on the netty threads. If you want to access paper's API, you must switch back to the main thread. Look into Run Task (SkBee) Run Section (Skript-Reflect).
Sync Processing
This processing type should be used for interacting with paper's API with the data of a packet.
on clientbound entity velocity sync processed:
set {_id} to packet entity id of event-packet
# this is not thread safe, thus sync processed
set {_entity} to entity with id {_id}
kill {_entity}Async Processing
This processing type should be used for heavy computation with the data of a packet. (Think anticheats analyzing movement packets)
on clientbound entity velocity async processed:
# ... heavy computation ...DANGER
Minecraft is not thread safe, you can not access most of paper's API on the netty threads. If you want to access paper's API, you must switch back to the main thread. Look into Run Task (SkBee) Run Section (Skript-Reflect).
Listening to All Packets
This is how you can listen to all packets sent or received by the server.
on any packet:
# event-packet and event-player are retrievable
# ... code ...Listening to a Packet Type
In PacketEventsSK packet types are defined by 2 parts, the direction (serverbound or clientbound) and the packet name (e.g. entity metadata). You can find a list of all packet types on SkDocs Packet Type
Here's an example:
on clientbound entity metadata packet:
# event-packet and event-player are retrievable
# ... code ...Listening Priority
There are 2 priorities when it comes to the on packet event: the bukkit event priority and the packet processing priority.
- The bukkit event priority is the same as the one used in normal bukkit events and can be set with the
with prioritysyntax element. - The PacketEvents priority is a separate priority that controls the listener priority within PacketEvents and can be set using the
with packet prioritysyntax element.
Both have 6 levels: monitor, lowest, low, normal, high and highest.
The priority for events is from the perspective of who should have the last say on the result, so lowest gets called first then all the way to highest and then monitor goes last.
on any packet with packet priority monitor:
# This will be the last listener to receive the packet
# and will only view the contents of the packet,
# you won't be able to modify under a "monitor" packet priority