File size: 7,300 Bytes
5842b47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e0eafc
5842b47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e0eafc
5842b47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
print("STARTED")

# Imports
import gradio as gr
import spaces, requests, os, time, threading, subprocess, shutil, tarfile, urllib.request, json

from huggingface_hub import hf_hub_download, upload_file

# ========================
# PATH SETUP (FIXED)
# ========================
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_DIR = os.path.join(ROOT_DIR, "minecraft_server")

WORLD_PATH = os.path.join(BASE_DIR, "world")
NETHER_PATH = os.path.join(BASE_DIR, "world_nether")
END_PATH = os.path.join(BASE_DIR, "world_the_end")

print("[DEBUG] ROOT:", ROOT_DIR)
print("[DEBUG] BASE:", BASE_DIR)

# ========================
# ENV VARIABLES
# ========================
ADMIN_PASSWORD = os.environ.get("ADMIN_PASSWORD", "")
HF_TOKEN = os.environ.get("HF_TOKEN", "")
PLAYIT_AUTH_KEY = os.environ.get("PLAYIT_AUTH_KEY", "")

HF_REPO_ID = "Dalleon/McSaves"
WORLD_ID = "world_default"

# ========================
# GLOBALS
# ========================
SERVER_PROCESS = None
TCP_PROCESS = None
PUBLIC_TCP = None
SERVER_ONLINE = False

SERVER_OUTPUT_BUFFER = []

# ========================
# JAVA INSTALL (fallback)
# ========================
def install_java21_local():
    jdk_url = "https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz"
    download_path = os.path.join(BASE_DIR, "jdk21.tar.gz")

    print("[JAVA] Downloading...")
    urllib.request.urlretrieve(jdk_url, download_path)

    extract_dir = os.path.join(BASE_DIR, "jdk21")
    os.makedirs(extract_dir, exist_ok=True)

    with tarfile.open(download_path, "r:gz") as tar:
        tar.extractall(path=extract_dir)

    for item in os.listdir(extract_dir):
        if item.startswith("jdk-21"):
            java_home = os.path.join(extract_dir, item)
            os.environ["JAVA_HOME"] = java_home
            os.environ["PATH"] = os.path.join(java_home, "bin") + ":" + os.environ["PATH"]
            print("[JAVA] Installed:", java_home)
            return

# ========================
# DOWNLOAD PAPER
# ========================
def download_paper():
    jar_path = os.path.join(BASE_DIR, "server.jar")
    if os.path.exists(jar_path):
        return

    print("[PAPER] Downloading...")
    url = "https://api.papermc.io/v2/projects/paper/versions/1.20.6/builds/latest/downloads/paper-1.20.6-latest.jar"
    urllib.request.urlretrieve(url, jar_path)

# ========================
# SERVER OUTPUT READER
# ========================
def read_server_output():
    global SERVER_PROCESS
    while True:
        if SERVER_PROCESS and SERVER_PROCESS.stdout:
            line = SERVER_PROCESS.stdout.readline()
            if line:
                print("[MC]", line.strip())
                SERVER_OUTPUT_BUFFER.append(line)
        time.sleep(0.1)

# ========================
# SAVE WORLD
# ========================
def save_world():
    global SERVER_PROCESS

    print("[SAVE] Saving world...")

    if SERVER_PROCESS:
        SERVER_PROCESS.stdin.write("save-off\n")
        SERVER_PROCESS.stdin.flush()

        SERVER_PROCESS.stdin.write("save-all\n")
        SERVER_PROCESS.stdin.flush()

        time.sleep(10)

        SERVER_PROCESS.stdin.write("save-on\n")
        SERVER_PROCESS.stdin.flush()

    tar_path = os.path.join(BASE_DIR, "world.tar.gz")

    with tarfile.open(tar_path, "w:gz") as tar:
        if os.path.exists(WORLD_PATH):
            tar.add(WORLD_PATH, arcname="world")

        if os.path.exists(NETHER_PATH):
            tar.add(NETHER_PATH, arcname="world_nether")

        if os.path.exists(END_PATH):
            tar.add(END_PATH, arcname="world_the_end")

    upload_file(
        path_or_fileobj=tar_path,
        path_in_repo="world.tar.gz",
        repo_id=HF_REPO_ID,
        token=HF_TOKEN,
        repo_type="model"
    )

    print("[SAVE] Done.")

# ========================
# LOAD WORLD
# ========================
def load_world():
    print("[LOAD] Loading world...")

    try:
        file_path = hf_hub_download(
            repo_id=HF_REPO_ID,
            filename="world.tar.gz",
            token=HF_TOKEN
        )
    except:
        print("[LOAD] No backup found.")
        return

    if os.path.exists(WORLD_PATH):
        shutil.rmtree(WORLD_PATH)

    with tarfile.open(file_path, "r:gz") as tar:
        tar.extractall(BASE_DIR)

    print("[LOAD] Done.")

# ========================
# PLAYIT TUNNEL (FIXED - NON-BLOCKING)
# ========================
def start_tcp():
    global PUBLIC_TCP, SERVER_ONLINE, TCP_PROCESS

    playit_path = os.path.join(BASE_DIR, "playit")

    if not os.path.exists(playit_path):
        print("[PLAYIT] Downloading...")
        url = "https://github.com/playit-cloud/playit-agent/releases/latest/download/playit-linux-amd64"
        urllib.request.urlretrieve(url, playit_path)
        os.chmod(playit_path, 0o755)

    if not PLAYIT_AUTH_KEY:
        print("[PLAYIT] Missing auth key")
        return

    # ✅ FIX: fully detached process (NO PIPE, NO READLINE, NO FREEZE)
    TCP_PROCESS = subprocess.Popen(
        [playit_path, "--secret", PLAYIT_AUTH_KEY],
        cwd=BASE_DIR,
        stdin=subprocess.DEVNULL,
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
        start_new_session=True
    )

    print("[PLAYIT] Agent started safely in background")

    # small delay so it can initialize without blocking HF
    time.sleep(5)

    # ❗ IMPORTANT: no blocking log parsing anymore
    PUBLIC_TCP = "Check Playit dashboard for address"
    SERVER_ONLINE = True

    print("[PLAYIT] Ready (no blocking, HF-safe)")
# ========================
# START SERVER
# ========================
def start_server():
    global SERVER_PROCESS

    load_world()

    if shutil.which("java") is None:
        install_java21_local()

    if shutil.which("java") is None:
        print("[ERROR] Java not found")
        return

    download_paper()

    with open(os.path.join(BASE_DIR, "eula.txt"), "w") as f:
        f.write("eula=true")

    SERVER_PROCESS = subprocess.Popen(
        [
            shutil.which("java"),
            "-Xms512M",
            "-Xmx1G",
            "-jar",
            "server.jar",
            "--nogui"
        ],
        cwd=BASE_DIR,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True
    )

    threading.Thread(target=read_server_output, daemon=True).start()

    time.sleep(5)
    start_tcp()

# ========================
# COMMAND EXEC
# ========================
def execute_command(password, cmd):
    if password != ADMIN_PASSWORD:
        return "Wrong password"

    if not SERVER_PROCESS:
        return "Server not running"

    SERVER_PROCESS.stdin.write(cmd + "\n")
    SERVER_PROCESS.stdin.flush()

    return "Executed"

# ========================
# GET ADDRESS
# ========================
def get_address():
    if not SERVER_ONLINE:
        return "Server starting..."

    return PUBLIC_TCP or "No address yet"

# ========================
# INIT
# ========================
threading.Thread(target=start_server, daemon=True).start()

# ========================
# UI
# ========================
with gr.Blocks() as app:
    gr.Markdown("## ⛏️ Minecraft Server")

    address = gr.Textbox(label="Server Address")
    btn = gr.Button("Get Address")

    btn.click(fn=get_address, outputs=address)

app.launch()