def read(self, file_object): self.map_id = VarInt.read(file_object) self.scale = Byte.read(file_object) if self.context.protocol_version >= 107: self.is_tracking_position = Boolean.read(file_object) else: self.is_tracking_position = True icon_count = VarInt.read(file_object) self.icons = [] for i in range(icon_count): type, direction = divmod(UnsignedByte.read(file_object), 16) x = Byte.read(file_object) z = Byte.read(file_object) icon = MapPacket.MapIcon(type, direction, (x, z)) self.icons.append(icon) self.width = UnsignedByte.read(file_object) if self.width: self.height = UnsignedByte.read(file_object) x = Byte.read(file_object) z = Byte.read(file_object) self.offset = (x, z) self.pixels = VarIntPrefixedByteArray.read(file_object) else: self.height = 0 self.offset = None self.pixels = None
def read(self, file_object): self.entity_id = VarInt.read(file_object) if self._context.protocol_version >= 49: self.objectUUID = UUID.read(file_object) type_id = Byte.read(file_object) self.type = SpawnObjectPacket.EntityType.get_type_by_id(type_id) if self._context.protocol_version >= 100: self.x = Double.read(file_object) self.y = Double.read(file_object) self.z = Double.read(file_object) else: self.x = Integer.read(file_object) self.y = Integer.read(file_object) self.z = Integer.read(file_object) self.pitch = UnsignedByte.read(file_object) self.yaw = UnsignedByte.read(file_object) self.data = Integer.read(file_object) if self._context.protocol_version < 49: if self.data > 0: self.velocity_x = Short.read(file_object) self.velocity_y = Short.read(file_object) self.velocity_z = Short.read(file_object) else: self.velocity_x = Short.read(file_object) self.velocity_y = Short.read(file_object) self.velocity_z = Short.read(file_object)
def write(self, socket, compression_threshold=None): packet_buffer = PacketBuffer() VarInt.send(self.id, packet_buffer) VarInt.send(self.map_id, packet_buffer) Byte.send(self.scale, packet_buffer) if self.context.protocol_version >= 107: Boolean.send(self.is_tracking_position, packet_buffer) VarInt.send(len(self.icons), packet_buffer) for icon in self.icons: type_and_direction = (icon.direction << 4) & 0xF0 type_and_direction |= (icon.type & 0xF) UnsignedByte.send(type_and_direction, packet_buffer) Byte.send(icon.location[0], packet_buffer) Byte.send(icon.location[1], packet_buffer) UnsignedByte.send(self.width, packet_buffer) if self.width: UnsignedByte.send(self.height, packet_buffer) UnsignedByte.send(self.offset[0], packet_buffer) # x UnsignedByte.send(self.offset[1], packet_buffer) # z VarIntPrefixedByteArray.send(self.pixels, packet_buffer) self._write_buffer(socket, packet_buffer, compression_threshold)
def read(self, file_object, parent): h_position = UnsignedByte.read(file_object) self.x, self.z = h_position >> 4, h_position & 0xF self.y = UnsignedByte.read(file_object) self.block_state_id = VarInt.read(file_object) # Absolute position in world to be compatible with BlockChangePacket self.location = Vector(self.position.x + parent.chunk_x * 16, self.position.y, self.position.z + parent.chunk_z * 16)
def read(self, file_object): self.chunk_x = Integer.read(file_object) self.chunk_z = Integer.read(file_object) records_count = VarInt.read(file_object) record_type = self.BaseRecord.get_subclass(self.context) self.records = [] for i in range(records_count): record = record_type(h_position=UnsignedByte.read(file_object), y_coordinate=UnsignedByte.read(file_object), blockData=VarInt.read(file_object)) self.records.append(record)
def send_with_context(self, record, socket, context): if context.protocol_later_eq(741): value = record.block_state_id << 12 | \ (record.x & 0xF) << 8 | \ (record.z & 0xF) << 4 | \ record.y & 0xF VarLong.send(value, socket) else: UnsignedByte.send(record.x << 4 | record.z & 0xF, socket) UnsignedByte.send(record.y, socket) VarInt.send(record.block_state_id, socket)
def send_with_context(self, record, socket, context): if context.protocol_version >= 741: value = (record.block_state_id << 12 | (record.x & 0xF) << 8 | (record.z & 0xF) << 4 | record.y & 0xF) VarLong.send(value, socket) else: UnsignedByte.send(record.x << 4 | record.z & 0xF, socket) UnsignedByte.send(record.y, socket) VarInt.send(record.block_state_id, socket)
def read(self, file_object): self.chunk_x = Integer.read(file_object) self.chunk_z = Integer.read(file_object) records_count = VarInt.read(file_object) self.records = [] for i in range(records_count): rec_horizontal_position = UnsignedByte.read(file_object) rec_y_coordinate = UnsignedByte.read(file_object) rec_blockData = VarInt.read(file_object) record = MultiBlockChangePacket.Record( rec_horizontal_position, rec_y_coordinate, rec_blockData) self.records.append(record)
def read_with_context(cls, file_object, context): record = cls() if context.protocol_version >= 741: value = VarLong.read(file_object) record.block_state_id = value >> 12 record.x = (value >> 8) & 0xF record.z = (value >> 4) & 0xF record.y = value & 0xF else: h_position = UnsignedByte.read(file_object) record.x = h_position >> 4 record.z = h_position & 0xF record.y = UnsignedByte.read(file_object) record.block_state_id = VarInt.read(file_object) return record
def read(self, file_object): self.empty = False self.block_count = Short.read(file_object) self.bpb = UnsignedByte.read(file_object) if self.bpb <= 4: self.bpb = 4 if self.bpb <= 8: # Indirect palette self.palette = [] size = VarInt.read(file_object) for i in range(size): self.palette.append(VarInt.read(file_object)) else: # Direct palette self.palette = None size = VarInt.read(file_object) longs = [] for i in range(size): longs.append(UnsignedLong.read(file_object)) self.blocks = [] mask = (1 << self.bpb)-1 for i in range(4096): l1 = int((i*self.bpb)/64) offset = (i*self.bpb)%64 l2 = int(((i+1)*self.bpb-1)/64) n = longs[l1] >> offset if l2>l1: n |= longs[l2] << (64-offset) n &= mask if self.palette: n = self.palette[n] self.blocks.append(n)
def write_fields(self, packet_buffer): VarInt.send(self.entity_id, packet_buffer) if self.context.protocol_version >= 49: UUID.send(self.object_uuid, packet_buffer) Byte.send(self.type_id, packet_buffer) xyz_type = Double if self.context.protocol_version >= 100 else Integer for coord in self.x, self.y, self.z: xyz_type.send(coord, packet_buffer) for coord in self.pitch, self.yaw: UnsignedByte.send(coord, packet_buffer) Integer.send(self.data, packet_buffer) if self.context.protocol_version >= 49 or self.data > 0: for coord in self.velocity_x, self.velocity_y, self.velocity_z: Short.send(coord, packet_buffer)
def read(self, file_object): self.map_id = VarInt.read(file_object) self.scale = Byte.read(file_object) if self.context.protocol_in_range(107, PRE | 6): self.is_tracking_position = Boolean.read(file_object) elif self.context.protocol_earlier(107): self.is_tracking_position = True if self.context.protocol_later_eq(452): self.is_locked = Boolean.read(file_object) else: self.is_locked = False if self.context.protocol_later_eq(PRE | 6): self.is_tracking_position = Boolean.read(file_object) icon_count = VarInt.read(file_object) self.icons = [] for i in range(icon_count): if self.context.protocol_later_eq(373): type = VarInt.read(file_object) else: type, direction = divmod(UnsignedByte.read(file_object), 16) x = Byte.read(file_object) z = Byte.read(file_object) if self.context.protocol_later_eq(373): direction = UnsignedByte.read(file_object) if self.context.protocol_later_eq(364): has_name = Boolean.read(file_object) display_name = String.read(file_object) if has_name else None else: display_name = None icon = MapPacket.MapIcon(type, direction, (x, z), display_name) self.icons.append(icon) self.width = UnsignedByte.read(file_object) if self.width: self.height = UnsignedByte.read(file_object) x = Byte.read(file_object) z = Byte.read(file_object) self.offset = (x, z) self.pixels = VarIntPrefixedByteArray.read(file_object) else: self.height = 0 self.offset = None self.pixels = None
def read(self, file_object): self.map_id = VarInt.read(file_object) self.scale = Byte.read(file_object) if self.context.protocol_version >= 107: self.is_tracking_position = Boolean.read(file_object) else: self.is_tracking_position = True if self.context.protocol_version >= 452: self.is_locked = Boolean.read(file_object) else: self.is_locked = False icon_count = VarInt.read(file_object) self.icons = [] for i in range(icon_count): if self.context.protocol_version >= 373: type = VarInt.read(file_object) else: type, direction = divmod(UnsignedByte.read(file_object), 16) x = Byte.read(file_object) z = Byte.read(file_object) if self.context.protocol_version >= 373: direction = UnsignedByte.read(file_object) if self.context.protocol_version >= 364: has_name = Boolean.read(file_object) display_name = String.read(file_object) if has_name else None else: display_name = None icon = MapPacket.MapIcon(type, direction, (x, z), display_name) self.icons.append(icon) self.width = UnsignedByte.read(file_object) if self.width: self.height = UnsignedByte.read(file_object) x = Byte.read(file_object) z = Byte.read(file_object) self.offset = (x, z) self.pixels = VarIntPrefixedByteArray.read(file_object) else: self.height = 0 self.offset = None self.pixels = None
def write_fields(self, packet_buffer): VarInt.send(self.entity_id, packet_buffer) if self.context.protocol_version >= 49: UUID.send(self.object_uuid, packet_buffer) if self.context.protocol_version >= 458: VarInt.send(self.type_id, packet_buffer) else: Byte.send(self.type_id, packet_buffer) xyz_type = Double if self.context.protocol_version >= 100 else Integer for coord in self.x, self.y, self.z: xyz_type.send(coord, packet_buffer) for coord in self.pitch, self.yaw: UnsignedByte.send(coord, packet_buffer) Integer.send(self.data, packet_buffer) if self.context.protocol_version >= 49 or self.data > 0: for coord in self.velocity_x, self.velocity_y, self.velocity_z: Short.send(coord, packet_buffer)
def read(self, file_object): self.entity_id = VarInt.read(file_object) if self.context.protocol_version >= 49: self.object_uuid = UUID.read(file_object) self.type_id = Byte.read(file_object) xyz_type = Double if self.context.protocol_version >= 100 else Integer for attr in 'x', 'y', 'z': setattr(self, attr, xyz_type.read(file_object)) for attr in 'pitch', 'yaw': setattr(self, attr, UnsignedByte.read(file_object)) self.data = Integer.read(file_object) if self.context.protocol_version >= 49 or self.data > 0: for attr in 'velocity_x', 'velocity_y', 'velocity_z': setattr(self, attr, Short.read(file_object))
def read(self, file_object): self.entity_id = VarInt.read(file_object) if self.context.protocol_version >= 49: self.object_uuid = UUID.read(file_object) if self.context.protocol_version >= 458: self.type_id = VarInt.read(file_object) else: self.type_id = Byte.read(file_object) xyz_type = Double if self.context.protocol_version >= 100 else Integer for attr in 'x', 'y', 'z': setattr(self, attr, xyz_type.read(file_object)) for attr in 'pitch', 'yaw': setattr(self, attr, UnsignedByte.read(file_object)) self.data = Integer.read(file_object) if self.context.protocol_version >= 49 or self.data > 0: for attr in 'velocity_x', 'velocity_y', 'velocity_z': setattr(self, attr, Short.read(file_object))
def write(self, packet_buffer): UnsignedByte.send(self.x << 4 | self.z & 0xF, packet_buffer) UnsignedByte.send(self.y, packet_buffer) VarInt.send(self.block_state_id, packet_buffer)
def read(self, file_object): h_position = UnsignedByte.read(file_object) self.x, self.z = h_position >> 4, h_position & 0xF self.y = UnsignedByte.read(file_object) self.block_state_id = VarInt.read(file_object)
def write_fields(self, packet_buffer): VarInt.send(self.map_id, packet_buffer) Byte.send(self.scale, packet_buffer) if self.context.protocol_version >= 107: Boolean.send(self.is_tracking_position, packet_buffer) VarInt.send(len(self.icons), packet_buffer) for icon in self.icons: if self.context.protocol_version >= 373: VarInt.send(icon.type, packet_buffer) else: type_and_direction = (icon.type << 4) & 0xF0 type_and_direction |= (icon.direction & 0xF) UnsignedByte.send(type_and_direction, packet_buffer) Byte.send(icon.location[0], packet_buffer) Byte.send(icon.location[1], packet_buffer) if self.context.protocol_version >= 373: UnsignedByte.send(icon.direction, packet_buffer) if self.context.protocol_version >= 364: Boolean.send(icon.display_name is not None, packet_buffer) if icon.display_name is not None: String.send(icon.display_name, packet_buffer) UnsignedByte.send(self.width, packet_buffer) if self.width: UnsignedByte.send(self.height, packet_buffer) UnsignedByte.send(self.offset[0], packet_buffer) # x UnsignedByte.send(self.offset[1], packet_buffer) # z VarIntPrefixedByteArray.send(self.pixels, packet_buffer)
def read_data(self, data, dimension): file_object = PacketBuffer() file_object.send(data) file_object.reset_cursor() for i in range(16): if self.bitmask & (1 << i): bits_per_block = UnsignedByte.read(file_object) palette = None if bits_per_block < GLOBAL_BITS_PER_BLOCK: palette_length = VarInt.read(file_object) palette = [] for _ in range(palette_length): palette.append(VarInt.read(file_object)) section = ChunkDataPacket.ChunkSection() data_length = VarInt.read(file_object) data = [] for _ in range(data_length): part = file_object.read(8) data.append(int.from_bytes(part, 'big')) section.data = data section.palette = palette block_mask = (1 << bits_per_block) - 1 # print(i) for y in range(16): for z in range(16): for x in range(16): block_mask = (1 << bits_per_block) - 1 number = (((y << 4) + z) << 4) + x long_number = (number * bits_per_block) >> 6 bit_in_long_number = (number * bits_per_block) & 63 block = (data[long_number] >> bit_in_long_number) & (block_mask) if bit_in_long_number + bits_per_block > 64: block |= (data[long_number + 1] & ( (1 << (bit_in_long_number + bits_per_block - 64)) - 1)) << (64 - bit_in_long_number) if palette: # if block > 0: # print(palette) # print(len(palette)) # print(block) # print(bits_per_block) # print((x, y, z, self.x, self.z)) block = palette[block] if type(block) is float: print(block) section.blocks[x][y][z] = block section.light = file_object.read(2048) if dimension == 0: section.sky_light = file_object.read(2048) self.sections[i] = section self.update_blocks()
def read(self, file_object): self.window_id = UnsignedByte.read(file_object) self.count = Short.read(file_object) self.slots = [Slot(-1)]*self.count for slot_number in range(self.count): self.slots[slot_number] = Slot.read(file_object)