Skip to content

Commit 0133d8d

Browse files
authored
Added a script to extract resource from JAR file (#565)
1 parent 5e762a2 commit 0133d8d

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

tools/extract_jar.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import os
2+
import zipfile
3+
import shutil
4+
import sys
5+
6+
def extract_minecraft_jar(jar_path, destination):
7+
"""
8+
Extract specific files and folders from a Minecraft JAR file.
9+
10+
Args:
11+
jar_path (str): Path to the minecraft.jar file.
12+
destination (str): Destination directory to copy the extracted files.
13+
"""
14+
# Ensure the JAR file exists
15+
if not os.path.isfile(jar_path):
16+
print(f"Error: The file {jar_path} does not exist.")
17+
return
18+
19+
# Ensure the destination directory exists
20+
if not os.path.exists(destination):
21+
os.makedirs(destination)
22+
23+
# List of files and folders to extract
24+
items_to_extract = [
25+
"armor/",
26+
"art/",
27+
"environment/",
28+
"font/",
29+
"gui/",
30+
"item/",
31+
"misc/",
32+
"mob/",
33+
"terrain/",
34+
"title/",
35+
"pack.png",
36+
"particles.png",
37+
"terrain.png",
38+
]
39+
40+
# Open the JAR file as a ZIP archive
41+
with zipfile.ZipFile(jar_path, 'r') as jar:
42+
for item in items_to_extract:
43+
found = False
44+
for file in jar.namelist():
45+
# Check if the file starts with the folder name or matches the file
46+
if file.startswith(item):
47+
found = True
48+
# Extract the file to the destination directory
49+
print(f"Extracting {file}...")
50+
jar.extract(file, destination)
51+
if not found:
52+
print(f"Warning: {item} not found in {jar_path}.")
53+
54+
print("Extraction complete.")
55+
56+
if __name__ == "__main__":
57+
# Check if a JAR path is provided as a command-line argument
58+
if len(sys.argv) > 1:
59+
jar_path = sys.argv[1]
60+
else:
61+
# Prompt the user for the JAR path if not provided
62+
jar_path = input("Enter the path to minecraft.jar: ").strip()
63+
64+
# Destination directory within the project
65+
destination = os.path.join("..", "game", "assets", "resource_packs", "minecraft")
66+
67+
# Extract the JAR file
68+
extract_minecraft_jar(jar_path, destination)

0 commit comments

Comments
 (0)