-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmcserver.sh
303 lines (276 loc) · 9.69 KB
/
mcserver.sh
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
#!/bin/bash
# Ram selection sanitization
if ! echo "$MC_RAM" | grep -Eq '^[0-9]+[MG]$'; then
if ! [ -z "$MC_RAM" ]
then
echo "\033[0;31mError: $MC_RAM is not a valid RAM format. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
fi
# Declare supported Lazymc archs
lazymc_supported_archs="aarch64 x64 armv7"
# Getting arch from system
CPU_ARCH=$(uname -m)
# Adapt the answer in x86 case to support Lazymc url schema
[ $CPU_ARCH = "x86_64" ] && CPU_ARCH="x64"
# Check if Lazymc is supported for that arch, if not, continue disabling Lazymc
if ! echo "$lazymc_supported_archs" | grep -wq "$CPU_ARCH"
then
echo "\033[0;31mWarning! Your CPU architecture ($CPU_ARCH) is not supported by Lazymc. Disabling it... \033[0m"
LAZYMC_VERSION="disabled"
fi
# Enter server directory
cd mcserver
#display current config to the user and save to server_cfg.txt
echo ""
echo "\033[0;33mMinecraft server settings: \033[0m" | tee server_cfg.txt
echo "" | tee -a server_cfg.txt
echo "Minecraft Version= \033[0;33m$MC_VERSION\033[0m" | tee -a server_cfg.txt
echo "Lazymc version= \033[0;33m$LAZYMC_VERSION\033[0m" | tee -a server_cfg.txt
echo "Server provider= \033[0;33m$SERVER_PROVIDER\033[0m" | tee -a server_cfg.txt
echo "Server build= \033[0;33m$SERVER_BUILD\033[0m" | tee -a server_cfg.txt
echo "Dedicated RAM= \033[0;33m${MC_RAM:-"Not specified."}\033[0m" | tee -a server_cfg.txt
echo "Java options= \033[0;33m${JAVA_OPTS:-"Not specified."}\033[0m" | tee -a server_cfg.txt
echo ""
echo "\033[0;33mCurrent configuration saved to mcserver/server_cfg.txt \033[0m"
echo ""
#give user time to read
sleep 1
# Lazymc setup handler
if [ "$LAZYMC_VERSION" = "disabled" ]
then
echo "\033[0;33mSkipping lazymc download... \033[0m"
echo ""
else
if [ "$LAZYMC_VERSION" = "latest" ]
then
LAZYMC_VERSION=$(wget -qO - https://api.github.com/repos/timvisee/lazymc/releases/latest | jq -r .tag_name | cut -c 2-)
if [ -z "$LAZYMC_VERSION" ]
then
echo "\033[0;31mError: Could not get latest version of lazymc. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
fi
LAZYMC_URL="https://github.com/timvisee/lazymc/releases/download/v$LAZYMC_VERSION/lazymc-v$LAZYMC_VERSION-linux-$CPU_ARCH"
status_code=$(curl -s -o /dev/null -w '%{http_code}' ${LAZYMC_URL})
if [ "$status_code" -ne 302 ]
then
echo "\033[0;31mError: Lazymc $LAZYMC_VERSION version does not exist or is not available. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
echo "\033[0;33mDownloading lazymc $LAZYMC_VERSION... \033[0m"
echo ""
wget -qO lazymc ${LAZYMC_URL}
chmod +x lazymc
fi
# Declaring supported types
allowed_modded_type="fabric forge"
allowed_servers_type="paper purpur"
# Determine server type
if [ "$SERVER_PROVIDER" = "vanilla" ]
then
SERVER_TYPE="vanilla"
elif [ -z "$SERVER_PROVIDER" ] || echo "$allowed_modded_type" | grep -wq "$SERVER_PROVIDER"
then
SERVER_TYPE="modded"
elif [ -z "$SERVER_PROVIDER" ] || echo "$allowed_servers_type" | grep -wq "$SERVER_PROVIDER"
then
SERVER_TYPE="servers"
else
echo "\033[0;31mError: $SERVER_PROVIDER is not supported. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
# FETCH LATEST VER API - thx to serverjars.com
API_FETCH_LATEST="https://serverjars.com/api/fetchLatest/${SERVER_TYPE}/${SERVER_PROVIDER}"
# FETCH VER DETAILS API - thx to serverjars.com
API_FETCH_DETAILS="https://serverjars.com/api/fetchDetails/${SERVER_TYPE}/${SERVER_PROVIDER}/${MC_VERSION}"
# Get the latest MC version
if [ ${MC_VERSION} = latest ]
then
echo "\033[0;33mGetting latest Minecraft version... \033[0m"
echo ""
if ! MC_VERSION=$(wget -qO - $API_FETCH_LATEST | jq -r '.response.version')
then
echo "\033[0;31mError: Could not get latest version of Minecraft. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
else
# Check if the version exists
if ! [ ${MC_VERSION} = "$(wget -qO - $API_FETCH_DETAILS | jq -r '.response.version')" ]
then
echo "\033[0;31mError: Minecraft version $MC_VERSION version does not exist or is not available. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
fi
# FETCH JAR API - thx to serverjars.com
API_FETCH_JAR="https://serverjars.com/api/fetchJar/${SERVER_TYPE}/${SERVER_PROVIDER}/${MC_VERSION}"
# Set the BUILD_FETCH_API value based on SERVER_PROVIDER
case $SERVER_PROVIDER in
"paper") BUILD_FETCH_API="https://papermc.io/api/v2/projects/paper/versions/${MC_VERSION}/builds/${SERVER_BUILD}";;
"purpur") BUILD_FETCH_API="https://api.purpurmc.org/v2/purpur/${MC_VERSION}/${SERVER_BUILD}";;
*) echo "\033[0;33mSkipping build check because $SERVER_PROVIDER does not support custom builds number \033[0m"
echo "";;
esac
#Server build handler
if [ ${SERVER_BUILD} = latest ]
then
# Get the latest build - GIMMICK CHECK CODE SINCE MAJOR SCRIPT UPDATE
echo "\033[0;33mGetting latest build for ${SERVER_PROVIDER}... \033[0m"
echo ""
else
# Check if the build exists
echo "\033[0;33mChecking existance of $SERVER_BUILD build for ${SERVER_PROVIDER} \033[0m"
echo ""
status_code=$(curl -s -o /dev/null -w '%{http_code}' ${BUILD_FETCH_API})
if [ "$status_code" -ne 200 ]
then
echo "\033[0;31mError: ${SERVER_PROVIDER} $SERVER_BUILD build does not exist or is not available. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
fi
# Set the jar file name
JAR_NAME=${SERVER_PROVIDER}-${MC_VERSION}-${SERVER_BUILD}.jar
# Update jar if necessary
if [ ! -e ${JAR_NAME} ]
then
# Remove old server jar(s)
echo "\033[0;33mRemoving old server jars... \033[0m"
echo ""
rm -f *.jar
fi
# Download new server jar
echo "\033[0;33mDownloading $JAR_NAME \033[0m"
echo ""
if ! curl -o ${JAR_NAME} -sS ${API_FETCH_JAR}
then
echo "\033[0;31mError: Jar URL does not exist or is not available. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
# install forge if necessary
if [ "$SERVER_PROVIDER" = "forge" ]
then
# .installed file is used to check if forge is already installed by a previous run of the container
if [ ! -e .installed ]
then
echo "\033[0;33mInstalling Forge... This will take a while...\033[0m"
echo ""
if ! java -jar $JAR_NAME --installServer > /dev/null 2>&1
then
echo "\033[0;31mError: Could not install Forge. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
touch .installed
else
echo "\033[0;33mForge already installed. Skipping installation... \033[0m"
echo ""
fi
fi
#determine run command
if [ -z "$RUN_COMMAND" ]
then
if [ "$SERVER_PROVIDER" = "forge" ]
then
#parse the mincraft verison if it is 1.17.0 or higher we need to use the new forge run command
mcmajor=$(echo $MC_VERSION | cut -d'.' -f1)
mcminor=$(echo $MC_VERSION | cut -d'.' -f2)
mcpatch=$(echo $MC_VERSION | cut -d'.' -f3)
if [ $mcmajor -ge 1 ] && [ $mcminor -ge 17 ] && [ $mcpatch -ge 0 ]
then
#grep the java line from the Run.sh file
echo "\033[0;33mGetting new forge run command from run.sh... \033[0m"
echo ""
rcmd=$(grep -m 1 "java" /mcserver/run.sh)
#strip the "$@" from the end of the line and add nogui to the end
rcmd=$(echo $rcmd | sed 's/"$@"/nogui/')
printf '\033[0;33mNew forge run command: %s \033[0m' "$rcmd"
echo ""
#if user has set MC_RAM then we will use it by appending it to the user_jvm_args.txt file
if [ ! -z "${MC_RAM}" ]
then
echo "\033[0;33mSetting user RAM Limit args... \033[0m"
echo ""
echo "-Xms512M -Xmx${MC_RAM}" >> /mcserver/user_jvm_args.txt
fi
RUN_COMMAND=$rcmd
else
RUN_COMMAND="java ${JAVA_OPTS} -jar $JAR_NAME nogui"
fi
fi
else
echo "\033[0;33mUsing custom run command... \033[0m"
fi
# Generate eula.txt if necessary
if [ ! -e eula.txt ]
then
echo "\033[0;33mGenerating eula.txt \033[0m"
echo ""
if ! echo "eula=true" > eula.txt
then
echo "\033[0;31mError: Could not generate eula.txt. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
fi
# Generate server.properties if not present, Prevents the server from failing to start on first run
if [ ! -e server.properties ]
then
echo "\033[0;33mGenerating server.properties \033[0m"
echo ""
touch server.properties
fi
# Add RAM options to Java options if necessary
echo "\033[0;33mSetting Java arguments... \033[0m"
echo ""
if [ ! -z "${MC_RAM}" ]
then
JAVA_OPTS="-Xms512M -Xmx${MC_RAM} ${JAVA_OPTS}"
else
JAVA_OPTS="-Xms512M ${JAVA_OPTS}"
fi
# Generate lazymc.toml if necessary
if [ ! -e lazymc.toml ] && [ ! "$LAZYMC_VERSION" = "disabled" ]
then
echo "\033[0;33mGenerating lazymc.toml \033[0m"
echo ""
if ! ./lazymc config generate
then
echo "\033[0;31mError: Could not generate lazymc.toml. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
fi
# Add new values to lazymc.toml
if [ ! "$LAZYMC_VERSION" = "disabled" ]
then
echo "\033[0;33mUpdating lazymc.toml with latest details... \033[0m"
echo ""
# Check if the comment is already present in the file
if ! grep -q "mcserver-lazymc-docker" lazymc.toml;
then
# Add the comment to the file
sed -i '/Command to start the server/i # Managed by mcserver-lazymc-docker, please do not edit this!' lazymc.toml
fi
if ! sed -i "s~command = .*~command = \"$RUN_COMMAND\"~" lazymc.toml
then
echo "\033[0;31mError: Could not update lazymc.toml. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
fi
# Server launch handler
if [ "$LAZYMC_VERSION" = "disabled" ]
then
# Start directly the server when lazymc is disabled
echo "\033[0;33mStarting the server! \033[0m"
echo ""
if ! $RUN_COMMAND
then
echo "\033[0;31mError: Could not start the server. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
else
echo "\033[0;33mStarting the server! \033[0m"
echo ""
if ! ./lazymc start
then
echo "\033[0;31mError: Could not start the server. Exiting... \033[0m" | tee server_cfg.txt
exit 1
fi
fi