/** * Register an outgoing packet, with id transformation and remapper. * * @param state The state which the packet is sent in. * @param oldPacketID The old packet ID * @param newPacketID The new packet ID * @param packetRemapper The remapper to use for the packet */ public void registerOutgoing(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper) { ProtocolPacket protocolPacket = new ProtocolPacket(state, oldPacketID, newPacketID, packetRemapper); outgoing.put(new Pair<>(state, oldPacketID), protocolPacket); }
/** * 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(); } }
/** * Register an incoming packet, with id transformation and remapper. * * @param state The state which the packet is sent in. * @param oldPacketID The old packet ID * @param newPacketID The new packet ID * @param packetRemapper The remapper to use for the packet */ public void registerIncoming(State state, int oldPacketID, int newPacketID, PacketRemapper packetRemapper) { ProtocolPacket protocolPacket = new ProtocolPacket(state, oldPacketID, newPacketID, packetRemapper); incoming.put(new Pair<>(state, newPacketID), protocolPacket); }