From 013d2d87d3fd288a498f0168dece748c3499031c Mon Sep 17 00:00:00 2001 From: Griefed Date: Tue, 23 Jul 2024 21:45:49 +0200 Subject: [PATCH] docs: Add comments, descriptions and explanations for the scripts --- .../server_files/default_java_template.ps1 | 50 ++-- .../server_files/default_java_template.sh | 21 +- .../server_files/default_template.ps1 | 261 +++++++++++++++--- .../server_files/default_template.sh | 80 +++++- 4 files changed, 323 insertions(+), 89 deletions(-) diff --git a/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_java_template.ps1 b/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_java_template.ps1 index 3df881c2a..e47b30fc8 100644 --- a/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_java_template.ps1 +++ b/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_java_template.ps1 @@ -22,35 +22,6 @@ # Java Install script generated by ServerPackCreator SPC_SERVERPACKCREATOR_VERSION_SPC. # The template which was used in the generation of this script can be found at: # https://github.com/Griefed/ServerPackCreator/blob/SPC_SERVERPACKCREATOR_VERSION_SPC/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_java_template.ps1 -# -# By default, running Powershell scripts from untrusted sources is probably disabled on your system. -# As such, you will not be able to run the install_java.ps-scripts just yet. You need to allow running -# unsigned scripts first. See https://superuser.com/a/106363 for a short explanation on how to -# enable/allow running unsigned scripts with Powershell. -# ATTENTION: -# Bear in mind that this introduces a security risk on your system. After making the changes from the -# link above, you can run any Powershell script you like, and as such, introduce any and all security -# risk into your system. So, beware when running scripts from unknown sources. -# -# Powershell scripts by default can not be opened with a double-click if the path to said script -# contains spaces. If you wish to remedy this or want to read more about this behaviour, this article -# talks about it in great detail: https://blog.danskingdom.com/fix-problem-where-windows-powershell-cannot-run-script-whose-path-contains-spaces/ -# You can thank Mircosoft for this. There is nothing the developers of ServerPackCreator can do about that. -# What you should do instead is: -# In your explorer, browse into the directory which contains your server pack. -# Shift-Rightclick into an empty space inside the directory -# Click "Open PowerShell window here" -# Type ".\install_java.ps1", without the "", and hit enter -# -# ATTENTION: -# Keep in mind though that things may still break when working with paths with spaces in them. If -# things still break with a path with spaces, even after trying the fixes from the link above, then I -# suggest moving things to a folder whose path contains no spaces. -# -# Depending on which Minecraft version is used in this server pack, a different Java version may be installed. -# -# ATTENTION: -# This script will NOT modify the JAVA_HOME variable for your user. Function Global:RunJavaInstallation { @@ -98,4 +69,25 @@ Function Global:RunJavaInstallation CMD /C ${Java} -version WARUM IST POWERSHELL SO EIN HAUFEN STINKENDE SCHEIßE Write-Host "Installation finished. Returning to start-script." + +<# + .SYNOPSIS + + Download and install Java with the version required by the Minecraft server. + + .DESCRIPTION + + Download and install Java with the version required by the Minecraft version set in the variables.txt which was + also shipped with this modpack. Should you want to use a different Java version with your server pack, change + the RECOMMENDED_JAVA_VERSION. Likewise, you can change the vendor of the JDK to another one by changing + JDK_VENDOR in your variables.txt + + Depending on which Minecraft version is used in this server pack, a different Java version may be installed. + + ATTENTION: + This script will NOT modify the JAVA_HOME variable for your user. + + A list of available JDK versions and vendors can be found at https://github.com/Jabba-Team/index/blob/main/index.json + Jabba is available at https://github.com/Jabba-Team/jabba +#> } \ No newline at end of file diff --git a/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_java_template.sh b/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_java_template.sh index 084e1c5b3..08f8343a7 100644 --- a/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_java_template.sh +++ b/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_java_template.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -############################################################################################### +############################################LICENSE################################################# # Copyright (C) 2024 Griefed # # This script is free software; you can redistribute it and/or @@ -18,8 +18,19 @@ # USA # # The full license can be found at https:github.com/Griefed/ServerPackCreator/blob/main/LICENSE -############################################################################################### +############################################DESCRIPTION################################################# # +# This script uses Jabba to download and install Java with the version required by the Minecraft version set in the +# variables.txt which was also shipped with this modpack. Should you want to use a different Java version with your +# server pack, change the RECOMMENDED_JAVA_VERSION. Likewise, you can change the vendor of the JDK to another one by +# changing JDK_VENDOR in your variables.txt +# +# Depending on which Minecraft version is used in this server pack, a different Java version may be installed. +# +# A list of available JDK versions and vendors can be found at https://github.com/Jabba-Team/index/blob/main/index.json +# Jabba is available at https://github.com/Jabba-Team/jabba +# +############################################NOTES################################################# # Java Install script generated by ServerPackCreator SPC_SERVERPACKCREATOR_VERSION_SPC. # The template which was used in the generation of this script can be found at: # https://github.com/Griefed/ServerPackCreator/blob/SPC_SERVERPACKCREATOR_VERSION_SPC/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_java_template.sh @@ -35,10 +46,14 @@ # ATTENTION: # This script will NOT modify the JAVA_HOME variable for your user. +# commandAvailable(command) +# Check whether the command $1 is available for execution. Can be used in if-statements. commandAvailable() { command -v "$1" > /dev/null 2>&1 } +# installJabba +# Downloads and installs Jabba, the software used to download and install Java for the Minecraft server. installJabba() { echo "Downloading and installing jabba." if commandAvailable curl ; then @@ -55,6 +70,8 @@ installJabba() { GBLIC_VERSION=$(ldd --version | awk '/ldd/{print $NF}') IFS="." read -ra GBLIC_SEMANTICS <<<"${GBLIC_VERSION}" +# Older Linux systems aren't supported, sadly. This mainly affects Ubuntu 20 and Linux distributions from around that time +# which use glibc versions older than 2.32 & 2.34. if [[ ${GBLIC_SEMANTICS[1]} -lt 32 ]];then echo "Jabba only supports systems with glibc 2.32 & 2.34 onward. You have $GBLIC_VERSION. Automated Java installation can not proceed." echo "DO NOT ATTEMPT TO UPDATE OR UPGRADE YOUR INSTALLED VERSION OF GLIBC! DOING SO MAY CORRUPT YOUR ENTIRE SYSTEM!" diff --git a/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_template.ps1 b/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_template.ps1 index a136235a3..3aad1f117 100644 --- a/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_template.ps1 +++ b/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_template.ps1 @@ -18,55 +18,91 @@ # # The full license can be found at https:github.com/Griefed/ServerPackCreator/blob/main/LICENSE ############################################################################################### -# -# Start script generated by ServerPackCreator SPC_SERVERPACKCREATOR_VERSION_SPC. -# The template which was used in the generation of this script can be found at: -# https://github.com/Griefed/ServerPackCreator/blob/SPC_SERVERPACKCREATOR_VERSION_SPC/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_template.ps1 -# -# By default, running Powershell scripts from untrusted sources is probably disabled on your system. -# As such, you will not be able to run the start.ps-scripts just yet. You need to allow running -# unsigned scripts first. See https://superuser.com/a/106363 for a short explanation on how to -# enable/allow running unsigned scripts with Powershell. -# You may run `start-process PowerShell -verb runas "Set-ExecutionPolicy RemoteSigned"` from a regular -# PowerShell to allow running of the start-script. -# ATTENTION: -# Bear in mind that this introduces a security risk on your system. After making the changes from the -# link above, you can run any Powershell script you like, and as such, introduce any and all security -# risk into your system. So, beware when running scripts from unknown sources. -# -# Powershell scripts by default can not be opened with a double-click if the path to said script -# contains spaces. If you wish to remedy this or want to read more about this behaviour, this article -# talks about it in great detail: https://blog.danskingdom.com/fix-problem-where-windows-powershell-cannot-run-script-whose-path-contains-spaces/ -# You can thank Mircosoft for this. There is nothing the developers of ServerPackCreator can do about that. -# What you should do instead is: -# In your explorer, browse into the directory which contains your server pack. -# Shift-Rightclick into an empty space inside the directory -# Click "Open PowerShell window here" -# Type ".\start.ps1", without the "", and hit enter -# -# ATTENTION: -# Keep in mind though that things may still break when working with paths with spaces in them. If -# things still break with a path with spaces, even after trying the fixes from the link above, then I -# suggest moving things to a folder whose path contains no spaces. -# -# Depending on which modloader is set, different checks are run to ensure the server will start accordingly. -# If the modloader checks and setup are passed, Minecraft and EULA checks are run. -# If everything is in order, the server is started. -# -# Depending on the Minecraft version you will require a different Java version to run the server. -# 1.16.5 and older requires Java 8 (Java 11 will run better and work with 99% of mods, give it a try) -# You may acquire a Java 8 install here: https://adoptium.net/temurin/releases/?variant=openjdk8&version=8&package=jdk&arch=x64&os=windows -# You may acquire a java 11 install here: https://adoptium.net/temurin/releases/?variant=openjdk11&version=11&package=jdk&arch=x64&os=windows -# 1.18.2 and newer requires Java 17 (Java 18 will run better and work with 99% of mods, give it a try) -# You may acquire a Java 17 install here: https://adoptium.net/temurin/releases/?variant=openjdk17&version=17&package=jdk&arch=x64&os=windows -# You may acquire a Java 18 install here: https://adoptium.net/temurin/releases/?variant=openjdk18&version=18&package=jdk&arch=x64&os=windows -# 1.20.5 and newer require Java 21 -# You may acquire a Java 21 install here: https://adoptium.net/temurin/releases/?variant=openjdk21&version=21&package=jdk&arch=x64&os=windows + +<# + .SYNOPSIS + + Start script to easily run your server pack. In order to run this script even easier, run the start.bat-file + which was also shipped with this server pack. + + .DESCRIPTION + + A start-script supporting Forge, NeoForge, Fabric, Quilt and LegacyFabric as well as their supported Minecraft + versions. + + This script downloads and installs the Modloader server depending on the settings in the acompanying variables.txt + which was also shipped with this server pack. Should no suitable Java installation be found and your $JAVA-variable + be set to "java", then a suitable Java-installation will also be downloaded and provided to this server pack. + + You can let the server restart by setting RESTART to true in your variables.txt. More information about the + various settings in said file. Go check it out. + + .NOTES + + Start script generated by ServerPackCreator SPC_SERVERPACKCREATOR_VERSION_SPC. + The template which was used in the generation of this script can be found at: + https://github.com/Griefed/ServerPackCreator/blob/SPC_SERVERPACKCREATOR_VERSION_SPC/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_template.ps1 + + By default, running Powershell scripts from untrusted sources is probably disabled on your system. + As such, you will not be able to run the start.ps-scripts just yet. You need to allow running + unsigned scripts first. See https://superuser.com/a/106363 for a short explanation on how to + enable/allow running unsigned scripts with Powershell. + You may run `start-process PowerShell -verb runas "Set-ExecutionPolicy RemoteSigned"` from a regular + PowerShell to allow running of the start-script. + ATTENTION: + Bear in mind that this introduces a security risk on your system. After making the changes from the + link above, you can run any Powershell script you like, and as such, introduce any and all security + risk into your system. So, beware when running scripts from unknown sources. + + Powershell scripts by default can not be opened with a double-click if the path to said script + contains spaces. If you wish to remedy this or want to read more about this behaviour, this article + talks about it in great detail: https://blog.danskingdom.com/fix-problem-where-windows-powershell-cannot-run-script-whose-path-contains-spaces/ + You can thank Mircosoft for this. There is nothing the developers of ServerPackCreator can do about that. + What you should do instead is: + In your explorer, browse into the directory which contains your server pack. + Shift-Rightclick into an empty space inside the directory + Click "Open PowerShell window here" + Type ".\start.ps1", without the "", and hit enter + + ATTENTION: + Keep in mind though that things may still break when working with paths with spaces in them. If + things still break with a path with spaces, even after trying the fixes from the link above, then I + suggest moving things to a folder whose path contains no spaces. + + Depending on which modloader is set, different checks are run to ensure the server will start accordingly. + If the modloader checks and setup are passed, Minecraft and EULA checks are run. + If everything is in order, the server is started. + + Depending on the Minecraft version you will require a different Java version to run the server. + 1.16.5 and older requires Java 8 (Java 11 will run better and work with 99% of mods, give it a try) + You may acquire a Java 8 install here: https://adoptium.net/temurin/releases/?variant=openjdk8&version=8&package=jdk&arch=x64&os=windows + You may acquire a java 11 install here: https://adoptium.net/temurin/releases/?variant=openjdk11&version=11&package=jdk&arch=x64&os=windows + 1.18.2 and newer requires Java 17 (Java 18 will run better and work with 99% of mods, give it a try) + You may acquire a Java 17 install here: https://adoptium.net/temurin/releases/?variant=openjdk17&version=17&package=jdk&arch=x64&os=windows + You may acquire a Java 18 install here: https://adoptium.net/temurin/releases/?variant=openjdk18&version=18&package=jdk&arch=x64&os=windows + 1.20.5 and newer require Java 21 + You may acquire a Java 21 install here: https://adoptium.net/temurin/releases/?variant=openjdk21&version=21&package=jdk&arch=x64&os=windows + + .INPUTS + + None. You cannot pipe objects to start.ps1. + + .OUTPUTS + + None. start.ps1 does not generate any output to use in piping. +#> + Function PauseScript { Write-Host "Press any key to continue" -ForegroundColor Yellow $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown") > $null + +<# + .SYNOPSIS + + Pause script execution. User input in the form of any keyboard key-press is required to continue execution. +#> } Function CrashServer @@ -75,11 +111,30 @@ Function CrashServer Write-Host "${Message}" PauseScript exit 1 + +<# + .SYNOPSIS + + Crash script execution with exit code 1. Print $1 to the console. + + .PARAMETER Message + The message to print to console before force-stopping script execution. +#> } -Function CommandAvailable($cmdname) +Function CommandAvailable { - return [bool](Get-Command -Name $cmdname -ErrorAction SilentlyContinue) + param ($CmdName) + return [bool](Get-Command -Name $CmdName -ErrorAction SilentlyContinue) + +<# + .SYNOPSIS + + Check whether the command is available for execution. Can be used in if-statements. + + .PARAMETER CmdName + The command to check for availability. +#> } Function GetJavaVersion() @@ -92,6 +147,12 @@ Function GetJavaVersion() { $script:JavaVersion = $JavaFullversion[1] } + +<# + .SYNOPSIS + + Set $script:JavaVersion by checking $Java using -fullversion. Only the major version is stored, e.g. 8, 11, 17, 21. +#> } Function InstallJava() @@ -103,6 +164,13 @@ Function InstallJava() { CrashServer "Java installation failed. Couldn't find ${Java}." } + +<# + .SYNOPSIS + + Sources the companion-script "install_java.ps1" and runs the contained function "Global:RunJavaInstallation" to install + the required Java version for this modded Minecraft server. +#> } Function DeleteFileSilently @@ -118,6 +186,16 @@ Function DeleteFileSilently Remove-Item "${FileToDelete}" } $ErrorActionPreference = "Continue"; + +<# + .SYNOPSIS + + Quietly / silently delete the specified file from the filesystem. If a folder is specified, then the entire + folder is deleted recursively. + + .PARAMETER FileToDelete + The file or folder to delete silently, without printing messages or errors to console. +#> } Function WriteFileUTF8NoBom @@ -127,12 +205,36 @@ Function WriteFileUTF8NoBom New-Item $AbsolutePath -type file $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False [IO.File]::WriteAllLines(($FilePath | Resolve-Path), $Content, $Utf8NoBomEncoding) + +<# + .SYNOPSIS + + Write a text-file using UTF-8, but without a BOM. No-BOM UTF-8 files are required by the ServerStarterJar + from the NeoForge-project for installing and running Forge and NeoForge servers, whilst using "user_jvm_args.txt". + + .PARAMETER FilePath + The path to the file which should be writte. The file is created by this function, so no need to create it yourself. + The path must be relative to the script. The function will take care of writing it in the base of the scripts + working-directory. + + .PARAMETER Content + The content to print to the file. +#> } Function global:RunJavaCommand { param ($CommandToRun) CMD /C "`"${Java}`" ${CommandToRun}" + +<# + .SYNOPSIS + + Runs the passed string as a Java command with the Java installation set in $Java. + + .PARAMETER CommandToRun + The command to run as a Java command. +#> } Function DownloadIfNotExists @@ -159,6 +261,26 @@ Function DownloadIfNotExists Write-Host "${FileToCheck} present." return $false } + +<# + .SYNOPSIS + + Checks whether $FileToCheck exists. If not, then it is downloaded from $DownloadURL and stored as $FileToDownload. + Can be used in if-statements. + + .PARAMETER FileToCheck + The file to check for existence. + + .PARAMETER FileToDownload + The filename to which store the download as. + + .PARAMETER DownloadURL + The URL from which to download the file from. + + .OUTPUTS + + Boolean. $true if the file was downloaded and exists, $false otherwise. +#> } Function global:SetupForge @@ -216,6 +338,13 @@ Function global:SetupForge DeleteFileSilently 'server.jar' DownloadIfNotExists "server.jar" "server.jar" "https://github.com/neoforged/ServerStarterJar/releases/latest/download/server.jar" } + +<# + .SYNOPSIS + + Download and install a Forge server for $ModLoaderVersion. For Minecraft 1.17 and newer the ServerStarterJar + from the NeoForge-group is used. This has the benefit of making this server pack compatible with most hosting-companies. +#> } # If modloader = NeoForge, run NeoForge-specific checks @@ -249,6 +378,13 @@ Function global:SetupNeoForge DeleteFileSilently 'server.jar' DownloadIfNotExists "server.jar" "server.jar" "https://github.com/neoforged/ServerStarterJar/releases/latest/download/server.jar" + +<# + .SYNOPSIS + + Download and install a NeoForge server for $ModLoaderVersion. The ServerStarterJar from the NeoForge-group is used. + This has the benefit of making this server pack compatible with most hosting-companies. +#> } Function global:SetupFabric @@ -307,6 +443,14 @@ Function global:SetupFabric } } $script:ServerRunCommand = "${script:JavaArgs} -jar ${script:LauncherJarLocation} nogui" + +<# + .SYNOPSIS + + Download and install a Fabric server for $ModLoaderVersion. If the Fabric Launcher is available for $MinecraftVersion + and $ModLoaderVersion, it is downloaded and used, otherwise the regular Fabric-installer is downloaded and used. + Checks are also performed to determine whether Fabric is available for $MinecraftVersion and $ModLoaderVersion. +#> } Function global:SetupQuilt @@ -335,6 +479,13 @@ Function global:SetupQuilt } $script:LauncherJarLocation = "quilt-server-launch.jar" $script:ServerRunCommand = "${JavaArgs} -jar ${LauncherJarLocation} nogui" + +<# + .SYNOPSIS + + Download and install a Quilt server for $ModLoaderVersion. + Checks are also performed to determine whether Quilt is available for $MinecraftVersion. +#> } Function global:SetupLegacyFabric @@ -363,14 +514,23 @@ Function global:SetupLegacyFabric } $script:LauncherJarLocation = "fabric-server-launch.jar" $script:ServerRunCommand = "${JavaArgs} -jar ${LauncherJarLocation} nogui" + +<# + .SYNOPSIS + + Download and install a LegacyFabric server for $ModLoaderVersion. + Checks are also performed to determine whether LegacyFabric is available for $MinecraftVersion. +#> } Write-Host "Start script generated by ServerPackCreator SPC_SERVERPACKCREATOR_VERSION_SPC." Write-Host "To change the launch settings of this server, such as JVM args / flags, Minecraft version, modloader version etc., edit the variables.txt-file." +# Ensures we are working in the directory which contains this script. $BaseDir = Split-Path -parent $script:MyInvocation.MyCommand.Path Push-Location $BaseDir +# Check whether the path to this directory contains spaces. Spaces in the path are prone to cause trouble. if ( ${BaseDir}.Contains(" ")) { "WARNING! The current location of this script contains spaces. This may cause this server to crash!" @@ -387,6 +547,8 @@ if ( ${BaseDir}.Contains(" ")) } } +# It is not recommended to run the server using root as this introduces security risks to your system. +# Using your regular user is enough. if ( (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Write-Host "Warning! Running with administrator-privileges is not recommended." @@ -434,6 +596,8 @@ if ($JavaArgs[$JavaArgs.Length - 1] -eq '"') $JavaArgs = $JavaArgs.Substring(0, $JavaArgs.Length - 1) } +# If Java checks are desired, then the available Java version is compared to the one required by the Minecraft server. +# Should no Java be found, or an incorrect version be available, the required one is installed by running installJava. if ("${SkipJavaCheck}" -eq "true") { "Skipping Java version check." @@ -467,6 +631,8 @@ else } } +# Check and warn the user if a 32bit Java-installation is used. Realistically, this should happen less and less, but +# it does happen from time to time. Best to warn people about it. $Bit = CMD /C "`"${Java}`" -version 2>&1" if (( ${Bit} | Select-String "32-Bit").Length -gt 0) { @@ -540,6 +706,9 @@ if (!("${LauncherJarLocation}" -eq "do_not_manually_edit")) RunJavaCommand "-version" "" +# Depending on $Restart the server runs in a loop, to make sure it comes right back up after crashing. Force exit can be +# achieved by hitting CTRL+C multiple times. Variables are not reloaded between server runs. Quit the script and re-run +# it if you wish to reload the variables. while ($true) { RunJavaCommand "${AdditionalArgs} ${ServerRunCommand}" diff --git a/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_template.sh b/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_template.sh index 52b44dd6f..5b1cd5abd 100644 --- a/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_template.sh +++ b/serverpackcreator-api/src/main/resources/de/griefed/resources/server_files/default_template.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -############################################################################################### +############################################LICENSE################################################# # Copyright (C) 2024 Griefed # # This script is free software; you can redistribute it and/or @@ -18,7 +18,22 @@ # USA # # The full license can be found at https:github.com/Griefed/ServerPackCreator/blob/main/LICENSE -############################################################################################### +############################################DESCRIPTION################################################# +# +# Start script to easily run your server pack. In order to run this script even easier, run the start.bat-file +# which was also shipped with this server pack. +# +# A start-script supporting Forge, NeoForge, Fabric, Quilt and LegacyFabric as well as their supported Minecraft +# versions. +# +# This script downloads and installs the Modloader server depending on the settings in the acompanying variables.txt +# which was also shipped with this server pack. Should no suitable Java installation be found and your $JAVA-variable +# be set to "java", then a suitable Java-installation will also be downloaded and provided to this server pack. +# +# You can let the server restart by setting RESTART to true in your variables.txt. More information about the +# various settings in said file. Go check it out. +# +############################################NOTES################################################# # # Start script generated by ServerPackCreator SPC_SERVERPACKCREATOR_VERSION_SPC. # The template which was used in the generation of this script can be found at: @@ -55,20 +70,28 @@ # macOS: # You may acquire a Java 21 install here: https://adoptium.net/temurin/releases/?variant=openjdk21&version=21&package=jdk&arch=x64&os=mac +# pause +# Pause script execution. User input in the form of any keyboard key-press is required to continue execution. pause() { read -n 1 -s -r -p "Press any key to continue" } +# crashServer(reason) +# Crash script execution with exit code 1. Print $1 to the console. crashServer() { echo "${1}" pause exit 1 } +# commandAvailable(command) +# Check whether the command $1 is available for execution. Can be used in if-statements. commandAvailable() { command -v "$1" > /dev/null 2>&1 } +# getJavaVersion +# Set $JAVA_VERSION by checking $JAVA using -fullversion. Only the major version is stored, e.g. 8, 11, 17, 21. getJavaVersion() { JAVA_VERSION=$("${JAVA}" -fullversion 2>&1 | awk -F '"' '/version/ {print $2}' | cut -d'.' -f1) if [[ "$JAVA_VERSION" -eq 1 ]];then @@ -76,6 +99,8 @@ getJavaVersion() { fi } +# installJava +# Runs the companion-script "install_java.sh" to install the required Java version for this modded Minecraft server. installJava() { echo "No suitable Java installation was found on your system. Proceeding to Java installation." . install_java.sh || crashServer "Java install-script failed. Install Java $RECOMMENDED_JAVA_VERSION manually or edit JAVA in your variables.txt to point to a Java installation of said version." @@ -84,6 +109,8 @@ installJava() { fi } +# downloadIfNotExist(fileToCheck,fileToDownload,downloadURL) +# Checks whether file $1 exists. If not, then it is downloaded from $3 and stored as $2. Can be used in if-statements. downloadIfNotExist() { if [[ ! -s "${1}" ]]; then @@ -112,11 +139,16 @@ downloadIfNotExist() { fi } +# runJavaCommand(command) +# Runs the command $1 using the Java installation set in $JAVA. runJavaCommand() { "$JAVA" ${1} } -setup_forge() { +# setupForge +# Download and install a Forge server for $MODLOADER_VERSION. For Minecraft 1.17 and newer the ServerStarterJar from the +# NeoForge-group is used. This has the benefit of making this server pack compatible with most hosting-companies. +setupForge() { echo "" echo "Running Forge checks and setup..." FORGE_INSTALLER_URL="https://files.minecraftforge.net/maven/net/minecraftforge/forge/${MINECRAFT_VERSION}-${MODLOADER_VERSION}/forge-${MINECRAFT_VERSION}-${MODLOADER_VERSION}-installer.jar" @@ -169,7 +201,10 @@ setup_forge() { fi } -setup_neoforge() { +# setupNeoForge +# Download and install a NeoForge server for $MODLOADER_VERSION. The ServerStarterJar from the NeoForge-group is used. This has +# the benefit of making this server pack compatible with most hosting-companies. +setupNeoForge() { echo "" echo "Running NeoForge checks and setup..." echo "Generating user_jvm_args.txt from variables..." @@ -198,7 +233,11 @@ setup_neoforge() { downloadIfNotExist "server.jar" "server.jar" "https://github.com/neoforged/ServerStarterJar/releases/latest/download/server.jar" } -setup_fabric() { +# setupFabric +# Download and install a Fabric server for $MODLOADER_VERSION. If the Fabric Launcher is available for $MINECRAFT_VERSION +# and $MODLOADER_VERSION, it is downloaded and used, otherwise the regular Fabric-installer is downloaded and used. +# Checks are also performed to determine whether Fabric is available for $MINECRAFT_VERSION and $MODLOADER_VERSION. +setupFabric() { echo "" echo "Running Fabric checks and setup..." @@ -247,7 +286,10 @@ setup_fabric() { SERVER_RUN_COMMAND="${JAVA_ARGS} -jar ${LAUNCHER_JAR_LOCATION} nogui" } -setup_quilt() { +# setupQuilt +# Download and install a Quilt server for $MODLOADER_VERSION. +# Checks are also performed to determine whether Quilt is available for $MINECRAFT_VERSION. +setupQuilt() { echo "" echo "Running Quilt checks and setup..." @@ -279,7 +321,10 @@ setup_quilt() { SERVER_RUN_COMMAND="${JAVA_ARGS} -jar ${LAUNCHER_JAR_LOCATION} nogui" } -setup_legacyfabric() { +# setupFabric +# Download and install a LegacyFabric server for $MODLOADER_VERSION. +# Checks are also performed to determine whether LegacyFabric is available for $MINECRAFT_VERSION. +setupLegacyFabric() { echo "" echo "Running LegacyFabric checks and setup..." @@ -315,6 +360,7 @@ echo "Start script generated by ServerPackCreator SPC_SERVERPACKCREATOR_VERSION_ echo "To change the launch settings of this server, such as JVM args / flags, Minecraft version, modloader version etc., edit the variables.txt-file." # Glorious StackOverflow to the rescue: https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script/246128#246128 +# This little snipped ensures we are working in the directory which contains this script. SOURCE=${BASH_SOURCE[0]} while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd ) @@ -324,6 +370,7 @@ done DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd ) cd "${DIR}" >/dev/null 2>&1 || exit +# Check whether the path to this directory contains spaces. Spaces in the path are prone to cause trouble. if [[ "${DIR}" == *" "* ]]; then echo "WARNING! The current location of this script contains spaces. This may cause this server to crash!" @@ -342,6 +389,8 @@ if [[ "${DIR}" == *" "* ]]; then fi fi +# It is not recommended to run the server using root as this introduces security risks to your system. +# Using your regular user is enough. if [[ "$(id -u)" == "0" ]]; then echo "Warning! Running with administrator-privileges is not recommended." fi @@ -357,6 +406,8 @@ SERVER_RUN_COMMAND="do_not_manually_edit" JAVA_VERSION="do_not_manually_edit" IFS="." read -ra SEMANTICS <<<"${MINECRAFT_VERSION}" +# If Java checks are desired, then the available Java version is compared to the one required by the Minecraft server. +# Should no Java be found, or an incorrect version be available, the required one is installed by running installJava. if [[ "${SKIP_JAVA_CHECK}" == "true" ]]; then echo "Skipping Java version checks." else @@ -379,23 +430,25 @@ else fi fi +# Check and warn the user if a 32bit Java-installation is used. Realistically, this should happen less and less, but +# it does happen from time to time. Best to warn people about it. "$JAVA" "-version" 2>&1 | grep -i "32-Bit" && echo "WARNING! 32-Bit Java detected! It is highly recommended to use a 64-Bit version of Java!" case ${MODLOADER} in "Forge") - setup_forge + setupForge ;; "NeoForge") - setup_neoforge + setupNeoForge ;; "Fabric") - setup_fabric + setupFabric ;; "Quilt") - setup_quilt + setupQuilt ;; "LegacyFabric") - setup_legacyfabric + setupLegacyFabric ;; *) crashServer "Incorrect modloader specified: ${MODLOADER}" @@ -440,6 +493,9 @@ echo "Java version:" "${JAVA}" -version echo "" +# Depending on $RESTART the server runs in a loop, to make sure it comes right back up after crashing. Force exit can be +# achieved by hitting CTRL+C multiple times. Variables are not reloaded between server runs. Quit the script and re-run +# it if you wish to reload the variables. while true do runJavaCommand "${ADDITIONAL_ARGS} ${SERVER_RUN_COMMAND}"