From 2623a857831bd228b80f3de6296320a3bfc904b7 Mon Sep 17 00:00:00 2001 From: Dhruv Narayanan Date: Sat, 5 Apr 2025 08:36:25 -0500 Subject: [PATCH] New Feature: Creating a default Variables JSON from environment variables (#469) --- README.md | 24 ++++++++++++++++++++++++ bedrock-entry.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/README.md b/README.md index a9952ed..0568238 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Typically, each property is configured instead by the UPPER_SNAKE_CASE equivalen - `EMIT_SERVER_TELEMETRY` - `MSA_GAMERTAGS_ONLY` - `ITEM_TRANSACTION_LOGGING_ENABLED` +- `VARIABLES` For example, to configure a flat, creative server instead of the default use: @@ -191,6 +192,29 @@ There are various tools to look XUIDs up online and they are also printed to the -e ALLOW_LIST_USERS="player1:1234567890,player2:0987654321" ``` +## Variables + +Custom server variables are supported by Bedrock. Details and usage instructions can be found on the official bedrock documentation, located here: + +- [Variables & Secrets - Minecraft Creator Docs](https://learn.microsoft.com/en-us/minecraft/creator/documents/scriptingservers?view=minecraft-bedrock-stable#variables-and-secrets) +- [Variables & Secrets - minecraft/server-admin example](https://learn.microsoft.com/en-us/minecraft/creator/scriptapi/minecraft/server-admin/serversecrets?view=minecraft-bedrock-experimental#getplayerprofilets-1) + +Custom server variables are passed in as comma-separated simple key-value pairs or as a full JSON string. + +Server variables are parsed into their most likely type (number-like turn into numbers, all other inputs are treated as string) using [jq's `fromjson` command](https://jqlang.github.io/jq/manual/#convert-to-from-json). In the example below, `var1` is a string, `var2` is a number, and `var3` is a string. + +For greater control on types, users can provide a full string JSON representation that is used as-is. + +All variables are written to the variables file located at `config/default/variables.json`. There is no support for Module-specific variable handling at this time. + +```shell +# passing in simple expressions +-e VARIABLES="var1=customStringValue,var2=1234,var3=true" + +# pass in a full json object: +-e VARIABLES='{"mobSpawnRate":22,"enableCheats":true,"worldArray":["My World", "Abc", 123]}' +``` + ## Mods Addons Also known as behavior or resource packs, in order to add mods into your server you can follow these steps, tested with [OPS (One Player Sleep)](https://foxynotail.com/addons/ops/) and [bedrocktweaks](https://bedrocktweaks.net/resource-packs/) diff --git a/bedrock-entry.sh b/bedrock-entry.sh index 6d7675f..b638a9c 100755 --- a/bedrock-entry.sh +++ b/bedrock-entry.sh @@ -179,6 +179,34 @@ if [[ -n "$ALLOW_LIST_USERS" || -n "$WHITE_LIST_USERS" ]]; then 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(",") + | map( + split("=") as $kv | + { ($kv[0]): ($kv[1] | fromjson? // $kv[1]) } + ) + | add + ' > "config/default/variables.json" + fi +fi + + + # 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