diff --git a/nbt/chunk.py b/nbt/chunk.py index 5444144..f980649 100644 --- a/nbt/chunk.py +++ b/nbt/chunk.py @@ -193,8 +193,8 @@ def _init_index_unpadded(self, nbt): num_bits = (len(self.names) - 1).bit_length() if num_bits < 4: num_bits = 4 - assert num_bits == len(states) * 64 / 4096 - mask = pow(2, num_bits) - 1 + assert num_bits == len(states) / 64 + mask = (1 << num_bits) - 1 i = 0 bits_left = 64 @@ -215,8 +215,8 @@ def _init_index_unpadded(self, nbt): next_long = states[i] remaining_bits = num_bits - bits_left - next_long = (next_long & (pow(2, remaining_bits) - 1)) << bits_left - curr_long = (curr_long & (pow(2, bits_left) - 1)) + next_long = (next_long & ((1 << remaining_bits) - 1)) << bits_left + curr_long = (curr_long & ((1 << bits_left) - 1)) self.indexes.append(next_long | curr_long) curr_long = states[i] @@ -236,7 +236,7 @@ def _init_index_padded(self, nbt): states = nbt['BlockStates'].value num_bits = (len(self.names) - 1).bit_length() if num_bits < 4: num_bits = 4 - mask = 2**num_bits - 1 + mask = (1 << num_bits) - 1 indexes_per_element = 64 // num_bits last_state_elements = 4096 % indexes_per_element @@ -261,7 +261,7 @@ def _init_index_padded(self, nbt): def get_block(self, x, y, z): # Blocks are stored in YZX order - i = y * 256 + z * 16 + x + i = (y << 8) + (z << 4) + x p = self.indexes[i] return self.names[p] @@ -312,7 +312,7 @@ def get_max_height(self): ymax = 0 for y in self.sections.keys(): if y > ymax: ymax = y - return ymax * 16 + 15 + return (ymax << 4) + 15 def get_block(self, x, y, z): @@ -389,7 +389,7 @@ def generate_heightmap(self, buffer=False, as_array=False): for z in range(16): for x in range(16): for y in range(127, -1, -1): - offset = y + z*128 + x*128*16 + offset = y + (z << 7) + (x << 11) if (self.blocksList[offset] not in non_solids or y == 0): bytes.append(y+1) break @@ -413,7 +413,7 @@ def set_blocks(self, list=None, dict=None, fill_air=False): for z in range(16): for y in range(128): coord = x,y,z - offset = y + z*128 + x*128*16 + offset = y + (z << 7) + (x << 11) if (coord in dict): list.append(dict[coord]) else: @@ -429,7 +429,7 @@ def set_blocks(self, list=None, dict=None, fill_air=False): def set_block(self, x,y,z, id, data=0): """Sets the block a x, y, z to the specified id, and optionally data.""" - offset = y + z*128 + x*128*16 + offset = y + (z << 7) + (x << 11) self.blocksList[offset] = id if (offset % 2 == 1): # offset is odd @@ -458,5 +458,5 @@ def get_block(self, x,y,z, coord=False): blocks.append(Block(x,y,z)) """ - offset = y + z*128 + x*128*16 if (coord == False) else coord[1] + coord[2]*128 + coord[0]*128*16 + offset = y + (z << 7) + (x << 11) if (coord == False) else coord[1] + (coord[2] << 7) + (coord[0] << 11) return self.blocksList[offset] diff --git a/nbt/region.py b/nbt/region.py index 765af8c..d7c747d 100644 --- a/nbt/region.py +++ b/nbt/region.py @@ -600,9 +600,9 @@ def get_nbt(self, x, z): try: nbt = NBTFile(buffer=data) if self.loc.x != None: - x += self.loc.x*32 + x += (self.loc.x << 5) if self.loc.z != None: - z += self.loc.z*32 + z += (self.loc.z << 5) nbt.loc = Location(x=x, z=z) return nbt # this may raise a MalformedFileError. Convert to ChunkDataError. @@ -674,11 +674,11 @@ def write_blockdata(self, x, z, data, compression=COMPRESSION_ZLIB): self.file.write(remaining_length * b"\x00") #seek to header record and write offset and length records - self.file.seek(4 * (x + 32*z)) + self.file.seek((x + (z << 5)) << 2) self.file.write(pack(">IB", sector, nsectors)[1:]) #write timestamp - self.file.seek(SECTOR_LENGTH + 4 * (x + 32*z)) + self.file.seek(SECTOR_LENGTH + ((x + (z << 5)) << 2)) timestamp = int(time.time()) self.file.write(pack(">I", timestamp)) @@ -733,9 +733,9 @@ def unlink_chunk(self, x, z): return # zero the region header for the chunk (offset length and time) - self.file.seek(4 * (x + 32*z)) + self.file.seek((x + (z << 5)) << 2) self.file.write(pack(">IB", 0, 0)[1:]) - self.file.seek(SECTOR_LENGTH + 4 * (x + 32*z)) + self.file.seek(SECTOR_LENGTH + ((x + (z << 5)) << 2)) self.file.write(pack(">I", 0)) # Check if file should be truncated: diff --git a/nbt/world.py b/nbt/world.py index 34877ed..510c72a 100644 --- a/nbt/world.py +++ b/nbt/world.py @@ -221,7 +221,7 @@ def get_boundingbox(self): b = BoundingBox() for rx,rz in self.regionfiles.keys(): region = self.get_region(rx,rz) - rx,rz = 32*rx,32*rz + rx,rz = rx << 5,rz << 5 for cc in region.get_chunk_coords(): x,z = (rx+cc['x'],rz+cc['z']) b.expand(x,None,z)