Files
build_pack.py
pack.mcmeta
README.md
Code
3 371
build_pack.py
from __future__ import annotations
import struct
import zlib
from pathlib import Path
ROOT = Path(__file__).parent
TEXTURES = ROOT / "assets" / "minecraft" / "textures"
Color = tuple[int, int, int, int]
def png(path: Path, width: int, height: int, pixels: list[list[Color]]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
raw = bytearray()
for row in pixels:
raw.append(0)
for red, green, blue, alpha in row:
raw.extend((red, green, blue, alpha))
def chunk(kind: bytes, data: bytes) -> bytes:
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF)
header = struct.pack(">IIBBBBB", width, height, 8, 6, 0, 0, 0)
image = b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header)
image += chunk(b"IDAT", zlib.compress(bytes(raw), 9))
image += chunk(b"IEND", b"")
path.write_bytes(image)
def canvas(width: int, height: int, color: Color = (0, 0, 0, 0)) -> list[list[Color]]:
return [[color for _ in range(width)] for _ in range(height)]
def pixel(image: list[list[Color]], x: int, y: int, color: Color) -> None:
if 0 <= y < len(image) and 0 <= x < len(image[0]):
image[y][x] = color
def rect(image: list[list[Color]], x0: int, y0: int, x1: int, y1: int, color: Color) -> None:
for y in range(max(0, y0), min(len(image), y1 + 1)):
for x in range(max(0, x0), min(len(image[0]), x1 + 1)):
image[y][x] = color
def line(image: list[list[Color]], x0: int, y0: int, x1: int, y1: int, color: Color) -> None:
dx = abs(x1 - x0)
dy = -abs(y1 - y0)
sx = 1 if x0 < x1 else -1
sy = 1 if y0 < y1 else -1
error = dx + dy
while True:
pixel(image, x0, y0, color)
if x0 == x1 and y0 == y1:
return
double_error = error * 2
if double_error >= dy:
error += dy
x0 += sx
if double_error <= dx:
error += dx
y0 += sy
def polygon(image: list[list[Color]], points: list[tuple[int, int]], color: Color) -> None:
min_x = max(0, min(x for x, _ in points))
max_x = min(len(image[0]) - 1, max(x for x, _ in points))
min_y = max(0, min(y for _, y in points))
max_y = min(len(image) - 1, max(y for _, y in points))
for y in range(min_y, max_y + 1):
intersections: list[int] = []
for index, (x0, y0) in enumerate(points):
x1, y1 = points[(index + 1) % len(points)]
if (y0 <= y < y1) or (y1 <= y < y0):
intersections.append(x0 + (y - y0) * (x1 - x0) // (y1 - y0))
intersections.sort()
for index in range(0, len(intersections) - 1, 2):
for x in range(max(min_x, intersections[index]), min(max_x, intersections[index + 1]) + 1):
pixel(image, x, y, color)
def save_item(name: str, image: list[list[Color]]) -> None:
png(TEXTURES / "item" / f"{name}.png", len(image[0]), len(image), image)
def sword(name: str, blade: Color) -> None:
image = canvas(32, 32)
outline = (43, 15, 55, 255)
glow = (255, 111, 224, 255)
core = (255, 190, 247, 255)
polygon(image, [(18, 2), (23, 7), (13, 24), (10, 21)], outline)
polygon(image, [(18, 4), (21, 7), (13, 21), (12, 19)], blade)
line(image, 19, 4, 13, 20, core)
pixel(image, 16, 11, glow)
pixel(image, 14, 15, glow)
pixel(image, 12, 19, glow)
line(image, 8, 20, 17, 29, outline)
line(image, 10, 20, 18, 28, (113, 43, 105, 255))
line(image, 7, 23, 13, 17, (255, 105, 220, 255))
line(image, 5, 25, 11, 19, (255, 191, 244, 255))
rect(image, 15, 27, 20, 29, outline)
save_item(name, image)
def tool(name: str, kind: str, metal: Color) -> None:
image = canvas(32, 32)
outline = (43, 15, 55, 255)
glow = (255, 111, 224, 255)
line(image, 8, 27, 23, 10, outline)
line(image, 10, 28, 24, 12, (104, 42, 105, 255))
line(image, 12, 27, 25, 14, glow)
if kind == "axe":
polygon(image, [(13, 5), (25, 5), (28, 11), (25, 16), (15, 14)], outline)
polygon(image, [(15, 7), (24, 7), (26, 11), (23, 13), (16, 12)], metal)
pixel(image, 21, 8, (255, 190, 247, 255))
elif kind == "pickaxe":
line(image, 6, 10, 25, 6, outline)
line(image, 7, 9, 24, 7, metal)
line(image, 8, 11, 12, 5, glow)
line(image, 21, 7, 25, 12, glow)
elif kind == "shovel":
polygon(image, [(18, 4), (25, 11), (21, 17), (14, 13)], outline)
polygon(image, [(19, 6), (23, 11), (20, 14), (16, 12)], metal)
else:
line(image, 17, 5, 25, 13, outline)
line(image, 18, 6, 24, 12, metal)
line(image, 15, 8, 20, 13, glow)
save_item(name, image)
def mace() -> None:
image = canvas(32, 32)
outline = (42, 13, 56, 255)
glow = (255, 108, 221, 255)
line(image, 9, 28, 22, 13, outline)
line(image, 11, 28, 23, 14, (111, 42, 111, 255))
line(image, 13, 26, 23, 15, glow)
rect(image, 17, 4, 25, 12, outline)
rect(image, 18, 5, 24, 10, (206, 68, 182, 255))
rect(image, 20, 3, 22, 13, glow)
pixel(image, 19, 6, (255, 207, 249, 255))
pixel(image, 23, 9, (255, 174, 240, 255))
line(image, 15, 4, 18, 1, glow)
line(image, 24, 5, 28, 2, glow)
save_item("mace", image)
def trident() -> None:
image = canvas(32, 32)
outline = (41, 16, 61, 255)
pink = (255, 116, 226, 255)
pale = (255, 199, 249, 255)
line(image, 8, 29, 23, 8, outline)
line(image, 10, 29, 24, 9, pink)
line(image, 18, 13, 25, 2, outline)
line(image, 20, 13, 26, 3, pale)
line(image, 15, 13, 15, 2, outline)
line(image, 16, 13, 16, 3, pink)
line(image, 12, 13, 6, 3, outline)
line(image, 13, 13, 8, 4, pink)
line(image, 8, 4, 6, 1, pale)
line(image, 16, 3, 16, 1, pale)
line(image, 26, 3, 28, 1, pale)
save_item("trident", image)
def bow(name: str, pull: int = 0) -> None:
image = canvas(32, 32)
outline = (44, 14, 53, 255)
wood = (139, 44, 110, 255)
pink = (255, 102, 216, 255)
line(image, 9, 4, 5, 16, outline)
line(image, 5, 16, 9, 28, outline)
line(image, 10, 5, 7, 16, wood)
line(image, 7, 16, 10, 27, pink)
if pull == 0:
line(image, 9, 4, 9, 28, (255, 194, 247, 255))
else:
line(image, 9, 4, 8 + pull * 2, 16, (255, 194, 247, 255))
line(image, 8 + pull * 2, 16, 9, 28, (255, 194, 247, 255))
line(image, 8 + pull * 2, 16, 25, 16, pink)
line(image, 24, 14, 27, 16, (255, 213, 250, 255))
line(image, 24, 18, 27, 16, (255, 213, 250, 255))
save_item(name, image)
def crossbow(name: str, loaded: bool = False) -> None:
image = canvas(32, 32)
outline = (42, 14, 55, 255)
wood = (137, 43, 109, 255)
pink = (255, 111, 222, 255)
line(image, 8, 9, 24, 25, outline)
line(image, 10, 10, 23, 23, wood)
line(image, 7, 7, 25, 7, outline)
line(image, 8, 8, 24, 8, pink)
line(image, 10, 6, 7, 10, pink)
line(image, 22, 6, 25, 10, pink)
if loaded: