Advertisement
Guest User

Medieval MC server start.ps1

a guest
Jul 20th, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.56 KB | Gaming | 0 0
  1. # Start script generated by ServerPackCreator 4.1.5.
  2. # Depending on which modloader is set, different checks are run to ensure the server will start accordingly.
  3. # If the modloader checks and setup are passed, Minecraft and EULA checks are run.
  4. # If everything is in order, the server is started.
  5.  
  6. if ( (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
  7. {
  8. Write-Host "Warning! Running with administrator-privileges is not recommended."
  9. }
  10.  
  11. # Acquire variable hashtable from variables.txt
  12. $ExternalVariablesFile = Split-Path $script:MyInvocation.MyCommand.Path
  13. $ExternalVariablesFile += "\variables.txt"
  14. $ExternalVariables = Get-Content -raw -Path $ExternalVariablesFile | ConvertFrom-StringData
  15.  
  16. # Set our in-script variables from contents of variables.txt-hashtable
  17. $MinecraftVersion = $ExternalVariables['MINECRAFT_VERSION']
  18. $ModLoader = $ExternalVariables['MODLOADER']
  19. $ModLoaderVersion = $ExternalVariables['MODLOADER_VERSION']
  20. $LegacyFabricInstallerVersion = $ExternalVariables['LEGACYFABRIC_INSTALLER_VERSION']
  21. $FabricInstallerVersion = $ExternalVariables['FABRIC_INSTALLER_VERSION']
  22. $QuiltInstallerVersion =$ExternalVariables['QUILT_INSTALLER_VERSION']
  23. $MinecraftServerUrl = $ExternalVariables['MINECRAFT_SERVER_URL']
  24. $JavaArgs = $ExternalVariables['JAVA_ARGS']
  25. $Java = $ExternalVariables['JAVA']
  26.  
  27. # Clean up quotes from the Java variable
  28. if ($Java[0] -eq '"')
  29. {
  30. $Java = $Java.Substring(1,$Java.Length-1)
  31. }
  32. if ($Java[$Java.Length-1] -eq '"')
  33. {
  34. $Java = $Java.Substring(0,$Java.Length-1)
  35. }
  36.  
  37. # Clean up quotes from the Java Args variable
  38. if ($JavaArgs[0] -eq '"')
  39. {
  40. $JavaArgs = $JavaArgs.Substring(1,$JavaArgs.Length-1)
  41. }
  42. if ($JavaArgs[$JavaArgs.Length-1] -eq '"')
  43. {
  44. $JavaArgs = $JavaArgs.Substring(0,$JavaArgs.Length-1)
  45. }
  46.  
  47. # Variables with do_not_manually_edit are set automatically during script execution,
  48. # so manually editing them will have no effect, as they will be overridden.
  49. $MinecraftServerJarLocation = "do_not_manually_edit"
  50. $LauncherJarLocation = "do_not_manually_edit"
  51. $ServerRunCommand = "do_not_manually_edit"
  52.  
  53. Function DeleteFileSilently
  54. {
  55. param ($FileToDelete)
  56.  
  57. $ErrorActionPreference = "SilentlyContinue";
  58. if ((Get-Item "${FileToDelete}").PSIsContainer)
  59. {
  60. Remove-Item "${FileToDelete}" -Recurse
  61. }
  62. else
  63. {
  64. Remove-Item "${FileToDelete}"
  65. }
  66. $ErrorActionPreference = "Continue";
  67. }
  68.  
  69. Function PauseScript
  70. {
  71. Write-Host "Press any key to continue" -ForegroundColor Yellow
  72. $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown") > $null
  73. }
  74.  
  75. Function Crash
  76. {
  77. Write-Host "Exiting..."
  78. PauseScript
  79. exit 1
  80. }
  81.  
  82. Function global:RunJavaCommand
  83. {
  84. param ($CommandToRun)
  85.  
  86. CMD /C "`"${Java}`" ${CommandToRun}"
  87. }
  88.  
  89. Function global:CheckJavaBitness
  90. {
  91. $Bit = CMD /C "`"${Java}`" -version 2>&1"
  92. if ( ( ${Bit} | Select-String "32-Bit" ).Length -gt 0)
  93. {
  94. Write-Host "WARNING! 32-Bit Java detected! It is highly recommended to use a 64-Bit version of Java!"
  95. }
  96. }
  97.  
  98. # $1 = Filename to check for
  99. # $2 = Filename to save download as
  100. # $3 = URL to download $2 from
  101. # true if the File was successfully downloaded, false if it already exists
  102. Function DownloadIfNotExists
  103. {
  104. param ($FileToCheck, $FileToDownload, $DownloadURL)
  105.  
  106. if (!(Test-Path -Path $FileToCheck -PathType Leaf))
  107. {
  108.  
  109. Write-Host "${FileToCheck} could not be found."
  110. Write-Host "Downloading ${FileToDownload}"
  111. Write-Host "from ${DownloadURL}"
  112.  
  113. Invoke-WebRequest -URI "${DownloadURL}" -OutFile "${FileToDownload}"
  114.  
  115. if (Test-Path -Path "${FileToDownload}" -PathType Leaf)
  116. {
  117. Write-Host "Download complete."
  118. return $true
  119. }
  120.  
  121. }
  122. else
  123. {
  124. Write-Host "${FileToCheck} present."
  125. return $false
  126. }
  127.  
  128. }
  129.  
  130. # If modloader = Forge, run Forge-specific checks
  131. Function global:SetupForge
  132. {
  133. ""
  134. "Running Forge checks and setup..."
  135. $ForgeInstallerUrl = "https://files.minecraftforge.net/maven/net/minecraftforge/forge/${MinecraftVersion}-${ModLoaderVersion}/forge-${MinecraftVersion}-${ModLoaderVersion}-installer.jar"
  136. $ForgeJarLocation = "do_not_manually_edit"
  137. $MINOR = ${MinecraftVersion}.Split(".")
  138.  
  139. if ($MINOR[1] -le 16)
  140. {
  141. $ForgeJarLocation = "forge.jar"
  142. $script:LauncherJarLocation = "forge.jar"
  143. $script:MinecraftServerJarLocation = "minecraft_server.${MinecraftVersion}.jar"
  144. $script:ServerRunCommand = "-Dlog4j2.formatMsgNoLookups=true ${JavaArgs} -jar ${LauncherJarLocation} nogui"
  145. }
  146. else
  147. {
  148. $ForgeJarLocation = "libraries/net/minecraftforge/forge/${MinecraftVersion}-${ModLoaderVersion}/forge-${MinecraftVersion}-${ModLoaderVersion}-server.jar"
  149. $script:MinecraftServerJarLocation = "libraries/net/minecraft/server/${MinecraftVersion}/server-${MinecraftVersion}.jar"
  150. $script:ServerRunCommand = "-Dlog4j2.formatMsgNoLookups=true @user_jvm_args.txt @libraries/net/minecraftforge/forge/${MinecraftVersion}-${ModLoaderVersion}/win_args.txt nogui"
  151.  
  152. if (!(Test-Path -Path 'user_jvm_args.txt' -PathType Leaf))
  153. {
  154. "# Xmx and Xms set the maximum and minimum RAM usage, respectively.`n" +
  155. "# They can take any number, followed by an M or a G.`n" +
  156. "# M means Megabyte, G means Gigabyte.`n" +
  157. "# For example, to set the maximum to 3GB: -Xmx3G`n" +
  158. "# To set the minimum to 2.5GB: -Xms2500M`n" +
  159. "# A good default for a modded server is 4GB.`n" +
  160. "# Uncomment the next line to set it.`n" +
  161. "# -Xmx4G`n" +
  162. "${script:JavaArgs}" | Out-File user_jvm_args.txt -encoding utf8
  163. }
  164. else
  165. {
  166. "user_jvm_args.txt present..."
  167. }
  168. }
  169.  
  170. if ((DownloadIfNotExists "${ForgeJarLocation}" "forge-installer.jar" "${ForgeInstallerUrl}"))
  171. {
  172. "Forge Installer downloaded. Installing..."
  173. RunJavaCommand "-jar forge-installer.jar --installServer"
  174.  
  175. if ($MINOR[1] -gt 16)
  176. {
  177. DeleteFileSilently 'run.bat'
  178. DeleteFileSilently 'run.sh'
  179. }
  180. else
  181. {
  182. "Renaming forge-${MinecraftVersion}-${ModLoaderVersion}.jar to forge.jar"
  183. Move-Item "forge-${MinecraftVersion}-${ModLoaderVersion}.jar" 'forge.jar'
  184. }
  185.  
  186. if ((Test-Path -Path "${ForgeJarLocation}" -PathType Leaf))
  187. {
  188. DeleteFileSilently 'forge-installer.jar'
  189. DeleteFileSilently 'forge-installer.jar.log'
  190. "Installation complete. forge-installer.jar deleted."
  191. }
  192. else
  193. {
  194. DeleteFileSilently 'forge-installer.jar'
  195. "Something went wrong during the server installation. Please try again in a couple of minutes and check your internet connection."
  196. Crash
  197. }
  198. }
  199. ""
  200. }
  201.  
  202. # If modloader = Fabric, run Fabric-specific checks
  203. Function global:SetupFabric
  204. {
  205. ""
  206. "Running Fabric checks and setup..."
  207. $FabricInstallerUrl = "https://maven.fabricmc.net/net/fabricmc/fabric-installer/${FabricInstallerVersion}/fabric-installer-${FabricInstallerVersion}.jar"
  208. $ImprovedFabricLauncherUrl = "https://meta.fabricmc.net/v2/versions/loader/${MinecraftVersion}/${ModLoaderVersion}/${FabricInstallerVersion}/server/jar"
  209.  
  210. $ErrorActionPreference = "SilentlyContinue";
  211. $script:ImprovedFabricLauncherAvailable = [int][System.Net.WebRequest]::Create("${ImprovedFabricLauncherUrl}").GetResponse().StatusCode
  212. $ErrorActionPreference = "Continue";
  213.  
  214. if ("${ImprovedFabricLauncherAvailable}" -eq "200")
  215. {
  216. "Improved Fabric Server Launcher available..."
  217. "The improved launcher will be used to run this Fabric server."
  218. $script:LauncherJarLocation = "fabric-server-launcher.jar"
  219. (DownloadIfNotExists "${script:LauncherJarLocation}" "${script:LauncherJarLocation}" "${ImprovedFabricLauncherUrl}") > $null
  220. }
  221. else
  222. {
  223. try
  224. {
  225. $ErrorActionPreference = "SilentlyContinue";
  226. $FabricAvailable = [int][System.Net.WebRequest]::Create("https://meta.fabricmc.net/v2/versions/loader/${MinecraftVersion}/${ModLoaderVersion}/server/json").GetResponse().StatusCode
  227. $ErrorActionPreference = "Continue";
  228. }
  229. catch
  230. {
  231. $FabricAvailable = "400"
  232. }
  233. if ("${FabricAvailable}" -ne "200")
  234. {
  235. "Fabric is not available for Minecraft ${MinecraftVersion}, Fabric ${ModLoaderVersion}."
  236. Crash
  237. }
  238.  
  239. if ((DownloadIfNotExists "fabric-server-launch.jar" "fabric-installer.jar" "${FabricInstallerUrl}"))
  240. {
  241. "Installer downloaded..."
  242. $script:LauncherJarLocation = "fabric-server-launch.jar"
  243. $script:MinecraftServerJarLocation = "server.jar"
  244. RunJavaCommand "-jar fabric-installer.jar server -mcversion ${MinecraftVersion} -loader ${ModLoaderVersion} -downloadMinecraft"
  245.  
  246. if ((Test-Path -Path 'fabric-server-launch.jar' -PathType Leaf))
  247. {
  248. DeleteFileSilently '.fabric-installer' -Recurse
  249. DeleteFileSilently 'fabric-installer.jar'
  250. "Installation complete. fabric-installer.jar deleted."
  251. }
  252. else
  253. {
  254. DeleteFileSilently 'fabric-installer.jar'
  255. "fabric-server-launch.jar not found. Maybe the Fabric servers are having trouble."
  256. "Please try again in a couple of minutes and check your internet connection."
  257. Crash
  258. }
  259. }
  260. else
  261. {
  262. "fabric-server-launch.jar present. Moving on..."
  263. $script:LauncherJarLocation = "fabric-server-launcher.jar"
  264. $script:MinecraftServerJarLocation = "server.jar"
  265. }
  266. }
  267. $script:ServerRunCommand = "-Dlog4j2.formatMsgNoLookups=true ${script:JavaArgs} -jar ${script:LauncherJarLocation} nogui"
  268. ""
  269. }
  270.  
  271. # If modloader = Quilt, run Quilt-specific checks
  272. Function global:SetupQuilt
  273. {
  274. ""
  275. "Running Quilt checks and setup..."
  276.  
  277. $QuiltInstallerUrl = "https://maven.quiltmc.org/repository/release/org/quiltmc/quilt-installer/${QuiltInstallerVersion}/quilt-installer-${QuiltInstallerVersion}.jar"
  278.  
  279. if ((ConvertFrom-JSON (Invoke-WebRequest -Uri "https://meta.fabricmc.net/v2/versions/intermediary/${MinecraftVersion}")).Length -eq 0)
  280. {
  281. "Quilt is not available for Minecraft ${MinecraftVersion}, Quilt ${ModLoaderVersion}."
  282. Crash
  283. }
  284. elseif ((DownloadIfNotExists "quilt-server-launch.jar" "quilt-installer.jar" "${QuiltInstallerUrl}"))
  285. {
  286. "Installer downloaded. Installing..."
  287. RunJavaCommand "-jar quilt-installer.jar install server ${MinecraftVersion} --download-server --install-dir=."
  288.  
  289. if ((Test-Path -Path 'quilt-server-launch.jar' -PathType Leaf))
  290. {
  291. DeleteFileSilently 'quilt-installer.jar'
  292. "Installation complete. quilt-installer.jar deleted."
  293. }
  294. else
  295. {
  296. DeleteFileSilently 'quilt-installer.jar'
  297. "quilt-server-launch.jar not found. Maybe the Quilt servers are having trouble."
  298. "Please try again in a couple of minutes and check your internet connection."
  299. Crash
  300. }
  301.  
  302. }
  303. else
  304. {
  305. "quilt-server-launch.jar present. Moving on..."
  306. }
  307. $script:LauncherJarLocation = "quilt-server-launch.jar"
  308. $script:MinecraftServerJarLocation = "server.jar"
  309. $script:ServerRunCommand = "-Dlog4j2.formatMsgNoLookups=true ${JavaArgs} -jar ${LauncherJarLocation} nogui"
  310. ""
  311. }
  312.  
  313. # If modloader = LegacyFabric, run LegacyFabric-specific checks
  314. Function global:SetupLegacyFabric
  315. {
  316. ""
  317. "Running LegacyFabric checks and setup..."
  318.  
  319. $LegacyFabricInstallerUrl = "https://maven.legacyfabric.net/net/legacyfabric/fabric-installer/${LegacyFabricInstallerVersion}/fabric-installer-${LegacyFabricInstallerVersion}.jar"
  320.  
  321. if ((ConvertFrom-JSON (Invoke-WebRequest -Uri "https://meta.legacyfabric.net/v2/versions/loader/${MinecraftVersion}")).Length -eq 0)
  322. {
  323. "LegacyFabric is not available for Minecraft ${MinecraftVersion}, LegacyFabric ${ModLoaderVersion}."
  324. Crash
  325. }
  326. elseif ((DownloadIfNotExists "fabric-server-launch.jar" "legacyfabric-installer.jar" "${LegacyFabricInstallerUrl}"))
  327. {
  328. "Installer downloaded. Installing..."
  329. RunJavaCommand "-jar legacyfabric-installer.jar server -mcversion ${MinecraftVersion} -loader ${ModLoaderVersion} -downloadMinecraft"
  330.  
  331. if ((Test-Path -Path 'fabric-server-launch.jar' -PathType Leaf))
  332. {
  333. DeleteFileSilently 'legacyfabric-installer.jar'
  334. "Installation complete. legacyfabric-installer.jar deleted."
  335. }
  336. else
  337. {
  338. DeleteFileSilently 'legacyfabric-installer.jar'
  339. "fabric-server-launch.jar not found. Maybe the LegacyFabric servers are having trouble."
  340. "Please try again in a couple of minutes and check your internet connection."
  341. Crash
  342. }
  343.  
  344. }
  345. else
  346. {
  347. "fabric-server-launch.jar present. Moving on..."
  348. }
  349. $script:LauncherJarLocation = "fabric-server-launch.jar"
  350. $script:MinecraftServerJarLocation = "server.jar"
  351. $script:ServerRunCommand = "-Dlog4j2.formatMsgNoLookups=true ${JavaArgs} -jar ${LauncherJarLocation} nogui"
  352. ""
  353. }
  354.  
  355. # Check for a minecraft server and download it if necessary
  356. Function global:Minecraft
  357. {
  358. ""
  359. if (($ModLoader -eq "Fabric") -and (${ImprovedFabricLauncherAvailable} -eq "200"))
  360. {
  361. "Skipping Minecraft Server JAR checks because we are using the improved Fabric Server Launcher."
  362. }
  363. else
  364. {
  365. (DownloadIfNotExists "${MinecraftServerJarLocation}" "${MinecraftServerJarLocation}" "${MinecraftServerUrl}") > $null
  366. }
  367. ""
  368. }
  369.  
  370. # Check for eula.txt and generate if necessary
  371. Function Eula
  372. {
  373. ""
  374. if (!(Test-Path -Path 'eula.txt' -PathType Leaf))
  375. {
  376. "Mojang's EULA has not yet been accepted. In order to run a Minecraft server, you must accept Mojang's EULA."
  377. "Mojang's EULA is available to read at https://aka.ms/MinecraftEULA"
  378. "If you agree to Mojang's EULA then type 'I agree'"
  379. $Answer = Read-Host -Prompt 'Answer: '
  380.  
  381. if (${Answer} -eq "I agree")
  382. {
  383. "User agreed to Mojang's EULA."
  384.  
  385. "#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://aka.ms/MinecraftEULA).`n" +
  386. "eula=true" | Out-File eula.txt -encoding utf8
  387. }
  388. else
  389. {
  390. "User did not agree to Mojang's EULA."
  391. "Entered: ${Answer}"
  392. "You can not run a Minecraft server unless you agree to Mojang#s EULA."
  393. Crash
  394. }
  395. }
  396. else
  397. {
  398. "eula.txt present. Moving on..."
  399. }
  400. ""
  401. }
  402.  
  403. if ( ${PSScriptRoot}.Contains(" "))
  404. {
  405. "WARNING! The current location of this script contains spaces. This may cause this server to crash!"
  406. "It is strongly recommended to move this server pack to a location whose path does NOT contain SPACES!"
  407. ""
  408. "Current path:"
  409. "${PSScriptRoot}"
  410. ""
  411.  
  412. $WhyMustPowerShellBeThisWayLikeSeriouslyWhatTheFrag = Read-Host -Prompt 'Are you sure you want to continue? (Yes/No): '
  413.  
  414. if (${WhyMustPowerShellBeThisWayLikeSeriouslyWhatTheFrag} -eq "Yes")
  415. {
  416. "Alrighty. Prepare for unforseen consequences, Mr. Freeman..."
  417. }
  418. else
  419. {
  420. Crash
  421. }
  422. }
  423.  
  424. # Main
  425. switch ( ${ModLoader} )
  426. {
  427. Forge
  428. {
  429. SetupForge
  430. }
  431. Fabric
  432. {
  433. SetupFabric
  434. }
  435. Quilt
  436. {
  437. SetupQuilt
  438. }
  439. LegacyFabric
  440. {
  441. SetupLegacyFabric
  442. }
  443. default
  444. {
  445. "Incorrect modloader specified: ${ModLoader}"
  446. Crash
  447. }
  448. }
  449.  
  450. CheckJavaBitness
  451. Minecraft
  452. Eula
  453.  
  454. ""
  455. "Starting server..."
  456. ""
  457. "Minecraft version: ${MinecraftVersion}"
  458. "Modloader: ${ModLoader}"
  459. "Modloader version: ${ModLoaderVersion}"
  460. if (!("${LauncherJarLocation}" -eq "do_not_manually_edit"))
  461. {
  462. "Launcher JAR: ${LauncherJarLocation}"
  463. }
  464. ""
  465. "Java args: ${JavaArgs}"
  466. "Java path: ${Java}"
  467. "Run Command: ${Java} ${ServerRunCommand}"
  468. "Java version:"
  469. RunJavaCommand "-version"
  470. ""
  471.  
  472. RunJavaCommand "${ServerRunCommand}"
  473.  
  474. ""
  475. "Exiting..."
  476. PauseScript
  477. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement