From ffe294fbde6417aef4f8dfa4a976e8f8724e95a6 Mon Sep 17 00:00:00 2001
From: Griefed <griefed@griefed.de>
Date: Wed, 11 Sep 2024 21:35:18 +0200
Subject: [PATCH] improv: Allow toggling of ServerStarterJar refresh, allow
 specifying version of SSJ to download

References https://github.com/Griefed/ServerPackCreator/issues/531
---
 .../api/config/PackConfig.kt                  | 25 +++---------
 .../api/serverpack/ServerPackHandler.kt       | 13 ++++++-
 .../server_files/default_template.ps1         | 38 +++++++++++++++++--
 .../server_files/default_template.sh          | 27 +++++++++++--
 4 files changed, 75 insertions(+), 28 deletions(-)

diff --git a/serverpackcreator-api/src/main/kotlin/de/griefed/serverpackcreator/api/config/PackConfig.kt b/serverpackcreator-api/src/main/kotlin/de/griefed/serverpackcreator/api/config/PackConfig.kt
index de1dae16d..3628e274d 100644
--- a/serverpackcreator-api/src/main/kotlin/de/griefed/serverpackcreator/api/config/PackConfig.kt
+++ b/serverpackcreator-api/src/main/kotlin/de/griefed/serverpackcreator/api/config/PackConfig.kt
@@ -167,24 +167,9 @@ private const val spcJabbaInstallVersionKey = "SPC_JABBA_INSTALL_VERSION_SPC"
 
 private const val spcAdditionalArgsKey = "SPC_ADDITIONAL_ARGS_SPC"
 
-private val scriptSettingsDefaultKeys = arrayOf(
-    spcVersionKey,
-    spcMinecraftVersionKey,
-    spcModloaderKey,
-    spcModloaderVersionKey,
-    spcJavaArgsKey,
-    spcFabricInstallerVersionKey,
-    spcQuiltInstallerVersionKey,
-    spcLegacyFabricInstallerVersionKey,
-    spcWaitForUserInputKey,
-    spcRestartServerKey,
-    spcSkipJavaCheckKey,
-    spcJDKVendorKey,
-    spcJabbaInstallURLShKey,
-    spcJabbaInstallURLPSKey,
-    spcJabbaInstallVersionKey,
-    spcAdditionalArgsKey
-)
+private const val spcServerStarterJarForceFetchKey = "SPC_SERVERSTARTERJAR_FORCE_FETCH_SPC"
+
+private const val spcServerStarterJarVersionKey = "SPC_SERVERSTARTERJAR_VERSION_SPC"
 
 /**
  * A PackConfig contains the settings required to create a server pack.
@@ -574,7 +559,9 @@ open class PackConfig() {
                 Pair(spcJabbaInstallURLShKey,"https://github.com/Jabba-Team/jabba/raw/main/install.sh"),
                 Pair(spcJabbaInstallURLPSKey,"https://github.com/Jabba-Team/jabba/raw/main/install.ps1"),
                 Pair(spcJabbaInstallVersionKey,"0.13.0"),
-                Pair(spcAdditionalArgsKey,"-Dlog4j2.formatMsgNoLookups=true")
+                Pair(spcAdditionalArgsKey,"-Dlog4j2.formatMsgNoLookups=true"),
+                Pair(spcServerStarterJarForceFetchKey, "true"),
+                Pair(spcServerStarterJarVersionKey, "latest")
             )
         }
     }
diff --git a/serverpackcreator-api/src/main/kotlin/de/griefed/serverpackcreator/api/serverpack/ServerPackHandler.kt b/serverpackcreator-api/src/main/kotlin/de/griefed/serverpackcreator/api/serverpack/ServerPackHandler.kt
index 8f405c8d7..ed030c249 100644
--- a/serverpackcreator-api/src/main/kotlin/de/griefed/serverpackcreator/api/serverpack/ServerPackHandler.kt
+++ b/serverpackcreator-api/src/main/kotlin/de/griefed/serverpackcreator/api/serverpack/ServerPackHandler.kt
@@ -118,12 +118,21 @@ class ServerPackHandler(
         #   - JAVA be set to 'java'
         #   - No 'java' command be available OR
         #   - The available Java version behind 'java' be incompatible with your Minecraft version.
-        # JABBA_VERSION has no effect on the installation of Jabba when using PowerShell.
+        # JABBA_INSTALL_VERSION has no effect on the installation of Jabba when using PowerShell.
         # MINECRAFT_VERSION is tightly coupled with the modloader version. Be careful when changing this, as the new
         #   new version you set may not be compatible with the modloader and modloader version combination.
         # MODLOADER and MODLOADER_VERSION same thing as with MINECRAFT_VERSION. Changing any of these three values may
         #   have unforseen consequences. Well, I say unforseen, it mostly causes the server to straight up not start,
         #   because of incompatibilities. Be very careful when changing these!
+        # SERVERSTARTERJAR_FORCE_FETCH true/false allows you to enable/disable the force-refreshing of the server.jar
+        #   when using Forge or NeoForge as your modloader. Force-refreshing means the file is replaced with a freshly
+        #   downloaded one every time you run the start scripts.
+        # SERVERSTARTERJAR_VERSION allows you to manually set the version of the server.jar downloaded by the scripts.
+        #   If you want to always use the latest version, set this to exactly "latest". For a specific version, see
+        #   https://github.com/neoforged/ServerStarterJar/releases and use the tags on the left as the version,
+        #   e.g. 0.1.24 or 0.1.25. When setting a specific version, make sure the release you pick actually has a server.jar
+        #   available for download. When the download fails with the "latest"-setting, then pick a specific one and/or
+        #   contact the devs of the ServerStarterJar about the latest release not having a server.jar to download.
         #
         # DO NOT EDIT THE FOLLOWING VARIABLES MANUALLY
         #   - FABRIC_INSTALLER_VERSION
@@ -151,6 +160,8 @@ class ServerPackHandler(
         JABBA_INSTALL_URL_SH=SPC_JABBA_INSTALL_URL_SH_SPC
         JABBA_INSTALL_URL_PS=SPC_JABBA_INSTALL_URL_PS_SPC
         JABBA_INSTALL_VERSION=SPC_JABBA_INSTALL_VERSION_SPC
+        SERVERSTARTERJAR_FORCE_FETCH=SPC_SERVERSTARTERJAR_FORCE_FETCH_SPC
+        SERVERSTARTERJAR_VERSION=SPC_SERVERSTARTERJAR_VERSION_SPC
     """.trimIndent()
     private val howToStartTheServer = """
         # How To Start / Run The Server
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 8b09a0a5f..0c268668e 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
@@ -283,6 +283,36 @@ Function DownloadIfNotExists
 #>
 }
 
+Function global:RefreshServerJar
+{
+    if ("${ServerStarterJarForceFetch}" -eq "true")
+    {
+        DeleteFileSilently  'server.jar'
+    }
+
+    $ServerStarterJarDownloadURL = ""
+    if ("${ServerStarterJarVersion}" -eq "latest")
+    {
+        $ServerStarterJarDownloadURL = "https://github.com/neoforged/ServerStarterJar/releases/latest/download/server.jar"
+    }
+    else
+    {
+        $ServerStarterJarDownloadURL = "https://github.com/neoforged/ServerStarterJar/releases/download/${ServerStarterJarVersion}/server.jar"
+    }
+
+    DownloadIfNotExists "server.jar" "server.jar" "${ServerStarterJarDownloadURL}"
+
+<#
+    .SYNOPSIS
+
+    Refresh the ServerStarterJar used for running Forge and NeoForge servers.
+    Depending on the value of SERVERSTARTERJAR_FORCE_FETCH in the variables.txt the server.jar is force-refreshed.
+    Meaning: If true, the server.jar will be deleted and then downloaded again.
+    Depending on the value of SERVERSTARTERJAR_VERSION in the variables.txt a different version is fetched. More on
+    this value in the variables.txt
+#>
+}
+
 Function global:SetupForge
 {
     ""
@@ -335,8 +365,7 @@ Function global:SetupForge
 
         $script:ServerRunCommand = "@user_jvm_args.txt -Djava.security.manager=allow -jar server.jar --installer-force --installer ${ForgeInstallerUrl} nogui"
 
-        DeleteFileSilently  'server.jar'
-        DownloadIfNotExists "server.jar" "server.jar" "https://github.com/neoforged/ServerStarterJar/releases/latest/download/server.jar"
+        RefreshServerJar
     }
 
 <#
@@ -376,8 +405,7 @@ Function global:SetupNeoForge
         $script:ServerRunCommand = "@user_jvm_args.txt -jar server.jar --installer-force --installer ${ModLoaderVersion} nogui"
     }
 
-    DeleteFileSilently  'server.jar'
-    DownloadIfNotExists "server.jar" "server.jar" "https://github.com/neoforged/ServerStarterJar/releases/latest/download/server.jar"
+    RefreshServerJar
 
 <#
     .SYNOPSIS
@@ -574,6 +602,8 @@ $AdditionalArgs = $ExternalVariables['ADDITIONAL_ARGS']
 $Restart = $ExternalVariables['RESTART']
 $SkipJavaCheck = $ExternalVariables['SKIP_JAVA_CHECK']
 $RecommendedJavaVersion = $ExternalVariables['RECOMMENDED_JAVA_VERSION']
+$ServerStarterJarForceFetch = $ExternalVariables['SERVERSTARTERJAR_FORCE_FETCH']
+$ServerStarterJarVersion = $ExternalVariables['SERVERSTARTERJAR_VERSION']
 $LauncherJarLocation = "do_not_manually_edit"
 $ServerRunCommand = "do_not_manually_edit"
 $JavaVersion = "do_not_manually_edit"
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 9a10760e5..97c35b67a 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
@@ -142,9 +142,30 @@ downloadIfNotExist() {
 # runJavaCommand(command)
 # Runs the command $1 using the Java installation set in $JAVA.
 runJavaCommand() {
+  # shellcheck disable=SC2086
   "$JAVA" ${1}
 }
 
+# refreshServerJar
+# Refresh the ServerStarterJar used for running Forge and NeoForge servers.
+# Depending on the value of SERVERSTARTERJAR_FORCE_FETCH in the variables.txt the server.jar is force-refreshed.
+# Meaning: If true, the server.jar will be deleted and then downloaded again.
+# Depending on the value of SERVERSTARTERJAR_VERSION in the variables.txt a different version is fetched. More on
+# this value in the variables.txt
+refreshServerJar() {
+  if [[ "${SERVERSTARTERJAR_FORCE_FETCH}" == "true" ]]; then
+    rm -f server.jar
+  fi
+
+  if [[ "${SERVERSTARTERJAR_VERSION}" == "latest" ]]; then
+    SERVERSTARTERJAR_DOWNLOAD_URL="https://github.com/neoforged/ServerStarterJar/releases/latest/download/server.jar"
+  else
+    SERVERSTARTERJAR_DOWNLOAD_URL="https://github.com/neoforged/ServerStarterJar/releases/download/${SERVERSTARTERJAR_VERSION}/server.jar"
+  fi
+
+  downloadIfNotExist "server.jar" "server.jar" "${SERVERSTARTERJAR_DOWNLOAD_URL}"
+}
+
 # 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.
@@ -196,8 +217,7 @@ setupForge() {
 
     SERVER_RUN_COMMAND="@user_jvm_args.txt -Djava.security.manager=allow -jar server.jar --installer-force --installer ${FORGE_INSTALLER_URL} nogui"
 
-    rm -f server.jar
-    downloadIfNotExist "server.jar" "server.jar" "https://github.com/neoforged/ServerStarterJar/releases/latest/download/server.jar"
+    refreshServerJar
   fi
 }
 
@@ -229,8 +249,7 @@ setupNeoForge() {
     SERVER_RUN_COMMAND="@user_jvm_args.txt -jar server.jar --installer-force --installer ${MODLOADER_VERSION} nogui"
   fi
 
-  rm -f server.jar
-  downloadIfNotExist "server.jar" "server.jar" "https://github.com/neoforged/ServerStarterJar/releases/latest/download/server.jar"
+  refreshServerJar
 }
 
 # setupFabric
-- 
GitLab