File size: 4,764 Bytes
c6cbfcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
print("STARTED")

import gradio as gr
import requests, os, time, threading, subprocess, shutil, tarfile, urllib.request
from huggingface_hub import hf_hub_download, upload_file

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

os.makedirs(BASE_DIR, exist_ok=True)

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")

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

HF_REPO_ID = "Dalleon/McSaves"

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

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

    if os.path.exists(jar_path):
        print("[PAPER] already exists")
        return

    print("[PAPER] downloading 1.20.6...")
    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)

# ========================
# READ OUTPUT
# ========================
def read_output():
    global SERVER_PROCESS
    while True:
        if SERVER_PROCESS and SERVER_PROCESS.stdout:
            line = SERVER_PROCESS.stdout.readline()
            if line:
                print("[MC]", line.strip())
        time.sleep(0.1)

# ========================
# LOAD WORLD
# ========================
def load_world():
    try:
        file_path = hf_hub_download(
            repo_id=HF_REPO_ID,
            filename="world.tar.gz",
            token=HF_TOKEN
        )
    except:
        print("[LOAD] no backup")
        return

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

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

# ========================
# PLAYIT
# ========================
def start_tcp():
    global PUBLIC_TCP, SERVER_ONLINE, TCP_PROCESS

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

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

    TCP_PROCESS = subprocess.Popen(
        [path, "--secret", PLAYIT_AUTH_KEY],
        cwd=BASE_DIR,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True
    )

    timeout = time.time() + 20

    while time.time() < timeout:
        line = TCP_PROCESS.stdout.readline()
        if line:
            print("[PLAYIT]", line.strip())

            if "playit.gg" in line:
                for part in line.split():
                    if "playit.gg" in part:
                        PUBLIC_TCP = part
                        break

        if PUBLIC_TCP:
            break

    SERVER_ONLINE = True
    print("[PLAYIT] address:", PUBLIC_TCP)

# ========================
# START SERVER
# ========================
def start_server():
    global SERVER_PROCESS

    print("[SERVER] starting...")

    load_world()
    download_paper()

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

    SERVER_PROCESS = subprocess.Popen(
        [
            "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_output, daemon=True).start()

    time.sleep(5)
    start_tcp()

# ========================
# START BUTTON HANDLER
# ========================
def start_background():
    threading.Thread(target=start_server, daemon=True).start()
    return "Starting server..."

# ========================
# GET ADDRESS
# ========================
def get_address():
    if not SERVER_ONLINE:
        return "Starting..."
    return PUBLIC_TCP or "No address yet"

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

    start_btn = gr.Button("Start Server")
    status = gr.Textbox(label="Status")

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

    start_btn.click(fn=start_background, outputs=status)
    addr_btn.click(fn=get_address, outputs=address)

app.launch()