diff --git a/.ms-manager/detect_server.sh b/.ms-manager/detect_server.sh new file mode 100644 index 0000000..7b6cac5 --- /dev/null +++ b/.ms-manager/detect_server.sh @@ -0,0 +1,51 @@ +#!/bin/bash +set -e +# 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 +} + diff --git a/.ms-manager/java.sh b/.ms-manager/java.sh new file mode 100644 index 0000000..444c73b --- /dev/null +++ b/.ms-manager/java.sh @@ -0,0 +1,192 @@ +#!/bin/bash +set -e +# Setup Java +function setup_java { + # Get the required Java version for the Minecraft version + get_required_java + + # Check if java was downloaded by the script + check_script_java + + # Check if java is installed + if [[ $java_version == false ]]; then + check_java_exec + if [[ $java_version != false ]]; then + echo "System Java $java_version will be used." + fi + fi + + if [[ $java_version == false ]]; then + >&2 echo "Java $required_java is not installed." + >&2 echo "Java $required_java is required to run Minecraft $version." + # Ask the user if they want to download Adoptium JRE + ask_jre + fi + echo + echo +} + +# Ask the user if they want to download Adoptium JRE +function ask_jre { + echo "This script can download the correct Adoptium JRE into '$(echo $HOME)/.adoptium_java' for you." + echo "You have 15 seconds to confirm or the script will exit." + read -t 12 -p "Do you want to download Adoptium JRE? (y/N)" download_java + + if [[ $download_java == "y" ]] || [[ $download_java == "Y" ]]; then + get_os_arch + download_jre + check_script_java + if [[ $java_version == false ]]; then + >&2 echo "Java $required_java was not downloaded correctly." + >&2 echo "Please install Java $required_java manually." + exit 4 + fi + else + >&2 echo "Please install Java $required_java and run this script again." + exit 12 + fi +} + +# Check if java was downloaded by the script +function check_script_java { + if [[ -d "$(echo $HOME)/.adoptium_java" ]]; then + if [[ $required_java == "8" ]]; then + if [[ -d "$(echo $HOME)/.adoptium_java/jre8" ]]; then + java_version=8 + if [[ -f "$(echo $HOME).adoptium_java/jre8/bin/java" ]]; then + PATH="$(echo $HOME)/.adoptium_java/jre8/bin:$PATH" + fi + fi + elif [[ $required_java == "11" ]]; then + if [[ -d "$(echo $HOME)/.adoptium_java/jre11" ]]; then + java_version=11 + PATH="$(echo $HOME)/.adoptium_java/jre11/bin:$PATH" + fi + elif [[ $required_java == "16" ]]; then + if [[ -d "$(echo $HOME)/.adoptium_java/jre16" ]]; then + java_version=16 + PATH="$(echo $HOME)/.adoptium_java/jre16/bin:$PATH" + fi + elif [[ $required_java == "17" ]]; then + if [[ -d "$(echo $HOME)/.adoptium_java/jre17" ]]; then + java_version=17 + PATH="$(echo $HOME)/.adoptium_java/jre17/bin:$PATH" + fi + fi + check_java_exec + if [[ $java_version == $required_java ]]; then + echo "Java $java_version detected in '$(echo $HOME)/.adoptium_java/jre$(echo $java_version)/bin/java.' will be used." + else + java_version=false + fi + else + java_version=false + fi +} + +# Get the system Java version +function check_java_exec { +# Check if java is installed + if ! command -v java &> /dev/null + then + java_version=false + fi + + # If java is installed, get the version (the java_version won't be 0) + if [[ $java_version != false ]]; 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 +} + +# Get the required Java version for the Minecraft version +# 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 get_required_java { + # Extract the middle number of the Minecraft version + minecraft_middle=$(echo "$version" | awk -F '.' '{print $2}') + + + # Get the java version for the defined server version + if (( 8 <= minecraft_middle && minecraft_middle <= 11 )); then + if ! [[ $java_version -eq 8 ]]; then + required_java=8 + fi + elif (( 12 <= minecraft_middle && minecraft_middle <= 16 )); then + if ! [[ $java_version -eq 11 ]]; then + required_java=11 + fi + elif (( minecraft_middle == 17 )); then + if ! [[ $java_version -eq 16 ]]; then + required_java=16 + fi + elif (( 18 <= minecraft_middle )); then + if ! [[ $java_version -eq 17 ]]; then + required_java=17 + fi + else + >&2 echo "Unsupported Minecraft version $select_version." + fi +} + +# Check host architecture +function get_os_arch { + if [[ $(uname -m) == "x86_64" ]]; then + arch="x64" + elif [[ $(uname -m) == "aarch64" ]]; then + arch="aarch64" + else + >&2 echo "Unsupported architecture $(uname -m)." + >&2 echo "Please install Java $required_java manually." + exit 3 + fi +} + +# Download openjdk jre +function download_jre { + # Check if .java folder exists + if ! [[ -d "$(echo $HOME)/.adoptium_java" ]]; then + echo "Creating $(echo $HOME)/.adoptium_java folder" + mkdir "$(echo $HOME)/.adoptium_java" + fi + # Download the correct version of Java + if [[ $required_java == "8" ]]; then + echo "Downloading Adoptium JRE 8" + curl -L -o java.tar.gz "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u362-b09/OpenJDK8U-jre_$(echo $arch)_linux_hotspot_8u362b09.tar.gz" + echo "Extracting Java 8" + tar -xzf java.tar.gz + echo "Moving Java 8 to $(echo $HOME)/.adoptium_java/jre8" + mv jdk8u362-b09-jre "$(echo $HOME)/.adoptium_java/jre8" + echo "Removing temporary files" + rm java.tar.gz + elif [[ $required_java == "11" ]]; then + echo "Downloading Adoptium JRE 11" + curl -L -o java.tar.gz "https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.18%2B10/OpenJDK11U-jre_$(echo $arch)_linux_hotspot_11.0.18_10.tar.gz" + echo "Extracting Java 11" + tar -xzf java.tar.gz + echo "Moving Java 11 to $(echo $HOME)/.adoptium_java/jre11" + mv jdk-11.0.18+10-jre "$(echo $HOME)/.adoptium_java/jre11" + echo "Removing temporary files" + rm java.tar.gz + elif [[ $required_java == "16" ]]; then + echo "Downloading Java 16" + curl -L -o java.tar.gz "https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.18%2B10/OpenJDK11U-jre_$(echo $arch)_linux_hotspot_16.0.2_7.tar.gz" + echo "Extracting Java 16" + tar -xzf java.tar.gz + echo "Moving Java 16 to $(echo $HOME)/.adoptium_java/jre16" + mv jdk-16.0.2+7-jre "$(echo $HOME)/.adoptium_java/jre16" + echo "Removing temporary files" + rm java.tar.gz + elif [[ $required_java == "17" ]]; then + echo "Downloading Java 17" + curl -L -o java.tar.gz "https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.6%2B10/OpenJDK17U-jre_$(echo $arch)_linux_hotspot_17.0.6_10.tar.gz" + echo "Extracting Java 17" + tar -xzf java.tar.gz + echo "Moving Java 17 to $(echo $HOME)/.adoptium_java/jre17" + mv jdk-17.0.6+10-jre "$(echo $HOME)/.adoptium_java/jre17" + echo "Removing temporary files" + rm java.tar.gz + fi +} diff --git a/.ms-manager/paper.sh b/.ms-manager/paper.sh new file mode 100644 index 0000000..3ff6e47 --- /dev/null +++ b/.ms-manager/paper.sh @@ -0,0 +1,124 @@ +#!/bin/bash +set -e +# 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 +} + diff --git a/.ms-manager/version.sh b/.ms-manager/version.sh new file mode 100644 index 0000000..9267022 --- /dev/null +++ b/.ms-manager/version.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +EXTRA_SCRIPTS_VERSION="v1.0.3" diff --git a/README.md b/README.md index 2d558c2..ba5efa1 100644 --- a/README.md +++ b/README.md @@ -80,22 +80,10 @@ the Minecraft version you selected. If you want this behaviour, leave the select_build veriable empty. Otherwise select the build you want and the script will download it for you. -### Versions - -> **Warning** -> -> - This script is not made for migrating versions. It won't make sure your plugins -are working or that your worlds won't get corrupted. It only downloads a new server -file, nothing else. -> - **I am not responsible for any lost data** - -This script is able to update/downgrade versions as you please. Just change the -`select_version` variable to the version you want and the script will download -it for you. - ## Default JVM flags used By default this script uses [Aikar's](https://docs.papermc.io/paper/aikars-flags) -flags. It's set up so that it automatically modifies them if over 12GB of memory +flags. +It's set up so that it automatically modifies them if over 12GB of memory is set for the server so you shouldn't need to change them unless you want to swap them out for something else. diff --git a/launch.cfg b/launch.cfg new file mode 100644 index 0000000..6c079a2 --- /dev/null +++ b/launch.cfg @@ -0,0 +1,56 @@ +############################################################# +# Settings for start_papermc.sh script # +# Available at https://github.com/jiriks74/start_papermc.sh # +############################################################# + +# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # +# This script is not made for migrating versions. # +# If you're migrating versions you're doing at your own risk. # +# If you still want to migrate versions, you can do so just by # +# changing the select_version variable to the version you want # +# to migrate to. # +# You'll be warned if you're about to migrate versions. # +# I recommend backing up your world and settings. # +# I am not responsible for any loss of data # +# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # + +# Only paper is currently supported +server_type="paper" +version="1.19.3" +# Leave blank to use the latest build (auto updates on every run) +build="" + +# +# Memory settings +# +# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # +# Do not allocate all of your available memory on a shared host! # +# Minecraft (and Java) needs to have more memory than the Xmx # +# parameter (maxMem below). You should set it at least 1500M # +# lower than your available memory if you're running just the # +# minecraft server. # +# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # +mem="6000M" + +############# +# Overrides # +############# + +# Flags for the server itself +# Usefull fot setting rcon password, server port, etc. +mc_launchoptions="-nogui" + +# If you want to get rid of script updates notifications, set the below to false +check_for_script_updates=true + +# The options below are for advanced users, modification is not needed in most cases +# ---------------------------------------------------------------------------------- + +# If you're purposefully using a different version of Java, you can override the version check +java_version_override=false + +# Aikar's flags are used by defauls (https://docs.papermc.io/paper/aikars-flags) +# And are set by memory usage so if you allocated more than 12GB or RAM they are set automatically +# 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 +java_launchoptions="" diff --git a/msman.sh b/msman.sh index 63ae2c7..1df0a58 100644 --- a/msman.sh +++ b/msman.sh @@ -1 +1,492 @@ -# MinecraftServerMANager +#!/bin/bash +set -e +############################################################################################################# +# MinecraftServerMANager # +# by jiriks74 # +# https://github.com/jiriks74/start_papermc.sh # +# This script is under GPLv3, if you want to distribute changes you have to do so under the same license # +# and acknowledge the original script and author. # +############################################################################################################# + +CURRENT_SCRIPT_VERSION="v1.0.3" + +# -------------------------------------------------- +# You shouldn't need to change anything in this file +# Settings are located in 'launch.cfg' +# -------------------------------------------------- +# +# The url of the script (used for updating) +REPO_OWNER="jiriks74" +REPO_NAME="msman.sh" + +# Check for dependencies +function check_dependencies { + # Check if curl is installed + if ! command -v curl &> /dev/null + then + echo "Error: Curl is not installed" + exit 1 + fi + + # Check if jq is installed + if ! command -v jq &> /dev/null + then + echo "Error: jq is not installed" + exit 1 + fi + + # Check if awk is installed + if ! command -v awk &> /dev/null + then + echo "Error: awk is not installed" + exit 1 + fi + + # Check if screen is installed + if ! command -v screen &> /dev/null + then + echo "Error: screen is not installed" + exit 1 + fi + + # Check if tar is installed + if ! command -v tar &> /dev/null + then + echo "Error: tar is not installed" + exit 1 + fi + + # Check if gzip is installed + # TODO: Check if this is needed and/or works + # if ! command -v gzip &> /dev/null + # then + # echo "Error: gzip is not installed" + # exit 1 + # fi +} + +# Ask if the user wants to continue anyway +# If the user doesn't answer in 15 seconds, the script will exit +function ask_continue { + if [[ $java_version_override != true ]]; then + answer="" + echo "You have 15 seconds to respond before the script exits." + read -t 15 -p "Do you want to continue anyway? [y/N]: " answer + if [[ $answer != "y" ]] && [[ $answer != "Y" ]]; then + echo "Exiting..." + exit 1 + fi + fi +} + +# Set arguments for java +function set_java_args { + # Check if $override_flags is not true + if [[ $override_flags != true ]]; then + if [[ "${mem%M}" -gt 12000 ]]; then + G1NewSize=40 + G1MaxNewSize=50 + G1HeapRegionSize=16M + G1Reserve=15 + InitiatingHeapOccupancy=20 + else + G1NewSize=30 + G1MaxNewSize=40 + G1HeapRegionSize=8M + G1Reserve=20 + InitiatingHeapOccupancy=15 + fi + # Aikar's flags are used by default + # See https://docs.papermc.io/paper/aikars-flags + java_launchoptions="-Xms$mem -Xmx$mem -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=$G1NewSize -XX:G1MaxNewSizePercent=$G1MaxNewSize -XX:G1HeapRegionSize=$G1HeapRegionSize -XX:G1ReservePercent=$G1Reserve -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=$InitiatingHeapOccupancy -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true" + fi +} + +# Ask if the new server version differs from the old one +function ask_version_differs { + echo + echo + 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 "Do you want to update the server version?" + 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 updating the server version." + echo + 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 + + 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 + + if [ "$version_differs" == "y" ] || [ "$version_differs" == "Y" ]; then + read -t 15 -p "Are you sure you want to update the server version? [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 +} + +# Ask if the new server version differs from the old one +function ask_server_differs { + echo + echo + echo "The current server type differs from the one you selected." + 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 + + if [ "$version_differs" == "y" ] || [ "$version_differs" == "Y" ]; then + read -t 15 -p "Are you sure you want to update the server version? [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 +} + +# Accept EULA +function accept_eula { + if test "$(cat eula.txt 2>/dev/null)" != "eula=true"; then + first_run=true + echo "'eula.txt' does not exist or EULA is not accepted" + 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 "You have 15 seconds to respond, or the script will exit" + read -t 15 -p "Do you agree to the Minecraft EULA? [y/N] " eula_agreed + + if [ "$eula_agreed" == "y" ] || [ "$eula_agreed" == "Y" ]; then + if [ ! -f eula.txt ]; then + echo "eula=true" > eula.txt + else + rm eula.txt + echo "eula=true" > eula.txt + fi + echo + echo + echo "EULA accepted" + echo + echo + else + echo + echo + echo "You did not agree to the Minecraft EULA" + echo "Exiting..." + exit 1 + fi + fi +} + +# Launch the server +function launch_server { + echo "Starting the server..." + echo + echo + java $java_launchoptions -jar "$(basename ./paper-*.jar)" $mc_launchoptions +} + +# Helper scripts update +function helper_scripts_update { + # Download matching version of helper scripts + echo "Updating helper scripts..." + # Download the file into ms-manager.tar.gz + curl -LJ -w '%{http_code}\n' "https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$CURRENT_SCRIPT_VERSION/ms-man-helper.tar.gz" > ms-man-helper.tar.gz + # Check if the download was successful by checking the last line of the file for 200 + if [[ $(cat ma-man.tar.gz | tail -n 1) == 200 ]]; then + # Remove the last line of the file + sed -i '$d' ms-man-helper.tar.gz + # Extract the files from ms-man-helper.tar.gz + tar -xzf ms-man-helper.tar.gz + # Remove the old script + echo "Removing old helper scripts..." + rm -rf .ms-manager + echo "Removed old script" + echo "Moving new helper scripts into place..." + mv ms-manager .ms-manager + echo "Removing temporary files..." + rm ms-manager-helper.tar.gz + echo "Helper scripts updated successfully." + echo + echo + else + echo "Failed to update helper scripts." + rm -rf ms-manager + rm ms-manager-helper.tar.gz + exit 5 + fi +} + +# Download the update for the script +function self_update { + # Download the latest version of the script + echo "Updating script..." + # Download the file into start_new.sh + curl -sLJ -w '%{http_code}\n' "https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$LATEST_VERSION/start.sh" > start_new.sh + # Check if the download was successful by checking the last line of the file for 200 + if [[ $(cat start_new.sh | tail -n 1) == 200 ]]; then + # Remove the last line of the file + sed -i '$d' start_new.sh + # Make the file executable + chmod +x start_new.sh + # Remove the old script + rm start.sh + echo "Removed old script" + # Rename the new script + mv start_new.sh start.sh + echo "Renamed new script" + echo "Script updated successfully." + echo + else + echo "Failed to update script." + rm start_new.sh + exit 5 + fi +} + +# Check helper scripts update +function check_helper_scripts { + if [[ -d .ms-manager ]]; then + source "./ms-manager/version.sh" + if [[ $CURRENT_SCRIPT_VERSION != $EXTRA_SCRIPTS_VERSION ]]; then + echo "Helper script verion mismatch!" + echo "Main script version: $CURRENT_SCRIPT_VERSION" + echo "Helper script version: $EXTRA_SCRIPTS_VERSION" + echo + echo "The script will now download the same version of the helper scripts as the main script." + helper_scripts_update + fi + else + echo "Helper scripts not found." + echo "The script will now download the helper scripts." + helper_scripts_update + fi +} + +# Get latest script version +function get_latest_script_release { +response=$(curl -sL "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/latest") + + if [[ $response =~ "API rate limit exceeded" ]]; then + echo "API limit exceeded. Will not check for updates." + LATEST_SCRIPT_VERSION=$CURRENT_SCRIPT_VERSION + else + # Extract the latest version from the response + LATEST_SCRIPT_VERSION=$(echo $response | jq -r ".tag_name") + fi +} + +# Function to update the script +function check_self_update { + # Get the latest version of the script + get_latest_script_release + + # Compare the current version with the latest version + if [[ "$CURRENT_SCRIPT_VERSION" != "$LATEST_VERSION" ]]; then + echo "An update is available!" + echo "Current version: $CURRENT_SCRIPT_VERSION" + echo "Latest version: $LATEST_VERSION" + echo + 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." + read -t 15 -p "Do you want to update and read the release notes? [y/N]" + if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then + # Extract the release notes from the response + RELEASE_NOTES=$(echo "$response" | jq -r ".body") + + # Print the release notes + echo "$RELEASE_NOTES" | less + + # Ask the user if they want to update + echo + 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." + read -t 15 -p "Do you want to update? [y/N] " update + if [ "$update" == "y" ] || [ "$update" == "Y" ]; then + self_update + CURRENT_VERSION=$LATEST_VERSION + check_helper_scripts + else + echo "Skipping update." + return + fi + fi + echo + echo + fi +} + +# Load config +function load_config { + # Check if the config file exists + if [ ! -f launch.cfg ]; then + echo "Config file does not exist." + echo "Downloading the default config file..." + # Download the default config file for the current version + curl -sLJ -w '%{http_code}' "https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$CURRENT_VERSION/launch.cfg" > launch.cfg + # Check if the download was successful by checking the last line of the file for 200 + if [[ $(cat launch.cfg | tail -n 1) == 200 ]]; then + # Remove the last line of the file + sed -i '$d' launch.cfg + + echo + read -p "Do you want to edit the config file? [y/N] " edit_config + if [ "$edit_config" == "y" ] || [ "$edit_config" == "Y" ]; then + if [ -z "$EDITOR" ]; then + >&2 echo "EDITOR is not set." + >&2 echo "Open 'launch.cfg' manually." + echo "Exiting..." + exit 1 + 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..." + $EDITOR launch.cfg + fi + fi + else + >&2 echo "Failed to download the default config file." + >&2 echo "Go to the GitHub repository for more information:" + >&2 echo "https://github.com/$REPO_OWNER/$REPO_NAME" + echo "Exiting..." + exit 1 + fi + fi + + # Load config + source launch.cfg +} + +# 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 { + # DONE: Check if the script files exist + # - Checked in check_helper_scripts + 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 +} + +# First run +first_run() { + if [[ $first_run == true ]]; then + answer="" + echo "Since eula wasn't accepted, this is probably the first run of the server" + echo "If you want to install plugins (or mods), answer 'n' and you can do so." + echo "If you don't answer, the server will start in 15 secondd." + read -t 15 -p "Do you want to start the server now? [Y/n] " answer + if [ "$answer" == "n" ] || [ "$answer" == "N" ]; then + echo "Exiting..." + exit 0 + fi + fi +} + +# Main function +function main { + # Check dependencies + check_dependencies + + # Load config + load_config + + if [[ $check_for_script_updates == true ]]; then + # Check for script updates + check_self_update + else + echo "Skipping script update check." + fi + + # Check helper scripts version mismatch + check_helper_scripts + + # Load the rest of the script + # Get the current server file, version and build + load_script + + # Gets the installed server info + get_existing_server + + # Check if the version and build are valid + check_version_valid + + # Check if the correct java version is installed + setup_java + + # Check if the server is up to date and if not, update it + check_and_update + + # Accept EULA + accept_eula + + # Check if this is the first run + first_run + + # Launch the server + launch_server +} + +# Check for updates on GitHub +if [[ "$1" == "--redownload" ]] || [[ "$1" == "-r" ]]; then + self_update +# TODO: Add `--edit-config` option +elif [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then + echo "Usage: ./script.sh [OPTION]" + echo "Starts the Minecraft server." + echo + echo "Options:" + echo " -r, --redownload Redownloads the script from GitHub." + echo " -h, --help Show this help message." + echo + 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 + echo "For more information, see:" + echo "https://github.com/$REPO_OWNER/$REPO_NAME" + exit 0 +else + # Run the main function + main +fi