Class PacketEvent

java.lang.Object
me.internalizable.numdrassl.api.event.packet.PacketEvent
All Implemented Interfaces:
Cancellable

public class PacketEvent extends Object implements 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 Details

  • Method Details

    • getPlayer

      @Nonnull public Player getPlayer()
      Get the player associated with this packet.
      Returns:
      the player
    • getDirection

      @Nonnull public PacketDirection 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

      @Nonnull public Object getPacket()
      Get the packet being transmitted.
      Returns:
      the packet object
    • getPacketAs

      @Nullable public <T> T getPacketAs(@Nonnull Class<T> type)
      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

      public boolean isPacketType(@Nonnull Class<?> type)
      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

      public void setPacket(@Nonnull Object packet)
      Replace the packet with a different one.
      Parameters:
      packet - the new packet
    • isCancelled

      public boolean isCancelled()
      Description copied from interface: Cancellable
      Check if this event has been cancelled.
      Specified by:
      isCancelled in interface Cancellable
      Returns:
      true if the event is cancelled
    • setCancelled

      public void setCancelled(boolean cancelled)
      Description copied from interface: Cancellable
      Set whether this event is cancelled.
      Specified by:
      setCancelled in interface Cancellable
      Parameters:
      cancelled - true to cancel the event