none / app.py
Techbitforge's picture
Create app.py
0eff85d verified
raw
history blame
3.75 kB
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)