From c84d8c7d4406131e8d31c35aad28bee0dfcbcdd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20=C5=A0tefka?= Date: Mon, 27 Feb 2023 05:39:44 +0100 Subject: [PATCH] Add initial support for Fabric --- .msman/detect_server.sh | 29 +++++++++- .msman/fabric.sh | 117 ++++++++++++++++++++++++++++++++++++++++ .msman/version.sh | 2 +- msman.cfg | 6 ++- msman.sh | 11 ++-- 5 files changed, 155 insertions(+), 10 deletions(-) create mode 100644 .msman/fabric.sh diff --git a/.msman/detect_server.sh b/.msman/detect_server.sh index 7b6cac5..cda61f9 100644 --- a/.msman/detect_server.sh +++ b/.msman/detect_server.sh @@ -5,9 +5,10 @@ function get_existing_server { if ls paper-*.jar 1> /dev/null 2>&1; then existing_server_type="paper" get_existing_paper + elif ls fabric-server-mc.*.jar 1> /dev/null 2>&1; then + existing_server_type="fabric" + get_existing_fabric # TODO: Add support for other server types - # elif ls fabric-*.jar 1> /dev/null 2>&1; then - # existing_server_type="fabric" # elif ls spigot-*.jar 1> /dev/null 2>&1; then # existing_server_type="spigot" # elif ls craftbukkit-*.jar 1> /dev/null 2>&1; then @@ -30,6 +31,30 @@ function get_existing_server { fi } +# Get existing version and build of fabric +function get_existing_fabric { + # Get the current server file name + server_file=$(basename ./fabric-server-mc.*.jar) + + # Assign the file name to a variable + FILE=$server_file + + # Remove the .jar extension + FILE=${FILE%.jar} + + # Split by - and get the third field (mc.x.x.x) + current_version=$(echo $FILE | cut -d. -f2,3,4 | cut -d- -f1) + + # Split by - and get the fourth field (launcher.x.x.x) + current_build=$(echo $FILE | cut -d- -f4 | cut -d. -f2,3,4) + + echo "Current server file: $server_file" + echo " - Version $current_version" + echo " - Build $current_build" + echo + echo +} + # Get existing version and build of paper function get_existing_paper { # Get the current server file name diff --git a/.msman/fabric.sh b/.msman/fabric.sh new file mode 100644 index 0000000..0900918 --- /dev/null +++ b/.msman/fabric.sh @@ -0,0 +1,117 @@ +#!/bin/bash +set -e +# API URL +api_url="https://meta.fabricmc.net/v2/versions/" + +# Example server's jar file name: +# fabric-server-mc.1.19.3-loader.0.14.14-launcher.0.11.1.jar + +function check_version_valid { + if [[ $(curl -s "$(echo $api_url)/loader/$(echo $version)") == "[]" ]]; then + >&2 echo "Error: Invalid version selected: $version" + exit 2 + else + # Check if selected build exists + if [ ! -z "$build" ]; then + if [[ $(curl -s "$(echo $api_url)/loader/$(echo $version)/$(echo $build)") == "\"no loader version found for $(echo $version)\"" ]]; then + >&2 echo "Error: Invalid build selected: $build" + exit 2 + fi + fi + fi +} + +function download_server { + # Download the server + echo "Downloading Fabric server..." + echo " - Version $version" + echo " - Build $download_build" + echo " - Installer $latest_installer" + curl "$(echo $api_url)/loader/$(echo $version)/$(echo $download_build)/$(echo $latest_installer)/server/jar" -o "./fabric-server-mc.$(echo $version)-loader.$(echo $download_build)-launcher.$(echo $latest_installer).jar" + echo "Download complete." +} + +function check_updates { + if [[ $server_file == false ]]; then + download_build=$latest_build + update_version=true + update_build=true + else + echo Checking for updates... + fi + + # Check if $build is empty + if [[ -z $build ]]; then + # Check if the current version is the same as the one selected + if [[ $current_version == $version ]]; then + # Check if the current build is the same as the one selected + if [[ $current_build == $latest_build ]]; then + echo "Server is up to date." + else + echo "Server is not up to date." + download_build=$latest_build + update_build=true + fi + else + # Check if $server_file is false + ask_version_differs + echo "Server is not up to date." + download_build=$latest_build + update_version=true + fi + else + # Check if the current version is the same as the one selected + if [[ $current_version == $version ]]; then + # Check if the current build is the same as the one selected + if [[ $current_build == $build ]]; then + echo "Server is up to date." + else + echo "Server is not up to date." + download_build=$build + update_build=true + fi + else + # Check if $server_file is false + ask_version_differs + echo "Server is not up to date." + download_build=$build + update_version=true + fi + fi +} + +# Get the latest build number and installer version +function get_latest_build { + # Get the latest build number + latest_build=$(curl -s "$(echo $api_url)/loader/$version" | jq -r '.[0].loader.version') + latest_installer=$(curl -s "$(echo $api_url)/installer/" | jq -r '.[0].version') +} + +# Check if the server is up to date and update if it isn't +function check_and_update { + if ! [[ $server_file == false ]]; then + echo Checking for updates... + fi + + # Get the latest build number + get_latest_build + + # Check if the current version is up to date + check_updates + + # Check if $build_update is true or $version_update is true + if [[ $update_build == true ]] || [[ $update_version == true ]]; then + if [[ $server_file != false ]]; then + old_server_file=$server_file + server_file="fabric-server-mc.$version-loader.$download_build-launcher.$latest_installer.jar" + download_server + # Delete the old server file + delete_old_server + else + server_file="fabric-server-mc.$version-loader.$download_build-launcher.$latest_installer.jar" + download_server + fi + fi + echo + echo +} diff --git a/.msman/version.sh b/.msman/version.sh index 4150dbc..c653207 100644 --- a/.msman/version.sh +++ b/.msman/version.sh @@ -1,3 +1,3 @@ #!/bin/bash -EXTRA_SCRIPTS_VERSION="v1.1.8" +EXTRA_SCRIPTS_VERSION="v2.0.0" diff --git a/msman.cfg b/msman.cfg index 7dd0c9e..e5778d0 100644 --- a/msman.cfg +++ b/msman.cfg @@ -14,7 +14,9 @@ # I am not responsible for any loss of data # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # -# Only paper is currently supported +# Supported server types: +# - paper +# - fabric server_type="paper" version="1.19.3" # Leave blank to use the latest build (auto updates on every run) @@ -38,7 +40,7 @@ mem="6000M" # Flags for the server itself # Usefull fot setting rcon password, server port, etc. -mc_launchoptions="-nogui" +mc_launchoptions="nogui" # If you want to get rid of script updates notifications, set the below to false check_for_script_updates=true diff --git a/msman.sh b/msman.sh index 04f01a3..6b22a37 100755 --- a/msman.sh +++ b/msman.sh @@ -8,7 +8,7 @@ set -e # and acknowledge the original script and author. # ############################################################################################################# -CURRENT_SCRIPT_VERSION="v1.1.8" +CURRENT_SCRIPT_VERSION="v2.0.0" # -------------------------------------------------- # You shouldn't need to change anything in this file @@ -199,7 +199,8 @@ function launch_server { echo "Starting the server..." echo echo - java $java_launchoptions -jar "$(basename ./paper-*.jar)" $mc_launchoptions + echo $server_file + java $java_launchoptions -jar "$(echo $server_file)" $mc_launchoptions } # Helper scripts update @@ -378,7 +379,7 @@ function load_config { # Delete old server file with name $old_server_file function delete_old_server { # Delete the old server file - echo "Deleting old server file $server_file..." + echo "Deleting old server file '$server_file...'" rm "$old_server_file" echo "Old server file deleted." } @@ -393,12 +394,12 @@ function load_script { # Load the correct script if [[ $server_type == "paper" ]]; then source "./.msman/paper.sh" + elif [[ $server_type == "fabric" ]]; then + source "./.msman/fabric.sh" # elif [[ $server_type == "vanilla" ]]; then # source "$cwd/msman/vanilla.sh" # elif [[ $server_type == "forge" ]]; then # source "$cwd/msman/forge.sh" - # elif [[ $server_type == "fabric" ]]; then - # source "$cwd/msman/fabric.sh" else >&2 echo "Unknown server type." echo "Exiting..."