def decode(self, bbuff, proto_comp_state): self.data = {} packet_length = datautils.unpack(MC_VARINT, bbuff) packet_data = bbuff.recv(packet_length) pbuff = BoundBuffer(packet_data) if proto_comp_state == mcdata.PROTO_COMP_ON: body_length = datautils.unpack(MC_VARINT, pbuff) if body_length > 0: body_data = zlib.decompress(pbuff.flush(), zlib.MAX_WBITS) pbuff.write(body_data) pbuff.save() try: # Ident self.__ident[2] = datautils.unpack(MC_VARINT, pbuff) self.ident = tuple(self.__ident) self.str_ident = mcdata.packet_ident2str[self.ident] # Payload for dtype, name in mcdata.hashed_structs[self.ident]: self.data[name] = datautils.unpack(dtype, pbuff) # Extension if self.ident in hashed_extensions: hashed_extensions[self.ident].decode_extra(self, pbuff) if len(pbuff) > 0: raise PacketDecodeFailure(self, pbuff) except BufferUnderflowException: raise PacketDecodeFailure(self, pbuff, True) return self
def decode(self, bbuff, proto_comp_state): self.data = {} packet_length = datautils.unpack(MC_VARINT, bbuff) packet_data = bbuff.recv(packet_length) pbuff = BoundBuffer(packet_data) if proto_comp_state == proto.PROTO_COMP_ON: body_length = datautils.unpack(MC_VARINT, pbuff) if body_length: body_data = zlib.decompress(pbuff.flush(), zlib.MAX_WBITS) pbuff.write(body_data) pbuff.save() try: # Ident self.__ident[2] = datautils.unpack(MC_VARINT, pbuff) self.ident = tuple(self.__ident) self.str_ident = proto.packet_ident2str[self.ident] # Payload for dtype, name in proto.hashed_structs[self.ident]: self.data[name] = datautils.unpack(dtype, pbuff) # Extension if self.ident in hashed_extensions: hashed_extensions[self.ident].decode_extra(self, pbuff) if pbuff: raise PacketDecodeFailure(self, pbuff) except BufferUnderflowException: raise PacketDecodeFailure(self, pbuff, True) return self
def encode_extra(packet): bbuff = BoundBuffer() if packet.data['nbt'] is None: packet.data['nbt'] = nbt._TagEnd() nbt.TagByte(packet.data['nbt'].id)._render_buffer(bbuff) if packet.data['nbt'].id == nbt.TAG_COMPOUND: nbt.TagString(packet.data['nbt'].name)._render_buffer(bbuff) packet.data['nbt']._render_buffer(bbuff) return bbuff.flush()
def pack_slot(slot): o = pack(MC_SHORT, slot['id']) if slot['id'] != -1: o += pack(MC_BYTE, slot['amount']) o += pack(MC_SHORT, slot['damage']) if 'enchants' in slot: ench = slot['enchants'] bbuff = BoundBuffer() nbt.TagByte(ench.id)._render_buffer(bbuff) nbt.TagString(ench.name)._render_buffer(bbuff) ench._render_buffer(bbuff) o += bbuff.flush() else: o += pack(MC_BYTE, 0) return o
def pack_slot(slot): o = pack(MC_SHORT, slot["id"]) if slot["id"] != -1: o += pack(MC_BYTE, slot["amount"]) o += pack(MC_SHORT, slot["damage"]) if "enchants" in slot: ench = slot["enchants"] bbuff = BoundBuffer() nbt.TagByte(ench.id)._render_buffer(bbuff) nbt.TagString(ench.name)._render_buffer(bbuff) ench._render_buffer(bbuff) o += bbuff.flush() else: o += pack(MC_BYTE, 0) return o