-
-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathbedrock-entry.sh
More file actions
executable file
·516 lines (459 loc) · 16.9 KB
/
Copy pathbedrock-entry.sh
File metadata and controls
executable file
·516 lines (459 loc) · 16.9 KB
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
#!/bin/bash
set -eo pipefail
: "${DOWNLOAD_DIR:=${PWD}/.downloads}"
: "${PREVIEW:=false}"
: "${USE_MINECRAFT_SERVICES:=true}"
: "${DOWNLOAD_LINKS_URL:=https://net.web.minecraft-services.net/api/v1.0/download/links}"
: "${DOWNLOAD_SECONDARY_LINKS_URL:=https://net.web.minecraft-services.net/api/v1.0/download/links}"
: "${PROCESSED_DOWNLOAD_LINKS_URL:=https://raw.githubusercontent.com/kittizz/bedrock-server-downloads/refs/heads/main/bedrock-server-downloads.json}"
: "${RESOLVE_XUID_API_URL:=https://mcprofile.io/api/v1/bedrock/gamertag}"
: "${USE_BOX64:=true}"
: "${DEBUG_CURL:=false}"
: "${FORCE_PACK_COPY:=false}"
: "${FORCE_WORLD_COPY:=false}"
: "${LEVEL_NAME:=Bedrock level}"
: "${DOWNLOAD_PROGRESS:=false}"
function isTrue() {
[[ "${1,,}" =~ ^(true|on|1)$ ]] && return 0
return 1
}
function logWarn() {
msg=${1?}
echo "WARN $msg" >&2
}
function logError() {
msg=${1?}
echo "ERROR $msg" >&2
}
function resolveXuid() {
local value="${1?}"
if [[ $value =~ ^[0-9]+$ ]] && ((10#$value >= 10**15)); then
echo "$value"
return
fi
local encoded=$(jq -sRr @uri <<< "$value")
local xuid=$(curl "${debugCurlArgs[@]}" -fsSL -A "itzg/minecraft-bedrock-server" "$RESOLVE_XUID_API_URL/$encoded" 2>/dev/null | jq -r '.xuid // empty')
echo "${xuid:-$value}"
}
function resolvePermissionXuids() {
local list="$1" perm="$2"
IFS=',' read -ra a <<< "$(tr '\n' ',' <<< "$list")"
for v in "${a[@]}"; do
[[ -z $v ]] && continue
resolveXuid "$v" | jq -R --arg p "$perm" '{permission:$p,xuid:.}'
done
}
function replaceVersionInUrl() {
local original_url="${1?}"
local new_version="${2?}"
# Use sed to replace the version number in the URL
local modified_url
modified_url=$(echo "$original_url" | sed -E "s/(bedrock-server-)[^/]+(\.zip)/\1${new_version}\2/")
echo "$modified_url"
}
function versionFromExisting() {
# shellcheck disable=SC2012
# since find doesn't support sort by version numbers, -v
if [[ $(ls -rv bedrock_server-* 2> /dev/null|head -1) =~ bedrock_server-(.*) ]]; then
VERSION=${BASH_REMATCH[1]}
return 0
elif ls bedrock_server 2> /dev/null; then
VERSION=none
return 0
else
return 1
fi
}
function lookupDownloadUrl() {
platform=${1:?Missing required platform indicator}
if isTrue "$USE_MINECRAFT_SERVICES"; then
if downloadUrl=$(lookupDownloadUrlFromMinecraftServices "$platform"); then
echo "$downloadUrl"
return 0
else
logWarn "Failed to lookup download URL from Minecraft Services, falling back to processed links"
fi
fi
# Map entry script platform arguments to the tracker's top-level keys
if [[ "$platform" == "serverBedrockPreviewLinux" ]]; then
local type="preview"
else
local type="release"
fi
if downloadUrl=$(curl "${debugCurlArgs[@]}" -fsSL "${PROCESSED_DOWNLOAD_LINKS_URL}" | \
jq --arg type "$type" -r '
.[$type]
| to_entries
| sort_by(.key | split(".") | map(tonumber))
| last
| .value.linux.url // empty
' 2>/dev/null
); then
if [[ -n "$downloadUrl" && "$downloadUrl" != "null" ]]; then
echo "$downloadUrl"
return 0
fi
fi
return 1
}
function lookupDownloadUrlFromMinecraftServices() {
platform=${1:?Missing required platform indicator}
for url in "$DOWNLOAD_LINKS_URL" "$DOWNLOAD_SECONDARY_LINKS_URL"; do
if downloadUrl=$(
curl "${debugCurlArgs[@]}" -fsSL "${url}" |
jq --arg platform "$platform" -rR '
try(fromjson) catch({}) |
.result.links // halt_error(1) |
map(
select(.downloadType == $platform)
) |
if length > 0 then
first |
.downloadUrl
else
(
"Error: could not find platform (\($platform))\n" |
stderr |
"" |
halt_error(2)
)
end
'
); then
echo "$downloadUrl"
return 0
fi
done
return 1
}
function extractVersionFromUrl() {
url=${1:?missing url}
# shellcheck disable=SC2012
if [[ ${url} =~ http.*/.*-(.*)\.zip ]]; then
echo ${BASH_REMATCH[1]}
return 0
elif versionFromExisting; then
logWarn "Minecraft download page failed, so using existing download of $VERSION"
echo $VERSION
return 0
else
logError "Failed to derive version from download URL: ${DOWNLOAD_URL}"
return 1
fi
}
echo "Image info: $(paste -d, -s /etc/image.properties)"
debugCurlArgs=()
if [[ ${DEBUG^^} == TRUE ]]; then
set -x
debugCurlArgs+=(-v)
echo "DEBUG: running as $(id -a) with $(ls -ld /data)"
echo " current directory is $(pwd)"
fi
export HOME="${PWD}"
export TMPDIR="${HOME}/.tmp"
mkdir -p "${TMPDIR}"
if [[ ${EULA^^} != TRUE ]]; then
echo
echo "EULA must be set to TRUE to indicate agreement with the Minecraft End User License"
echo "See https://minecraft.net/terms"
echo
echo "Current value is '${EULA}'"
echo
exit 1
fi
# Check for DIRECT_DOWNLOAD_URL override first
if [[ -n "${DIRECT_DOWNLOAD_URL}" ]]; then
echo "Using direct download URL from DIRECT_DOWNLOAD_URL environment variable."
DOWNLOAD_URL="${DIRECT_DOWNLOAD_URL}"
# If VERSION is not explicitly set, try to extract it from the URL
if [[ -z "${VERSION}" ]]; then
if [[ "${DOWNLOAD_URL}" =~ bedrock-server-([0-9\.]+)\.zip ]]; then
VERSION=${BASH_REMATCH[1]}
echo "Extracted VERSION=${VERSION} from DIRECT_DOWNLOAD_URL."
else
echo "WARNING: Could not extract VERSION from DIRECT_DOWNLOAD_URL. Please ensure VERSION environment variable is set."
# Optionally exit here if VERSION is strictly required, but for testing, often the test will fail later.
fi
else
echo "VERSION=${VERSION} is explicitly set, using it with DIRECT_DOWNLOAD_URL."
fi
else # Original logic: if DIRECT_DOWNLOAD_URL is NOT set, proceed with lookup
case ${VERSION^^} in
PREVIEW)
echo "Looking up latest preview version..."
if ! DOWNLOAD_URL=$(lookupDownloadUrl serverBedrockPreviewLinux); then
logError " failed to lookup download URL"
exit 1
fi
VERSION=$(extractVersionFromUrl "$DOWNLOAD_URL")
;;
LATEST)
echo "Looking up latest version..."
if ! DOWNLOAD_URL=$(lookupDownloadUrl serverBedrockLinux); then
logError "failed to lookup download URL"
exit 1
fi
VERSION=$(extractVersionFromUrl "$DOWNLOAD_URL")
;;
EXISTING)
if versionFromExisting; then
echo "Using existing bedrock server executable"
else
logError " unable to locate existing bedrock server executable"
exit 1
fi
;;
*)
if isTrue "$PREVIEW"; then
echo "Using given preview version ${VERSION}"
if ! DOWNLOAD_URL=$(lookupDownloadUrl serverBedrockPreviewLinux); then
logError " failed to lookup Bedrock version and download URL"
exit 1
fi
DOWNLOAD_URL=$(replaceVersionInUrl "${DOWNLOAD_URL}" "${VERSION}")
else
echo "Using given version ${VERSION}"
if ! DOWNLOAD_URL=$(lookupDownloadUrl serverBedrockLinux); then
logError " failed to lookup Bedrock version and download URL"
exit 1
fi
DOWNLOAD_URL=$(replaceVersionInUrl "${DOWNLOAD_URL}" "${VERSION}")
fi
;;
esac
fi
if [[ $VERSION = none ]]; then
SERVER=bedrock_server
else
SERVER="bedrock_server-${VERSION}"
fi
if [[ ! -f "$SERVER" ]]; then
[[ $DOWNLOAD_DIR != /tmp ]] && mkdir -p "$DOWNLOAD_DIR"
TMP_ZIP="$DOWNLOAD_DIR/$(basename "${DOWNLOAD_URL}")"
echo "Downloading Bedrock server version ${VERSION} ..."
curlProgressArgs=(-fsSL)
if isTrue "$DOWNLOAD_PROGRESS"; then
curlProgressArgs=(-fL --progress-bar)
fi
if ! curl "${debugCurlArgs[@]}" "${curlProgressArgs[@]}" -o "${TMP_ZIP}" -A "itzg/minecraft-bedrock-server" "${DOWNLOAD_URL}"; then
logError " failed to download from ${DOWNLOAD_URL}
Double check that the given VERSION is valid"
exit 2
fi
# remove only binaries and some docs, to allow for an upgrade of those
rm -rf -- bedrock_server bedrock_server-* *.so release-notes.txt bedrock_server_how_to.html valid_known_packs.json premium_cache 2> /dev/null
bkupDir=backup-pre-${VERSION}
# fixup any previous interrupted upgrades
rm -rf "${bkupDir}"
for d in behavior_packs definitions minecraftpe resource_packs structures treatments world_templates; do
if [[ -d $d && -n "$(ls $d)" ]]; then
mkdir -p "${bkupDir}/$d"
echo "Backing up $d into $bkupDir"
if [[ "$d" == "resource_packs" ]]; then
# Copy over resource packs to ensure user-supplied ones remain
cp -a $d/* "${bkupDir}/$d/"
# ...however, need to fully remove Mojang provided resource packs to ensure consistent content
for rp_dir in chemistry vanilla editor; do
if [[ -d "$d/$rp_dir" ]]; then
# shellcheck disable=SC2115
rm -rf "$d/$rp_dir"
fi
done
elif [[ "$d" == "behavior_packs" ]]; then
# remove Mojang provided ones
find behavior_packs \( -name 'vanilla*' -o -name 'chemistry*' -o -name 'experimental*' \) -exec rm -rf {} +
else
mv $d "${bkupDir}/"
fi
fi
done
# remove old package backups, but keep PACKAGE_BACKUP_KEEP
if (( ${PACKAGE_BACKUP_KEEP:=2} >= 0 )); then
shopt -s nullglob
# shellcheck disable=SC2012
for d in $( ls -td1 backup-pre-* | tail +$(( PACKAGE_BACKUP_KEEP + 1 )) ); do
echo "Pruning backup directory: $d"
rm -rf "$d"
done
fi
# Do not overwrite existing files, which means the cleanup above needs to account for things
# that MUST be replaced on upgrade
unzip -q -n "${TMP_ZIP}"
[[ $DOWNLOAD_DIR != /tmp ]] && rm -rf "$DOWNLOAD_DIR"
chmod +x bedrock_server
mv bedrock_server "$SERVER"
fi
if [[ -n "${MC_PACK:-}" ]]; then
finallyShopt="$(shopt -p nullglob || true)"
shopt -s nullglob
if [[ -d "$MC_PACK" ]]; then
srcDir="$MC_PACK"
cleanupTmp=
elif [[ -f "$MC_PACK" ]]; then
srcDir=$(mktemp -d)
cleanupTmp=1
unzip -q -o "$MC_PACK" -d "$srcDir"
else
logWarn "MC_PACK is set but path does not exist or is not a file/directory: $MC_PACK"
srcDir=
fi
if [[ -n "$srcDir" ]]; then
if [[ ! -d "$srcDir/behavior_packs" && ! -d "$srcDir/resource_packs" ]] && \
[[ -f "$srcDir/data/manifest.json" || -f "$srcDir/resources/manifest.json" ]]; then
for subdir in data resources; do
packManifestFile="$srcDir/$subdir/manifest.json"
if [[ ! -f "$packManifestFile" ]]; then
continue;
fi
packId=$(jq -r '.header.uuid' "$packManifestFile")
destName="behavior_packs"; [[ "$subdir" == "resources" ]] && destName="resource_packs"
worldPacksFile="$srcDir/world_${destName}.json"
worldPacksJson=$(jq -c '[{pack_id: .header.uuid, version: .header.version}]' "$packManifestFile")
mkdir -p "$srcDir/$destName"
mv "$srcDir/$subdir" "$srcDir/$destName/$packId"
echo "$worldPacksJson" > "$worldPacksFile" && echo "Generated $worldPacksFile as $worldPacksJson"
done
fi
if [[ -d "$srcDir/addon" ]]; then
for addonSub in "$srcDir/addon"/*; do
packManifestFile="$addonSub/manifest.json"
if [[ ! -f "$packManifestFile" ]]; then
logWarn "addon/$(basename "$addonSub"): manifest.json not found; skipping";
continue;
fi
packId=$(jq -r '.header.uuid' "$packManifestFile")
packKind=$(jq -r '
[ .modules[]?.type ] as $types
| if ($types | any(IN("data","script"))) then "behavior_packs"
elif ($types | any(. == "resources")) then "resource_packs"
else empty
end
' "$packManifestFile")
if [[ -z "$packId" || "$packId" == "null" || -z "$packKind" ]]; then
logWarn "addon/$(basename "$addonSub"): unsupported manifest modules; skipping";
continue;
fi
dest="$srcDir/$packKind/$packId"
[[ -d "$dest" ]] && rm -rf "$dest"
mkdir -p "$srcDir/$packKind" && mv "$addonSub" "$dest" && echo "Moved addon $(basename "$addonSub") to $packKind/$packId"
done
rm -rf "$srcDir/addon"
fi
for dir in behavior_packs resource_packs; do
if [[ -d "$srcDir/$dir" ]]; then
mkdir -p "$dir"
for sub in "$srcDir/$dir"/*; do
destSub="$dir/$(basename "$sub")"
if isTrue "$FORCE_PACK_COPY" && [[ -d "$destSub" ]]; then
logWarn "Removing existing pack at $destSub (FORCE_PACK_COPY=TRUE)"
rm -rf "$destSub"
fi
[[ ! -d "$destSub" ]] && echo "Copying pack $(basename "$sub") to $dir" && cp -a "$sub" "$dir/"
done
fi
done
levelDir="worlds/$LEVEL_NAME"
if [[ -f "$srcDir/level.dat" ]]; then
if isTrue "$FORCE_WORLD_COPY" || [[ ! -f "$levelDir/level.dat" ]]; then
if isTrue "$FORCE_WORLD_COPY" && [[ -d "$levelDir" ]]; then
logWarn "Removing existing world at $levelDir (FORCE_WORLD_COPY=TRUE)"
rm -rf "$levelDir"
fi
for item in "$srcDir"/*; do
name=$(basename "$item")
if [[ "$name" == "behavior_packs" || "$name" == "resource_packs" || "$name" == "addon" ]]; then
continue;
fi
mkdir -p "$levelDir"
echo "Copying world item $name to $levelDir"
cp -a "$item" "$levelDir/"
done
fi
fi
[[ -n "$cleanupTmp" ]] && rm -rf "$srcDir"
fi
eval "$finallyShopt"
fi
if [[ -n "$OPS" || -n "$MEMBERS" || -n "$VISITORS" ]]; then
echo "Updating permissions"
{
resolvePermissionXuids "$OPS" operator
resolvePermissionXuids "$MEMBERS" member
resolvePermissionXuids "$VISITORS" visitor
} | jq -s '.' > permissions.json
fi
if [[ -n "$ALLOW_LIST_USERS" || -n "$WHITE_LIST_USERS" ]]; then
allowListUsers=${ALLOW_LIST_USERS:-$WHITE_LIST_USERS}
if [[ "$allowListUsers" ]]; then
echo "Setting allow list"
if [[ "$allowListUsers" != *":"* ]]; then
jq -c -n --arg users "$allowListUsers" '$users | split("[,\\n]+"; "") | map(select(length>0) | {"ignoresPlayerLimit":false,"name": .})' > "allowlist.json"
else
jq -c -n --arg users "$allowListUsers" '$users | split("[,\\n]+"; "") | map(select(length>0) | split(":") | {"ignoresPlayerLimit":false,"name": .[0], "xuid": .[1]})' > "allowlist.json"
fi
# activate server property to enable list usage
ALLOW_LIST=true
else
ALLOW_LIST=false
rm -f allowlist.json
fi
fi
if [[ -n "$VARIABLES" ]]; then
echo "Setting variables"
mkdir -p config/default
# Try to parse VARIABLES as JSON
if echo "$VARIABLES" | jq empty >/dev/null 2>&1; then
# VARIABLES is valid JSON
echo "$VARIABLES" | jq '.' > "config/default/variables.json"
else
# VARIABLES is not valid JSON, attempt to parse as custom format
echo "VARIABLES is not valid JSON, attempting to parse as custom format"
# Parse the VARIABLES using custom format (key:value,key:value)
# Note: Values should not contain unescaped commas or colons
jq -n --arg vars "$VARIABLES" '
$vars
| split("[,\\n]+"; "")
| map(
select(length>0) |
split("=") as $kv |
{ ($kv[0]): ($kv[1] | fromjson? // $kv[1]) }
)
| add
' > "config/default/variables.json"
fi
fi
if [ -f server.properties ]; then
# prevent issue with bind mounted server.properties which can not be moved (sed tries to move the file when '-i' is used)
_SERVER_PROPERTIES=$(sed '/^white-list=.*/d' server.properties) #Removes white-list= line from server.properties
echo "${_SERVER_PROPERTIES}" > server.properties
fi
export ALLOW_LIST
# update server.properties with environment settings
set-property --file server.properties --bulk /etc/bds-property-definitions.json
export LD_LIBRARY_PATH=.
: "${ENABLE_BDS_V6BIND_FIX:=false}"
if isTrue "${ENABLE_BDS_V6BIND_FIX}"; then
if [[ -f /opt/bds-ipv6fix.so ]]; then
export LD_PRELOAD=/opt/bds-ipv6fix.so
else
echo "WARNING: ENABLE_BDS_V6BIND_FIX=true but bds-ipv6fix.so is not available for this platform"
fi
fi
mcServerRunnerArgs=()
if isTrue "${ENABLE_SSH}"; then
mcServerRunnerArgs+=(--remote-console)
if ! [[ -v RCON_PASSWORD ]]; then
RCON_PASSWORD=$(openssl rand -hex 12)
export RCON_PASSWORD
fi
# For ssh access by tools, export the current password.
# Use rcon's format to align with Java, as Java uses the rcon password for SSH as well.
echo "password=${RCON_PASSWORD}" > "$HOME/.remote-console.env"
echo "password: \"${RCON_PASSWORD}\"" > "$HOME/.remote-console.yaml"
fi
echo "Starting Bedrock server..."
if [[ -f /usr/local/bin/box64 ]] && isTrue "${USE_BOX64}" ; then
exec mc-server-runner "${mcServerRunnerArgs[@]}" box64 ./"$SERVER"
else
exec mc-server-runner "${mcServerRunnerArgs[@]}" ./"$SERVER"
fi