Skip to content

Commit 2623a85

Browse files
authored
New Feature: Creating a default Variables JSON from environment variables (#469)
1 parent bd08ec2 commit 2623a85

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Typically, each property is configured instead by the UPPER_SNAKE_CASE equivalen
109109
- `EMIT_SERVER_TELEMETRY`
110110
- `MSA_GAMERTAGS_ONLY`
111111
- `ITEM_TRANSACTION_LOGGING_ENABLED`
112+
- `VARIABLES`
112113

113114
For example, to configure a flat, creative server instead of the default use:
114115

@@ -191,6 +192,29 @@ There are various tools to look XUIDs up online and they are also printed to the
191192
-e ALLOW_LIST_USERS="player1:1234567890,player2:0987654321"
192193
```
193194

195+
## Variables
196+
197+
Custom server variables are supported by Bedrock. Details and usage instructions can be found on the official bedrock documentation, located here:
198+
199+
- [Variables & Secrets - Minecraft Creator Docs](https://learn.microsoft.com/en-us/minecraft/creator/documents/scriptingservers?view=minecraft-bedrock-stable#variables-and-secrets)
200+
- [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)
201+
202+
Custom server variables are passed in as comma-separated simple key-value pairs or as a full JSON string.
203+
204+
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.
205+
206+
For greater control on types, users can provide a full string JSON representation that is used as-is.
207+
208+
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.
209+
210+
```shell
211+
# passing in simple expressions
212+
-e VARIABLES="var1=customStringValue,var2=1234,var3=true"
213+
214+
# pass in a full json object:
215+
-e VARIABLES='{"mobSpawnRate":22,"enableCheats":true,"worldArray":["My World", "Abc", 123]}'
216+
```
217+
194218
## Mods Addons
195219

196220
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/)

bedrock-entry.sh

+28
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,34 @@ if [[ -n "$ALLOW_LIST_USERS" || -n "$WHITE_LIST_USERS" ]]; then
179179
fi
180180
fi
181181

182+
if [[ -n "$VARIABLES" ]]; then
183+
echo "Setting variables"
184+
mkdir -p config/default
185+
186+
# Try to parse VARIABLES as JSON
187+
if echo "$VARIABLES" | jq empty >/dev/null 2>&1; then
188+
# VARIABLES is valid JSON
189+
echo "$VARIABLES" | jq '.' > "config/default/variables.json"
190+
else
191+
# VARIABLES is not valid JSON, attempt to parse as custom format
192+
echo "VARIABLES is not valid JSON, attempting to parse as custom format"
193+
194+
# Parse the VARIABLES using custom format (key:value,key:value)
195+
# Note: Values should not contain unescaped commas or colons
196+
jq -n --arg vars "$VARIABLES" '
197+
$vars
198+
| split(",")
199+
| map(
200+
split("=") as $kv |
201+
{ ($kv[0]): ($kv[1] | fromjson? // $kv[1]) }
202+
)
203+
| add
204+
' > "config/default/variables.json"
205+
fi
206+
fi
207+
208+
209+
182210
# prevent issue with bind mounted server.properties which can not be moved (sed tries to move the file when '-i' is used)
183211
_SERVER_PROPERTIES=$(sed '/^white-list=.*/d' server.properties) #Removes white-list= line from server.properties
184212
echo "${_SERVER_PROPERTIES}" > server.properties

0 commit comments

Comments
 (0)