diff --git a/cavemen/nixrunserver.sh b/cavemen/nixrunserver.sh index 8ba90ee..d706dfd 100755 --- a/cavemen/nixrunserver.sh +++ b/cavemen/nixrunserver.sh @@ -1,23 +1,61 @@ #!/usr/bin/env nix-shell #! nix-shell -i bash --pure #! nix-shell -p javaPackages.compiler.openjdk17 +#! nix-shell -p miniupnpc #! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/tarball/nixos-25.11 +PACK_AFTER=0 +UPNP=0 +for arg in "$@"; do + [[ "$arg" == "--pack-after" ]] && PACK_AFTER=1 + [[ "$arg" == "--upnp" ]] && UPNP=1 +done + source /home/h3lp/mineserver/minecraft.env +if [[ "$UPNP" == 1 ]]; then + SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" + PROPERTIES="$SCRIPT_DIR/server.properties" + if [[ -f "$PROPERTIES" ]]; then + PORT=$(grep -oP '(?<=^server-port=)\d+' "$PROPERTIES") + fi + PORT="${PORT:-25565}" + LOCAL_IP="$(upnpc -s 2>/dev/null | grep -oP '(?<=Local LAN ip address : ).*')" + + echo "Opening port $PORT/TCP via UPnP..." + upnpc -a "$LOCAL_IP" "$PORT" "$PORT" TCP + TCP_OK=$? + echo "Opening port $PORT/UDP via UPnP..." + upnpc -a "$LOCAL_IP" "$PORT" "$PORT" UDP + UDP_OK=$? + + if [[ $TCP_OK -ne 0 || $UDP_OK -ne 0 ]]; then + echo "Warning: One or more UPnP port mappings failed. The port may not be reachable from outside." + fi + + cleanup() { + echo "Removing UPnP port mappings for $PORT..." + upnpc -d "$PORT" TCP + upnpc -d "$PORT" UDP + } + trap cleanup EXIT +fi + java -Xmx${MEMORY_MAX}M -Xms${MEMORY_MIN}M -jar minecraft_server.jar nogui -read -rp "Pack and publish world? [y/N] " answer -if [[ "$answer" =~ ^[Yy]$ ]]; then - set -e - WORLD=/home/h3lp/mineserver/world - OUTPUT=/var/www/hostedfiles/world.tar.xz - TMP=$(mktemp) - tar -cJf "$TMP" -C "$WORLD" . - mv "$TMP" "$OUTPUT" - chmod 644 "$OUTPUT" - echo "Done: $OUTPUT" - set +e +if [[ "$PACK_AFTER" == 1 ]]; then + read -rp "Pack and publish world? [y/N] " answer + if [[ "$answer" =~ ^[Yy]$ ]]; then + set -e + WORLD=/home/h3lp/mineserver/world + OUTPUT=/var/www/hostedfiles/world.tar.xz + TMP=$(mktemp) + tar -cJf "$TMP" -C "$WORLD" . + mv "$TMP" "$OUTPUT" + chmod 644 "$OUTPUT" + echo "Done: $OUTPUT" + set +e + fi fi read -rp "Press Enter to continue..."