Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import urllib.request
|
2 |
+
from pathlib import Path
|
3 |
+
import os
|
4 |
+
import subprocess
|
5 |
+
import sys
|
6 |
+
|
7 |
+
PAPER_JAR_URL = "https://fill-data.papermc.io/v1/objects/234a9b32098100c6fc116664d64e36ccdb58b5b649af0f80bcccb08b0255eaea/paper-1.20.1-196.jar"
|
8 |
+
DATA_DIR = Path("/data")
|
9 |
+
JAR_NAME = "paper-1.20.1-196.jar"
|
10 |
+
JAR_PATH = DATA_DIR / JAR_NAME
|
11 |
+
|
12 |
+
def download_file(url, destination):
|
13 |
+
print(f"Downloading {url}...")
|
14 |
+
try:
|
15 |
+
destination.parent.mkdir(parents=True, exist_ok=True)
|
16 |
+
|
17 |
+
headers = {
|
18 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
19 |
+
}
|
20 |
+
request = urllib.request.Request(url, headers=headers)
|
21 |
+
|
22 |
+
with urllib.request.urlopen(request) as response:
|
23 |
+
with open(destination, 'wb') as f:
|
24 |
+
total_size = int(response.headers.get('content-length', 0))
|
25 |
+
block_size = 8192
|
26 |
+
downloaded = 0
|
27 |
+
|
28 |
+
while True:
|
29 |
+
buffer = response.read(block_size)
|
30 |
+
if not buffer:
|
31 |
+
break
|
32 |
+
|
33 |
+
downloaded += len(buffer)
|
34 |
+
f.write(buffer)
|
35 |
+
|
36 |
+
if total_size:
|
37 |
+
percent = min(100, (downloaded * 100) // total_size)
|
38 |
+
print(f"\rProgress: {percent}% ({downloaded}/{total_size} bytes)", end="")
|
39 |
+
|
40 |
+
print(f"\n✓ Downloaded: {destination}")
|
41 |
+
return True
|
42 |
+
except Exception as e:
|
43 |
+
print(f"\n✗ Failed to download {url}: {e}")
|
44 |
+
return False
|
45 |
+
|
46 |
+
def setup_minecraft_server():
|
47 |
+
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
48 |
+
|
49 |
+
if not JAR_PATH.exists():
|
50 |
+
print(f"Downloading Minecraft server to {JAR_PATH}")
|
51 |
+
if not download_file(PAPER_JAR_URL, JAR_PATH):
|
52 |
+
return False
|
53 |
+
else:
|
54 |
+
print(f"Server JAR already exists at {JAR_PATH}")
|
55 |
+
|
56 |
+
server_properties_path = DATA_DIR / "server.properties"
|
57 |
+
if not server_properties_path.exists():
|
58 |
+
server_properties = """# Minecraft server properties
|
59 |
+
server-port=25565
|
60 |
+
gamemode=survival
|
61 |
+
difficulty=easy
|
62 |
+
spawn-protection=16
|
63 |
+
max-players=20
|
64 |
+
online-mode=false
|
65 |
+
white-list=false
|
66 |
+
motd=Hugging Face Minecraft Server
|
67 |
+
"""
|
68 |
+
with open(server_properties_path, 'w') as f:
|
69 |
+
f.write(server_properties)
|
70 |
+
|
71 |
+
eula_path = DATA_DIR / "eula.txt"
|
72 |
+
if not eula_path.exists():
|
73 |
+
with open(eula_path, 'w') as f:
|
74 |
+
f.write("eula=true\n")
|
75 |
+
return True
|
76 |
+
|
77 |
+
def start_server():
|
78 |
+
if not JAR_PATH.exists():
|
79 |
+
print("Server JAR not found. Run setup first.")
|
80 |
+
return False
|
81 |
+
|
82 |
+
os.chdir(DATA_DIR)
|
83 |
+
|
84 |
+
cmd = [
|
85 |
+
"java",
|
86 |
+
"-Xmx2G",
|
87 |
+
"-Xms1G",
|
88 |
+
"-jar", str(JAR_PATH),
|
89 |
+
"--nogui"
|
90 |
+
]
|
91 |
+
|
92 |
+
print(f"Starting Minecraft server with command: {' '.join(cmd)}")
|
93 |
+
print(f"Working directory: {DATA_DIR}")
|
94 |
+
|
95 |
+
try:
|
96 |
+
process = subprocess.Popen(
|
97 |
+
cmd,
|
98 |
+
stdout=subprocess.PIPE,
|
99 |
+
stderr=subprocess.STDOUT,
|
100 |
+
universal_newlines=True,
|
101 |
+
bufsize=1
|
102 |
+
)
|
103 |
+
|
104 |
+
for line in process.stdout:
|
105 |
+
print(line.strip())
|
106 |
+
|
107 |
+
except Exception as e:
|
108 |
+
print(f"Failed to start server: {e}")
|
109 |
+
return False
|
110 |
+
|
111 |
+
if __name__ == "__main__":
|
112 |
+
setup_minecraft_server()
|
113 |
+
start_server()
|