Selaa lähdekoodia

feat(mcserver): add server variants with Fabric, PaperMC, Purpur, Folia and Leaf (#5234)

Adds server variants: one server script can install and switch between
different builds of the same game. mcserver gains Fabric, PaperMC, Purpur,
Folia and Leaf as variants of vanilla.

* New variant setting and check_variant.sh module, reusable by other games
* --variant and --version options on install, auto-install, update and force-update
* Interactive install prompts for variant and version
* Variant switching on update with warnings, recorded in serverfiles/variant.txt
* details and check-update show the variant
* Leaf from GitHub releases, with its loader auto-update disabled
* pmcserver unchanged

Fixes #4377
Daniel Gibbs 1 päivä sitten
vanhempi
sitoutus
6b493a13e6

+ 2 - 0
lgsm/config-default/config-lgsm/mcserver/_default.cfg

@@ -20,6 +20,8 @@ startparameters="nogui"
 extraparams=""
 
 ## Release Settings | https://docs.linuxgsm.com/game-servers/minecraft#release-settings
+# Variant (vanilla|fabric|paper|purpur|folia|leaf)
+variant="vanilla"
 # Branch (release|snapshot)
 branch="release"
 # Version (latest|1.16)

+ 11 - 7
lgsm/modules/check_deps.sh

@@ -308,6 +308,10 @@ fn_deps_detector() {
 		return
 	# Java: Added for users using Oracle JRE to bypass check.
 	elif [[ ${deptocheck} == "openjdk"* ]] || [[ ${deptocheck} == "java"* ]]; then
+		mcpaper=0
+		if [ "${shortname}" == "pmc" ] || [[ "${variant}" =~ ^(paper|purpur|folia|leaf)$ ]]; then
+			mcpaper=1
+		fi
 		# Is java already installed?
 		if [ -n "${javaversion}" ]; then
 			javamajorversion="$(echo "${javaversion}" | grep -Eo 'version "[0-9]+' | grep -Eo '[0-9]+' | head -1)"
@@ -318,17 +322,17 @@ fn_deps_detector() {
 				elif [[ "${mcversion}" =~ ^[0-9]+\.[0-9]+([.][0-9]+)?$ ]]; then
 					if fn_deps_version_ge "${mcversion}" "26.1"; then
 						javarequiredmajor="25"
-					elif [ "${shortname}" == "mc" ] && fn_deps_version_ge "${mcversion}" "1.20.5"; then
+					elif [ "${mcpaper}" != "1" ] && fn_deps_version_ge "${mcversion}" "1.20.5"; then
 						javarequiredmajor="21"
-					elif [ "${shortname}" == "pmc" ] && fn_deps_version_ge "${mcversion}" "1.20" && fn_deps_version_ge "1.21.11" "${mcversion}"; then
+					elif [ "${mcpaper}" == "1" ] && fn_deps_version_ge "${mcversion}" "1.20" && fn_deps_version_ge "1.21.11" "${mcversion}"; then
 						javarequiredmajor="21"
-					elif [ "${shortname}" == "pmc" ] && fn_deps_version_ge "${mcversion}" "1.17" && fn_deps_version_ge "1.19" "${mcversion}"; then
+					elif [ "${mcpaper}" == "1" ] && fn_deps_version_ge "${mcversion}" "1.17" && fn_deps_version_ge "1.19" "${mcversion}"; then
 						javarequiredmajor="17"
-					elif [ "${shortname}" == "pmc" ] && [ "${mcversion}" == "1.16.5" ]; then
+					elif [ "${mcpaper}" == "1" ] && [ "${mcversion}" == "1.16.5" ]; then
 						javarequiredmajor="16"
-					elif [ "${shortname}" == "pmc" ] && fn_deps_version_ge "${mcversion}" "1.12" && ! fn_deps_version_ge "${mcversion}" "1.16.5"; then
+					elif [ "${mcpaper}" == "1" ] && fn_deps_version_ge "${mcversion}" "1.12" && ! fn_deps_version_ge "${mcversion}" "1.16.5"; then
 						javarequiredmajor="11"
-					elif [ "${shortname}" == "pmc" ] && ! fn_deps_version_ge "${mcversion}" "1.16.5"; then
+					elif [ "${mcpaper}" == "1" ] && ! fn_deps_version_ge "${mcversion}" "1.16.5"; then
 						javarequiredmajor="8"
 					fi
 				fi
@@ -367,7 +371,7 @@ fn_deps_detector() {
 			if [ "${shortname}" == "mc" ] || [ "${shortname}" == "pmc" ]; then
 				if [ "${mcversion}" == "latest" ] || fn_deps_version_ge "${mcversion}" "26.1"; then
 					javarequiredmajor="25"
-				elif { [ "${shortname}" == "mc" ] && fn_deps_version_ge "${mcversion}" "1.20.5"; } || { [ "${shortname}" == "pmc" ] && fn_deps_version_ge "${mcversion}" "1.20" && fn_deps_version_ge "1.21.11" "${mcversion}"; }; then
+				elif { [ "${mcpaper}" != "1" ] && fn_deps_version_ge "${mcversion}" "1.20.5"; } || { [ "${mcpaper}" == "1" ] && fn_deps_version_ge "${mcversion}" "1.20" && fn_deps_version_ge "1.21.11" "${mcversion}"; }; then
 					javarequiredmajor="21"
 				fi
 			fi

+ 2 - 2
lgsm/modules/check_system_requirements.sh

@@ -35,10 +35,10 @@ elif [ "${shortname}" == "hw" ]; then
 	ramrequirementgb="3"
 elif [ "${shortname}" == "hyt" ]; then
 	ramrequirementgb="4"
+elif [ "${shortname}" == "pmc" ] || { [ "${shortname}" == "mc" ] && [[ "${variant}" =~ ^(paper|purpur|folia|leaf)$ ]]; }; then
+	ramrequirementgb="2"
 elif [ "${shortname}" == "mc" ]; then
 	ramrequirementgb="1"
-elif [ "${shortname}" == "pmc" ]; then
-	ramrequirementgb="2"
 elif [ "${shortname}" == "mh" ]; then
 	ramrequirementgb="4"
 elif [ "${shortname}" == "ns2" ] || [ "${shortname}" == "ns2c" ]; then

+ 264 - 0
lgsm/modules/check_variant.sh

@@ -0,0 +1,264 @@
+#!/bin/bash
+# LinuxGSM check_variant.sh module
+# Author: Daniel Gibbs
+# Contributors: https://linuxgsm.com/contrib
+# Website: https://linuxgsm.com
+# Description: Handles server variants and the --variant and --version options.
+
+moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
+
+# Variants available for this game server. The first is the default.
+fn_variant_list() {
+	variants=()
+	variantnames=()
+	if [ "${shortname}" == "mc" ]; then
+		variants=("vanilla" "fabric" "paper" "purpur" "folia" "leaf")
+		variantnames=("Vanilla" "Fabric" "PaperMC" "Purpur" "Folia" "Leaf")
+	fi
+}
+
+# Settings that change with the variant.
+fn_variant_settings() {
+	if [ "${shortname}" == "mc" ]; then
+		if [ "${variant}" == "vanilla" ]; then
+			variantexecutable="./minecraft_server.jar"
+		elif [ "${variant}" == "fabric" ]; then
+			variantexecutable="./fabric-server-launch.jar"
+		elif [ "${variant}" == "paper" ]; then
+			variantexecutable="./paper.jar"
+		elif [ "${variant}" == "purpur" ]; then
+			variantexecutable="./purpur.jar"
+		elif [ "${variant}" == "folia" ]; then
+			variantexecutable="./folia.jar"
+		elif [ "${variant}" == "leaf" ]; then
+			variantexecutable="./leaf.jar"
+		fi
+	fi
+	if [ "${variant}" != "${variants[0]}" ] && [ -n "${variantexecutable}" ]; then
+		executable="${variantexecutable}"
+	fi
+}
+
+# The config setting that --version changes.
+fn_variant_versionvar() {
+	versionvar=""
+	if [ "${shortname}" == "mc" ] || [ "${shortname}" == "pmc" ]; then
+		versionvar="mcversion"
+	fi
+}
+
+# Writes a setting to the config that already sets it, otherwise to common.cfg.
+fn_variant_set_config() {
+	local key="${1}"
+	local value="${2}"
+	local cfgfile=""
+	local checkcfg
+	for checkcfg in "${configdirserver}/secrets-${selfname}.cfg" "${configdirserver}/${selfname}.cfg" "${configdirserver}/secrets-common.cfg"; do
+		if [ -f "${checkcfg}" ] && grep -qE "^[[:blank:]]*${key}=" "${checkcfg}"; then
+			cfgfile="${checkcfg}"
+			break
+		fi
+	done
+	if [ -z "${cfgfile}" ]; then
+		cfgfile="${configdirserver}/common.cfg"
+	fi
+	if grep -qE "^[[:blank:]]*${key}=" "${cfgfile}"; then
+		sed -i -E "s|^[[:blank:]]*${key}=.*|${key}=\"${value}\"|" "${cfgfile}"
+	else
+		if [ -s "${cfgfile}" ] && [ -n "$(tail -c 1 "${cfgfile}")" ]; then
+			echo "" >> "${cfgfile}"
+		fi
+		echo "${key}=\"${value}\"" >> "${cfgfile}"
+	fi
+	printf -v "${key}" '%s' "${value}"
+	fn_print_ok_nl "Setting ${key} to ${value} in ${cfgfile##*/}"
+	fn_script_log_pass "Setting ${key} to ${value} in ${cfgfile}"
+}
+
+fn_variant_parse_args() {
+	local argcommands=(i install ai auto-install u update fu force-update)
+	local argcommand
+	local acceptargs=0
+	local index
+	local arg
+	for argcommand in "${argcommands[@]}"; do
+		if [ "${getopt}" == "${argcommand}" ]; then
+			acceptargs=1
+		fi
+	done
+	if [ "${acceptargs}" != "1" ]; then
+		return
+	fi
+	# Scripts installed before options were supported do not pass them on.
+	if ! declare -p lgsmargs > /dev/null 2>&1; then
+		if [[ "${userinput2}" == --* ]]; then
+			fn_print_fail_nl "Options need a newer ${selfname} script: run ./${selfname} update-lgsm first"
+			fn_script_log_fail "Options need a newer ${selfname} script"
+			core_exit.sh
+		fi
+		return
+	fi
+	for ((index = 1; index < ${#lgsmargs[@]}; index++)); do
+		arg="${lgsmargs[index]}"
+		if [[ "${arg}" == --variant=* ]]; then
+			argvariant="${arg#*=}"
+		elif [ "${arg}" == "--variant" ]; then
+			((index++))
+			argvariant="${lgsmargs[index]}"
+		elif [[ "${arg}" == --version=* ]]; then
+			argversion="${arg#*=}"
+		elif [ "${arg}" == "--version" ]; then
+			((index++))
+			argversion="${lgsmargs[index]}"
+		else
+			fn_print_fail_nl "Unknown option: ${arg}"
+			fn_script_log_fail "Unknown option: ${arg}"
+			core_exit.sh
+		fi
+	done
+}
+
+fn_variant_prompt() {
+	local answer
+	local checkcfg
+	local variantindex
+	if [ "${getopt}" != "i" ] && [ "${getopt}" != "install" ]; then
+		return
+	fi
+	if [ "${#variants[@]}" -lt 2 ] || [ -n "${argvariant+x}" ]; then
+		return
+	fi
+	for checkcfg in "${configdirserver}/secrets-${selfname}.cfg" "${configdirserver}/${selfname}.cfg" "${configdirserver}/secrets-common.cfg" "${configdirserver}/common.cfg"; do
+		if [ -f "${checkcfg}" ] && grep -qE "^[[:blank:]]*variant=" "${checkcfg}"; then
+			return
+		fi
+	done
+	echo -e ""
+	echo -e "${bold}${lightyellow}Select ${gamename} Server Variant${default}"
+	fn_messages_separator
+	for variantindex in "${!variants[@]}"; do
+		if [ "${variantindex}" -eq 0 ]; then
+			echo -e "$((variantindex + 1))) ${variantnames[variantindex]} (default)"
+		else
+			echo -e "$((variantindex + 1))) ${variantnames[variantindex]}"
+		fi
+	done
+	while true; do
+		answer=""
+		if ! read -e -p "Variant [1]: " -r answer && [ -z "${answer}" ]; then
+			echo -e ""
+		fi
+		answer="${answer:-1}"
+		if [[ "${answer}" =~ ^[0-9]+$ ]] && [ "${answer}" -ge 1 ] && [ "${answer}" -le "${#variants[@]}" ]; then
+			argvariant="${variants[answer - 1]}"
+			break
+		elif [[ " ${variants[*]} " == *" ${answer} "* ]]; then
+			argvariant="${answer}"
+			break
+		fi
+		fn_print_warning_nl "Enter a number from 1 to ${#variants[@]}"
+	done
+	fn_variant_versionvar
+	if [ -n "${versionvar}" ] && [ -z "${argversion+x}" ]; then
+		while true; do
+			answer=""
+			if ! read -e -p "Version [${!versionvar:-latest}]: " -r answer && [ -z "${answer}" ]; then
+				echo -e ""
+			fi
+			if [ -z "${answer}" ]; then
+				break
+			elif [[ "${answer}" =~ ^[A-Za-z0-9._+-]+$ ]]; then
+				argversion="${answer}"
+				break
+			fi
+			fn_print_warning_nl "Invalid version: ${answer}"
+		done
+	fi
+}
+
+fn_variant_apply_args() {
+	if [ -n "${argvariant+x}" ]; then
+		if [ "${#variants[@]}" -eq 0 ]; then
+			fn_print_fail_nl "${gamename} has no variants"
+			fn_script_log_fail "${gamename} has no variants"
+			core_exit.sh
+		fi
+		if [[ " ${variants[*]} " != *" ${argvariant} "* ]]; then
+			fn_print_fail_nl "Unknown variant: ${argvariant}. Available variants: ${variants[*]}"
+			fn_script_log_fail "Unknown variant: ${argvariant}"
+			core_exit.sh
+		fi
+		fn_variant_set_config "variant" "${argvariant}"
+	fi
+	if [ -n "${argversion+x}" ]; then
+		fn_variant_versionvar
+		if [ -z "${versionvar}" ]; then
+			fn_print_fail_nl "${gamename} does not support --version"
+			fn_script_log_fail "${gamename} does not support --version"
+			core_exit.sh
+		fi
+		if [[ ! "${argversion}" =~ ^[A-Za-z0-9._+-]+$ ]]; then
+			fn_print_fail_nl "Invalid version: ${argversion}"
+			fn_script_log_fail "Invalid version: ${argversion}"
+			core_exit.sh
+		fi
+		fn_variant_set_config "${versionvar}" "${argversion}"
+	fi
+}
+
+# Records the variant now installed in serverfiles.
+fn_variant_record() {
+	if [ -n "${variant}" ] && [ -d "${serverfiles}" ]; then
+		echo "${variant}" > "${serverfiles}/variant.txt"
+		installedvariant="${variant}"
+	fi
+}
+
+# Warns about data that does not carry across a variant change.
+fn_variant_change_warning() {
+	if [ "${shortname}" == "mc" ]; then
+		if [[ "${installedvariant}" =~ ^(paper|purpur|folia|leaf)$ ]] && [[ "${variant}" =~ ^(vanilla|fabric)$ ]]; then
+			fn_print_warning_nl "${installedvariantname} keeps the Nether and End in world_nether and world_the_end. ${variantname} does not load them from there."
+			fn_script_log_warn "${installedvariantname} keeps the Nether and End in world_nether and world_the_end. ${variantname} does not load them from there."
+		fi
+		if [ "${installedvariant}" == "fabric" ] || [ "${variant}" == "fabric" ]; then
+			fn_print_warning_nl "Mods and plugins are not shared between ${installedvariantname} and ${variantname}."
+			fn_script_log_warn "Mods and plugins are not shared between ${installedvariantname} and ${variantname}."
+		fi
+		if [ "${variant}" == "folia" ]; then
+			fn_print_warning_nl "Folia only loads plugins that declare Folia support."
+			fn_script_log_warn "Folia only loads plugins that declare Folia support."
+		fi
+	fi
+	fn_print_info_nl "Take a backup before changing variant: ./${selfname} backup"
+}
+
+fn_variant_list
+fn_variant_parse_args
+fn_variant_prompt
+fn_variant_apply_args
+
+if [ "${#variants[@]}" -gt 0 ]; then
+	if [ -z "${variant}" ]; then
+		variant="${variants[0]}"
+	fi
+	variantname=""
+	installedvariant="$(head -n 1 "${serverfiles}/variant.txt" 2> /dev/null)"
+	installedvariant="${installedvariant:-${variants[0]}}"
+	for variantindex in "${!variants[@]}"; do
+		if [ "${variants[variantindex]}" == "${variant}" ]; then
+			variantname="${variantnames[variantindex]}"
+		fi
+		if [ "${variants[variantindex]}" == "${installedvariant}" ]; then
+			installedvariantname="${variantnames[variantindex]}"
+		fi
+	done
+	if [ -z "${variantname}" ]; then
+		fn_print_fail_nl "Unknown variant: ${variant}. Available variants: ${variants[*]}"
+		fn_script_log_fail "Unknown variant: ${variant}"
+		core_exit.sh
+	fi
+	fn_variant_settings
+else
+	unset variant
+fi

+ 2 - 0
lgsm/modules/core_getopt.sh

@@ -192,6 +192,8 @@ fn_opt_usage() {
 	core_exit.sh
 }
 
+check_variant.sh
+
 # Check if command exists and run corresponding scripts, or display script usage.
 if [ -z "${getopt}" ]; then
 	fn_opt_usage

+ 5 - 0
lgsm/modules/core_modules.sh

@@ -295,6 +295,11 @@ check_version.sh() {
 	fn_fetch_module
 }
 
+check_variant.sh() {
+	modulefile="${FUNCNAME[0]}"
+	fn_fetch_module
+}
+
 # Compress
 
 compress_unreal2_maps.sh() {

+ 5 - 0
lgsm/modules/info_messages.sh

@@ -311,6 +311,11 @@ fn_info_messages_gameserver() {
 			echo -e "${lightblue}App ID:\t${default}${appid}"
 		fi
 
+		# Variant
+		if [ -n "${variantname}" ]; then
+			echo -e "${lightblue}Variant:\t${default}${variantname}"
+		fi
+
 		# Branch
 		if [ -n "${branch}" ]; then
 			echo -e "${lightblue}Branch:\t${default}${branch}"

+ 12 - 1
lgsm/modules/install_server_files.sh

@@ -240,8 +240,19 @@ if [ "${shortname}" == "mc" ] || [ "${shortname}" == "pmc" ] || [ "${shortname}"
 fi
 
 echo -e ""
-echo -e "${bold}${lightyellow}Installing ${gamename} Server${default}"
+if [ -n "${variantname}" ]; then
+	echo -e "${bold}${lightyellow}Installing ${gamename} Server (${variantname})${default}"
+else
+	echo -e "${bold}${lightyellow}Installing ${gamename} Server${default}"
+fi
 fn_messages_separator
+if [ -n "${variantname}" ] && [ "${#variants[@]}" -gt 1 ] && [ -z "${argvariant+x}" ]; then
+	if [ "${autoinstall}" == "1" ]; then
+		echo -e "Variants available: ${variants[*]}. Choose one with: ./${selfname} auto-install --variant <variant>"
+	else
+		echo -e "Variants available: ${variants[*]}. Choose one with: ./${selfname} install --variant <variant>"
+	fi
+fi
 
 if [ "${appid}" ]; then
 	remotelocation="SteamCMD"

+ 124 - 11
lgsm/modules/update_mc.sh

@@ -9,18 +9,32 @@ moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
 
 fn_update_dl() {
 	# Download and extract files to serverfiles.
-	fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "chmodx" "norun" "noforce" "nohash"
-	cp -f "${tmpdir}/${remotebuildfilename}" "${serverfiles}/minecraft_server.jar"
+	fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "chmodx" "norun" "noforce" "${remotebuildhash:-nohash}"
+	cp -f "${tmpdir}/${remotebuildfilename}" "${serverfiles}/${variantexecutable#./}"
+	if [ "${variant}" != "vanilla" ]; then
+		echo "${remotebuild}" > "${serverfiles}/build.txt"
+	fi
+	if [ "${variant}" == "leaf" ] && [ ! -f "${serverfiles}/config/leaf-loader.yml" ]; then
+		mkdir -p "${serverfiles}/config"
+		printf '%s\n' "auto-update:" "  enabled: false" > "${serverfiles}/config/leaf-loader.yml"
+	fi
+	fn_variant_record
 	fn_clear_tmp
 }
 
 fn_update_localbuild() {
 	# Gets local build info.
 	fn_print_dots "Checking local build: ${remotelocation}"
-	# Uses executable to get local build.
-	if [ -d "${executabledir}" ]; then
-		cd "${executabledir}" || exit
-		localbuild=$(unzip -p "minecraft_server.jar" version.json | jq -r '.id')
+	if [ "${installedvariant}" != "${variant}" ]; then
+		localbuild="${installedvariant}"
+	elif [ "${variant}" == "vanilla" ]; then
+		# Uses executable to get local build.
+		if [ -d "${executabledir}" ]; then
+			cd "${executabledir}" || exit
+			localbuild=$(unzip -p "minecraft_server.jar" version.json | jq -r '.id')
+		fi
+	else
+		localbuild=$(head -n 1 "${serverfiles}/build.txt" 2> /dev/null)
 	fi
 	if [ -z "${localbuild}" ]; then
 		fn_print_error "Checking local build: ${remotelocation}: missing local build info"
@@ -33,8 +47,7 @@ fn_update_localbuild() {
 	fi
 }
 
-fn_update_remotebuild() {
-	# Gets remote build info.
+fn_update_remotebuild_vanilla() {
 	apiurl="https://launchermeta.mojang.com/mc/game/version_manifest.json"
 	remotebuildresponse=$(curl -s "${apiurl}")
 	# Latest release.
@@ -51,7 +64,83 @@ fn_update_remotebuild() {
 	# Generate link to version manifest json.
 	remotebuildmanifest=$(echo "${remotebuildresponse}" | jq -r --arg branch "${branch}" --arg mcversion "${remotebuild}" '.versions | .[] | select(.type==$branch and .id==$mcversion) | .url')
 	# Generate link to server.jar
-	remotebuildurl=$(curl -s "${remotebuildmanifest}" | jq -r '.downloads.server.url')
+	remotebuildmanifestresponse=$(curl -s "${remotebuildmanifest}")
+	remotebuildurl=$(echo "${remotebuildmanifestresponse}" | jq -r '.downloads.server.url')
+	remotebuildhash=$(echo "${remotebuildmanifestresponse}" | jq -r '.downloads.server.sha1 // "nohash"')
+}
+
+fn_update_remotebuild_fabric() {
+	apiurl="https://meta.fabricmc.net/v2/versions"
+	remotebuildresponse=$(curl -s "${apiurl}/game")
+	if [ "${mcversion}" == "latest" ]; then
+		if [ "${branch}" == "snapshot" ]; then
+			remotebuildmcversion=$(echo "${remotebuildresponse}" | jq -r '.[0].version')
+		else
+			remotebuildmcversion=$(echo "${remotebuildresponse}" | jq -r '[.[] | select(.stable)][0].version')
+		fi
+	else
+		remotebuildmcversion=$(echo "${remotebuildresponse}" | jq -r --arg mcversion "${mcversion}" '.[] | select(.version==$mcversion) | .version')
+	fi
+	remotebuildloader=$(curl -s "${apiurl}/loader" | jq -r '[.[] | select(.stable)][0].version')
+	remotebuildinstaller=$(curl -s "${apiurl}/installer" | jq -r '[.[] | select(.stable)][0].version')
+	if [ -n "${remotebuildmcversion}" ] && [ "${remotebuildmcversion}" != "null" ] && [ "${remotebuildloader}" != "null" ] && [ "${remotebuildinstaller}" != "null" ]; then
+		# Combines Minecraft: Java Edition version and Fabric loader. e.g 1.21.10-0.17.3
+		remotebuild="${remotebuildmcversion}-${remotebuildloader}"
+		remotebuildfilename="fabric-server-mc.${remotebuildmcversion}-loader.${remotebuildloader}-launcher.${remotebuildinstaller}.jar"
+		remotebuildurl="${apiurl}/loader/${remotebuildmcversion}/${remotebuildloader}/${remotebuildinstaller}/server/jar"
+	fi
+	remotebuildhash="nohash"
+}
+
+fn_update_remotebuild_purpur() {
+	apiurl="https://api.purpurmc.org/v2/purpur"
+	remotebuildresponse=$(curl -s "${apiurl}")
+	if [ "${mcversion}" == "latest" ]; then
+		remotebuildmcversion=$(echo "${remotebuildresponse}" | jq -r '.versions[-1]')
+	else
+		remotebuildmcversion=$(echo "${remotebuildresponse}" | jq -r --arg mcversion "${mcversion}" '.versions[] | select(.==$mcversion)')
+	fi
+	if [ -n "${remotebuildmcversion}" ] && [ "${remotebuildmcversion}" != "null" ]; then
+		remotebuildpurpurbuild=$(curl -s "${apiurl}/${remotebuildmcversion}" | jq -r '.builds.latest')
+		remotebuildhash=$(curl -s "${apiurl}/${remotebuildmcversion}/${remotebuildpurpurbuild}" | jq -r '.md5 // "nohash"')
+		# Combines Minecraft: Java Edition version and Purpur build. e.g 1.21.10-2535
+		remotebuild="${remotebuildmcversion}-${remotebuildpurpurbuild}"
+		remotebuildfilename="purpur-${remotebuild}.jar"
+		remotebuildurl="${apiurl}/${remotebuildmcversion}/${remotebuildpurpurbuild}/download"
+	fi
+}
+
+fn_update_remotebuild_leaf() {
+	apiurl="https://api.github.com/repos/Winds-Studio/Leaf/releases"
+	remotebuildresponse=$(curl -s "${apiurl}?per_page=100")
+	if [ "${mcversion}" == "latest" ]; then
+		remotebuildmcversion=$(echo "${remotebuildresponse}" | jq -r '.[] | select((.prerelease | not) and (.tag_name | startswith("ver-"))) | .tag_name | sub("^ver-"; "")' | sort -V | tail -n 1)
+	else
+		remotebuildmcversion="${mcversion}"
+	fi
+	remotebuildrelease=$(echo "${remotebuildresponse}" | jq -c --arg tag "ver-${remotebuildmcversion}" 'first(.[] | select(.tag_name==$tag)) | first(.assets[] | select(.name | test("^leaf-.*\\.jar$")))')
+	if [ -n "${remotebuildrelease}" ] && [ "${remotebuildrelease}" != "null" ]; then
+		remotebuildfilename=$(echo "${remotebuildrelease}" | jq -r '.name')
+		remotebuildurl=$(echo "${remotebuildrelease}" | jq -r '.browser_download_url')
+		remotebuildhash=$(echo "${remotebuildrelease}" | jq -r '.digest // "nohash" | sub("^sha256:"; "")')
+		# Combines Minecraft: Java Edition version and Leaf build. e.g 26.2-131
+		remotebuild="${remotebuildfilename#leaf-}"
+		remotebuild="${remotebuild%.jar}"
+	fi
+}
+
+fn_update_remotebuild() {
+	# Gets remote build info.
+	unset remotebuild remotebuildurl remotebuildhash
+	if [ "${variant}" == "fabric" ]; then
+		fn_update_remotebuild_fabric
+	elif [ "${variant}" == "purpur" ]; then
+		fn_update_remotebuild_purpur
+	elif [ "${variant}" == "leaf" ]; then
+		fn_update_remotebuild_leaf
+	else
+		fn_update_remotebuild_vanilla
+	fi
 
 	if [ "${firstcommandname}" != "INSTALL" ]; then
 		fn_print_dots "Checking remote build: ${remotelocation}"
@@ -77,7 +166,7 @@ fn_update_remotebuild() {
 fn_update_compare() {
 	fn_print_dots "Checking for update: ${remotelocation}"
 	# Update has been found or force update.
-	if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
+	if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ] || [ "${installedvariant}" != "${variant}" ]; then
 		# Create update lockfile.
 		date '+%s' > "${lockdir:?}/update.lock"
 		fn_print_ok_nl "Checking for update: ${remotelocation}"
@@ -85,6 +174,11 @@ fn_update_compare() {
 		fn_print_nl "${bold}${underline}Update${default} available"
 		fn_print_nl "* Local build: ${red}${localbuild}${default}"
 		fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
+		if [ "${installedvariant}" != "${variant}" ]; then
+			fn_print_nl "* Variant: ${red}${installedvariantname}${default} > ${green}${variantname}${default}"
+		else
+			fn_print_nl "* Variant: ${variantname}"
+		fi
 		if [ -n "${branch}" ]; then
 			fn_print_nl "* Branch: ${branch}"
 		fi
@@ -99,10 +193,14 @@ fn_update_compare() {
 		fn_script_log_info "Update available"
 		fn_script_log_info "Local build: ${localbuild}"
 		fn_script_log_info "Remote build: ${remotebuild}"
+		fn_script_log_info "Variant: ${installedvariant} > ${variant}"
 		if [ -n "${branch}" ]; then
 			fn_script_log_info "Branch: ${branch}"
 		fi
 		fn_script_log_info "${localbuild} > ${remotebuild}"
+		if [ "${installedvariant}" != "${variant}" ]; then
+			fn_variant_change_warning
+		fi
 
 		if [ "${commandname}" == "UPDATE" ]; then
 			unset updateonstart
@@ -128,6 +226,7 @@ fn_update_compare() {
 		fn_print_nl "${bold}${underline}No update${default} available"
 		fn_print_nl "* Local build: ${green}${localbuild}${default}"
 		fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
+		fn_print_nl "* Variant: ${variantname}"
 		if [ -n "${branch}" ]; then
 			fn_print_nl "* Branch: ${branch}"
 		fi
@@ -135,6 +234,7 @@ fn_update_compare() {
 		fn_script_log_info "No update available"
 		fn_script_log_info "Local build: ${localbuild}"
 		fn_script_log_info "Remote build: ${remotebuild}"
+		fn_script_log_info "Variant: ${variant}"
 		if [ -n "${branch}" ]; then
 			fn_script_log_info "Branch: ${branch}"
 		fi
@@ -148,8 +248,21 @@ fn_update_compare() {
 	fi
 }
 
+if [ "${variant}" == "paper" ] || [ "${variant}" == "folia" ]; then
+	update_pmc.sh
+	return
+fi
+
 # The location where the builds are checked and downloaded.
-remotelocation="mojang.com"
+if [ "${variant}" == "fabric" ]; then
+	remotelocation="fabricmc.net"
+elif [ "${variant}" == "purpur" ]; then
+	remotelocation="purpurmc.org"
+elif [ "${variant}" == "leaf" ]; then
+	remotelocation="github.com"
+else
+	remotelocation="mojang.com"
+fi
 
 if [ "${firstcommandname}" == "INSTALL" ]; then
 	fn_update_remotebuild

+ 28 - 2
lgsm/modules/update_pmc.sh

@@ -12,6 +12,9 @@ fn_update_dl() {
 	fn_fetch_file "${remotebuildurl}" "" "" "" "${tmpdir}" "${remotebuildfilename}" "chmodx" "norun" "forcedl" "${remotebuildhash}"
 	cp -f "${tmpdir}/${remotebuildfilename}" "${serverfiles}/${executable#./}"
 	echo "${remotebuild}" > "${serverfiles}/build.txt"
+	if [ -n "${variant}" ]; then
+		fn_variant_record
+	fi
 	fn_clear_tmp
 }
 
@@ -19,7 +22,11 @@ fn_update_localbuild() {
 	# Gets local build info.
 	fn_print_dots "Checking local build: ${remotelocation}"
 	# Uses build file to get local build.
-	localbuild=$(head -n 1 "${serverfiles}/build.txt" 2> /dev/null)
+	if [ -n "${variant}" ] && [ "${installedvariant}" != "${variant}" ]; then
+		localbuild="${installedvariant}"
+	else
+		localbuild=$(head -n 1 "${serverfiles}/build.txt" 2> /dev/null)
+	fi
 	if [ -z "${localbuild}" ]; then
 		fn_print_error "Checking local build: ${remotelocation}: missing local build info"
 		fn_script_log_error "Missing local build info"
@@ -88,7 +95,7 @@ fn_update_remotebuild() {
 fn_update_compare() {
 	fn_print_dots "Checking for update: ${remotelocation}"
 	# Update has been found or force update.
-	if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ]; then
+	if [ "${localbuild}" != "${remotebuild}" ] || [ "${forceupdate}" == "1" ] || [ "${installedvariant}" != "${variant}" ]; then
 		# Create update lockfile.
 		date '+%s' > "${lockdir:?}/update.lock"
 		fn_print_ok_nl "Checking for update: ${remotelocation}"
@@ -96,6 +103,11 @@ fn_update_compare() {
 		fn_print_nl "${bold}${underline}Update${default} available"
 		fn_print_nl "* Local build: ${red}${localbuild}${default}"
 		fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
+		if [ -n "${variant}" ] && [ "${installedvariant}" != "${variant}" ]; then
+			fn_print_nl "* Variant: ${red}${installedvariantname}${default} > ${green}${variantname}${default}"
+		elif [ -n "${variant}" ]; then
+			fn_print_nl "* Variant: ${variantname}"
+		fi
 		if [ -n "${branch}" ]; then
 			fn_print_nl "* Branch: ${branch}"
 		fi
@@ -114,6 +126,10 @@ fn_update_compare() {
 			fn_script_log_info "Branch: ${branch}"
 		fi
 		fn_script_log_info "${localbuild} > ${remotebuild}"
+		if [ -n "${variant}" ] && [ "${installedvariant}" != "${variant}" ]; then
+			fn_script_log_info "Variant: ${installedvariant} > ${variant}"
+			fn_variant_change_warning
+		fi
 
 		if [ "${commandname}" == "UPDATE" ]; then
 			unset updateonstart
@@ -139,6 +155,9 @@ fn_update_compare() {
 		fn_print_nl "${bold}${underline}No update${default} available"
 		fn_print_nl "* Local build: ${green}${localbuild}${default}"
 		fn_print_nl "* Remote build: ${green}${remotebuild}${default}"
+		if [ -n "${variant}" ]; then
+			fn_print_nl "* Variant: ${variantname}"
+		fi
 		if [ -n "${branch}" ]; then
 			fn_print_nl "* Branch: ${branch}"
 		fi
@@ -146,6 +165,9 @@ fn_update_compare() {
 		fn_script_log_info "No update available"
 		fn_script_log_info "Local build: ${localbuild}"
 		fn_script_log_info "Remote build: ${remotebuild}"
+		if [ -n "${variant}" ]; then
+			fn_script_log_info "Variant: ${variant}"
+		fi
 		if [ -n "${branch}" ]; then
 			fn_script_log_info "Branch: ${branch}"
 		fi
@@ -164,6 +186,10 @@ remotelocation="papermc.io"
 
 if [ "${shortname}" == "pmc" ]; then
 	paperproject="paper"
+elif [ "${shortname}" == "mc" ] && [ "${variant}" == "folia" ]; then
+	paperproject="folia"
+elif [ "${shortname}" == "mc" ]; then
+	paperproject="paper"
 elif [ "${shortname}" == "vpmc" ]; then
 	paperproject="velocity"
 elif [ "${shortname}" == "wmc" ]; then

+ 1 - 0
linuxgsm.sh

@@ -49,6 +49,7 @@ configdirserver="${configdir}/${gameservername}"
 configdirdefault="${lgsmdir}/config-default"
 userinput="${1}"
 userinput2="${2}"
+lgsmargs=("${@}")
 
 ## GitHub Branch Select
 # Allows for the use of different function files