From 5225ba06c8c0d0e441c364d489bdccd9b203bb6d Mon Sep 17 00:00:00 2001 From: Geoff Bourne Date: Sun, 21 Mar 2021 11:43:21 -0500 Subject: [PATCH] Added support for spiget to download Spigot/Bukkit/Paper plugins #808 --- README.md | 17 ++++++++++++ start-deployBukkitSpigot | 3 +-- start-deployPaper | 3 +-- start-spiget | 58 ++++++++++++++++++++++++++++++++++++++++ start-utils | 9 ++++++- 5 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 start-spiget diff --git a/README.md b/README.md index 0d20a9f6..0353f811 100644 --- a/README.md +++ b/README.md @@ -462,6 +462,8 @@ You can build spigot from source by adding `-e BUILD_FROM_SOURCE=true` If you have attached a host directory to the `/data` volume, then you can install plugins within the `plugins` subdirectory. You can also [attach a `/plugins` volume](#deploying-plugins-from-attached-volume). If you add plugins while the container is running, you'll need to restart it to pick those up. +[You can also auto-download plugins using `SPIGET_RESOURCES`.](#auto-downloading-spigotmcbukkitpapermc-plugins) + > NOTE some of the `VERSION` values are not as intuitive as you would think, so make sure to click into the version entry to find the **exact** version needed for the download. For example, "1.8" is not sufficient since their download naming expects `1.8-R0.1-SNAPSHOT-latest` exactly. ## Running a Paper server @@ -484,6 +486,8 @@ An example compose file is provided at If you have attached a host directory to the `/data` volume, then you can install plugins via the `plugins` subdirectory. You can also [attach a `/plugins` volume](#deploying-plugins-from-attached-volume). If you add plugins while the container is running, you'll need to restart it to pick those up. +[You can also auto-download plugins using `SPIGET_RESOURCES`.](#auto-downloading-spigotmcbukkitpapermc-plugins) + ## Running a Tuinity server A [Tuinity](https://github.com/Spottedleaf/Tuinity) server, which is a fork of Paper aimed at improving server performance at high playercounts. @@ -705,6 +709,19 @@ There is one additional volume that can be mounted; `/plugins`. Any files in thi This works well if you want to have a common set of plugins in a separate location, but still have multiple worlds with different server requirements in either persistent volumes or a downloadable archive. +## Auto-downloading SpigotMC/Bukkit/PaperMC plugins + +The `SPIGET_RESOURCES` variable can be set with a comma-separated list of SpigotMC resource IDs to automatically download [SpigotMC resources/plugins](https://www.spigotmc.org/resources/) using [the spiget API](https://spiget.org/). Resources that are zip files will be expanded into the plugins directory and resources that are simply jar files will be moved there. + +The **resource ID** can be located from the numerical part of the URL after the shortname and a dot. For example, the ID is **9089** from + + https://www.spigotmc.org/resources/essentialsx.9089/ + ==== + +For example, the following will auto-download the [EssentialsX](https://www.spigotmc.org/resources/essentialsx.9089/) and [Vault](https://www.spigotmc.org/resources/vault.34315/) plugins: + + -e SPIGET_RESOURCES=9089,34315 + ## Running with a custom server JAR If you would like to run a custom server JAR, set `-e TYPE=CUSTOM` and pass the custom server diff --git a/start-deployBukkitSpigot b/start-deployBukkitSpigot index 023d87b2..0047730d 100644 --- a/start-deployBukkitSpigot +++ b/start-deployBukkitSpigot @@ -125,5 +125,4 @@ fi export TYPE=SPIGOT export SKIP_LOG4J_CONFIG=true -# Continue to Final Setup -exec ${SCRIPTS:-/}start-finalSetupWorld $@ +exec ${SCRIPTS:-/}start-spiget "$@" diff --git a/start-deployPaper b/start-deployPaper index 1427bb7e..e221eb58 100644 --- a/start-deployPaper +++ b/start-deployPaper @@ -76,5 +76,4 @@ fi export TYPE=SPIGOT export SKIP_LOG4J_CONFIG=true -# Continue to Final Setup -exec ${SCRIPTS:-/}start-finalSetupWorld "$@" +exec ${SCRIPTS:-/}start-spiget "$@" diff --git a/start-spiget b/start-spiget new file mode 100644 index 00000000..c3ca5f41 --- /dev/null +++ b/start-spiget @@ -0,0 +1,58 @@ +#!/bin/bash +set -euo pipefail +IFS=$'\n\t' + +. ${SCRIPTS:-/}start-utils +handleDebugMode + +: ${SPIGET_RESOURCES:=} + +containsJars() { + file=${1?} + + pat='\.jar$' + + while read -r line; do + if [[ $line =~ $pat ]]; then + return 0 + fi + done <<< $(unzip -l "$file") + + return 1 +} + +getResourceFromSpiget() { + resource=${1?} + + log "Downloading resource ${resource} ..." + + tmpfile="/tmp/${resource}.zip" + url="https://api.spiget.org/v2/resources/${resource}/download" + if ! curl -o "${tmpfile}" -fsSL -H "User-Agent: itzg/minecraft-server" "${extraCurlArgs[@]}" "${url}"; then + log "ERROR failed to download resource '${resource}' from ${url}" + exit 2 + fi + + mkdir -p /data/plugins + if containsJars "${tmpfile}"; then + log "Extracting contents of resource ${resource} into plugins" + unzip -o -q -d /data/plugins "${tmpfile}" + rm "${tmpfile}" + else + log "Moving resource ${resource} into plugins" + mv "${tmpfile}" "/data/plugins/${resource}.jar" + fi + +} + +if [[ ${SPIGET_RESOURCES} ]]; then + log "Getting plugins via Spiget" + IFS=',' read -r -a resources <<< "${SPIGET_RESOURCES}" + for resource in "${resources[@]}" + do + getResourceFromSpiget "${resource}" + done +fi + +# Continue to Final Setup +exec ${SCRIPTS:-/}start-finalSetupWorld $@ diff --git a/start-utils b/start-utils index 51c70d64..05f569ce 100644 --- a/start-utils +++ b/start-utils @@ -57,13 +57,20 @@ function isTrue() { } function isDebugging() { - if [[ -v DEBUG ]] && [[ ${DEBUG^^} == TRUE ]]; then + if isTrue "${DEBUG:-false}"; then return 0 else return 1 fi } +function handleDebugMode() { + if isDebugging; then + set -x + extraCurlArgs=(-v) + fi +} + function debug() { if isDebugging; then log "DEBUG: $*"