Interface PacketWrapper


  • public interface PacketWrapper
    • Method Detail

      • create

        static PacketWrapper create​(@Nullable PacketType packetType,
                                    UserConnection connection)
        Creates a new packet wrapper instance.
        Parameters:
        packetType - packet type, or null if none should be written to the buffer (raw id = -1)
        connection - user connection
        Returns:
        new packet wrapper
      • create

        static PacketWrapper create​(@Nullable PacketType packetType,
                                    @Nullable io.netty.buffer.ByteBuf inputBuffer,
                                    UserConnection connection)
        Creates a new packet wrapper instance.
        Parameters:
        packetType - packet type, or null if none should be written to the buffer (raw id = -1)
        inputBuffer - input buffer
        connection - user connection
        Returns:
        new packet wrapper
      • create

        @Deprecated
        static PacketWrapper create​(int packetId,
                                    @Nullable io.netty.buffer.ByteBuf inputBuffer,
                                    UserConnection connection)
        Deprecated.
        Creates a new packet wrapper instance.
        Parameters:
        packetId - packet id, or -1 if none should be written to the buffer
        inputBuffer - input buffer
        connection - user connection
        Returns:
        new packet wrapper
      • get

        <T> T get​(Type<T> type,
                  int index)
           throws java.lang.Exception
        Get a part from the output
        Type Parameters:
        T - The return type of the type you wish to get.
        Parameters:
        type - The type of the part you wish to get.
        index - The index of the part (relative to the type)
        Returns:
        The requested type or throws ArrayIndexOutOfBounds
        Throws:
        InformativeException - If it fails to find it, an exception will be thrown.
        java.lang.Exception
      • is

        boolean is​(Type type,
                   int index)
        Check if a type is at an index
        Parameters:
        type - The type of the part you wish to get.
        index - The index of the part (relative to the type)
        Returns:
        True if the type is at the index
      • isReadable

        boolean isReadable​(Type type,
                           int index)
        Check if a type is at an index
        Parameters:
        type - The type of the part you wish to get.
        index - The index of the part (relative to the type)
        Returns:
        True if the type is at the index
      • set

        <T> void set​(Type<T> type,
                     int index,
                     T value)
              throws java.lang.Exception
        Set a currently existing part in the output
        Type Parameters:
        T - The return type of the type you wish to set.
        Parameters:
        type - The type of the part you wish to set.
        index - The index of the part (relative to the type)
        value - The value of the part you wish to set it to.
        Throws:
        InformativeException - If it fails to set it, an exception will be thrown.
        java.lang.Exception
      • read

        <T> T read​(Type<T> type)
            throws java.lang.Exception
        Read a type from the input.
        Type Parameters:
        T - The return type of the type you wish to read.
        Parameters:
        type - The type you wish to read
        Returns:
        The requested type
        Throws:
        InformativeException - If it fails to read
        java.lang.Exception
      • write

        <T> void write​(Type<T> type,
                       T value)
        Write a type to the output.
        Type Parameters:
        T - The return type of the type you wish to write.
        Parameters:
        type - The type to write.
        value - The value of the type to write.
      • passthrough

        <T> T passthrough​(Type<T> type)
                   throws java.lang.Exception
        Take a value from the input and write to the output.
        Type Parameters:
        T - The return type of the type you wish to pass through.
        Parameters:
        type - The type to read and write.
        Returns:
        The type which was read/written.
        Throws:
        java.lang.Exception - If it failed to read or write
      • passthroughAll

        void passthroughAll()
                     throws java.lang.Exception
        Take all the inputs and write them to the output.
        Throws:
        java.lang.Exception - If it failed to read or write
      • writeToBuffer

        void writeToBuffer​(io.netty.buffer.ByteBuf buffer)
                    throws java.lang.Exception
        Write the current output to a buffer.
        Parameters:
        buffer - The buffer to write to.
        Throws:
        InformativeException - Throws an exception if it fails to write a value.
        java.lang.Exception
      • clearInputBuffer

        void clearInputBuffer()
        Clear the input buffer / readable objects
      • clearPacket

        void clearPacket()
        Clear the packet, used if you have to change the packet completely
      • send

        default void send​(java.lang.Class<? extends Protocol> protocol)
                   throws java.lang.Exception
        Send this packet to the connection on the current thread, skipping the current protocol.
        Parameters:
        protocol - protocol to be sent through
        Throws:
        java.lang.Exception - if it fails to write
      • send

        void send​(java.lang.Class<? extends Protocol> protocol,
                  boolean skipCurrentPipeline)
           throws java.lang.Exception
        Send this packet to the connection on the current thread.
        Parameters:
        protocol - protocol to be sent through
        skipCurrentPipeline - whether transformation of the current protocol should be skipped
        Throws:
        java.lang.Exception - if it fails to write
      • scheduleSend

        default void scheduleSend​(java.lang.Class<? extends Protocol> protocol)
                           throws java.lang.Exception
        Send this packet to the connection, submitted to netty's event loop and skipping the current protocol.
        Parameters:
        protocol - protocol to be sent through
        Throws:
        java.lang.Exception - if it fails to write
      • scheduleSend

        void scheduleSend​(java.lang.Class<? extends Protocol> protocol,
                          boolean skipCurrentPipeline)
                   throws java.lang.Exception
        Send this packet to the connection, submitted to netty's event loop.
        Parameters:
        protocol - protocol to be sent through
        skipCurrentPipeline - whether transformation of the current protocol should be skipped
        Throws:
        java.lang.Exception - if it fails to write
      • sendFuture

        io.netty.channel.ChannelFuture sendFuture​(java.lang.Class<? extends Protocol> packetProtocol)
                                           throws java.lang.Exception
        Send this packet to the associated user. Be careful not to send packets twice. (Sends it after current) Also returns the packets ChannelFuture
        Parameters:
        packetProtocol - The protocol version of the packet.
        Returns:
        The packets ChannelFuture
        Throws:
        java.lang.Exception - if it fails to write
      • send

        @Deprecated
        default void send()
                   throws java.lang.Exception
        Deprecated.
        misleading; use sendRaw(). This method will be removed in 5.0.0
        Throws:
        java.lang.Exception
      • sendRaw

        void sendRaw()
              throws java.lang.Exception
        Sends this packet to the connection. Unlike send(Class), this method does not handle the pipeline with packet id and data changes.
        Throws:
        java.lang.Exception - if it fails to write
      • scheduleSendRaw

        void scheduleSendRaw()
                      throws java.lang.Exception
        Sends this packet to the associated user, submitted to netty's event loop. Unlike send(Class), this method does not handle the pipeline with packet id and data changes.
        Throws:
        java.lang.Exception - if it fails to write
      • create

        default PacketWrapper create​(PacketType packetType)
        Creates a new packet for the target of this packet.
        Parameters:
        packetType - packet type of the new packet
        Returns:
        The newly created packet wrapper
      • create

        default PacketWrapper create​(PacketType packetType,
                                     PacketHandler handler)
                              throws java.lang.Exception
        Creates a new packet with values.
        Parameters:
        packetType - packet type of the new packet
        handler - handler to write to the packet
        Returns:
        newly created packet wrapper
        Throws:
        java.lang.Exception - if it failed to write the values from the ValueCreator
      • create

        PacketWrapper create​(int packetId)
        Creates a new packet for the target of this packet.
        Parameters:
        packetId - id of the packet
        Returns:
        newly created packet wrapper
      • create

        PacketWrapper create​(int packetId,
                             PacketHandler handler)
                      throws java.lang.Exception
        Creates a new packet with values.
        Parameters:
        packetId - id of the packet
        handler - handler to write to the packet
        Returns:
        newly created packet wrapper
        Throws:
        java.lang.Exception - if it failed to write the values from the ValueCreator
      • apply

        PacketWrapper apply​(Direction direction,
                            State state,
                            int index,
                            java.util.List<Protocol> pipeline,
                            boolean reverse)
                     throws java.lang.Exception
        Applies a pipeline from an index to the wrapper.
        Parameters:
        direction - protocol direction
        state - protocol state
        index - index to start from, will be reversed depending on the reverse parameter
        pipeline - protocol pipeline
        reverse - whether the array should be looped in reverse, will also reverse the given index
        Returns:
        The current packetwrapper
        Throws:
        java.lang.Exception - If it fails to transform a packet, exception will be thrown
      • cancel

        void cancel()
        Cancel this packet from sending
      • isCancelled

        boolean isCancelled()
        Check if this packet is cancelled.
        Returns:
        True if the packet won't be sent.
      • user

        UserConnection user()
        Get the user associated with this Packet
        Returns:
        The user
      • resetReader

        void resetReader()
        Reset the reader, so that it can be read again.
      • sendToServer

        @Deprecated
        default void sendToServer()
                           throws java.lang.Exception
        Deprecated.
        misleading; use sendToServerRaw(). This method will be removed in 5.0.0
        Send the current packet to the server. (Ensure the ID is suitable for viaversion)
        Throws:
        java.lang.Exception - If it failed to write
      • sendToServerRaw

        void sendToServerRaw()
                      throws java.lang.Exception
        Sends this packet to the server. Unlike sendToServer(Class), this method does not handle the pipeline with packet id and data changes.
        Throws:
        java.lang.Exception - if it fails to write
      • scheduleSendToServerRaw

        void scheduleSendToServerRaw()
                              throws java.lang.Exception
        Sends this packet to the server, submitted to netty's event loop. Unlike sendToServer(Class), this method does not handle the pipeline with packet id and data changes.
        Throws:
        java.lang.Exception - if it fails to write
      • sendToServer

        default void sendToServer​(java.lang.Class<? extends Protocol> protocol)
                           throws java.lang.Exception
        Send this packet to the server on the current thread, skipping the current protocol.
        Parameters:
        protocol - protocol to be sent through
        Throws:
        java.lang.Exception - if it fails to write
      • sendToServer

        void sendToServer​(java.lang.Class<? extends Protocol> protocol,
                          boolean skipCurrentPipeline)
                   throws java.lang.Exception
        Send this packet to the server on the current thread.
        Parameters:
        protocol - protocol to be sent through
        skipCurrentPipeline - whether transformation of the current protocol should be skipped
        Throws:
        java.lang.Exception - if it fails to write
      • scheduleSendToServer

        default void scheduleSendToServer​(java.lang.Class<? extends Protocol> protocol)
                                   throws java.lang.Exception
        Send this packet to the server, submitted to netty's event loop and skipping the current protocol.
        Parameters:
        protocol - protocol to be sent through
        Throws:
        java.lang.Exception - if it fails to write
      • scheduleSendToServer

        void scheduleSendToServer​(java.lang.Class<? extends Protocol> protocol,
                                  boolean skipCurrentPipeline)
                           throws java.lang.Exception
        Send this packet to the server, submitted to netty's event loop.
        Parameters:
        protocol - protocol to be sent through
        skipCurrentPipeline - whether transformation of the current protocol should be skipped
        Throws:
        java.lang.Exception - if it fails to write
      • getPacketType

        @Nullable PacketType getPacketType()
        Returns the packet type. Currently only non-null for manually constructed packets before transformation.
        Returns:
        packet type if set
      • setPacketType

        void setPacketType​(@Nullable PacketType packetType)
        Sets the packet type. If set to null, it will not be written to the buffer with writeToBuffer(ByteBuf). Setting the type to null also sets the raw packet id to -1.
        Parameters:
        packetType - packet type
      • getId

        int getId()
        Returns the raw packet id.
        Returns:
        raw packet id
      • setId

        @Deprecated
        default void setId​(PacketType packetType)
        Deprecated.
        use setPacketType(PacketType). This method will be removed in 5.0.0
        Sets the packet type.
        Parameters:
        packetType - packet type
      • setId

        @Deprecated
        void setId​(int id)
        Deprecated.
        magic id, loses packet type info; use setPacketType(PacketType)
        Sets the packet id. If set to -1, it will not be written to the buffer with writeToBuffer(ByteBuf).
        Parameters:
        id - packet id