File size: 3,751 Bytes
0eff85d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import subprocess
import threading
import gradio as gr
import time
import shutil

# ========================
# CONFIG
# ========================
MINECRAFT_VERSION = "1.21.1"
PAPER_JAR_URL = "https://api.papermc.io/v2/projects/paper/versions/1.21.1/builds/70/downloads/paper-1.21.1-70.jar"
SERVER_DIR = "mcserver"
JAR_FILE = f"{SERVER_DIR}/server.jar"
PLAYIT_URL = "https://github.com/playit-cloud/playit-agent/releases/latest/download/playit-linux-amd64"
PLAYIT_BIN = f"{SERVER_DIR}/playit"
MCMYADMIN_URL = "https://mcmya.s3.amazonaws.com/McMyAdmin2-Linux.tar.gz"  # public link
MCMYADMIN_DIR = "McMyAdmin"
GOOGLE_DRIVE_MOUNT = "/content/drive"

# ========================
# INSTALL DEPENDENCIES
# ========================
def setup_environment():
    os.makedirs(SERVER_DIR, exist_ok=True)

    # Install Java if missing
    subprocess.run("apt-get update && apt-get install -y openjdk-21-jre-headless", shell=True)

    # Download PaperMC
    if not os.path.exists(JAR_FILE):
        subprocess.run(f"wget -O {JAR_FILE} {PAPER_JAR_URL}", shell=True)

    # Accept EULA
    with open(f"{SERVER_DIR}/eula.txt", "w") as f:
        f.write("eula=true\n")

    # Download Playit
    if not os.path.exists(PLAYIT_BIN):
        subprocess.run(f"wget -O {PLAYIT_BIN} {PLAYIT_URL} && chmod +x {PLAYIT_BIN}", shell=True)

    # Install McMyAdmin2
    if not os.path.exists(MCMYADMIN_DIR):
        subprocess.run(f"wget -O McMyAdmin2.tar.gz {MCMYADMIN_URL}", shell=True)
        subprocess.run("mkdir -p McMyAdmin && tar -xzf McMyAdmin2.tar.gz -C McMyAdmin", shell=True)
        subprocess.run("chmod +x McMyAdmin/McMyAdmin", shell=True)

setup_environment()

# ========================
# SERVER CONTROL
# ========================
server_process = None
playit_process = None
mcmya_process = None

def start_server():
    global server_process, playit_process, mcmya_process
    if server_process is None:
        server_process = subprocess.Popen(
            ["java", "-Xms1G", "-Xmx2G", "-jar", JAR_FILE, "--nogui"],
            cwd=SERVER_DIR,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT
        )
        # Start Playit tunnel
        playit_process = subprocess.Popen(
            [PLAYIT_BIN],
            cwd=SERVER_DIR,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT
        )
        # Start McMyAdmin Panel
        mcmya_process = subprocess.Popen(
            ["./McMyAdmin"],
            cwd=MCMYADMIN_DIR,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT
        )
        return "βœ… Minecraft server + Playit + McMyAdmin started!"
    return "⚠️ Server already running."

def stop_server():
    global server_process, playit_process, mcmya_process
    if server_process:
        server_process.terminate()
        playit_process.terminate()
        mcmya_process.terminate()
        server_process, playit_process, mcmya_process = None, None, None
        return "πŸ›‘ Server stopped."
    return "⚠️ Server not running."

def server_logs():
    if server_process:
        try:
            return server_process.stdout.read().decode("utf-8", errors="ignore")
        except:
            return "⚠️ No logs available yet."
    return "⚠️ Server not running."

# ========================
# GRADIO UI
# ========================
with gr.Blocks() as demo:
    gr.Markdown("# 🌍 Minecraft Server on Hugging Face (with McMyAdmin + Playit)")

    start_btn = gr.Button("πŸš€ Start Server")
    stop_btn = gr.Button("πŸ›‘ Stop Server")
    log_box = gr.Textbox(label="Server Logs", lines=20)

    start_btn.click(start_server, outputs=log_box)
    stop_btn.click(stop_server, outputs=log_box)

demo.launch(server_name="0.0.0.0", server_port=7860)