/** * Construct a new packet container from a given handle. * @param packet - the NMS packet. * @return The packet container. */ public static PacketContainer fromPacket(Object packet) { PacketType type = PacketType.fromClass(packet.getClass()); return new PacketContainer(type, 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); }