/** * Send this packet to the associated user. * Be careful not to send packets twice. * (Sends it after current) * * @param packetProtocol - The protocol version of the packet. * @param skipCurrentPipeline - Skip the current pipeline * @param currentThread - Run in the same thread * @throws Exception if it fails to write */ public void send(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception { if (!isCancelled()) { ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline, Direction.OUTGOING); user().sendRawPacket(output, currentThread); } }
/** * Send this packet to the server. * * @param packetProtocol - The protocol version of the packet. * @param skipCurrentPipeline - Skip the current pipeline * @param currentThread - Run in the same thread * @throws Exception if it fails to write */ public void sendToServer(Class<? extends Protocol> packetProtocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception { if (!isCancelled()) { ByteBuf output = constructPacket(packetProtocol, skipCurrentPipeline, Direction.INCOMING); user().sendRawPacketToServer(output, currentThread); } }
/** * Send the current packet to the server. * (Ensure the ID is suitable for viaversion) * * @throws Exception If it failed to write */ @Deprecated public void sendToServer() throws Exception { if (!isCancelled()) { ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer(); writeToBuffer(output); user().sendRawPacketToServer(output, true); } }
/** * Send this packet to the associated user. * Be careful not to send packets twice. * (Sends it after current) * <b>This method is no longer used, it's favoured to use {@link #send(Class)} as it will handle the pipeline properly.</b> * * @throws Exception if it fails to write */ @Deprecated public void send() throws Exception { if (!isCancelled()) { // Send ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer(); writeToBuffer(output); user().sendRawPacket(output); } }
/** * Send this packet to the associated user. * Be careful not to send packets twice. * (Sends it after current) * Also returns the packets ChannelFuture * * @param packetProtocol - The protocol version of the packet. * @return The packets ChannelFuture * @throws Exception if it fails to write */ public ChannelFuture sendFuture(Class<? extends Protocol> packetProtocol) throws Exception { if (!isCancelled()) { ByteBuf output = constructPacket(packetProtocol, true, Direction.OUTGOING); return user().sendRawPacketFuture(output); } return user().getChannel().newFailedFuture(new Exception("Cancelled packet")); }
/** * Transform a packet using this protocol * * @param direction The direction the packet is going in * @param state The current protocol state * @param packetWrapper The packet wrapper to transform * @throws Exception Throws exception if it fails to transform */ public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception { Pair<State, Integer> statePacket = new Pair<>(state, packetWrapper.getId()); Map<Pair<State, Integer>, ProtocolPacket> packetMap = (direction == Direction.OUTGOING ? outgoing : incoming); ProtocolPacket protocolPacket = packetMap.get(statePacket); if (protocolPacket == null) { return; } // write packet id int newID = direction == Direction.OUTGOING ? protocolPacket.getNewID() : protocolPacket.getOldID(); packetWrapper.setId(newID); // remap if (protocolPacket.getRemapper() != null) { protocolPacket.getRemapper().remap(packetWrapper); if (packetWrapper.isCancelled()) throw new CancelException(); } }