-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.ps1
394 lines (348 loc) · 13 KB
/
start.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# Start script generated by ServerPackCreator 3.11.1.
# 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.
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."
}
Set-Location "$PSScriptRoot"
$MinecraftVersion = "1.16.5"
$ModLoader = "Forge"
$ModLoaderVersion = "36.2.23"
$Args = ""
$Java = "java"
$FabricInstallerVersion = "0.11.0"
$QuiltInstallerVersion = "0.4.3"
$MinecraftServerUrl = "https://launcher.mojang.com/v1/objects/1b557e7b033b583cd9f66746b7a9ab1ec1673ced/server.jar"
# Variables with do_not_manually_edit are set automatically during script execution,
# so manually editing them will have no effect, as they will be overridden.
$MinecraftServerJarLocation = "do_not_manually_edit"
$LauncherJarLocation = "do_not_manually_edit"
$ServerRunCommand = "do_not_manually_edit"
Function DeleteFileSilently
{
param ($FileToDelete)
$ErrorActionPreference = "SilentlyContinue";
if ((Get-Item "${FileToDelete}").PSIsContainer)
{
Remove-Item "${FileToDelete}" -Recurse
}
else
{
Remove-Item "${FileToDelete}"
}
$ErrorActionPreference = "Continue";
}
Function PauseScript
{
Write-Host "Press any key to continue" -ForegroundColor Yellow
$host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown") > $null
}
Function Crash
{
Write-Host "Exiting..."
PauseScript
exit 1
}
Function global:RunJavaCommand
{
param ($CommandToRun)
CMD /C "`"${Java}`" ${CommandToRun}"
}
# $1 = Filename to check for
# $2 = Filename to save download as
# $3 = URL to download $2 from
# true if the File was successfully downloaded, false if it already exists
Function DownloadIfNotExists
{
param ($FileToCheck, $FileToDownload, $DownloadURL)
if (!(Test-Path -Path $FileToCheck -PathType Leaf))
{
Write-Host "${FileToCheck} could not be found."
Write-Host "Downloading ${FileToDownload}"
Write-Host "from ${DownloadURL}"
Invoke-WebRequest -URI "${DownloadURL}" -OutFile "${FileToDownload}"
if (Test-Path -Path "${FileToDownload}" -PathType Leaf)
{
Write-Host "Download complete."
return $true
}
}
else
{
Write-Host "${FileToCheck} present."
return $false
}
}
# If modloader = Forge, run Forge-speci}c checks
Function global:SetupForge
{
""
"Running Forge checks and setup..."
$ForgeInstallerUrl = "https://files.minecraftforge.net/maven/net/minecraftforge/forge/${MinecraftVersion}-${ModLoaderVersion}/forge-${MinecraftVersion}-${ModLoaderVersion}-installer.jar"
$ForgeJarLocation = "do_not_manually_edit"
$MINOR = ${MinecraftVersion}.Split(".")
if ($MINOR[1] -le 16)
{
$ForgeJarLocation = "forge.jar"
$script:LauncherJarLocation = "forge.jar"
$script:MinecraftServerJarLocation = "minecraft_server.${MinecraftVersion}.jar"
$script:ServerRunCommand = "-Dlog4j2.formatMsgNoLookups=true ${Args} -jar ${LauncherJarLocation} nogui"
}
else
{
$ForgeJarLocation = "libraries/net/minecraftforge/forge/${MinecraftVersion}-${ModLoaderVersion}/forge-${MinecraftVersion}-${ModLoaderVersion}-server.jar"
$script:MinecraftServerJarLocation = "libraries/net/minecraft/server/${MinecraftVersion}/server-${MinecraftVersion}.jar"
$script:ServerRunCommand = "-Dlog4j2.formatMsgNoLookups=true @user_jvm_args.txt @libraries/net/minecraftforge/forge/${MinecraftVersion}-${ModLoaderVersion}/win_args.txt nogui"
if (!(Test-Path -Path 'user_jvm_args.txt' -PathType Leaf))
{
"# Xmx and Xms set the maximum and minimum RAM usage, respectively.`n" +
"# They can take any number, followed by an M or a G.`n" +
"# M means Megabyte, G means Gigabyte.`n" +
"# For example, to set the maximum to 3GB: -Xmx3G`n" +
"# To set the minimum to 2.5GB: -Xms2500M`n" +
"# A good default for a modded server is 4GB.`n" +
"# Uncomment the next line to set it.`n" +
"# -Xmx4G`n" +
"${script:Args}" | Out-File user_jvm_args.txt -encoding utf8
}
else
{
"user_jvm_args.txt present..."
}
}
if ((DownloadIfNotExists "${ForgeJarLocation}" "forge-installer.jar" "${ForgeInstallerUrl}"))
{
"Forge Installer downloaded. Installing..."
RunJavaCommand "-jar forge-installer.jar --installServer"
if ($MINOR[1] -gt 16)
{
DeleteFileSilently 'run.bat'
DeleteFileSilently 'run.sh'
}
else
{
"Renaming forge-${MinecraftVersion}-${ModLoaderVersion}.jar to forge.jar"
Move-Item "forge-${MinecraftVersion}-${ModLoaderVersion}.jar" 'forge.jar'
}
if ((Test-Path -Path "${ForgeJarLocation}" -PathType Leaf))
{
DeleteFileSilently 'forge-installer.jar'
DeleteFileSilently 'forge-installer.jar.log'
"Installation complete. forge-installer.jar deleted."
}
else
{
DeleteFileSilently 'forge-installer.jar'
"Something went wrong during the server installation. Please try again in a couple of minutes and check your internet connection."
Crash
}
}
""
}
# If modloader = Fabric, run Fabric-speci}c checks
Function global:SetupFabric
{
""
"Running Fabric checks and setup..."
$FabricInstallerUrl = "https://maven.fabricmc.net/net/fabricmc/fabric-installer/${FabricInstallerVersion}/fabric-installer-${FabricInstallerVersion}.jar"
$ImprovedFabricLauncherUrl = "https://meta.fabricmc.net/v2/versions/loader/${MinecraftVersion}/${ModLoaderVersion}/${FabricInstallerVersion}/server/jar"
$ErrorActionPreference = "SilentlyContinue";
$script:ImprovedFabricLauncherAvailable = [int][System.Net.WebRequest]::Create("${ImprovedFabricLauncherUrl}").GetResponse().StatusCode
$ErrorActionPreference = "Continue";
if ("${ImprovedFabricLauncherAvailable}" -eq "200")
{
"Improved Fabric Server Launcher available..."
"The improved launcher will be used to run this Fabric server."
$script:LauncherJarLocation = "fabric-server-launcher.jar"
(DownloadIfNotExists "${script:LauncherJarLocation}" "${script:LauncherJarLocation}" "${ImprovedFabricLauncherUrl}") > $null
}
else
{
try
{
$ErrorActionPreference = "SilentlyContinue";
$FabricAvailable = [int][System.Net.WebRequest]::Create("https://meta.fabricmc.net/v2/versions/loader/${MinecraftVersion}/${ModLoaderVersion}/server/json").GetResponse().StatusCode
$ErrorActionPreference = "Continue";
}
catch
{
$FabricAvailable = "400"
}
if ("${FabricAvailable}" -ne "200")
{
"Fabric is not available for Minecraft ${MinecraftVersion}, Fabric ${ModLoaderVersion}."
Crash
}
if ((DownloadIfNotExists "fabric-server-launch.jar" "fabric-installer.jar" "${FabricInstallerUrl}"))
{
"Installer downloaded..."
$script:LauncherJarLocation = "fabric-server-launch.jar"
$script:MinecraftServerJarLocation = "server.jar"
RunJavaCommand "-jar fabric-installer.jar server -mcversion ${MinecraftVersion} -loader ${ModLoaderVersion} -downloadMinecraft"
if ((Test-Path -Path 'fabric-server-launch.jar' -PathType Leaf))
{
DeleteFileSilently '.fabric-installer' -Recurse
DeleteFileSilently 'fabric-installer.jar'
"Installation complete. fabric-installer.jar deleted."
}
else
{
DeleteFileSilently 'fabric-installer.jar'
"fabric-server-launch.jar not found. Maybe the Fabric servers are having trouble."
"Please try again in a couple of minutes and check your internet connection."
Crash
}
}
else
{
"fabric-server-launch.jar present. Moving on..."
$script:LauncherJarLocation = "fabric-server-launcher.jar"
$script:MinecraftServerJarLocation = "server.jar"
}
}
$script:ServerRunCommand = "-Dlog4j2.formatMsgNoLookups=true ${script:Args} -jar ${script:LauncherJarLocation} nogui"
""
}
# If modloader = Quilt, run Quilt-speci}c checks
Function global:SetupQuilt
{
""
"Running Quilt checks and setup..."
$QuiltInstallerUrl = "https://maven.quiltmc.org/repository/release/org/quiltmc/quilt-installer/${QuiltInstallerVersion}/quilt-installer-${QuiltInstallerVersion}.jar"
if ((ConvertFrom-JSON (Invoke-WebRequest -Uri "https://meta.fabricmc.net/v2/versions/intermediary/${MinecraftVersion}")).Length -eq 0)
{
"Quilt is not available for Minecraft ${MinecraftVersion}, Quilt ${ModLoaderVersion}."
Crash
}
elseif ((DownloadIfNotExists "quilt-server-launch.jar" "quilt-installer.jar" "${QuiltInstallerUrl}"))
{
"Installer downloaded. Installing..."
RunJavaCommand "-jar quilt-installer.jar install server ${MinecraftVersion} --download-server --install-dir=."
if ((Test-Path -Path 'quilt-server-launch.jar' -PathType Leaf))
{
DeleteFileSilently 'quilt-installer.jar'
"Installation complete. quilt-installer.jar deleted."
}
else
{
DeleteFileSilently 'quilt-installer.jar'
"quilt-server-launch.jar not found. Maybe the Quilt servers are having trouble."
"Please try again in a couple of minutes and check your internet connection."
Crash
}
}
else
{
"quilt-server-launch.jar present. Moving on..."
}
$script:LauncherJarLocation = "quilt-server-launch.jar"
$script:MinecraftServerJarLocation = "server.jar"
$script:ServerRunCommand = "-Dlog4j2.formatMsgNoLookups=true ${Args} -jar ${LauncherJarLocation} nogui"
""
}
# Check for a minecraft server and download it if necessary
Function global:Minecraft
{
""
if (($ModLoader -eq "Fabric") -and (${ImprovedFabricLauncherAvailable} -eq "200"))
{
"Skipping Minecraft Server JAR checks because we are using the improved Fabric Server Launcher."
}
else
{
(DownloadIfNotExists "${MinecraftServerJarLocation}" "${MinecraftServerJarLocation}" "${MinecraftServerUrl}") > $null
}
""
}
# Check for eula.txt and generate if necessary
Function Eula
{
""
if (!(Test-Path -Path 'eula.txt' -PathType Leaf))
{
"Mojang's EULA has not yet been accepted. In order to run a Minecraft server, you must accept Mojang's EULA."
"Mojang's EULA is available to read at https://aka.ms/MinecraftEULA"
"If you agree to Mojang's EULA then type 'I agree'"
$Answer = Read-Host -Prompt 'Answer: '
if (${Answer} -eq "I agree")
{
"User agreed to Mojang's EULA."
"#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://aka.ms/MinecraftEULA).`n" +
"eula=true" | Out-File eula.txt -encoding utf8
}
else
{
"User did not agree to Mojang's EULA."
"Entered: ${Answer}"
"You can not run a Minecraft server unless you agree to Mojang#s EULA."
Crash
}
}
else
{
"eula.txt present. Moving on..."
}
""
}
if ( ${PSScriptRoot}.Contains(" "))
{
"WARNING! The current location of this script contains spaces. This may cause this server to crash!"
"It is strongly recommended to move this server pack to a location whose path does NOT contain SPACES!"
""
"Current path:"
"${PSScriptRoot}"
""
$WhyMustPowerShellBeThisWayLikeSeriouslyWhatTheFrag = Read-Host -Prompt 'Are you sure you want to continue? (Yes/No): '
if (${WhyMustPowerShellBeThisWayLikeSeriouslyWhatTheFrag} -eq "Yes")
{
"Alrighty. Prepare for unforseen consequences, Mr. Freeman..."
}
else
{
Crash
}
}
# Main
if ("${ModLoader}" -eq "Forge")
{
SetupForge
}
elseif ( "${ModLoader}" -eq "Fabric" )
{
SetupFabric
}
elseif ( "${ModLoader}" -eq "Quilt" )
{
SetupQuilt
}
else
{
"Incorrect modloader specified."
Crash
}
Minecraft
Eula
""
"Starting server..."
""
"Minecraft version: ${MinecraftVersion}"
"Modloader: ${ModLoader}"
"Modloader version: ${ModLoaderVersion}"
if (!("${LauncherJarLocation}" -eq "do_not_manually_edit"))
{
"Launcher JAR: ${LauncherJarLocation}"
}
""
"Java args: ${Args}"
"Java path: ${Java}"
"Run Command: ${Java} ${ServerRunCommand}"
"Java version:"
RunJavaCommand "-version"
""
RunJavaCommand "${ServerRunCommand}"
""
"Exiting..."
PauseScript
exit 0