diff --git a/.gitignore b/.gitignore index fafe575..d3de357 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ rclone.conf /grafana /mysql /portainer +/datapacks-*/ diff --git a/update-datapacks.sh b/update-datapacks.sh new file mode 100755 index 0000000..063a29a --- /dev/null +++ b/update-datapacks.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +SERVER_NAME="ichigo" +SERVER_DP_PATH="docker/games/minecraft/spigot/worlds/world/datapacks/" +DATAPACKS_FILE=datapacks.json +CRAFTINGTWEAKS_FILE=craftingtweaks.json +OUT_DIR=datapacks + +set -e + +check() { + if ! command -v "$1" > /dev/null 2>&1; then + echo "error: command '$1' is not available" >&2 + if [[ -n $2 ]]; then + echo " $2" + fi + exit 1 + fi +} + +retry() { + local timeout=1 + local max_retries=10 + local n=0 + + until "$@"; do + if [[ $n -gt "$max_retries" ]]; then + echo "Command failed after $max_retries retries" + exit 1 + fi + sleep "$timeout" + (( attempt++ )) + done +} + +check vtdl "You can download it from https://github.com/zekrotja/vtdl/releases or via 'cargo install --git https://github.com/zekrotja/vtdl'" +check jq + +out_dir="$OUT_DIR" + +if [ -d "$out_dir" ]; then + rm -rf "$out_dir" +fi + +updated=0 + +if [[ -f $DATAPACKS_FILE ]]; then + echo "Downloading datapacks ..." + vtdl download --packages "$DATAPACKS_FILE" --out-dir "$out_dir" + (( updated++ )) +fi + +if [[ -f $CRAFTINGTWEAKS_FILE ]]; then + echo "Downloading craftingtweaks ..." + vtdl download --packages "$CRAFTINGTWEAKS_FILE" --out-dir "$out_dir" + (( updated++ )) +fi + +if [[ $updated -lt 1 ]]; then + echo "error: nothing to upload" >&2 + exit 1 +fi + +echo "Deleting old datapacks on server ..." +retry ssh "$SERVER_NAME" 'find '"$SERVER_DP_PATH"' -maxdepth 1 -type f -name "*.zip" -exec rm -f {} \+' + +echo "Uploading new datapacks ..." +retry scp -r "$out_dir"/* "$SERVER_NAME:$SERVER_DP_PATH"