-
Notifications
You must be signed in to change notification settings - Fork 0
/
new
executable file
·199 lines (147 loc) · 6.97 KB
/
new
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
#!/bin/bash
# Create a new bedrock server instance
# Usage: ./new <instance name>
#
# <instance name>
# Name that will be used as the systemd service name in the form of `mcbs-<instance name>` and
# in the `./instances` and `./run` directories for the new bedrock server instance. This argument
# will be converted to lowercase and it's whitespace trimmed.
# Script arguments.
instance_name=$1
# Abort if <instance name> already exists in instances directory.
instance_name="$(echo "$instance_name" | awk '{print tolower($0)}')"
instance_name="$(echo -e "${instance_name}" | tr -d '[[:space:]]')"
if [ -d "instances/${instance_name}" ]; then
echo "Directory ./instances/${instance_name}/ already exists, use another instance name or delete with:"
echo ""
echo " rm -rf ./instances/${instance_name}"
echo ""
exit 1
fi
# Fetch current bedrock server download url.
echo "Trying to fetch minecraft.net bedrock server url..."
url="$(curl -s https://www.minecraft.net/en-us/download/server/bedrock -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36 Edg/93.0.961.38" | grep -P '(https:\/\/.+linux\/.+\.zip)' -o)"
filename="$(echo $url | grep -P '(bedrock-server.+\.zip)' -o)"
echo "url: [${url}]"
echo "filename: [${filename}]"
echo ""
# Download current bedrock server or skip if already downloaded.
if [ -f "cache/${filename}" ]; then
echo "${filename} exists in cache directory, download will be skipped."
else
printf "Downloading..."
wget $url -q -P cache/ &
pid=$!
spin='-\|/'
i=0
while kill -0 $pid 2>/dev/null
do
i=$(( (i+1) %4 ))
printf "\rDownloading... ${spin:$i:1}"
sleep .1
done
printf "\rDownloading... \n"
echo "Download finished."
fi
# Create ./instances/ and ./run/ if not exists.
if [ ! -d instances ]; then
mkdir instances
fi
if [ ! -d run ]; then
mkdir run
fi
# Prepare directory to run instance at ./run/<instance name>
# Extract bedrock server zip file to ./run/<instance name>
# Create launcher script for instance.
rm -rf "run/${instance_name}"
mkdir "run/${instance_name}"
echo "Extracting ${filename} to ./run/${instance_name}/"
unzip -qq "cache/${filename}" -d "run/${instance_name}/"
echo "Finished extraction."
cat > "run/${instance_name}/bedrock_server.sh" <<- EOM
#!/bin/bash
cd "\${0%/*}"
LD_LIBRARY_PATH=. ./bedrock_server
EOM
chmod 755 "run/${instance_name}/bedrock_server.sh"
chmod +x "run/${instance_name}/bedrock_server"
run_user="$(stat -c "%U" "run/${instance_name}/bedrock_server")"
run_path="$(readlink -f "run/${instance_name}")"
exec_path="$(readlink -f "run/${instance_name}/bedrock_server")"
# Create directory for instance files at ./instances/<instance name>
# Create bedrock server files
# Create systemd service unit file.
mkdir "instances/${instance_name}"
mkdir "instances/${instance_name}/worlds"
echo "Creating bedrock server files at .instances/${instance_name}/"
cp "run/${instance_name}/permissions.json" "instances/${instance_name}/permissions.json"
cp "run/${instance_name}/server.properties" "instances/${instance_name}/server.properties"
cp "run/${instance_name}/allowlist.json" "instances/${instance_name}/allowlist.json"
cat > "instances/${instance_name}/mcbs-${instance_name}.service" <<- EOM
[Unit]
Description=Minecraft Bedrock Server Instance (${instance_name})
After=network.target
After=systemd-user-sessions.service
After=network-online.target
[Service]
Type=simple
User=$run_user
WorkingDirectory=$run_path
Environment=LD_LIBRARY_PATH=.
ExecStart=$exec_path
[Install]
WantedBy=multi-user.target
EOM
# Save the current minecraft bedrock server version
printf "%s" "$filename" > "instances/${instance_name}/running-version"
# Link instance files to run directory.
echo "Linking instance files to run directory..."
ln -rsf "instances/${instance_name}/worlds" "run/${instance_name}/worlds"
ln -rsf "instances/${instance_name}/permissions.json" "run/${instance_name}/permissions.json"
ln -rsf "instances/${instance_name}/server.properties" "run/${instance_name}/server.properties"
ln -rsf "instances/${instance_name}/allowlist.json" "run/${instance_name}/allowlist.json"
# Configure the new Bedrock Server instance
PS3="?"
echo ""
echo "New instance server.properties configuration"
read -p 'Server name: ' server_name
echo "Game mode:"
select game_mode in "survival" "creative" "adventure"; do break; done
echo "Difficulty:"
select difficulty in "peaceful" "easy" "normal" "hard"; do break; done
echo "Allow cheats:"
select allow_cheats in "false" "true"; do break; done
echo "Default player permission level"
select default_player_permission_level in "visitor" "member" "operator"; do break; done
read -p 'Max players: ' max_players
read -p 'Server IPv4 port: ' server_port
read -p 'Server IPv6 port : ' server_portv6
read -p 'Level name: ' level_name
read -p 'Level seed: ' level_seed
read -p 'View distance: ' view_distance
read -p 'Max threads: ' max_threads
read -p 'Tick distance: ' tick_distance
echo ""
echo "Saving configuration..."
server_properties="$(cat instances/${instance_name}/server.properties)"
server_properties=`sed -e "s/^server-name=.*$/server-name=${server_name}/" <<< "${server_properties}"`
server_properties=`sed -e "s/^gamemode=.*$/gamemode=${game_mode}/" <<< "${server_properties}"`
server_properties=`sed -e "s/^difficulty=.*$/difficulty=${difficulty}/" <<< "${server_properties}"`
server_properties=`sed -e "s/^allow-cheats=.*$/allow-cheats=${allow_cheats}/" <<< "${server_properties}"`
server_properties=`sed -e "s/^default-player-permission-level=.*$/default-player-permission-level=${default_player_permission_level}/" <<< "${server_properties}"`
[[ ! -z "$max_players" ]] && server_properties=`sed -e "s/^max-players=.*$/max-players=${max_players}/" <<< "${server_properties}"`
[[ ! -z "$server_port" ]] && server_properties=`sed -e "s/^server-port=.*$/server-port=${server_port}/" <<< "${server_properties}"`
[[ ! -z "$server_portv6" ]] && server_properties=`sed -e "s/^server-portv6=.*$/server-portv6=${server_portv6}/" <<< "${server_properties}"`
[[ ! -z "$level_name" ]] && server_properties=`sed -e "s/^level-name=.*$/level-name=${level_name}/" <<< "${server_properties}"`
[[ ! -z "$level_seed" ]] && server_properties=`sed -e "s/^level-seed=.*$/level-seed=${level_seed}/" <<< "${server_properties}"`
[[ ! -z "$view_distance" ]] && server_properties=`sed -e "s/^view-distance=.*$/view-distance=${view_distance}/" <<< "${server_properties}"`
[[ ! -z "$max_threads" ]] && server_properties=`sed -e "s/^max-threads=.*$/max-threads=${max_threads}/" <<< "${server_properties}"`
[[ ! -z "$tick_distance" ]] && server_properties=`sed -e "s/^tick-distance=.*$/tick-distance=${tick_distance}/" <<< "${server_properties}"`
printf "%s\n" "$server_properties" > "instances/${instance_name}/server.properties"
echo "server.properties has been saved."
echo ""
echo "Bedrock Server new instance creation completed."
echo "To create, enable and start the systemd service for <${instance_name}>, run:"
echo ""
echo " sudo ./install-service ${instance_name}"
echo ""