/** * Constructs a new WirePacket with a given type and contents * @param type Type of the packet * @param bytes Contents of the packet */ public WirePacket(PacketType type, byte[] bytes) { this.id = checkNotNull(type, "type cannot be null").getCurrentId(); this.bytes = bytes; }
@Override public int compareTo(PacketType other) { return ComparisonChain.start(). compare(protocol, other.getProtocol()). compare(sender, other.getSender()). compare(currentId, other.getCurrentId()). result(); }
private String getPacketId(PacketType type) { return Strings.padStart(Integer.toString(type.getCurrentId()), 2, '0') + " (Legacy: " + type.getLegacyId() + ")"; }
/** * Creates a WirePacket from an existing PacketContainer * @param packet Existing packet * @return The resulting WirePacket */ public static WirePacket fromPacket(PacketContainer packet) { int id = packet.getType().getCurrentId(); return new WirePacket(id, bytesFromPacket(packet)); }
/** * Creates a WirePacket from an existing Minecraft packet * @param packet Existing Minecraft packet * @return The resulting WirePacket * @throws IllegalArgumentException If the packet is null or not a Minecraft packet */ public static WirePacket fromPacket(Object packet) { checkNotNull(packet, "packet cannot be null!"); checkArgument(MinecraftReflection.isPacketClass(packet), "packet must be a Minecraft packet"); PacketType type = PacketType.fromClass(packet.getClass()); int id = type.getCurrentId(); ByteBuf buffer = PacketContainer.createPacketBuffer(); Method write = MinecraftMethods.getPacketWriteByteBufMethod(); try { write.invoke(packet, buffer); } catch (ReflectiveOperationException ex) { throw new RuntimeException("Failed to serialize packet contents.", ex); } byte[] bytes = getBytes(buffer); buffer.release(); return new WirePacket(id, bytes); }
/** * Add a collection of packet types to the lookup. * @param types - the types to add. */ public PacketTypeLookup addPacketTypes(Iterable<? extends PacketType> types) { Preconditions.checkNotNull(types, "types cannot be NULL"); for (PacketType type : types) { int legacy = type.getLegacyId(); // Skip unknown legacy packets if (legacy != PacketType.UNKNOWN_PACKET) { if (type.isServer()) serverLookup.put(type.getLegacyId(), type); if (type.isClient()) clientLookup.put(type.getLegacyId(), type); legacyLookup.put(type.getLegacyId(), type); } // Skip unknown current packets if (type.getCurrentId() != PacketType.UNKNOWN_PACKET) { idLookup.getMap(type.getProtocol(), type.getSender()).put(type.getCurrentId(), type); classLookup.getMap(type.getProtocol(), type.getSender()).put(type.getClassNames()[0], type); } nameLookup.put(type.name(), type); } return this; }