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

View file

@ -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
View 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
View 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
View file

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