Skip to content

Instantly share code, notes, and snippets.

@onnowhere
Last active July 13, 2020 10:02
Show Gist options
  • Save onnowhere/48655f2cac8a2c6cb5aac308ea89360c to your computer and use it in GitHub Desktop.
Save onnowhere/48655f2cac8a2c6cb5aac308ea89360c to your computer and use it in GitHub Desktop.
Deletes all chunks that don't contain a specific block within a coordinate range. Files are read from `./region` and output to `./output`. Default scans for any glass block at y=255 in every chunk.
# Prune Chunks
# Deletes all chunks that don't contain a specific block within a coordinate range
# By Onnowhere
import os
import sys
import subprocess
import pkg_resources
import pathlib
required = {'anvil-parser'}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed
if missing:
print("Installing:", *missing)
subprocess.check_call([sys.executable, "-m", "pip", "install", *missing])
import anvil
def create_path(filepath):
if not os.path.exists(filepath):
try:
os.makedirs(filepath)
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
def pruneChunks(in_file, out_file, checkedblock, fixedcoord=([0,15], [255,255], [0,15]), debug=False):
# Fixed coord can be values within: (0-15, 0-255, 0-15)
# Defines the region to scan for a block within a chunk
# ([0,15], [255,255], [0,15]) scans all blocks at y=255 in each chunk
create_path(os.path.dirname(in_file))
create_path(os.path.dirname(out_file))
region = anvil.Region.from_file(in_file)
print("Input:", in_file)
file_parts = os.path.basename(in_file).split(".")
regionx = int(file_parts[1])
regionz = int(file_parts[2])
if debug:
print("Reading region at ({0}, {1})".format(regionx, regionz))
region_out = anvil.EmptyRegion(regionx, regionz)
chunk_count = 0
pruned_count = 0
for rx in range(0, 32):
for rz in range(0, 32):
if debug:
print("Reading chunk at ({0}, {1})".format(rx, rz))
try:
chunk = anvil.Chunk.from_region(region, rx, rz)
except:
if debug:
print("Skipping chunk as empty...")
continue
chunk_count += 1
found = False
for posx in range(fixedcoord[0][1], fixedcoord[0][1] + 1):
for posy in range(fixedcoord[1][1], fixedcoord[1][1] + 1):
for posz in range(fixedcoord[2][1], fixedcoord[2][1] + 1):
#print("Checking coordinate ({0}, {1}, {2})".format(posx, posy, posz))
block = chunk.get_block(posx, posy, posz)
if block.id == checkedblock:
found = True
if found:
break
if found:
break
if not found:
if debug:
print("Chunk unused, deleting...")
pruned_count += 1
else:
if debug:
print("Chunk used, saving...")
region_out.add_chunk(chunk)
print("Saving region...")
print("Total original chunks:", chunk_count)
print("Pruned chunks:", pruned_count)
print("Saved chunks:", chunk_count - pruned_count)
print("Output:", out_file)
print("-----------------------------------------------")
region_out.save(out_file)
if __name__ == "__main__":
in_path = "./region"
out_path = "./output"
for file in os.listdir(in_path):
in_file = os.path.join(in_path, file)
out_file = os.path.join(out_path, file)
pruneChunks(in_file, out_file, "glass", fixedcoord=([0,15], [255,255], [0,15]), debug=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment