-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinecraftPackRandomizer.py
More file actions
439 lines (364 loc) · 18.9 KB
/
Copy pathMinecraftPackRandomizer.py
File metadata and controls
439 lines (364 loc) · 18.9 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import os
import shutil
import random
import zipfile
import uuid
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter import ttk
import json
import threading
CATEGORIES = ["blocks", "items", "ui", "gui", "environment", "models"]
# ---- Core Functions ----
def unzip_mcpack(mcpack_path, extract_to):
with zipfile.ZipFile(mcpack_path, 'r') as zip_ref:
zip_ref.extractall(extract_to)
def zip_to_mcpack(folder_path, output_path):
with zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED) as zip_ref:
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, folder_path)
zip_ref.write(file_path, arcname)
def generate_manifest(name, description):
return {
"format_version": 2,
"header": {
"name": name,
"description": description,
"uuid": str(uuid.uuid4()),
"version": [1, 0, 0],
"min_engine_version": [1, 16, 0]
},
"modules": [
{
"type": "resources",
"uuid": str(uuid.uuid4()),
"version": [1, 0, 0]
}
]
}
# ---- Single-folder randomizer (default) ----
def randomize_pack_single_folder(packs_dir, output_dir, pack_name, description, icon_path, log_box, categories, progress_bar=None):
if not output_dir:
output_dir = os.path.dirname(packs_dir)
temp_dir = os.path.join(output_dir, "TempExtractedPacks")
os.makedirs(temp_dir, exist_ok=True)
unpacked_paths = []
pack_files = [f for f in os.listdir(packs_dir) if f.endswith(".mcpack")]
total_files = len(pack_files)
for idx, file in enumerate(pack_files, 1):
pack_path = os.path.join(packs_dir, file)
extract_folder = os.path.join(temp_dir, file.replace(".mcpack", ""))
unzip_mcpack(pack_path, extract_folder)
unpacked_paths.append(extract_folder)
log_box.insert(tk.END, f"Unzipped {file}\n")
log_box.see(tk.END)
if progress_bar:
progress_bar["value"] = idx / total_files * 50
progress_bar.update_idletasks()
if not unpacked_paths:
log_box.insert(tk.END, "No .mcpack files found.\n")
return
randomized_pack = os.path.join(output_dir, pack_name)
os.makedirs(randomized_pack, exist_ok=True)
textures_folder = os.path.join(randomized_pack, "textures")
os.makedirs(textures_folder, exist_ok=True)
# Models
models_packs = [p for p in unpacked_paths if os.path.exists(os.path.join(p, "textures", "models"))]
if "models" in categories and models_packs:
chosen_models = random.choice(models_packs)
shutil.copytree(os.path.join(chosen_models, "textures", "models"), os.path.join(textures_folder, "models"), dirs_exist_ok=True)
for item in ["diamond_helmet.png","diamond_chestplate.png","diamond_leggings.png","diamond_boots.png"]:
item_src = os.path.join(chosen_models, "textures", "items", item)
if os.path.exists(item_src):
os.makedirs(os.path.join(textures_folder, "items"), exist_ok=True)
shutil.copy2(item_src, os.path.join(textures_folder, "items", item))
log_box.insert(tk.END, f"models → {os.path.basename(chosen_models)}\n")
log_box.see(tk.END)
# Other categories
for category in categories:
if category == "models":
continue
if category == "ui":
available_packs = [p for p in unpacked_paths if os.path.exists(os.path.join(p, "textures", "ui")) and os.path.exists(os.path.join(p, "textures", "gui"))]
if not available_packs:
log_box.insert(tk.END, "ui/gui folder not found in any pack.\n")
continue
chosen_pack = random.choice(available_packs)
for sub in ["ui","gui"]:
src = os.path.join(chosen_pack, "textures", sub)
dest = os.path.join(textures_folder, sub)
shutil.copytree(src, dest, dirs_exist_ok=True)
log_box.insert(tk.END, f"ui+gui → {os.path.basename(chosen_pack)}\n")
log_box.see(tk.END)
continue
available_packs = [p for p in unpacked_paths if os.path.exists(os.path.join(p, "textures", category))]
if not available_packs:
log_box.insert(tk.END, f"{category} not found in any pack.\n")
log_box.see(tk.END)
continue
chosen_pack = random.choice(available_packs)
src = os.path.join(chosen_pack, "textures", category)
dest = os.path.join(textures_folder, category)
shutil.copytree(src, dest, dirs_exist_ok=True)
log_box.insert(tk.END, f"{category} → {os.path.basename(chosen_pack)}\n")
log_box.see(tk.END)
manifest = generate_manifest(pack_name, description)
with open(os.path.join(randomized_pack, "manifest.json"), "w") as f:
json.dump(manifest, f, indent=4)
if icon_path and os.path.exists(icon_path):
shutil.copy(icon_path, os.path.join(randomized_pack, "pack_icon.png"))
output_path = os.path.join(output_dir, f"{pack_name}.mcpack")
if os.path.exists(output_path):
os.remove(output_path)
zip_to_mcpack(randomized_pack, output_path)
shutil.rmtree(temp_dir, ignore_errors=True)
shutil.rmtree(randomized_pack, ignore_errors=True)
log_box.insert(tk.END, f"✅ Pack created: {output_path}\n")
log_box.see(tk.END)
if progress_bar:
progress_bar["value"] = 100
progress_bar.update_idletasks()
# ---- Per-category folder randomizer ----
def randomize_pack_per_category(category_folders, output_dir, pack_name, description, icon_path, log_box, categories):
if not output_dir:
output_dir = os.path.expanduser("~")
randomized_pack = os.path.join(output_dir, pack_name)
os.makedirs(randomized_pack, exist_ok=True)
textures_folder = os.path.join(randomized_pack, "textures")
os.makedirs(textures_folder, exist_ok=True)
for category, folder_path in category_folders.items():
if not folder_path or not os.path.exists(folder_path):
log_box.insert(tk.END, f"No folder selected for {category}, skipping.\n")
continue
pack_files = [f for f in os.listdir(folder_path) if f.endswith(".mcpack")]
if not pack_files:
log_box.insert(tk.END, f"No .mcpack found in {category} folder.\n")
continue
chosen_pack = os.path.join(folder_path, random.choice(pack_files))
extract_folder = os.path.join(folder_path, "_temp_"+category)
unzip_mcpack(chosen_pack, extract_folder)
src = os.path.join(extract_folder, "textures", category)
if os.path.exists(src):
shutil.copytree(src, os.path.join(textures_folder, category), dirs_exist_ok=True)
log_box.insert(tk.END, f"{category} → {os.path.basename(chosen_pack)}\n")
shutil.rmtree(extract_folder, ignore_errors=True)
manifest = generate_manifest(pack_name, description)
with open(os.path.join(randomized_pack, "manifest.json"), "w") as f:
json.dump(manifest, f, indent=4)
if icon_path and os.path.exists(icon_path):
shutil.copy(icon_path, os.path.join(randomized_pack, "pack_icon.png"))
output_path = os.path.join(output_dir, f"{pack_name}.mcpack")
if os.path.exists(output_path):
os.remove(output_path)
zip_to_mcpack(randomized_pack, output_path)
shutil.rmtree(randomized_pack, ignore_errors=True)
log_box.insert(tk.END, f"✅ Pack created: {output_path}\n")
log_box.see(tk.END)
# ---- GUI ----
def build_gui():
root = tk.Tk()
root.title("Minecraft Pack Randomizer")
root.geometry("800x900")
root.configure(bg="#121212")
# Variables
folder_path = tk.StringVar()
output_dir = tk.StringVar()
pack_name = tk.StringVar(value="Pack Central Generated Pack")
description = tk.StringVar(value="Generated with Tay's Pack Randomizer")
icon_path = tk.StringVar()
use_per_category = tk.BooleanVar(value=False)
category_vars = {cat: tk.BooleanVar(value=True) for cat in CATEGORIES}
category_folder_paths = {cat: tk.StringVar() for cat in CATEGORIES}
# Main frame
main_frame = tk.Frame(root, bg="#121212")
main_frame.pack(fill="both", expand=True, padx=10, pady=10)
# Title
title_label = tk.Label(main_frame, text="Pack Central Pack Generator", font=("Arial", 20, "bold"),
fg="#ffffff", bg="#121212")
title_label.pack(pady=(0, 5))
# Credit
credit_label = tk.Label(main_frame, text="Made by lowtapertay on discord!", font=("Arial", 10, "italic"),
fg="#888888", bg="#121212")
credit_label.pack(pady=(0, 15))
# Mode selection
mode_frame = tk.Frame(main_frame, bg="#121212")
mode_frame.pack(fill="x", pady=(0, 15))
tk.Label(mode_frame, text="Mode:", font=("Arial", 12, "bold"), fg="#ffffff", bg="#121212").pack(side="left")
mode_check = tk.Checkbutton(mode_frame, text="Use separate folders per category", variable=use_per_category,
fg="#ffffff", bg="#121212", selectcolor="#333333",
activebackground="#121212", activeforeground="#ffffff")
mode_check.pack(side="left", padx=(10, 0))
# Folder selection (single folder mode)
def select_folder():
folder = filedialog.askdirectory(title="Select folder containing .mcpack files")
if folder:
folder_path.set(folder)
folder_frame = tk.Frame(main_frame, bg="#121212")
folder_frame.pack(fill="x", pady=(0, 10))
tk.Label(folder_frame, text="Input Folder:", font=("Arial", 10), fg="#ffffff", bg="#121212").pack(anchor="w")
folder_subframe = tk.Frame(folder_frame, bg="#121212")
folder_subframe.pack(fill="x", pady=(5, 0))
folder_entry = tk.Entry(folder_subframe, textvariable=folder_path, bg="#333333", fg="#ffffff",
insertbackground="#ffffff")
folder_entry.pack(side="left", fill="x", expand=True)
tk.Button(folder_subframe, text="Browse", command=select_folder, bg="#1f1f1f", fg="#ffffff").pack(side="right", padx=(5, 0))
# Output folder selection
def select_output():
folder = filedialog.askdirectory(title="Select output folder")
if folder:
output_dir.set(folder)
output_frame = tk.Frame(main_frame, bg="#121212")
output_frame.pack(fill="x", pady=(0, 10))
tk.Label(output_frame, text="Output Folder:", font=("Arial", 10), fg="#ffffff", bg="#121212").pack(anchor="w")
output_subframe = tk.Frame(output_frame, bg="#121212")
output_subframe.pack(fill="x", pady=(5, 0))
output_entry = tk.Entry(output_subframe, textvariable=output_dir, bg="#333333", fg="#ffffff",
insertbackground="#ffffff")
output_entry.pack(side="left", fill="x", expand=True)
tk.Button(output_subframe, text="Browse", command=select_output, bg="#1f1f1f", fg="#ffffff").pack(side="right", padx=(5, 0))
# Pack details
details_frame = tk.Frame(main_frame, bg="#121212")
details_frame.pack(fill="x", pady=(0, 15))
# Pack name
name_frame = tk.Frame(details_frame, bg="#121212")
name_frame.pack(fill="x", pady=(0, 5))
tk.Label(name_frame, text="Pack Name:", font=("Arial", 10), fg="#ffffff", bg="#121212").pack(anchor="w")
tk.Entry(name_frame, textvariable=pack_name, bg="#333333", fg="#ffffff",
insertbackground="#ffffff").pack(fill="x", pady=(2, 0))
# Description
desc_frame = tk.Frame(details_frame, bg="#121212")
desc_frame.pack(fill="x", pady=(0, 5))
tk.Label(desc_frame, text="Description:", font=("Arial", 10), fg="#ffffff", bg="#121212").pack(anchor="w")
tk.Entry(desc_frame, textvariable=description, bg="#333333", fg="#ffffff",
insertbackground="#ffffff").pack(fill="x", pady=(2, 0))
# Icon selection
def select_icon():
file_path = filedialog.askopenfilename(title="Select pack icon",
filetypes=[("PNG files", "*.png"), ("All files", "*.*")])
if file_path:
icon_path.set(file_path)
icon_frame = tk.Frame(details_frame, bg="#121212")
icon_frame.pack(fill="x")
tk.Label(icon_frame, text="Pack Icon (optional):", font=("Arial", 10), fg="#ffffff", bg="#121212").pack(anchor="w")
icon_subframe = tk.Frame(icon_frame, bg="#121212")
icon_subframe.pack(fill="x", pady=(2, 0))
icon_entry = tk.Entry(icon_subframe, textvariable=icon_path, bg="#333333", fg="#ffffff",
insertbackground="#ffffff")
icon_entry.pack(side="left", fill="x", expand=True)
tk.Button(icon_subframe, text="Browse", command=select_icon, bg="#1f1f1f", fg="#ffffff").pack(side="right", padx=(5, 0))
# Categories selection
categories_frame = tk.Frame(main_frame, bg="#121212")
categories_frame.pack(fill="x", pady=(15, 0))
tk.Label(categories_frame, text="Categories to Include:", font=("Arial", 12, "bold"),
fg="#ffffff", bg="#121212").pack(anchor="w")
cat_grid_frame = tk.Frame(categories_frame, bg="#121212")
cat_grid_frame.pack(fill="x", pady=(10, 15))
# Create checkboxes for categories
for i, category in enumerate(CATEGORIES):
row = i // 3
col = i % 3
cat_frame = tk.Frame(cat_grid_frame, bg="#121212")
cat_frame.grid(row=row, column=col, sticky="w", padx=(0, 20), pady=2)
checkbox = tk.Checkbutton(cat_frame, text=category.title(), variable=category_vars[category],
fg="#ffffff", bg="#121212", selectcolor="#333333",
activebackground="#121212", activeforeground="#ffffff")
checkbox.pack(anchor="w")
# Per-category folder selection (initially hidden)
per_cat_frame = tk.Frame(main_frame, bg="#121212")
per_cat_frame.pack(fill="x", pady=(0, 15))
tk.Label(per_cat_frame, text="Category Folders (Per-Category Mode):",
font=("Arial", 11, "bold"), fg="#ffffff", bg="#121212").pack(anchor="w")
def create_category_folder_selector(cat):
def select_cat_folder():
folder = filedialog.askdirectory(title=f"Select folder for {cat} packs")
if folder:
category_folder_paths[cat].set(folder)
return select_cat_folder
cat_folder_widgets = {}
for category in CATEGORIES:
cat_folder_frame = tk.Frame(per_cat_frame, bg="#121212")
cat_folder_frame.pack(fill="x", pady=(5, 0))
tk.Label(cat_folder_frame, text=f"{category.title()}:", font=("Arial", 9),
fg="#ffffff", bg="#121212", width=12, anchor="w").pack(side="left")
entry = tk.Entry(cat_folder_frame, textvariable=category_folder_paths[category],
bg="#333333", fg="#ffffff", insertbackground="#ffffff")
entry.pack(side="left", fill="x", expand=True, padx=(0, 5))
button = tk.Button(cat_folder_frame, text="Browse",
command=create_category_folder_selector(category),
bg="#1f1f1f", fg="#ffffff")
button.pack(side="right")
cat_folder_widgets[category] = (cat_folder_frame, entry, button)
# Function to toggle per-category widgets visibility
def toggle_per_category():
if use_per_category.get():
per_cat_frame.pack(fill="x", pady=(0, 15))
folder_frame.pack_forget()
else:
per_cat_frame.pack_forget()
folder_frame.pack(fill="x", pady=(0, 10), before=output_frame)
use_per_category.trace("w", lambda *args: toggle_per_category())
toggle_per_category() # Initial setup
# Progress bar
progress_frame = tk.Frame(main_frame, bg="#121212")
progress_frame.pack(fill="x", pady=(0, 10))
tk.Label(progress_frame, text="Progress:", font=("Arial", 10), fg="#ffffff", bg="#121212").pack(anchor="w")
progress = ttk.Progressbar(progress_frame, mode="determinate", style="Custom.Horizontal.TProgressbar")
progress.pack(fill="x", pady=(5, 0))
# Log box
log_frame = tk.Frame(main_frame, bg="#121212")
log_frame.pack(fill="both", expand=True, pady=(0, 15))
tk.Label(log_frame, text="Log:", font=("Arial", 10), fg="#ffffff", bg="#121212").pack(anchor="w")
log_text_frame = tk.Frame(log_frame, bg="#121212")
log_text_frame.pack(fill="both", expand=True, pady=(5, 0))
log_box = tk.Text(log_text_frame, bg="#333333", fg="#ffffff", insertbackground="#ffffff",
height=8, wrap="word")
log_scrollbar = tk.Scrollbar(log_text_frame, command=log_box.yview)
log_box.configure(yscrollcommand=log_scrollbar.set)
log_box.pack(side="left", fill="both", expand=True)
log_scrollbar.pack(side="right", fill="y")
# Run button functions
def run_randomizer_threaded():
progress["value"] = 0
progress.update_idletasks()
threading.Thread(target=run_randomizer, daemon=True).start()
def run_randomizer():
try:
selected_categories = [cat for cat, var in category_vars.items() if var.get()]
if not selected_categories:
messagebox.showerror("Error", "Please select at least one category.")
return
log_box.delete(1.0, tk.END)
log_box.insert(tk.END, "Starting randomization...\n")
if use_per_category.get():
cat_folders = {cat: category_folder_paths[cat].get() for cat in selected_categories}
randomize_pack_per_category(cat_folders, output_dir.get(), pack_name.get(),
description.get(), icon_path.get(), log_box, selected_categories)
else:
if not folder_path.get():
messagebox.showerror("Error", "Please select a folder with .mcpack files.")
return
randomize_pack_single_folder(folder_path.get(), output_dir.get(), pack_name.get(),
description.get(), icon_path.get(), log_box,
selected_categories, progress_bar=progress)
except Exception as e:
log_box.insert(tk.END, f"Error: {str(e)}\n")
messagebox.showerror("Error", f"An error occurred: {str(e)}")
# Run button
run_button = tk.Button(main_frame, text="Create Randomized Pack", command=run_randomizer_threaded,
bg="#4a4a4a", fg="#ffffff", font=("Arial", 12, "bold"),
pady=10, cursor="hand2")
run_button.pack(fill="x", pady=(0, 10))
# Style the progress bar
style = ttk.Style()
style.theme_use('default')
style.configure("Custom.Horizontal.TProgressbar",
background='#4a9eff',
troughcolor='#333333',
borderwidth=1,
lightcolor='#4a9eff',
darkcolor='#4a9eff')
root.mainloop()
if __name__ == "__main__":
build_gui()