Class PacketEvent
java.lang.Object
me.internalizable.numdrassl.api.event.packet.PacketEvent
- All Implemented Interfaces:
Cancellable
Event fired when a packet is received by the proxy.
This event allows plugins to inspect, modify, or cancel packets flowing
between clients and backend servers.
The proxy intercepts all packets flowing in both directions, allowing plugins to inspect, modify, or relay them as needed while keeping the connection transparent to both endpoints.
Example usage:
@Subscribe
public void onPacket(PacketEvent event) {
Object packet = event.getPacket();
if (packet instanceof ChatMessage) {
ChatMessage chat = (ChatMessage) packet;
// Inspect or modify the chat message
if (chat.getMessage().contains("bad word")) {
event.setCancelled(true); // Block the packet
}
}
}
To replace a packet with a different one:
@Subscribe
public void onPacket(PacketEvent event) {
if (event.getPacket() instanceof SomePacket) {
// Replace with a modified version
event.setPacket(new SomePacket(modifiedData));
}
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGet the direction of the packet.Get the packet being transmitted.<T> TgetPacketAs(Class<T> type) Get the packet as a specific type.Get the player associated with this packet.booleanCheck if this event has been cancelled.booleanCheck if this packet is traveling from the client to the server.booleanisPacketType(Class<?> type) Check if the packet is of a specific type.booleanCheck if this packet is traveling from the server to the client.voidsetCancelled(boolean cancelled) Set whether this event is cancelled.voidReplace the packet with a different one.
-
Constructor Details
-
PacketEvent
public PacketEvent(@Nonnull Player player, @Nonnull PacketDirection direction, @Nonnull Object packet)
-
-
Method Details
-
getPlayer
-
getDirection
Get the direction of the packet.- Returns:
- the packet direction
-
isClientbound
public boolean isClientbound()Check if this packet is traveling from the client to the server.- Returns:
- true if clientbound
-
isServerbound
public boolean isServerbound()Check if this packet is traveling from the server to the client.- Returns:
- true if serverbound
-
getPacket
-
getPacketAs
Get the packet as a specific type.- Type Parameters:
T- the packet type- Parameters:
type- the expected packet class- Returns:
- the packet, or null if it's not the expected type
-
isPacketType
Check if the packet is of a specific type.- Parameters:
type- the packet class to check- Returns:
- true if the packet is an instance of the type
-
setPacket
Replace the packet with a different one.- Parameters:
packet- the new packet
-
isCancelled
public boolean isCancelled()Description copied from interface:CancellableCheck if this event has been cancelled.- Specified by:
isCancelledin interfaceCancellable- Returns:
- true if the event is cancelled
-
setCancelled
public void setCancelled(boolean cancelled) Description copied from interface:CancellableSet whether this event is cancelled.- Specified by:
setCancelledin interfaceCancellable- Parameters:
cancelled- true to cancel the event
-