Repository navigation
Expand file tree
/
Copy pathgenerate.py
More file actions
executable file
·215 lines (184 loc) · 5.57 KB
/
Copy pathgenerate.py
File metadata and controls
executable file
·215 lines (184 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/python3
from os import getcwd
from pathlib import Path
import shutil
import zipfile
import argparse
import json
# parse arguments
parser = argparse.ArgumentParser(description="Generate templated datapack.")
parser.add_argument(
"--namespace",
"-n",
dest="namespace",
help="the custom namespace to use",
default="kaituo",
)
parser.add_argument(
"--templates",
"-t",
dest="template_folder",
help="template folder",
default="templates",
)
parser.add_argument(
"--out", "-o", dest="output_folder", help="output folder", default="out"
)
parser.add_argument(
"--pack-format",
"-f",
type=int,
dest="pack_format",
help="pack format number",
default=15,
)
parser.add_argument(
"--output-zip-name",
"-z",
dest="output_zip_name",
help="output zip name",
default="KaituoRecipeDatapack.zip",
)
parser.add_argument(
"--verbose", "-v", dest="verbose", action="store_true", help="show verbose output"
)
parser.add_argument(
"--no-cleanup",
"-c",
dest="cleanup",
action="store_false",
help="stop clean up output folder after generating",
)
parser.add_argument(
"--no-unlock-recipes",
"-u",
dest="unlock_recipes",
action="store_false",
default=True,
help="disable generating a function to unlock all custom recipes",
)
args = parser.parse_args()
# record the files
FILES: list[Path] = []
CWD = getcwd()
# make directories
out_path = Path(args.output_folder)
out_path.mkdir(exist_ok=True)
data_path = out_path / "data"
data_path.mkdir(exist_ok=True)
namespace_path = data_path / str(args.namespace)
namespace_path.mkdir(exist_ok=True)
categories = [
"function",
"structure",
"tags",
"advancement",
"item_modifier",
"loot_table",
"number_provider",
"predicate",
"recipe",
"slot_source",
]
for category in categories:
path = namespace_path / category
if path.exists():
shutil.rmtree(path)
# generate pack.mcmeta
pack_meta_path = out_path / "pack.mcmeta"
with open(pack_meta_path, "w") as f:
json.dump(
{
"pack": {
"description": "Kaituo datapack",
"min_format": args.pack_format,
"max_format": args.pack_format,
}
},
f,
indent=2,
)
FILES.append(pack_meta_path)
if args.verbose:
print(f"[{__file__}] generated pack.mcmeta")
# generate from templates
templates_path = Path(args.template_folder).absolute()
def get_suffix(category: str) -> str:
match category:
case "function":
return ".mcfunction"
case "structure":
return ".nbt"
case _:
return ".json"
def write_file(src: Path, content: str | None = None):
dest = namespace_path / src.relative_to(templates_path)
if not dest.parent.exists():
dest.parent.mkdir(parents=True, exist_ok=True)
if content:
dest.write_text(content, encoding="utf-8")
else:
dest.write_bytes(src.read_bytes())
FILES.append(dest)
if args.verbose:
print(f"[{__file__}] generated {dest.relative_to(data_path)}")
def do_templates(dir_path: Path, category: str):
enum_json = (dir_path / "enum.json").read_text(encoding="utf-8")
enum = json.loads(enum_json)
for subdir_path, _, filenames in dir_path.walk():
for filename in filenames:
file_path = subdir_path / filename
if file_path.suffix != get_suffix(category):
continue
if filename == "enum.json":
continue
for entry in enum:
content = file_path.read_text(encoding="utf-8")
content = content.replace("$$", entry)
new_file_path = Path(str(file_path).replace("$$", entry))
write_file(new_file_path, content)
for category in categories:
category_path = templates_path / category
if not category_path.exists():
continue
for dir_path, subdir_names, filenames in category_path.walk():
for subdir_name in subdir_names.copy():
subdir_path = dir_path / subdir_name
if (subdir_path / "enum.json").exists():
subdir_names.remove(subdir_name)
do_templates(subdir_path, category)
for filename in filenames:
file_path = dir_path / filename
if file_path.suffix == get_suffix(category):
write_file(file_path)
# generate unlock-all-recipes function
if args.unlock_recipes:
recipe_files = [
f
for f in FILES
if f.suffix == ".json" and (namespace_path / "recipe") in f.parents
]
lines = []
for rf in recipe_files:
recipe_name = rf.relative_to(namespace_path / "recipe").as_posix()
recipe_name = recipe_name.removesuffix(".json")
recipe_id = f"{args.namespace}:{recipe_name}"
lines.append(f"recipe give @s {recipe_id}")
unlock_function = "\n".join(lines) + "\n"
function_dir = namespace_path / "function" / "recipes"
function_dir.mkdir(parents=True, exist_ok=True)
function_path = function_dir / "unlock_all.mcfunction"
function_path.write_text(unlock_function, encoding="utf-8")
FILES.append(function_path)
if args.verbose:
print(f"[{__file__}] generated function/recipes/unlock_all.mcfunction")
# pack files into a zip
out_zip_path = out_path / args.output_zip_name
outzip = zipfile.ZipFile(out_zip_path, "w")
for file in FILES:
outzip.write(file, file.absolute().relative_to(out_path.absolute()))
outzip.close()
# clean up
if args.cleanup:
shutil.rmtree(data_path)
pack_meta_path.unlink()