Interface ChannelHandler<C extends ListenerContext>

Type Parameters:
C - the listener context
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface
public interface ChannelHandler<C extends ListenerContext>
Handles packets in a channel.

This is supposed to be implemented by API users to accomplish packet handling functionalities.

See Also:
PacketReceiver
  • Method Details

    • receive

      void receive​(C context, net.minecraft.network.PacketByteBuf buf)
      Receives a packet.

      This method is executed on netty's event loops. Modification to the game should be scheduled.

      The buf will be released on exiting this method. To ensure access to the buf later, you should retain the buf.

      An example usage can be like below, assuming C is a client context:

      (context, buf) -&rt; { String message = buf.readString(32767); context.getEngine().submit(() -&rt; { context.getEngine().send(() -&rt; context.getEngine().inGameHud.setOverlayMessage(message, true)); }); }

      When this method throws an exception, it will be captured and logged. The exception will be fed to rethrow(Throwable), which by default throws the exception to the event loop and handled by ClientConnection.exceptionCaught(ChannelHandlerContext, Throwable), causing a disconnection.

      Parameters:
      context - the context for the packet
      buf - the content of the packet
    • rethrow

      default <E extends Throwable> void rethrow​(Throwable ex) throws E extends Throwable
      Handles a exception thrown by receive(ListenerContext, PacketByteBuf).

      By default, this implementation will simply throw the captured exception.

      Throwables thrown by this method will be handled by ClientConnection.exceptionCaught(ChannelHandlerContext, Throwable).

      Type Parameters:
      E - the throwable type variable, which allows throwing any throwable as unchecked
      Parameters:
      ex - the captured exception
      Throws:
      E - any exception as a result of exception handling.