yobberbyte commited on
Commit
c6cbfcd
·
verified ·
1 Parent(s): 5114296

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +190 -0
app.py ADDED
@@ -0,0 +1,190 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ print("STARTED")
2
+
3
+ import gradio as gr
4
+ import requests, os, time, threading, subprocess, shutil, tarfile, urllib.request
5
+ from huggingface_hub import hf_hub_download, upload_file
6
+
7
+ # ========================
8
+ # PATHS
9
+ # ========================
10
+ ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
11
+ BASE_DIR = os.path.join(ROOT_DIR, "minecraft_server")
12
+
13
+ os.makedirs(BASE_DIR, exist_ok=True)
14
+
15
+ WORLD_PATH = os.path.join(BASE_DIR, "world")
16
+ NETHER_PATH = os.path.join(BASE_DIR, "world_nether")
17
+ END_PATH = os.path.join(BASE_DIR, "world_the_end")
18
+
19
+ # ========================
20
+ # ENV
21
+ # ========================
22
+ HF_TOKEN = os.environ.get("HF_TOKEN", "")
23
+ PLAYIT_AUTH_KEY = os.environ.get("PLAYIT_AUTH_KEY", "")
24
+
25
+ HF_REPO_ID = "Dalleon/McSaves"
26
+
27
+ # ========================
28
+ # GLOBALS
29
+ # ========================
30
+ SERVER_PROCESS = None
31
+ TCP_PROCESS = None
32
+ PUBLIC_TCP = None
33
+ SERVER_ONLINE = False
34
+
35
+ # ========================
36
+ # DOWNLOAD PAPER (1.20.6)
37
+ # ========================
38
+ def download_paper():
39
+ jar_path = os.path.join(BASE_DIR, "server.jar")
40
+
41
+ if os.path.exists(jar_path):
42
+ print("[PAPER] already exists")
43
+ return
44
+
45
+ print("[PAPER] downloading 1.20.6...")
46
+ url = "https://api.papermc.io/v2/projects/paper/versions/1.20.6/builds/latest/downloads/paper-1.20.6-latest.jar"
47
+ urllib.request.urlretrieve(url, jar_path)
48
+
49
+ # ========================
50
+ # READ OUTPUT
51
+ # ========================
52
+ def read_output():
53
+ global SERVER_PROCESS
54
+ while True:
55
+ if SERVER_PROCESS and SERVER_PROCESS.stdout:
56
+ line = SERVER_PROCESS.stdout.readline()
57
+ if line:
58
+ print("[MC]", line.strip())
59
+ time.sleep(0.1)
60
+
61
+ # ========================
62
+ # LOAD WORLD
63
+ # ========================
64
+ def load_world():
65
+ try:
66
+ file_path = hf_hub_download(
67
+ repo_id=HF_REPO_ID,
68
+ filename="world.tar.gz",
69
+ token=HF_TOKEN
70
+ )
71
+ except:
72
+ print("[LOAD] no backup")
73
+ return
74
+
75
+ if os.path.exists(WORLD_PATH):
76
+ shutil.rmtree(WORLD_PATH)
77
+
78
+ with tarfile.open(file_path, "r:gz") as tar:
79
+ tar.extractall(BASE_DIR)
80
+
81
+ # ========================
82
+ # PLAYIT
83
+ # ========================
84
+ def start_tcp():
85
+ global PUBLIC_TCP, SERVER_ONLINE, TCP_PROCESS
86
+
87
+ path = os.path.join(BASE_DIR, "playit")
88
+
89
+ if not os.path.exists(path):
90
+ print("[PLAYIT] downloading...")
91
+ urllib.request.urlretrieve(
92
+ "https://github.com/playit-cloud/playit-agent/releases/latest/download/playit-linux-amd64",
93
+ path
94
+ )
95
+ os.chmod(path, 0o755)
96
+
97
+ TCP_PROCESS = subprocess.Popen(
98
+ [path, "--secret", PLAYIT_AUTH_KEY],
99
+ cwd=BASE_DIR,
100
+ stdout=subprocess.PIPE,
101
+ stderr=subprocess.STDOUT,
102
+ text=True
103
+ )
104
+
105
+ timeout = time.time() + 20
106
+
107
+ while time.time() < timeout:
108
+ line = TCP_PROCESS.stdout.readline()
109
+ if line:
110
+ print("[PLAYIT]", line.strip())
111
+
112
+ if "playit.gg" in line:
113
+ for part in line.split():
114
+ if "playit.gg" in part:
115
+ PUBLIC_TCP = part
116
+ break
117
+
118
+ if PUBLIC_TCP:
119
+ break
120
+
121
+ SERVER_ONLINE = True
122
+ print("[PLAYIT] address:", PUBLIC_TCP)
123
+
124
+ # ========================
125
+ # START SERVER
126
+ # ========================
127
+ def start_server():
128
+ global SERVER_PROCESS
129
+
130
+ print("[SERVER] starting...")
131
+
132
+ load_world()
133
+ download_paper()
134
+
135
+ # accept eula
136
+ with open(os.path.join(BASE_DIR, "eula.txt"), "w") as f:
137
+ f.write("eula=true")
138
+
139
+ SERVER_PROCESS = subprocess.Popen(
140
+ [
141
+ "java",
142
+ "-Xms512M",
143
+ "-Xmx1G",
144
+ "-jar",
145
+ "server.jar",
146
+ "--nogui"
147
+ ],
148
+ cwd=BASE_DIR,
149
+ stdin=subprocess.PIPE,
150
+ stdout=subprocess.PIPE,
151
+ stderr=subprocess.STDOUT,
152
+ text=True
153
+ )
154
+
155
+ threading.Thread(target=read_output, daemon=True).start()
156
+
157
+ time.sleep(5)
158
+ start_tcp()
159
+
160
+ # ========================
161
+ # START BUTTON HANDLER
162
+ # ========================
163
+ def start_background():
164
+ threading.Thread(target=start_server, daemon=True).start()
165
+ return "Starting server..."
166
+
167
+ # ========================
168
+ # GET ADDRESS
169
+ # ========================
170
+ def get_address():
171
+ if not SERVER_ONLINE:
172
+ return "Starting..."
173
+ return PUBLIC_TCP or "No address yet"
174
+
175
+ # ========================
176
+ # UI
177
+ # ========================
178
+ with gr.Blocks() as app:
179
+ gr.Markdown("## ⛏️ Minecraft Server")
180
+
181
+ start_btn = gr.Button("Start Server")
182
+ status = gr.Textbox(label="Status")
183
+
184
+ addr_btn = gr.Button("Get Address")
185
+ address = gr.Textbox(label="Server Address")
186
+
187
+ start_btn.click(fn=start_background, outputs=status)
188
+ addr_btn.click(fn=get_address, outputs=address)
189
+
190
+ app.launch()