forked from MinecraftServerControl/mscs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsctl
executable file
·3186 lines (3032 loc) · 110 KB
/
msctl
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# ---------------------------------------------------------------------------
# Copyright (c) 2011-2016, Jason M. Wood <sandain@hotmail.com>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Minecraft Server Control Script
#
# A powerful command-line control script for Linux-powered Minecraft servers.
# ---------------------------------------------------------------------------
# Get executable name
# ---------------------------------------------------------------------------
PROG=$(basename $0)
# Required Software
# ---------------------------------------------------------------------------
# Detect its presence and location for later.
JAVA=$(which java)
PERL=$(which perl)
PYTHON=$(which python)
WGET=$(which wget)
RDIFF_BACKUP=$(which rdiff-backup)
RSYNC=$(which rsync)
SOCAT=$(which socat)
# Script Usage
# ---------------------------------------------------------------------------
usage() {
cat <<EOF
Usage: $PROG [<options>] <action>
Actions:
start <world1> <world2> <...>
Start the Minecraft world server(s). Start all world servers by default.
stop <world1> <world2> <...>
Stop the Minecraft world server(s). Stop all world servers by default.
force-stop <world1> <world2> <...>
Forcibly stop the Minecraft world server(s). Forcibly stop all world
servers by default.
restart <world1> <world2> <...>
Restart the Minecraft world server(s). Restart all world servers by default.
force-restart <world1> <world2> <...>
Forcibly restart the Minecraft world server(s). Forcibly restart all world
servers by default.
create <world> <port> [<ip>]
Create a Minecraft world server. The world name and port must be
provided, the IP address is usually blank. Without arguments, create a
a default world at the default port.
import <directory> <world> <port> [<ip>]
Import an existing world server. The world name and port must be
provided, the IP address is usually blank.
rename <original world> <new world>
Rename an existing world server.
delete <world>
Delete a Minecraft world server.
disable <world1> <world2> <...>
Temporarily disables world server(s). Disables all world servers by default.
enable <world1> <world2> <...>
Enable disabled world server(s). Enables all world servers by default.
ls <option>
Display a list of worlds.
Options:
enabled Display a list of enabled worlds, default.
disabled Display a list of disabled worlds.
running Display a list of running worlds.
stopped Display a list of stopped worlds.
If no option, all available worlds are listed.
list <option>
Same as 'ls' but more detailed.
status <world1> <world2> <...>
Display the status of Minecraft world server(s). Display the status of
all world servers by default.
sync <world1> <world2> <...>
Synchronize the data stored in the mirror images of the Minecraft world
server(s). Synchronizes all of the world servers by default. This option
is only available when the mirror image option is enabled.
broadcast <command>
Broadcast a command to all running Minecraft world servers.
send <world> <command>
Send a command to a Minecraft world server.
console <world>
Connect to the Minecraft world server's console. Hit <Ctrl-D> to detach.
watch <world>
Watch the log file for the Minecraft world server.
logrotate <world1> <world2> <...>
Rotate the log file for the Minecraft world(s). Rotate the log file for
all worlds by default.
backup <world1> <world2> <...>
Backup the Minecraft world(s). Backup all worlds by default.
list-backups <world>
List the datetime of the backups for the world.
restore-backup <world> <datetime>
Restore a backup for a world that was taken at the datetime.
map <world1> <world2> <...>
Run the Minecraft Overviewer mapping software on the Minecraft world(s).
Map all worlds by default.
update <world1> <world2> <...>
Update the server software for the Minecraft world server(s). Update
server software for all worlds by default.
force-update <world1> <world2> <...>
Refresh version information prior to running update for the world
server(s), regardless of how recently the version information was updated.
Refreshes version information and updates all world servers by default.
query <world1> <world2> <...>
Run a detailed Query on the Minecraft world server(s). Run a detailed
query on all world servers by default.
Options:
-c <config_file>
Read configuration from <config_files> instead of default locations.
-l <location>
Uses <location> as the base path for data. Overrides configuration file
options.
EOF
}
mscs_defaults() {
cat <<EOF
; MSCS defaults file for adjusting global server properties.
; Default values in the script can be overridden by adding certain properties
; to one of the mscs.defaults files. The mscs.defaults files can be found
; found in one of three places depending on how the script is being used. When
; using the mscs script, the mscs.defaults file can be found at
; /opt/mscs/mscs.defaults. When using the msctl script in multi-user mode,
; the mscs.defaults file can be found at either \$HOME/mscs.defaults or
; \$HOME/.config/mscs/mscs.defaults.
; Uncomment key=value pairs (remove the #) to customize the value for your
; configuration. The values shown are the default values used in the script.
; Location of the mscs files.
# mscs-location=/opt/mscs
; Location of world files.
# mscs-worlds-location=/opt/mscs/worlds
; URL to download the version_manifest.json file.
# mscs-versions-url=https://launchermeta.mojang.com/mc/game/version_manifest.json
; Location of the version_manifest.json file.
# mscs-versions-json=/opt/mscs/version_manifest.json
; Length in minutes to keep the version_manifest.json file before updating.
# mscs-versions-duration=30
; Length in minutes to keep lock files before removing.
# mscs-lockfile-duration=1440
; Properties to return for detailed listings.
# mscs-detailed-listing=motd server-ip server-port max-players level-type online-mode
; Default world name.
# mscs-default-world=world
; Default Port.
# mscs-default-port=25565
; Default IP address. Leave this blank unless you want to bind all world
; servers to a single network interface by default.
# mscs-default-ip=
; Default version type (release or snapshot).
# mscs-default-version-type=release
; Default version of the client software. You can use the \$CURRENT_VERSION
; variable to access the latest version based on the version type selected.
# mscs-default-client-version=\$CURRENT_VERSION
; Default .jar file for the client software. The \$CLIENT_VERSION variable
; allows access to the client version selected.
# mscs-default-client-jar=\$CLIENT_VERSION.jar
; Default download URL for the client software. The \$CLIENT_VERSION variable
; allows access to the client version selected.
# mscs-default-client-url=
; Default location of the client .jar file. The \$CLIENT_VERSION variable
; allows access to the client version selected.
# mscs-default-client-location=/opt/mscs/.minecraft/versions/\$CLIENT_VERSION
; Default version of the server software. You can use the \$CURRENT_VERSION
; variable to access the latest version based on the version type selected.
# mscs-default-server-version=\$CURRENT_VERSION
; Default arguments for the JVM.
# mscs-default-jvm-args=
; Default .jar file for the server software. The \$SERVER_VERSION variable
; allows access to the server version selected.
# mscs-default-server-jar=minecraft_server.\$SERVER_VERSION.jar
; Default download URL for the server software. The \$SERVER_VERSION variable
; allows access to the server version selected.
# mscs-default-server-url=
; Default arguments for a world server.
# mscs-default-server-args=nogui
; Default initial amount of memory for a world server.
# mscs-default-initial-memory=128M
; Default maximum amount of memory for a world server.
# mscs-default-maximum-memory=2048M
; Default location of the server .jar file.
# mscs-default-server-location=/opt/mscs/server
; Default command to run for a world server. You can use the \$JAVA variable to
; access the results of \$(which java). The \$INITIAL_MEMORY and \$MAXIMUM_MEMORY
; variables provide access to the amounts of memory selected. The
; \$SERVER_LOCATION and \$SERVER_JAR variables provide access to the location
; and file name of the server software selected. The \$SERVER_ARGS variable
; provides access to the arguments for the world server selected.
# mscs-default-server-command=\$JAVA -Xms\$INITIAL_MEMORY -Xmx\$MAXIMUM_MEMORY -jar \$SERVER_LOCATION/\$SERVER_JAR \$SERVER_ARGS
; Location to store backup files.
# mscs-backup-location=/opt/mscs/backups
; Location of the backup log file.
# mscs-backup-log=/opt/mscs/backups/backup.log
; Length in days that backups survive. A value less than 1 disables backup deletion.
# mscs-backup-duration=15
; Length in days that logs survive. A value less than 1 disables log deletion.
# mscs-log-duration=15
; Enable the mirror option by default for worlds (default disabled). Change
; to a 1 to enable.
# mscs-enable-mirror=0
; Default path for the mirror files.
# mscs-mirror-path=/dev/shm/mscs
; Location of Overviewer.
# mscs-overviewer-bin=/usr/bin/overviewer.py
; URL for Overviewer.
# mscs-overviewer-url=http://overviewer.org
; Location of Overviewer generated map files.
# mscs-maps-location=/opt/mscs/maps
; URL for accessing Overviewer generated maps.
# mscs-maps-url=http://minecraft.server.com/maps
EOF
}
# ---------------------------------------------------------------------------
# Internal Methods
# ---------------------------------------------------------------------------
#
# NOTE: Nothing below this point should need to be edited directly.
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Get the PID of the Java process for the world server.
#
# @param 1 The world server of interest.
# @return The Java PID.
# ---------------------------------------------------------------------------
getJavaPID() {
local PID MCUSER
MCUSER=$(whoami)
PID=$(
ps -a -U $MCUSER -o pid,comm,args -ww |
$PERL -ne 'if ($_ =~ /^\s*(\d+)\s+java.+mscs-world='$1'$/) { print $1; }'
)
printf "%d\n" $PID
}
# ---------------------------------------------------------------------------
# Get the amount of memory used by the Java process for the world server.
#
# @param 1 The world server of interest.
# @return The amount of memory used.
# ---------------------------------------------------------------------------
getJavaMemory() {
local PID
PID=$(getJavaPID "$1")
ps --no-headers -p $PID -o rss
}
# ---------------------------------------------------------------------------
# Check to see if the world server is running.
#
# @param 1 The world server of interest.
# @return A 0 if the server is thought to be running, a 1 otherwise.
# ---------------------------------------------------------------------------
serverRunning() {
# Try to determine if the world is running.
if [ $(getJavaPID "$1") -gt 0 ]; then
return 0
else
return 1
fi
}
# ---------------------------------------------------------------------------
# Send a command to the world server.
#
# @param 1 The world server of interest.
# @param 2 The command to send.
# ---------------------------------------------------------------------------
sendCommand() {
echo "$2" | $PERL -e '
while (<>) { $_ =~ s/[\r\n]+//g; $cmd .= $_; } print "$cmd\r";
' >>$WORLDS_LOCATION/$1/console.in
}
# ---------------------------------------------------------------------------
# Check whether the item is in the list.
#
# @param 1 The item being searched for.
# @param 2 The list being searched.
# @return A 0 if the list contains the item, a 1 otherwise.
# ---------------------------------------------------------------------------
listContains() {
local MATCH ITEM
MATCH=1
for ITEM in $2; do
if [ "$ITEM" = "$1" ]; then
MATCH=0
fi
done
return $MATCH
}
# ---------------------------------------------------------------------------
# Compare two datetime strings.
#
# @param 1 The first datetime string.
# @param 2 The second datetime string.
# @return The result of the comparison: -1, 0, 1.
# ---------------------------------------------------------------------------
compareTime() {
local T1 T2
T1=$(date --date="$1" +%s)
T2=$(date --date="$2" +%s)
printf $(($T1 < $T2 ? -1 : $T1 > $T2 ? 1 : 0))
}
# ---------------------------------------------------------------------------
# Compare two Minecraft version numbers.
#
# @param 1 The first Minecraft version number.
# @param 2 The second Minecraft version number.
# @return The result of the comparison: -1, 0, 1.
# ---------------------------------------------------------------------------
compareMinecraftVersions() {
local T1 T2
T1=$(getMinecraftVersionReleaseTime "$1")
T2=$(getMinecraftVersionReleaseTime "$2")
compareTime "$T1" "$T2"
}
# ---------------------------------------------------------------------------
# Create a world.
#
# @param 1 The world server to create.
# @param 2 The port of the world server.
# @param 3 The IP address of the world server.
# ---------------------------------------------------------------------------
createWorld() {
# Create a basic server.properties file. Values not supplied here
# will use default values when the world server is first started.
mkdir -p "$WORLDS_LOCATION/$1"
setServerPropertiesValue "$1" "level-name" "$1"
setServerPropertiesValue "$1" "server-port" "$2"
setServerPropertiesValue "$1" "server-ip" "$3"
setServerPropertiesValue "$1" "enable-query" "true"
setServerPropertiesValue "$1" "query.port" "$2"
setMSCSValue "$1" "mscs-enabled" "true"
}
# ---------------------------------------------------------------------------
# Delete a world.
#
# @param 1 The world server to delete.
# ---------------------------------------------------------------------------
deleteWorld() {
# Delete the world directory.
rm -Rf "$WORLDS_LOCATION/$1"
}
# ---------------------------------------------------------------------------
# Disable a world.
#
# @param 1 The world server to disable.
# ---------------------------------------------------------------------------
disableWorld() {
# Disable the world.
setMSCSValue "$1" "mscs-enabled" "false"
}
# ---------------------------------------------------------------------------
# Enable a world.
#
# @param 1 The world server to enable.
# ---------------------------------------------------------------------------
enableWorld() {
# Enable the world.
setMSCSValue "$1" "mscs-enabled" "true"
}
# ---------------------------------------------------------------------------
# Import a world.
#
# @param 1 The location of the world to import.
# @param 2 The name of the new imported world.
# @param 3 The port of the new imported world.
# @param 4 The IP address of the new imported world.
# ---------------------------------------------------------------------------
importWorld() {
local NAME
mkdir -p "$WORLDS_LOCATION/$2"
cp -R $1/* $WORLDS_LOCATION/$2
chown $(id -un):$(id -gn) -R $WORLDS_LOCATION/$2
NAME=$(getServerPropertiesValue $2 'level-name')
if [ -d $WORLDS_LOCATION/$2/$NAME ]; then
mv $WORLDS_LOCATION/$2/$NAME $WORLDS_LOCATION/$2/$2
fi
createWorld "$2" "$3" "$4"
}
# ---------------------------------------------------------------------------
# Grab the list of enabled worlds.
#
# @return The list of enabled worlds.
# ---------------------------------------------------------------------------
getEnabledWorlds() {
local WORLD WORLDS
mkdir -p "$WORLDS_LOCATION"
WORLDS=""
for WORLD in $(ls $WORLDS_LOCATION); do
if [ -d $WORLDS_LOCATION/$WORLD ]; then
if [ "$(getMSCSValue $WORLD 'mscs-enabled' 'true')" = "true" ]; then
WORLDS="$WORLDS $WORLD"
fi
fi
done
printf "$WORLDS"
}
# ---------------------------------------------------------------------------
# Grab the list of disabled worlds.
#
# @return The list of disabled worlds.
# ---------------------------------------------------------------------------
getDisabledWorlds() {
local WORLD WORLDS
WORLDS=""
for WORLD in $(ls $WORLDS_LOCATION); do
if [ -d $WORLDS_LOCATION/$WORLD ]; then
if [ "$(getMSCSValue $WORLD 'mscs-enabled' 'true')" != "true" ]; then
WORLDS="$WORLDS $WORLD"
fi
fi
done
printf "$WORLDS"
}
# ---------------------------------------------------------------------------
# Grab the list of all available worlds.
#
# @return The list of all available worlds.
# ---------------------------------------------------------------------------
getAvailableWorlds() {
printf "$(getEnabledWorlds) $(getDisabledWorlds)"
}
# ---------------------------------------------------------------------------
# Check to see if the world is enabled.
#
# @param 1 The world of interest.
# @return A 0 if the world is enabled, a 1 otherwise.
# ---------------------------------------------------------------------------
isWorldEnabled() {
local WORLDS
WORLDS=$(getEnabledWorlds)
if [ -n "$1" ] && listContains "$1" "$WORLDS"; then
return 0
else
return 1
fi
}
# ---------------------------------------------------------------------------
# Check to see if the world is disabled.
#
# @param 1 The world of interest.
# @return A 0 if the world is disabled, a 1 otherwise.
# ---------------------------------------------------------------------------
isWorldDisabled() {
local WORLDS
WORLDS=$(getDisabledWorlds)
if [ -n "$1" ] && listContains "$1" "$WORLDS"; then
return 0
else
return 1
fi
}
# ---------------------------------------------------------------------------
# Check to see if the world is available (exists).
#
# @param 1 The world of interest.
# @return A 0 if the world is available, a 1 otherwise.
# ---------------------------------------------------------------------------
isWorldAvailable() {
local WORLDS
WORLDS=$(getAvailableWorlds)
if [ -n "$1" ] && listContains "$1" "$WORLDS"; then
return 0
else
return 1
fi
}
# ---------------------------------------------------------------------------
# Get the value of a key from the provided file.
#
# @param 1 The file containing the key/value combo.
# @param 2 The key to get.
# @param 3 The default value.
# ---------------------------------------------------------------------------
getValue() {
local KEY VALUE
# Make sure the file exists.
if [ -e "$1" ]; then
# Find the key/value combo.
KEY=$($PERL -ne '
# Remove single and double quotes plus CR and LF.
$_ =~ s/[\x22\x27\r\n]//g;
# Remove comments that begin with # or ;.
$_ =~ s/^\s*[\x23\x3B].*//;
# Extract the key.
if ($_ =~ /^\s*('$2')\s*=\s*.*$/i) { print lc $1; }
' $1)
VALUE=$($PERL -ne '
# Remove single and double quotes plus CR and LF.
$_ =~ s/[\x22\x27\r\n]//g;
# Remove comments that begin with # or ;.
$_ =~ s/^\s*[\x23\x3B].*//;
# Extract the value.
if ($_ =~ /^\s*'$2'\s*=\s*(.*)$/i) { print $1; }
' $1)
fi
# Return the value if found, the default value if not.
if [ -n "$KEY" ] && [ -n "$VALUE" ]; then
# $VALUE may contains flag-like strings not intended for printf
printf -- "$VALUE"
else
printf -- "$3"
fi
}
# ---------------------------------------------------------------------------
# Modify the value of a key/value combo in the provided file.
#
# @param 1 The file containing the key/value combo.
# @param 2 The key to modify.
# @param 3 The value to assign to the key.
# ---------------------------------------------------------------------------
setValue() {
local KEY_VALUE
# Make sure that the file exists.
touch "$1"
# Replace the key/value combo if it already exists, otherwise just
# append it to the end of the file.
KEY_VALUE=$($PERL -ne '
$_ =~ s/[\r]//;
if ($_ =~ /^('$2'=.*)$/) { print "$1"; }
' $1)
if [ -n "$KEY_VALUE" ]; then
$PERL -i -ne '
$_ =~ s/[\r]//;
if ($_ =~ /^'$2'=.*$/) { print "'$2'='$3'\n"; } else { print; }
' $1
else
printf "$2=$3\n" >>"$1"
fi
}
# ---------------------------------------------------------------------------
# Get the value of a key in the mscs.defaults file.
#
# @param 1 The key to get.
# @param 2 The default value.
# ---------------------------------------------------------------------------
getDefaultsValue() {
getValue "$MSCS_DEFAULTS" "$1" "$2"
}
# ---------------------------------------------------------------------------
# Get the value of the EULA boolean.
#
# @param 1 The world server of interest.
# ---------------------------------------------------------------------------
getEULAValue() {
local EULA_FILE
EULA_FILE=$WORLDS_LOCATION/$1/eula.txt
getValue "$EULA_FILE" "eula" "true" | $PERL -ne 'print lc'
}
# ---------------------------------------------------------------------------
# Get the value of a key in a mscs.properties file.
#
# @param 1 The world server of interest.
# @param 2 The key to get.
# @param 3 The default value.
# ---------------------------------------------------------------------------
getMSCSValue() {
local PROPERTY_FILE
PROPERTY_FILE=$WORLDS_LOCATION/$1/mscs.properties
getValue "$PROPERTY_FILE" "$2" "$3"
}
# ---------------------------------------------------------------------------
# Modify the value of a key/value combo in a mscs.properties file.
#
# @param 1 The world server of interest.
# @param 2 The key to modify.
# @param 3 The value to assign to the key.
# ---------------------------------------------------------------------------
setMSCSValue() {
local PROPERTY_FILE
PROPERTY_FILE=$WORLDS_LOCATION/$1/mscs.properties
setValue "$PROPERTY_FILE" "$2" "$3"
}
# ---------------------------------------------------------------------------
# Get the value of a key in a server.properties file.
#
# @param 1 The world server of interest.
# @param 2 The key to get.
# @param 3 The default value.
# ---------------------------------------------------------------------------
getServerPropertiesValue() {
local PROPERTY_FILE
PROPERTY_FILE="$WORLDS_LOCATION/$1/server.properties"
getValue "$PROPERTY_FILE" "$2" "$3"
}
# ---------------------------------------------------------------------------
# Modify the value of a key/value combo in a server.properties file.
#
# @param 1 The world server of interest.
# @param 2 The key to modify.
# @param 3 The value to assign to the key.
# ---------------------------------------------------------------------------
setServerPropertiesValue() {
local PROPERTY_FILE
PROPERTY_FILE=$WORLDS_LOCATION/$1/server.properties
setValue "$PROPERTY_FILE" "$2" "$3"
}
# ---------------------------------------------------------------------------
# Update the version_manifest.json file.
# ---------------------------------------------------------------------------
updateVersionsJSON() {
if [ -s $VERSIONS_JSON ]; then
# Make a backup copy of the version_manifest.json file.
cp -p "$VERSIONS_JSON" "$VERSIONS_JSON.bak"
# Delete the version_manifest.json file if it is old.
find "$VERSIONS_JSON" -mmin +"$VERSIONS_DURATION" -delete
if [ -s $VERSIONS_JSON ]; then
printf "The cached copy of the version manifest is up to date.\n"
printf "Use the force-update option to ensure a new copy is downloaded.\n"
else
printf "The version manifest cache was out of date, it has been removed.\n"
fi
fi
# Download the version_manifest.json file if it does not exist.
if [ ! -s $VERSIONS_JSON ]; then
printf "Downloading current Minecraft version manifest.\n"
$WGET --no-use-server-timestamps -qO "$VERSIONS_JSON" "$MINECRAFT_VERSIONS_URL"
if [ $? -ne 0 ]; then
if [ -s $VERSIONS_JSON.bak ]; then
printf "Error downloading the version manifest, using a backup.\n"
cp -p "$VERSIONS_JSON.bak" "$VERSIONS_JSON"
else
printf "Error downloading the version manifest, exiting.\n"
exit 1
fi
fi
fi
}
# ---------------------------------------------------------------------------
# Get the current Minecraft version number.
#
# @param 1 The world server.
# @return The current Minecraft version number.
# ---------------------------------------------------------------------------
getCurrentMinecraftVersion() {
local VERSION TYPE
# Determine the version type for the current world.
TYPE=$(getMSCSValue "$1" "mscs-version-type" "$DEFAULT_VERSION_TYPE")
# Extract the current version information.
VERSION=$($PERL -0777ne '
use JSON;
$json = decode_json ($_);
$version = $json->{latest}{'$TYPE'};
$version =~ s/[\s#%*+?^\${}()|[\]\\]/-/g;
print $version;
' $VERSIONS_JSON)
# Print an error and exit if the version string is empty.
if [ -z "$VERSION" ]; then
printf "Error detecting the current Minecraft version.\n"
exit 1
fi
printf "$VERSION"
}
# ---------------------------------------------------------------------------
# Get the sha1sum for a Minecraft client or server version.
#
# @param 1 The Minecraft version.
# @param 2 The type of sha1sum needed (client, server).
# @return The sha1sum for the Minecraft client or server.
# ---------------------------------------------------------------------------
getMinecraftVersionDownloadSHA1() {
$PERL -0777ne '
use JSON;
use LWP::Simple;
my $json = decode_json ($_);
my $version;
foreach $ver (@{$json->{versions}}) {
$id = $ver->{id};
$id =~ s/[\s#%*+?^\${}()|[\]\\]/-/g;
if ($id eq "'$1'") {
$version = $ver;
last;
}
}
$json = decode_json (get ($version->{url}));
print $json->{downloads}{'$2'}{sha1};
' $VERSIONS_JSON
}
# ---------------------------------------------------------------------------
# Get the download URL for a Minecraft client or server version.
#
# @param 1 The Minecraft version.
# @param 2 The type of download URL needed (client, server).
# @return The download URL for the Minecraft client or server.
# ---------------------------------------------------------------------------
getMinecraftVersionDownloadURL() {
$PERL -0777ne '
use JSON;
use LWP::Simple;
my $json = decode_json ($_);
my $version;
foreach $ver (@{$json->{versions}}) {
$id = $ver->{id};
$id =~ s/[\s#%*+?^\${}()|[\]\\]/-/g;
if ($id eq "'$1'") {
$version = $ver;
last;
}
}
$json = decode_json (get ($version->{url}));
print $json->{downloads}{'$2'}{url};
' $VERSIONS_JSON
}
# ---------------------------------------------------------------------------
# Get the release time of the Minecraft version number.
#
# @param 1 The Minecraft version.
# @return The release time of the Minecraft version number.
# ---------------------------------------------------------------------------
getMinecraftVersionReleaseTime() {
$PERL -0777ne '
use JSON;
$json = decode_json ($_);
foreach $ver (@{$json->{versions}}) {
$id = $ver->{id};
$id =~ s/[\s#%*+?^\${}()|[\]\\]/-/g;
print $ver->{releaseTime} if ($id eq "'$1'");
}
' $VERSIONS_JSON
}
# ---------------------------------------------------------------------------
# Retrieve the version of the client for the world.
#
# @param 1 The world server.
# @return CLIENT_VERSION
# ---------------------------------------------------------------------------
getClientVersion() {
local CURRENT_VERSION
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
# Get the client version, use the default version if not provided.
getMSCSValue "$1" "mscs-client-version" "$DEFAULT_CLIENT_VERSION" |
$PERL -ne '
$current_version="'$CURRENT_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
$_ =~ s/[\s#%*+?^\${}()|[\]\\]/-/g;
print;
'
}
# ---------------------------------------------------------------------------
# Retrieve the .jar file name for the client for the world.
#
# @param 1 The world server.
# @return CLIENT_JAR
# ---------------------------------------------------------------------------
getClientJar() {
local CURRENT_VERSION CLIENT_VERSION
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
CLIENT_VERSION=$(getClientVersion "$1")
if [ $? -ne 0 ]; then
printf "$CLIENT_VERSION\n"
exit 1
fi
# Get the client jar, use the default value if not provided.
getMSCSValue "$1" "mscs-client-jar" "$DEFAULT_CLIENT_JAR" |
$PERL -ne '
$current_version="'$CURRENT_VERSION'";
$client_version="'$CLIENT_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
$_ =~ s/\$CLIENT_VERSION/$client_version/g;
print;
'
}
# ---------------------------------------------------------------------------
# Retrieve the location of the client files for the world.
#
# @param 1 The world server.
# @return CLIENT_LOCATION
# ---------------------------------------------------------------------------
getClientLocation() {
local CURRENT_VERSION CLIENT_VERSION
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
CLIENT_VERSION=$(getClientVersion "$1")
if [ $? -ne 0 ]; then
printf "$CLIENT_VERSION\n"
exit 1
fi
# Get the client location, use the default value if not provided.
getMSCSValue "$1" "mscs-client-location" "$DEFAULT_CLIENT_LOCATION" |
$PERL -ne '
$current_version="'$CURRENT_VERSION'";
$client_version="'$CLIENT_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
$_ =~ s/\$CLIENT_VERSION/$client_version/g;
print;
'
}
# ---------------------------------------------------------------------------
# Retrieve the URL to download the client for the world.
#
# @param 1 The world server.
# @return CLIENT_URL
# ---------------------------------------------------------------------------
getClientURL() {
local CURRENT_VERSION CLIENT_VERSION URL
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
CLIENT_VERSION=$(getClientVersion "$1")
if [ $? -ne 0 ]; then
printf "$CLIENT_VERSION\n"
exit 1
fi
# Get the client download URL, use the default value if not provided.
URL=$(getMSCSValue "$1" "mscs-client-url" "$DEFAULT_CLIENT_URL" | $PERL -ne '
$current_version="'$CURRENT_VERSION'";
$client_version="'$CLIENT_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
$_ =~ s/\$CLIENT_VERSION/$client_version/g;
$_ =~ s/\\\:/\:/g;
print;
')
# If the download URL was not specified, extract it from the version
# manifest.
if [ -z "$URL" ]; then
URL=$(getMinecraftVersionDownloadURL $CLIENT_VERSION 'client')
fi
printf "$URL"
}
# ---------------------------------------------------------------------------
# Retrieve the version of the server running the world.
#
# @param 1 The world server.
# @return SERVER_VERSION
# ---------------------------------------------------------------------------
getServerVersion() {
local CURRENT_VERSION
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
# Get the server version, use the default version if not provided.
getMSCSValue "$1" "mscs-server-version" "$DEFAULT_SERVER_VERSION" |
$PERL -ne '
$current_version="'$CURRENT_VERSION'";
$_ =~ s/\$CURRENT_VERSION/$current_version/g;
$_ =~ s/[\s#%*+?^\${}()|[\]\\]/-/g;
print;
'
}
# ---------------------------------------------------------------------------
# Retrieve the .jar file name for the server running the world.
#
# @param 1 The world server.
# @return SERVER_JAR
# ---------------------------------------------------------------------------
getServerJar() {
local CURRENT_VERSION SERVER_VERSION SERVER_JAR
CURRENT_VERSION=$(getCurrentMinecraftVersion "$1")
if [ $? -ne 0 ]; then
printf "$CURRENT_VERSION\n"
exit 1
fi
SERVER_VERSION=$(getServerVersion "$1")
if [ $? -ne 0 ]; then
printf "$SERVER_VERSION\n"
exit 1
fi
# Get the server jar, use the default value if not provided.
getMSCSValue "$1" "mscs-server-jar" "$DEFAULT_SERVER_JAR" |
$PERL -ne '