Spaces:
Running
Running
File size: 4,858 Bytes
b676880 417d689 33671b3 f17545d 8416633 f17545d b676880 f17545d 417d689 f17545d b676880 417d689 f17545d b676880 417d689 5ba1f2c f17545d 417d689 f17545d 417d689 f17545d 417d689 b676880 f17545d b676880 f17545d b676880 f17545d b676880 417d689 f17545d 417d689 b676880 417d689 f17545d 417d689 b676880 f17545d b676880 b63e107 f17545d b63e107 f17545d b676880 33671b3 f17545d 33671b3 f17545d 33671b3 f17545d 33671b3 f17545d 33671b3 f17545d 33671b3 b676880 33671b3 f17545d |
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 |
import urllib.request
from pathlib import Path
import os
import subprocess
import sys
import shutil
import threading
import time
import signal
import gradio as gr
from mcstatus import JavaServer
# URLs
PAPER_JAR_URL = "https://fill-data.papermc.io/v1/objects/234a9b32098100c6fc116664d64e36ccdb58b5b649af0f80bcccb08b0255eaea/paper-1.20.1-196.jar"
PLAYIT_PLUGIN_URL = "https://github.com/playit-cloud/playit-minecraft-plugin/releases/download/v0.1.4/playit-minecraft-plugin.jar"
# Paths
DATA_DIR = Path("/data")
JAVA_DIR = DATA_DIR / "java"
PLUGINS_DIR = DATA_DIR / "plugins"
JAR_NAME = "paper-1.20.1-196.jar"
JAR_PATH = DATA_DIR / JAR_NAME
# Java download URLs (OpenJDK 17)
JAVA_URLS = {
"linux_x64": "https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz",
"linux_aarch64": "https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-aarch64_bin.tar.gz"
}
def get_platform():
import platform
machine = platform.machine().lower()
if 'x86_64' in machine or 'amd64' in machine:
return "linux_x64"
elif 'aarch64' in machine or 'arm64' in machine:
return "linux_aarch64"
else:
print(f"Unsupported architecture: {machine}")
return "linux_x64"
def check_java():
java_path = shutil.which("java")
if java_path:
return java_path
custom_java = JAVA_DIR / "bin" / "java"
if custom_java.exists():
return str(custom_java)
return None
def download_file(url, destination):
print(f"Downloading {url} to {destination}...")
try:
destination.parent.mkdir(parents=True, exist_ok=True)
urllib.request.urlretrieve(url, destination)
print(f"β Downloaded {destination}")
return True
except Exception as e:
print(f"β Failed to download {url}: {e}")
return False
def install_java():
platform_key = get_platform()
java_url = JAVA_URLS.get(platform_key)
if not java_url:
return False
java_archive = DATA_DIR / "java.tar.gz"
if not download_file(java_url, java_archive):
return False
import tarfile
with tarfile.open(java_archive, 'r:gz') as tar:
temp_dir = DATA_DIR / "java_temp"
temp_dir.mkdir(exist_ok=True)
tar.extractall(temp_dir)
jdk_dir = next(d for d in temp_dir.iterdir() if d.is_dir() and d.name.startswith('jdk'))
if JAVA_DIR.exists():
shutil.rmtree(JAVA_DIR)
shutil.move(str(jdk_dir), str(JAVA_DIR))
shutil.rmtree(temp_dir)
java_archive.unlink()
(JAVA_DIR / "bin" / "java").chmod(0o755)
print(f"β Java installed to {JAVA_DIR}")
return True
def setup_minecraft_server():
DATA_DIR.mkdir(parents=True, exist_ok=True)
java_path = check_java()
if not java_path:
print("Java not found. Installing...")
if not install_java():
return False
java_path = check_java()
if not java_path:
return False
if not JAR_PATH.exists():
if not download_file(PAPER_JAR_URL, JAR_PATH):
return False
# Server config
(DATA_DIR / "server.properties").write_text("""server-port=25565
gamemode=survival
difficulty=easy
spawn-protection=0
max-players=64
online-mode=true
motd=Minecraft Server
enable-query=true
query.port=25565
""")
(DATA_DIR / "eula.txt").write_text("eula=true\n")
# Download Playit plugin
PLUGINS_DIR.mkdir(parents=True, exist_ok=True)
playit_plugin_path = PLUGINS_DIR / "playit-minecraft-plugin.jar"
if download_file(PLAYIT_PLUGIN_URL, playit_plugin_path):
shutil.copy(playit_plugin_path, DATA_DIR / "playit-minecraft-plugin.jar")
print("β Minecraft server setup complete")
return True
def start_server_background():
global server_process
java_path = check_java()
cmd = [java_path, "-Xmx2G", "-Xms1G", "-jar", str(JAR_PATH), "--nogui"]
server_process = subprocess.Popen(cmd, cwd=DATA_DIR, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
threading.Thread(target=lambda: [print(f"[SERVER] {line.strip()}") for line in server_process.stdout], daemon=True).start()
time.sleep(5)
print("β Server started in background")
return True
def stop_server():
global server_process
if server_process:
print("Stopping server...")
server_process.terminate()
server_process.wait(timeout=10)
server_process = None
print("β Server stopped")
def signal_handler(signum, frame):
stop_server()
sys.exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
if setup_minecraft_server():
start_server_background()
while True:
time.sleep(1)
|