Files
generate_jungle_chest_room.py
Code
1 158
generate_jungle_chest_room.py
#!/usr/bin/env python3
"""Generate a Minecraft 1.8.9/WorldEdit legacy .schematic file."""
import gzip
import struct
import sys
WIDTH = 10
LENGTH = 10
HEIGHT = 5
def nbt_string(value):
encoded = value.encode("utf-8")
return struct.pack(">H", len(encoded)) + encoded
def tag_byte(name, value):
return b"\x01" + nbt_string(name) + struct.pack(">b", value)
def tag_short(name, value):
return b"\x02" + nbt_string(name) + struct.pack(">h", value)
def tag_int(name, value):
return b"\x03" + nbt_string(name) + struct.pack(">i", value)
def tag_string(name, value):
return b"\x08" + nbt_string(name) + nbt_string(value)
def tag_byte_array(name, values):
return (
b"\x07"
+ nbt_string(name)
+ struct.pack(">i", len(values))
+ bytes((value & 0xFF for value in values))
)
def compound_payload(tags):
return b"".join(tags) + b"\x00"
def tag_list(name, element_type, values):
return (
b"\x09"
+ nbt_string(name)
+ struct.pack(">b", element_type)
+ struct.pack(">i", len(values))
+ b"".join(values)
)
def chest_tile_entity(x, y, z):
return compound_payload(
[
tag_string("id", "Chest"),
tag_int("x", x),
tag_int("y", y),
tag_int("z", z),
tag_list("Items", 10, []),
]
)
def create_room():
blocks = [0] * (WIDTH * LENGTH * HEIGHT)
data = [0] * len(blocks)
chests = []
def index(x, y, z):
return x + WIDTH * (z + LENGTH * y)
def set_block(x, y, z, block_id, block_data=0):
if 0 <= x < WIDTH and 0 <= y < HEIGHT and 0 <= z < LENGTH:
position = index(x, y, z)
blocks[position] = block_id
data[position] = block_data
# Jungle-plank floor with mossy cobblestone accents.
for x in range(WIDTH):
for z in range(LENGTH):
set_block(x, 0, z, 5, 3)
for x, z in ((2, 2), (7, 2), (2, 7), (7, 7), (4, 4), (5, 5)):
set_block(x, 0, z, 48)
# Jungle-plank walls, with jungle-log corner supports.
for y in range(1, 4):
for x in range(WIDTH):
set_block(x, y, 0, 5, 3)
set_block(x, y, LENGTH - 1, 5, 3)
for z in range(1, LENGTH - 1):
set_block(0, y, z, 5, 3)
set_block(WIDTH - 1, y, z, 5, 3)
for x, z in ((0, 0), (WIDTH - 1, 0), (0, LENGTH - 1), (WIDTH - 1, LENGTH - 1)):
for y in range(1, 4):
set_block(x, y, z, 17, 3)
# Glass windows on both side walls.
for z in range(3, 7):
set_block(0, 2, z, 20)
set_block(WIDTH - 1, 2, z, 20)
# Leaf canopy roof with log beams and glowstone lighting.
for x in range(WIDTH):
for z in range(LENGTH):
set_block(x, 4, z, 18, 3)
for x in range(WIDTH):
set_block(x, 4, 0, 17, 3)
set_block(x, 4, LENGTH - 1, 17, 3)
for z in range(1, LENGTH - 1):
set_block(0, 4, z, 17, 3)
set_block(WIDTH - 1, 4, z, 17, 3)
for x, z in ((3, 3), (6, 3), (3, 6), (6, 6)):
set_block(x, 4, z, 89)
# Twelve chests: six on each long wall, all facing into the room.
for x in range(1, 7):
set_block(x, 1, 1, 54, 3)
chests.append((x, 1, 1))
set_block(x, 1, LENGTH - 2, 54, 2)
chests.append((x, 1, LENGTH - 2))
return blocks, data, chests
def write_schematic(path):
blocks, data, chests = create_room()
tile_entities = [chest_tile_entity(x, y, z) for x, y, z in chests]
root = (
b"\x0a"
+ nbt_string("Schematic")
+ compound_payload(
[
tag_short("Width", WIDTH),
tag_short("Height", HEIGHT),
tag_short("Length", LENGTH),
tag_string("Materials", "Alpha"),
tag_byte_array("Blocks", blocks),
tag_byte_array("Data", data),
tag_list("Entities", 10, []),
tag_list("TileEntities", 10, tile_entities),
]
)
)
with gzip.open(path, "wb") as schematic:
schematic.write(root)
if __name__ == "__main__":
output = sys.argv[1] if len(sys.argv) > 1 else "jungle-chest-room.schematic"
write_schematic(output)
print("Created {}".format(output))