Move to a more modular architecture

This commit is contained in:
Jiří Štefka 2023-02-25 08:33:13 +01:00
parent d76a626707
commit a5351440b2
6 changed files with 344 additions and 226 deletions

@ -13,9 +13,12 @@
# I recommend backing up your world and settings. # # I recommend backing up your world and settings. #
# I am not responsible for any loss of data # # I am not responsible for any loss of data #
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #
select_version="1.19.3"
# Only paper is currently supported
server_type="paper"
version="1.19.3"
# Leave blank to use the latest build (auto updates on every run) # Leave blank to use the latest build (auto updates on every run)
select_build="" build=""
# #
# Memory settings # Memory settings
@ -51,4 +54,3 @@ java_version_override=false
# If you want to override them, set the below to true and set the flags you want to use in the java_launchoptions variable # If you want to override them, set the below to true and set the flags you want to use in the java_launchoptions variable
override_java_flags=false override_java_flags=false
java_launchoptions="" java_launchoptions=""

@ -0,0 +1,51 @@
#!/bin/bash
# Detect the server type
function get_existing_server {
if ls paper-*.jar 1> /dev/null 2>&1; then
existing_server_type="paper"
get_existing_paper
# 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
# existing_server_type="craftbukkit"
# elif ls vanilla-*.jar 1> /dev/null 2>&1; then
# existing_server_type="vanilla"
else
if ! ls *.jar 1> /dev/null 2>&1; then
echo "No server file found."
echo
echo
existing_server_type=false
server_file=false
else
>&2 echo "$(ls *.jar) file was found."
>&2 echo "Unknown server type."
exit 10
# TODO: Ask the user if they want to continue
fi
fi
}
# Get existing version and build of paper
function get_existing_paper {
# Get the current server file name
server_file=$(basename ./paper-*.jar)
# Extract the version and build number using cut command
current_version=$(echo "$server_file" | cut -d'-' -f2)
current_build=$(echo "$server_file" | cut -d'-' -f3)
# Remove the rest of the file names
current_version="${current_version%-*}"
current_build="${current_build%.jar}"
echo "Current server file: $server_file"
echo " - Version $current_version"
echo " - Build $current_build"
echo
echo
}

66
ms-manager/java.sh Normal file

@ -0,0 +1,66 @@
#!/bin/bash
# Check if the correct version of Java is installed
# For version 1.8 to 1.11 use java 8
# For version 1.12 to 1.16.4 use java 11
# For version 1.16.5 use java 16
# For version 1.17.1 to 1.18.1+ use java 17
function check_java {
# Check if java is installed
if ! command -v java &> /dev/null
then
>&2 echo "Error: Java is not installed"
java_version="0"
fi
# Extract the middle number of the Minecraft version
minecraft_middle=$(echo "$version" | awk -F '.' '{print $2}')
# If java is installed, get the version (the java_version won't be 0)
if [[ $java_version != "0" ]]; then
# Get the current Java version and extract the build number
java_version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}' | awk -F '.' '{print $1}')
echo "Java $java_version is installed."
fi
# Check if the correct version of java is installed
if (( 8 <= minecraft_middle && minecraft_middle <= 11 )); then
if ! [[ $java_version -eq 8 ]]; then
>&2 echo "Java 8 is required for Minecraft version $select_version. Please install Java 8."
if [[ $java_version == 0 ]]; then
exit 3
fi
ask_continue
fi
elif (( 12 <= minecraft_middle && minecraft_middle <= 16 )); then
if ! [[ $java_version -eq 11 ]]; then
>&2 echo "Java 11 is required for Minecraft version $select_version. Please install Java 11."
if [[ $java_version == 0 ]]; then
exit 3
fi
ask_continue
fi
elif (( minecraft_middle == 17 )); then
if ! [[ $java_version -eq 16 ]]; then
>&2 echo "Java 16 is required for Minecraft version $select_version. Please install Java 16."
if [[ $java_version == 0 ]]; then
exit 3
fi
ask_continue
fi
elif (( 18 <= minecraft_middle )); then
if ! [[ $java_version -eq 17 ]]; then
>&2 echo "Java 17 is required for Minecraft version $select_version. Please install Java 17."
if [[ $java_version == 0 ]]; then
exit 3
fi
ask_continue
fi
else
>&2 echo "Unsupported Minecraft version $select_version."
if [[ $java_version == 0 ]]; then
exit 3
fi
ask_continue
fi
}

124
ms-manager/paper.sh Normal file

@ -0,0 +1,124 @@
#!/bin/bash
# API URL
api_url="https://api.papermc.io/v2/projects/paper/versions/$version/builds"
# Check if the version and build are valid
function check_version_valid {
if curl -s "$api_url" | grep -q '{"error":"Version not found."}'; then
>&2 echo "Error: Invalid version selected: $version"
exit 2
else
# Check if selected build exists
if [ ! -z "$build" ]; then
# WARNING: Check if the shortened versin works
# if curl -Is "https://api.papermc.io/v2/projects/paper/versions/$version/builds/$build/downloads/paper-$version-$build.jar" | grep "HTTP/2 404" >/dev/null; then
if curl -Is "$api_url/$build/downloads/paper-$version-$build.jar" | grep "HTTP/2 404" >/dev/null; then
>&2 echo "Error: Invalid build selected: $build"
exit 2
fi
fi
fi
}
# Download server set by $version and $download_build
function download_server {
# Download the server
echo "Downloading PaperMC server..."
echo " - Version $version"
echo " - Build $download_build"
curl "https://api.papermc.io/v2/projects/paper/versions/$version/builds/$download_build/downloads/paper-$version-$download_build.jar" -o "./paper-$version-$download_build.jar"
echo "Download complete."
}
# Check if up to date
function check_updates {
if [[ $server_file == false ]]; then
download_build=$latest_build
update_version=true
update_build=true
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
if [[ $server_file != false ]]; then
ask_version_differs
echo "Server is not up to date."
download_build=$latest_build
update_version=true
fi
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
if [[ $server_file != false ]]; then
ask_version_differs
echo "Server is not up to date."
download_build=$build
update_version=true
fi
fi
fi
}
# Get the latest build number
function get_latest_build {
# Get the latest build number
latest_build=$(curl -s $api_url | jq '.builds[-1].build')
}
# Check for updates
function check_and_update {
if [[ $server_file == false ]]; then
download_build=$latest_build
update_version=true
update_build=true
else
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="paper-$version-$download_build.jar"
download_server
# Delete the old server file
delete_old_server
else
server_file="paper-$version-$download_build.jar"
download_server
fi
fi
echo
echo
}

3
ms-manager/version.sh Normal file

@ -0,0 +1,3 @@
#!/bin/bash
CURRENT_SCRIPTS_VERSION="v1.0.3"

@ -7,7 +7,7 @@
# and acknowledge the original script and author. # # and acknowledge the original script and author. #
############################################################################################################# #############################################################################################################
CURRENT_VERSION="v1.0.3" CURRENT_SCRIPT_VERSION="v1.0.3"
# -------------------------------------------------- # --------------------------------------------------
# You shouldn't need to change anything in this file # You shouldn't need to change anything in this file
@ -16,7 +16,7 @@ CURRENT_VERSION="v1.0.3"
# #
# The url of the script (used for updating) # The url of the script (used for updating)
REPO_OWNER="jiriks74" REPO_OWNER="jiriks74"
REPO_NAME="start_papermc.sh" REPO_NAME="msman.sh"
# Check for dependencies # Check for dependencies
function check_dependencies { function check_dependencies {
@ -40,21 +40,12 @@ function check_dependencies {
echo "Error: awk is not installed" echo "Error: awk is not installed"
exit 1 exit 1
fi fi
}
# Check if the version and build are valid # Check if screen is installed
function check_version_valid { if ! command -v screen &> /dev/null
if curl -s "$api_url" | grep -q '{"error":"Version not found."}'; then then
echo "Error: Invalid version selected: $select_version" echo "Error: screen is not installed"
exit 2 exit 1
else
# Check if selected build exists
if [ ! -z "$select_build" ]; then
if curl -Is "https://api.papermc.io/v2/projects/paper/versions/$select_version/builds/$select_build/downloads/paper-$select_version-$select_build.jar" | grep "HTTP/2 404" >/dev/null; then
echo "Error: Invalid build selected: $select_build"
exit 2
fi
fi
fi fi
} }
@ -72,112 +63,6 @@ function ask_continue {
fi fi
} }
# Check if the correct version of Java is installed
# For version 1.8 to 1.11 use java 8
# For version 1.12 to 1.16.4 use java 11
# For version 1.16.5 use java 16
# For version 1.17.1 to 1.18.1+ use java 17
function check_java {
# Check if java is installed
if ! command -v java &> /dev/null
then
echo "Error: Java is not installed"
java_version="0"
fi
# Extract the middle number of the Minecraft version
minecraft_middle=$(echo "$select_version" | awk -F '.' '{print $2}')
# If java is installed, get the version (the java_version won't be 0)
if [[ $java_version != "0" ]]; then
# Get the current Java version and extract the build number
java_version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}' | awk -F '.' '{print $1}')
fi
# Check if the correct version of java is installed
if (( 8 <= minecraft_middle && minecraft_middle <= 11 )); then
if ! [[ $java_version -eq 8 ]]; then
echo "Java $java_version is installed."
echo "Java 8 is required for Minecraft version $select_version. Please install Java 8."
if [[ $java_version == 0 ]]; then
exit 3
fi
ask_continue
fi
elif (( 12 <= minecraft_middle && minecraft_middle <= 16 )); then
if ! [[ $java_version -eq 11 ]]; then
echo "Java $java_version is installed."
echo "Java 11 is required for Minecraft version $select_version. Please install Java 11."
if [[ $java_version == 0 ]]; then
exit 3
fi
ask_continue
fi
elif (( minecraft_middle == 17 )); then
if ! [[ $java_version -eq 16 ]]; then
echo "Java $java_version is installed."
echo "Java 16 is required for Minecraft version $select_version. Please install Java 16."
if [[ $java_version == 0 ]]; then
exit 3
fi
ask_continue
fi
elif (( 18 <= minecraft_middle )); then
if ! [[ $java_version -eq 17 ]]; then
echo "Java $java_version is installed."
echo "Java 17 is required for Minecraft version $select_version. Please install Java 17."
if [[ $java_version == 0 ]]; then
exit 3
fi
ask_continue
fi
else
echo "Unsupported Minecraft version $select_version."
if [[ $java_version == 0 ]]; then
exit 3
fi
ask_continue
fi
}
# Get existing version and build
function get_existing_version {
if ls paper-*.jar 1> /dev/null 2>&1; then
# Get the current server file name
server_file=$(basename ./paper-*.jar)
# Extract the version and build number using cut command
current_version=$(echo "$server_file" | cut -d'-' -f2)
current_build=$(echo "$server_file" | cut -d'-' -f3)
current_version="${current_version%-*}"
current_build="${current_build%.jar}"
echo "Current server file: $server_file"
echo " - Version $current_version"
echo " - Build $current_build"
else
echo "No old server file found."
server_file=false
fi
}
# Download server set by $select_version and $download_build
function download_server {
# Download the server
echo "Downloading PaperMC server..."
echo " - Version $select_version"
echo " - Build $download_build"
curl -s "https://api.papermc.io/v2/projects/paper/versions/$select_version/builds/$download_build/downloads/paper-$select_version-$download_build.jar" -o "./paper-$select_version-$download_build.jar"
echo "Download complete."
}
# 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..."
rm "$old_server_file"
echo "Old server file deleted."
}
# Set arguments for java # Set arguments for java
function set_java_args { function set_java_args {
# Check if $override_flags is not true # Check if $override_flags is not true
@ -205,12 +90,12 @@ function set_java_args {
function ask_version_differs { function ask_version_differs {
echo echo
echo echo
echo "The server version differs from the one you selected." echo "The current server version differs from the one you selected."
echo "The server version is $current_version and the selected version is $select_version." echo "The server version is $current_version and the selected version is $select_version."
echo "Do you want to update the server version?" echo "Do you want to update the server version?"
echo "This can cause many issues if you don't know what you are doing." echo "This can cause many issues if you don't know what you are doing."
echo echo
echo "I am not responsible for any damage caused by updating the server version." echo "I am not responsible for any data loss caused by updating the server version."
echo echo
echo "You have 15 seconds to respond, or the script will exit" echo "You have 15 seconds to respond, or the script will exit"
read -t 15 -p "Do you want to update the server version? [y/N] " version_differs read -t 15 -p "Do you want to update the server version? [y/N] " version_differs
@ -231,87 +116,32 @@ function ask_version_differs {
fi fi
} }
# Check if up to date # Ask if the new server version differs from the old one
function check_up_to_date { function ask_server_differs {
if [[ $server_file == false ]]; then echo
download_build=$latest_build echo
update_version=true echo "The current server type differs from the one you selected."
update_build=true echo "The current server type is $current_server_type and the selected version is $server_type."
echo "Do you want to change server types?"
echo "This can cause many issues if you don't know what you are doing."
echo
echo "I am not responsible for any data loss caused by changing server types."
echo
echo "You have 15 seconds to respond, or the script will exit"
read -t 15 -p "Do you want to change the server version type? [y/N] " version_differs
if [ "$version_differs" != "y" ] && [ "$version_differs" != "Y" ]; then
echo "Server version not updated."
echo "To start the server again with the current version, change the version in the config to $current_version."
exit 4
fi fi
# Check if $select_build is empty if [ "$version_differs" == "y" ] || [ "$version_differs" == "Y" ]; then
if [[ -z $select_build ]]; then read -t 15 -p "Are you sure you want to update the server version? [y/N] " version_differs
# Check if the current version is the same as the one selected if [ "$version_differs" != "y" ] && [ "$version_differs" != "Y" ]; then
if [[ $current_version == $select_version ]]; then echo "Server version not updated."
# Check if the current build is the same as the one selected echo "To start the server again with the current version, change the version in the config to $current_version."
if [[ $current_build == $latest_build ]]; then exit 4
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
if [[ $server_file != false ]]; then
ask_version_differs
echo "Server is not up to date."
download_build=$latest_build
update_version=true
fi
fi
else
# Check if the current version is the same as the one selected
if [[ $current_version == $select_version ]]; then
# Check if the current build is the same as the one selected
if [[ $current_build == $select_build ]]; then
echo "Server is up to date."
else
echo "Server is not up to date."
download_build=$select_build
update_build=true
fi
else
# Check if $server_file is false
if [[ $server_file != false ]]; then
ask_version_differs
echo "Server is not up to date."
download_build=$select_build
update_version=true
fi
fi
fi
}
# Get the latest build number
function get_latest_build {
# Get the latest build number
latest_build=$(curl -s $api_url | jq '.builds[-1].build')
}
# Check for updates
function check_and_update {
# Get current version and build
get_existing_version
echo Checking for updates...
# Get the latest build number
get_latest_build
# Check if the current version is up to date
check_up_to_date
# 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="paper-$select_version-$download_build.jar"
download_server
# Delete the old server file
delete_old_server
else
server_file="paper-$select_version-$download_build.jar"
download_server
fi fi
fi fi
} }
@ -319,8 +149,6 @@ function check_and_update {
# Accept EULA # Accept EULA
function accept_eula { function accept_eula {
if test "$(cat eula.txt 2>/dev/null)" != "eula=true"; then if test "$(cat eula.txt 2>/dev/null)" != "eula=true"; then
echo
echo
echo "'eula.txt' does not exist or EULA is not accepted" echo "'eula.txt' does not exist or EULA is not accepted"
echo "You have to accept the Minecraft EULA to run the server" echo "You have to accept the Minecraft EULA to run the server"
echo "By entering 'y' you are indicating your agreement to Minecraft's EULA (https://aka.ms/MinecraftEULA)." echo "By entering 'y' you are indicating your agreement to Minecraft's EULA (https://aka.ms/MinecraftEULA)."
@ -337,6 +165,8 @@ function accept_eula {
echo echo
echo echo
echo "EULA accepted" echo "EULA accepted"
echo
echo
else else
echo echo
echo echo
@ -349,8 +179,6 @@ function accept_eula {
# Launch the server # Launch the server
function launch_server { function launch_server {
echo
echo
echo "Starting the server..." echo "Starting the server..."
echo echo
echo echo
@ -406,14 +234,14 @@ function check_self_update {
LATEST_VERSION=$(echo $response | jq -r ".tag_name") LATEST_VERSION=$(echo $response | jq -r ".tag_name")
# Compare the current version with the latest version # Compare the current version with the latest version
if [[ "$CURRENT_VERSION" != "$LATEST_VERSION" ]]; then if [[ "$CURRENT_SCRIPT_VERSION" != "$LATEST_VERSION" ]]; then
echo "An update is available!" echo "An update is available!"
echo "Current version: $CURRENT_VERSION" echo "Current version: $CURRENT_SCRIPT_VERSION"
echo "Latest version: $LATEST_VERSION" echo "Latest version: $LATEST_VERSION"
echo echo
echo "The script will continue without updating in 15 seconds." echo "The script will continue without updating in 15 seconds."
echo "If you decide to update it is your responsibility to check the release notes for any breaking changes." echo "If you decide to update it is your responsibility to check the release notes for any breaking changes."
read -t 15 -p "Do you want to read the release notes? [y/N]" read -t 15 -p "Do you want to update and read the release notes? [y/N]"
if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then
# Extract the release notes from the response # Extract the release notes from the response
RELEASE_NOTES=$(echo "$response" | jq -r ".body") RELEASE_NOTES=$(echo "$response" | jq -r ".body")
@ -433,6 +261,8 @@ function check_self_update {
return return
fi fi
fi fi
echo
echo
fi fi
fi fi
} }
@ -454,19 +284,26 @@ function load_config {
read -p "Do you want to edit the config file? [y/N] " edit_config read -p "Do you want to edit the config file? [y/N] " edit_config
if [ "$edit_config" == "y" ] || [ "$edit_config" == "Y" ]; then if [ "$edit_config" == "y" ] || [ "$edit_config" == "Y" ]; then
if [ -z "$EDITOR" ]; then if [ -z "$EDITOR" ]; then
echo "EDITOR is not set." >&2 echo "EDITOR is not set."
echo "Open 'launch.cfg' manually." >&2 echo "Open 'launch.cfg' manually."
echo "Exiting..." echo "Exiting..."
exit 1 exit 1
else else
# Check if $EDITOR is installed
if ! command -v $EDITOR &> /dev/null; then
>&2 echo "$EDITOR is not installed."
>&2 echo "Open 'launch.cfg' manually."
echo "Exiting..."
exit 1
fi
echo "Opening the config file in $EDITOR..." echo "Opening the config file in $EDITOR..."
$EDITOR launch.cfg $EDITOR launch.cfg
fi fi
fi fi
else else
echo "Failed to download the default config file." >&2 echo "Failed to download the default config file."
echo "Go to the GitHub repository for more information:" >&2 echo "Go to the GitHub repository for more information:"
echo "https://github.com/$REPO_OWNER/$REPO_NAME" >&2 echo "https://github.com/$REPO_OWNER/$REPO_NAME"
echo "Exiting..." echo "Exiting..."
exit 1 exit 1
fi fi
@ -474,8 +311,36 @@ function load_config {
# Load config # Load config
source launch.cfg source launch.cfg
# API URL }
api_url="https://api.papermc.io/v2/projects/paper/versions/$select_version/builds"
# 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..."
rm "$old_server_file"
echo "Old server file deleted."
}
# Load the rest of the script
function load_script {
# TODO: Check if the script files exist
source "./ms-manager/detect_server.sh"
source "./ms-manager/java.sh"
# Load the correct script
if [[ $server_type == "paper" ]]; then
source ".//ms-manager/paper.sh"
# elif [[ $server_type == "vanilla" ]]; then
# source "$cwd/ms-manager/vanilla.sh"
# elif [[ $server_type == "forge" ]]; then
# source "$cwd/ms-manager/forge.sh"
# elif [[ $server_type == "fabric" ]]; then
# source "$cwd/ms-manager/fabric.sh"
else
>&2 echo "Unknown server type."
echo "Exiting..."
exit 1
fi
} }
# Main function # Main function
@ -486,13 +351,20 @@ function main {
# Load config # Load config
load_config load_config
if [[ $check_self_update == true ]]; then # Load the rest of the script
# Get the current server file, version and build
load_script
if [[ $check_for_script_updates == true ]]; then
# Check for script updates # Check for script updates
check_self_update check_self_update
else else
echo "Skipping script update check." echo "Skipping script update check."
fi fi
# Gets the installed server info
get_existing_server
# Check if the version and build are valid # Check if the version and build are valid
check_version_valid check_version_valid
@ -510,16 +382,16 @@ function main {
} }
# Check for updates on GitHub # Check for updates on GitHub
if [[ "$1" == "--redownload" ]]; then if [[ "$1" == "--redownload" ]] || [[ "$1" == "-r" ]]; then
self_update self_update
fi # TODO: Add `--edit-config` option
if [[ "$1" == "--help" ]]; then elif [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
echo "Usage: ./script.sh [OPTION]" echo "Usage: ./script.sh [OPTION]"
echo "Starts the Minecraft server." echo "Starts the Minecraft server."
echo echo
echo "Options:" echo "Options:"
echo " --redownload Redownloads the script from GitHub." echo " -r, --redownload Redownloads the script from GitHub."
echo " --help Show this help message." echo " -h, --help Show this help message."
echo echo
echo "To change the settings of the script, edit the 'launch.cfg' file." echo "To change the settings of the script, edit the 'launch.cfg' file."
echo "If the file does not exist, it will be downloaded automatically when the script is run and you will be asked if you want to edit it." echo "If the file does not exist, it will be downloaded automatically when the script is run and you will be asked if you want to edit it."