def scan_chunk(region_file, coords, global_coords, entity_limit): """ Takes a RegionFile obj and the local coordinatesof the chunk as inputs, then scans the chunk and returns all the data.""" el = entity_limit try: chunk = region_file.get_chunk(*coords) data_coords = world.get_chunk_data_coords(chunk) num_entities = len(chunk["Level"]["Entities"]) if data_coords != global_coords: status = world.CHUNK_WRONG_LOCATED status_text = "Mismatched coordinates (wrong located chunk)." scan_time = time() elif num_entities > el: status = world.CHUNK_TOO_MANY_ENTITIES status_text = "The chunks has too many entities (it has {0}, and it's more than the limit {1})".format(num_entities, entity_limit) scan_time = time() else: status = world.CHUNK_OK status_text = "OK" scan_time = time() except InconceivedChunk as e: chunk = None data_coords = None num_entities = None status = world.CHUNK_NOT_CREATED status_text = "The chunk doesn't exist" scan_time = time() except RegionHeaderError as e: error = "Region header error: " + e.msg status = world.CHUNK_CORRUPTED status_text = error scan_time = time() chunk = None data_coords = None global_coords = world.get_global_chunk_coords(split(region_file.filename)[1], coords[0], coords[1]) num_entities = None except ChunkDataError as e: error = "Chunk data error: " + e.msg status = world.CHUNK_CORRUPTED status_text = error scan_time = time() chunk = None data_coords = None global_coords = world.get_global_chunk_coords(split(region_file.filename)[1], coords[0], coords[1]) num_entities = None except ChunkHeaderError as e: error = "Chunk herader error: " + e.msg status = world.CHUNK_CORRUPTED status_text = error scan_time = time() chunk = None data_coords = None global_coords = world.get_global_chunk_coords(split(region_file.filename)[1], coords[0], coords[1]) num_entities = None return chunk, (num_entities, status) if status != world.CHUNK_NOT_CREATED else None
def scan_chunk(region_file, x, z): """ Returns a tuple with (chunk, status_integer, error_text). Status integers are: 0 if exists and it's OK, -1 if it's corrupted, -2 if it the header coords doesn't match the coords stored in the chunk data (wrong located chunk) and 1 if it doesn't exist. The variable chunk can be None if there's no chunk to return.""" try: chunk = region_file.get_chunk(x,z) if chunk: data_coords = world.get_chunk_data_coords(chunk) header_coords = world.get_global_chunk_coords(region_file.filename, x, z) if data_coords != header_coords: return (chunk, -2, "Mismatched coordinates.") except region.RegionHeaderError as e: error = "Region header error: " + e.msg return (None, -1, error) except region.ChunkDataError as e: error = "Chunk data error: " + e.msg return (None, -1, error) except region.ChunkHeaderError as e: error = "Chunk herader error: " + e.msg return (None, -1, error) if chunk != None: return (chunk, 0, "OK") return (None, 1, "The chunk doesn't exist")
def scan_chunk(region_file, coords, global_coords, options, block_aggregation, containers, biomes): """ Takes a RegionFile obj and the local coordinatesof the chunk as inputs, then scans the chunk and returns all the data.""" try: chunk_block_aggregation = [0] * 4096 chunk = region_file.get_chunk(*coords) data_coords = world.get_chunk_data_coords(chunk) num_entities = len(chunk["Level"]["Entities"]) if data_coords != global_coords: status = world.CHUNK_WRONG_LOCATED status_text = "Mismatched coordinates (wrong located chunk)." scan_time = time.time() elif num_entities > options.entity_limit: status = world.CHUNK_TOO_MANY_ENTITIES status_text = "The chunks has too many entities (it has {0}, and it's more than the limit {1})".format(num_entities, options.entity_limit) scan_time = time.time() else: status = world.CHUNK_OK status_text = "OK" scan_time = time.time() # Grab our chunk info real_chunk = nbt_chunk.Chunk(chunk) # Let's just parse NBT chunk sections to get accurate block counts chunk_sections = real_chunk.chunk_data['Sections'] parsed_sections = [0] * 16 for section in chunk_sections: blocksBytes = section['Blocks'].value if isinstance(blocksBytes, (bytearray, array.array)): blocks = blocksBytes else: continue # Assume AIR for all blocks in section parsed_sections[section['Y'].value] = 1 for i, block_id in enumerate(blocks): chunk_block_aggregation[block_id] += 1 block_aggregation[block_id] += 1 for was_parsed in parsed_sections: if was_parsed: continue # Chunk section was AIR, y16*z16*x16 = 4096 blocks chunk_block_aggregation[0] += 4096 block_aggregation[0] += 4096 real_chunk_data = real_chunk.chunk_data if findTag(real_chunk_data, 'Biomes'): biomeBytes = real_chunk_data['Biomes'].value if isinstance(biomeBytes, (bytearray, array.array)): for i, biome in enumerate(biomeBytes): if biome > -1: biomes[biome] += 1; if findTag(real_chunk_data, 'Entities'): for entity in findTag(real_chunk_data, 'Entities').tags: container_contents = logContainer(entity) if container_contents: containers.append(container_contents) if findTag(real_chunk_data, 'TileEntities'): for entity in findTag(real_chunk_data, 'TileEntities').tags: container_contents = logContainer(entity) if container_contents: containers.append(container_contents) saveBlockStats('chunk', global_coords, chunk_block_aggregation) except region.InconceivedChunk as e: chunk = None data_coords = None num_entities = None status = world.CHUNK_NOT_CREATED status_text = "The chunk doesn't exist" scan_time = time.time() except region.RegionHeaderError as e: error = "Region header error: " + e.msg status = world.CHUNK_CORRUPTED status_text = error scan_time = time.time() chunk = None data_coords = None global_coords = world.get_global_chunk_coords(split(region_file.filename)[1], coords[0], coords[1]) num_entities = None except region.ChunkDataError as e: error = "Chunk data error: " + e.msg status = world.CHUNK_CORRUPTED status_text = error scan_time = time.time() chunk = None data_coords = None global_coords = world.get_global_chunk_coords(split(region_file.filename)[1], coords[0], coords[1]) num_entities = None except region.ChunkHeaderError as e: error = "Chunk header error: " + e.msg status = world.CHUNK_CORRUPTED status_text = error scan_time = time.time() chunk = None data_coords = None global_coords = world.get_global_chunk_coords(split(region_file.filename)[1], coords[0], coords[1]) num_entities = None return chunk, (num_entities, status) if status != world.CHUNK_NOT_CREATED else None
def scan_chunk(region_file, coords, global_coords, options, block_aggregation, containers): """ Takes a RegionFile obj and the local coordinatesof the chunk as inputs, then scans the chunk and returns all the data.""" try: chunk_block_aggregation = [0] * 4096 chunk = region_file.get_chunk(*coords) data_coords = world.get_chunk_data_coords(chunk) num_entities = len(chunk["Level"]["Entities"]) if data_coords != global_coords: status = world.CHUNK_WRONG_LOCATED status_text = "Mismatched coordinates (wrong located chunk)." scan_time = time.time() elif num_entities > options.entity_limit: status = world.CHUNK_TOO_MANY_ENTITIES status_text = "The chunks has too many entities (it has {0}, and it's more than the limit {1})".format( num_entities, options.entity_limit) scan_time = time.time() else: status = world.CHUNK_OK status_text = "OK" scan_time = time.time() # Grab our chunk info real_chunk = nbt_chunk.Chunk(chunk) # Let's just parse NBT chunk sections to get accurate block counts chunk_sections = real_chunk.chunk_data['Sections'] parsed_sections = [0] * 16 for section in chunk_sections: blocksBytes = section['Blocks'].value if isinstance(blocksBytes, (bytearray, array.array)): blocks = blocksBytes else: continue # Assume AIR for all blocks in section parsed_sections[section['Y'].value] = 1 for i, block_id in enumerate(blocks): chunk_block_aggregation[block_id] += 1 block_aggregation[block_id] += 1 for was_parsed in parsed_sections: if was_parsed: continue # Chunk section was AIR, y16*z16*x16 = 4096 blocks chunk_block_aggregation[0] += 4096 block_aggregation[0] += 4096 real_chunk_data = real_chunk.chunk_data if findTag(real_chunk_data, 'Entities'): for entity in findTag(real_chunk_data, 'Entities').tags: container_contents = logContainer(entity) if container_contents: containers.append(container_contents) if findTag(real_chunk_data, 'TileEntities'): for entity in findTag(real_chunk_data, 'TileEntities').tags: container_contents = logContainer(entity) if container_contents: containers.append(container_contents) saveBlockStats('chunk', global_coords, chunk_block_aggregation) except region.InconceivedChunk as e: chunk = None data_coords = None num_entities = None status = world.CHUNK_NOT_CREATED status_text = "The chunk doesn't exist" scan_time = time.time() except region.RegionHeaderError as e: error = "Region header error: " + e.msg status = world.CHUNK_CORRUPTED status_text = error scan_time = time.time() chunk = None data_coords = None global_coords = world.get_global_chunk_coords( split(region_file.filename)[1], coords[0], coords[1]) num_entities = None except region.ChunkDataError as e: error = "Chunk data error: " + e.msg status = world.CHUNK_CORRUPTED status_text = error scan_time = time.time() chunk = None data_coords = None global_coords = world.get_global_chunk_coords( split(region_file.filename)[1], coords[0], coords[1]) num_entities = None except region.ChunkHeaderError as e: error = "Chunk herader error: " + e.msg status = world.CHUNK_CORRUPTED status_text = error scan_time = time.time() chunk = None data_coords = None global_coords = world.get_global_chunk_coords( split(region_file.filename)[1], coords[0], coords[1]) num_entities = None return chunk, (num_entities, status) if status != world.CHUNK_NOT_CREATED else None