Files
generate_resource_pack.py
Code
1 157
generate_resource_pack.py
#!/usr/bin/env python3
"""Generate a minimal Minecraft Java resource pack with a custom Blaze Rod."""
import argparse
import json
import os
import struct
import zlib
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile
SIZE = 16
COLORS = {
"outline": (49, 24, 20, 255),
"dark": (83, 35, 23, 255),
"ember": (147, 43, 16, 255),
"red": (207, 54, 13, 255),
"orange": (247, 83, 10, 255),
"fire": (255, 139, 12, 255),
"gold": (255, 190, 24, 255),
"yellow": (255, 226, 57, 255),
"glint": (255, 245, 133, 255),
}
def make_texture():
pixels = [[(0, 0, 0, 0) for _ in range(SIZE)] for _ in range(SIZE)]
start_x, start_y = 2.0, 14.2
axis_x, axis_y = 0.64, -0.768
across_x, across_y = -axis_y, axis_x
bands = ((3.2, 4.45), (8.5, 9.75), (13.0, 14.15))
for y in range(SIZE):
for x in range(SIZE):
dx, dy = x + 0.5 - start_x, y + 0.5 - start_y
along = dx * axis_x + dy * axis_y
across = dx * across_x + dy * across_y
side = abs(across)
head = 13.5 <= along <= 17.0
if head:
radius = 2.65 - 0.48 * abs(along - 15.1)
if side > radius:
continue
elif not (-0.35 <= along <= 14.0 and side <= 1.9):
continue
band = any(low <= along <= high for low, high in bands)
if band and side <= 2.05:
color = COLORS["outline"] if side > 1.45 else COLORS["dark"]
if side < 0.48:
color = COLORS["gold"]
elif side < 0.9:
color = COLORS["yellow"]
elif head:
if side > 1.62:
color = COLORS["dark"]
elif side > 0.95:
color = COLORS["ember"]
elif side < 0.34:
color = COLORS["yellow"] if int(along * 2) % 3 else COLORS["glint"]
elif side < 0.72:
color = COLORS["gold"]
else:
color = COLORS["fire"]
elif side > 1.38:
color = COLORS["outline"] if side > 1.66 else COLORS["dark"]
elif side > 0.88:
color = COLORS["ember"]
elif side < 0.25:
color = COLORS["yellow"] if int(along * 1.7) % 5 else COLORS["glint"]
elif side < 0.57:
color = COLORS["gold"] if int(along * 1.4) % 4 else COLORS["yellow"]
else:
color = COLORS["orange"] if int(along * 1.2) % 3 else COLORS["red"]
pixels[y][x] = color
return png_bytes(pixels)
def png_bytes(pixels):
scanlines = bytearray()
for row in pixels:
scanlines.append(0)
for red, green, blue, alpha in row:
scanlines.extend((red, green, blue, alpha))
def chunk(name, data):
return (
struct.pack(">I", len(data))
+ name
+ data
+ struct.pack(">I", zlib.crc32(name + data) & 0xFFFFFFFF)
)
return (
b"\x89PNG\r\n\x1a\n"
+ chunk(b"IHDR", struct.pack(">IIBBBBB", SIZE, SIZE, 8, 6, 0, 0, 0))
+ chunk(b"IDAT", zlib.compress(bytes(scanlines), 9))
+ chunk(b"IEND", b"")
)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--pack-format", type=int, default=46,
help="Minecraft resource-pack format (default: 46 for Java 1.21.4)")
parser.add_argument("--version", default="1.21.4",
help="Minecraft version label used in the pack name")
parser.add_argument("--output", type=Path, default=Path("."),
help="directory where the pack folder and ZIP are created")
args = parser.parse_args()
pack_name = "EmberBlazeRod"
pack_root = args.output / pack_name
texture_path = pack_root / "assets" / "minecraft" / "textures" / "item" / "blaze_rod.png"
texture_path.parent.mkdir(parents=True, exist_ok=True)
texture_path.write_bytes(make_texture())
metadata = {
"pack": {
"pack_format": args.pack_format,
"description": "A clean ember-forged Blaze Rod",
}
}
metadata_text = json.dumps(metadata, indent=2) + "\n"
(pack_root / "pack.mcmeta").write_text(metadata_text, encoding="utf-8")
zip_path = args.output / f"EmberBlazeRod-{args.version}.zip"
temporary_zip = zip_path.with_name(zip_path.name + ".tmp")
with ZipFile(temporary_zip, "w", compression=ZIP_DEFLATED) as archive:
for path in sorted(pack_root.rglob("*")):
if path.is_file():
archive.write(path, path.relative_to(pack_root).as_posix())
with ZipFile(temporary_zip, "r") as archive:
if archive.testzip() is not None:
temporary_zip.unlink()
raise RuntimeError("Generated ZIP failed its CRC integrity check")
names = set(archive.namelist())
required = {
"pack.mcmeta",
"assets/minecraft/textures/item/blaze_rod.png",
}
if not required.issubset(names):
temporary_zip.unlink()
raise RuntimeError("Generated ZIP is missing required resource-pack files")
json.loads(archive.read("pack.mcmeta"))
os.replace(temporary_zip, zip_path)
print(f"Created and verified {zip_path}")
if __name__ == "__main__":
main()