Пример #1
0
def process_region_file(filename, start, stop):
    """Given a region filename, return the number of blocks of each ID in that file"""
    pieces = filename.split('.')
    rx = int(pieces[1])
    rz = int(pieces[2])

    block_data_totals = [[0] * 16 for i in range(256)
                         ]  # up to 16 data numbers in 256 block IDs

    # Does the region overlap the bounding box at all?
    if (start != None):
        if ((rx + 1) * 512 - 1 < int(start[0])
                or (rz + 1) * 512 - 1 < int(start[2])):
            return block_data_totals
    elif (stop != None):
        if (rx * 512 - 1 > int(stop[0]) or rz * 512 - 1 > int(stop[2])):
            return block_data_totals

    file = RegionFile(filename)

    # Get all chunks
    chunks = file.get_chunks()
    print("Parsing %s... %d chunks" %
          (os.path.basename(filename), len(chunks)))
    for c in chunks:
        # Does the chunk overlap the bounding box at all?
        if (start != None):
            if ((c['x'] + 1) * 16 + rx * 512 - 1 < int(start[0])
                    or (c['z'] + 1) * 16 + rz * 512 - 1 < int(start[2])):
                continue
        elif (stop != None):
            if (c['x'] * 16 + rx * 512 - 1 > int(stop[0])
                    or c['z'] * 16 + rz * 512 - 1 > int(stop[2])):
                continue

        chunk = Chunk(file.get_chunk(c['x'], c['z']))
        assert chunk.get_coords() == (c['x'] + rx * 32, c['z'] + rz * 32)
        #print("Parsing chunk ("+str(c['x'])+", "+str(c['z'])+")")
        # Parse the blocks

        # Fast code if no start or stop coordinates are specified
        # TODO: also use this code if start/stop is specified, but the complete chunk is included
        if (start == None and stop == None):
            stats_per_chunk(chunk, block_data_totals)
        else:
            # Slow code that iterates through each coordinate
            bounded_stats_per_chunk(chunk, block_data_totals, start, stop)

    return block_data_totals
Пример #2
0
def countBlocks(worldFolder, id):
    count = 0
    for region in worldFolder.iter_regions():
        print("Searching through {}".format(region.filename))
        for chunk in region.get_metadata():
            #print("Searching through {},{}".format(chunk.x, chunk.z))
            x = Chunk(region.get_chunk(chunk.x, chunk.z))
            for i, section in zip(range(16), x.sections):
                if not section:
                    continue
                for yzx, (block, add, data) in zip(range(16 * 16 * 16),
                                                   section.get_all()):
                    if (add << 8 | block) == id:
                        count += 1
                        print(count, i * 16 + yzx // 256)
    return count