@Override public void transform(Direction direction, State state, PacketWrapper packetWrapper) throws Exception { super.transform(direction, state, packetWrapper); if (direction == Direction.INCOMING && state == State.HANDSHAKE) { // Disable if it isn't a handshake packet. if (packetWrapper.getId() != 0) { packetWrapper.user().setActive(false); } } } }
return type.read(inputBuffer); } catch (Exception e) { throw new InformativeException(e).set("Type", type.getTypeName()).set("Packet ID", getId()).set("Data", packetValues); } else { Exception e = new IOException("Unable to read type " + type.getTypeName() + ", found " + read.getKey().getTypeName()); throw new InformativeException(e).set("Type", type.getTypeName()).set("Packet ID", getId()).set("Data", packetValues);
/** * Set a currently existing part in the output * * @param type The type of the part you wish to set. * @param <T> The return type of the type you wish to set. * @param index The index of the part (relative to the type) * @param value The value of the part you wish to set it to. * @throws Exception If it fails to set it, an exception will be thrown. */ public <T> void set(Type<T> type, int index, T value) throws Exception { int currentIndex = 0; for (Pair<Type, Object> packetValue : packetValues) { if (packetValue.getKey() == type) { // Ref check if (currentIndex == index) { packetValue.setValue(value); return; } currentIndex++; } } Exception e = new ArrayIndexOutOfBoundsException("Could not find type " + type.getTypeName() + " at " + index); throw new InformativeException(e).set("Type", type.getTypeName()).set("Index", index).set("Packet ID", getId()); }
throw new InformativeException(e).set("Index", index).set("Type", packetValue.getKey().getTypeName()).set("Packet ID", getId()).set("Data", packetValues);
/** * Get a part from the output * * @param type The type of the part you wish to get. * @param <T> The return type of the type you wish to get. * @param index The index of the part (relative to the type) * @return The requested type or throws ArrayIndexOutOfBounds * @throws Exception If it fails to find it, an exception will be thrown. */ public <T> T get(Type<T> type, int index) throws Exception { int currentIndex = 0; for (Pair<Type, Object> packetValue : packetValues) { if (packetValue.getKey() == type) { // Ref check if (currentIndex == index) { return (T) packetValue.getValue(); } currentIndex++; } } Exception e = new ArrayIndexOutOfBoundsException("Could not find type " + type.getTypeName() + " at " + index); throw new InformativeException(e).set("Type", type.getTypeName()).set("Index", index).set("Packet ID", getId()).set("Data", packetValues); }
/** * 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(); } }