File size: 4,056 Bytes
019c960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4152a2
 
 
 
 
 
019c960
 
 
f4152a2
019c960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4152a2
019c960
 
 
 
 
 
 
 
 
 
 
f4152a2
019c960
 
f4152a2
019c960
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
set -e

# ═══════════════════════════════════════════════════════════
#  Minecraft Java Server β€” startup script
#  Runs inside Docker on Hugging Face Spaces
# ═══════════════════════════════════════════════════════════

DATA_DIR="/data"
SERVER_JAR="${DATA_DIR}/server.jar"
LOG_DIR="${DATA_DIR}/logs"

MC_VERSION="${MC_VERSION:-1.21.4}"
MC_MEMORY="${MC_MEMORY:-1G}"
MC_MAX_MEMORY="${MC_MAX_MEMORY:-2G}"
MC_WORLD="${MC_WORLD:-world}"
SERVER_PORT="${SERVER_PORT:-25565}"

mkdir -p "${DATA_DIR}" "${LOG_DIR}"

echo "══════════════════════════════════════════"
echo " Minecraft Java Server"
echo " Version  : ${MC_VERSION}"
echo " Memory   : ${MC_MEMORY} β†’ ${MC_MAX_MEMORY}"
echo " World    : ${MC_WORLD}"
echo " Port     : ${SERVER_PORT}"
echo "══════════════════════════════════════════"

# ── 1. Start HTTP status page (required by HF Spaces on port 7860) ──
echo "[~] Starting HTTP status server on port 7860..."
python3 /home/minecraft/status_server.py &
echo "[βœ“] HTTP status server running."

# ── 2. Accept EULA automatically ───────────────────────────
echo "eula=true" > "${DATA_DIR}/eula.txt"
echo "[βœ“] EULA accepted."

# ── 3. Download server JAR if not cached ──────────────────
if [ ! -f "${SERVER_JAR}" ]; then
    echo "[~] Fetching Minecraft ${MC_VERSION} server JAR..."

    MANIFEST_URL="https://launchermeta.mojang.com/mc/game/version_manifest.json"
    VERSION_URL=$(curl -sL "${MANIFEST_URL}" | \
        python3 -c "
import sys, json
data = json.load(sys.stdin)
target = '${MC_VERSION}'
for v in data['versions']:
    if v['id'] == target:
        print(v['url'])
        break
")

    if [ -z "${VERSION_URL}" ]; then
        echo "[βœ—] Version ${MC_VERSION} not found in Mojang manifest!"
        exit 1
    fi

    SERVER_URL=$(curl -sL "${VERSION_URL}" | \
        python3 -c "import sys, json; d=json.load(sys.stdin); print(d['downloads']['server']['url'])")

    curl -L "${SERVER_URL}" -o "${SERVER_JAR}"
    echo "[βœ“] Server JAR downloaded."
else
    echo "[βœ“] Server JAR already cached."
fi

# ── 4. Write server.properties (first run only) ────────────
PROPS_FILE="${DATA_DIR}/server.properties"
if [ ! -f "${PROPS_FILE}" ]; then
    sed \
        -e "s|{{WORLD}}|${MC_WORLD}|g" \
        -e "s|{{PORT}}|${SERVER_PORT}|g" \
        /home/minecraft/server.properties.template > "${PROPS_FILE}"
    echo "[βœ“] server.properties created."
else
    echo "[βœ“] server.properties already exists β€” skipping."
fi

# ── 5. Move to data dir ────────────────────────────────────
cd "${DATA_DIR}"

# ── 6. Launch Minecraft! ───────────────────────────────────
echo ""
echo "[β˜…] Starting Minecraft server..."
echo ""

exec java \
    -Xms${MC_MEMORY} \
    -Xmx${MC_MAX_MEMORY} \
    -XX:+UseG1GC \
    -XX:+ParallelRefProcEnabled \
    -XX:MaxGCPauseMillis=200 \
    -XX:+UnlockExperimentalVMOptions \
    -XX:+DisableExplicitGC \
    -XX:G1NewSizePercent=30 \
    -XX:G1MaxNewSizePercent=40 \
    -XX:G1HeapRegionSize=8M \
    -XX:G1ReservePercent=20 \
    -XX:G1HeapWastePercent=5 \
    -XX:G1MixedGCCountTarget=4 \
    -XX:InitiatingHeapOccupancyPercent=15 \
    -XX:G1MixedGCLiveThresholdPercent=90 \
    -XX:G1RSetUpdatingPauseTimePercent=5 \
    -XX:SurvivorRatio=32 \
    -XX:+PerfDisableSharedMem \
    -XX:MaxTenuringThreshold=1 \
    -Dusing.aikars.flags=https://mcflags.emc.gs \
    -jar "${SERVER_JAR}" nogui