-
Notifications
You must be signed in to change notification settings - Fork 0
/
minecraft-server
396 lines (330 loc) · 13.6 KB
/
minecraft-server
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
#!/bin/bash
#:: RTD System System Managment Script
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#:: Author: Vonschutter
#:: Version: 1.04
#::
#::
#:: Purpose: The purpose of the script is to perform managment tasks on Linux systems
#::
#:: This is a script that will start Minecraft Server on an Ubuntu or Debian (Ubuntu) based server. It will start
#:: multiple panes using "byobu", and start the server etc there...
#::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#:::::::::::::: ::::::::::::::::::::::
#:::::::::::::: Script Settings ::::::::::::::::::::::
#:::::::::::::: ::::::::::::::::::::::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Variables that govern the behavior or the script and location of files are
# set here. There should be no reason to change any of this to setup and
# get a working Minecraft server on Ubuntu. However, if you have updated scripts
# and need to download yours from someplace else you only need to change these
# setings.
# Your Minecraft version (this is a preference only), script will always
# get the latest Minecraft if it does not find a local server.
MINECRAFT_VERSION=Latest
# Your local Minecraft directory.
MINECRAFT_HOME=~/bin/minecraft.server/Minecraft.$MINECRAFT_VERSION
MINECRAFT_JAR=server.jar
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#:::::::::::::: ::::::::::::::::::::::
#:::::::::::::: Script Functions ::::::::::::::::::::::
#:::::::::::::: ::::::::::::::::::::::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#
function check_dependencies (){
# Simple function to check if sofware is available and take action
# if it is not. Software name must match command to envoke it.
#---------------------------------------------------------------
echo "Checking for script dependencies and install if not there..."
#---------------------------------------------------------------
if hash "$1" 2>/dev/null; then
echo "I found that $1 is present on this system... thankyou for that! "
else
echo "You seem to have no $1... I will try to get it... "
install_software "$1"
if [ $? != 0 ];
then
echo "That install didn't work out so well."
echo "Please manually try to add the software since I couldn't do it."
exit
fi
echo "OK Done! Continuing..."
fi
}
function install_software (){
# Simple function to help installing software on several linux distributions
# Should work on Fedora, SUSE, RedHat, Ubuntu, Debian etc. unless the naming
# convention of the software package is different between distributions.
if hash pkcon 2>/dev/null; then
sudo pkcon -y install "$@"
elif hash yum 2>/dev/null; then
sudo yum -y install "$@"
elif hash zypper 2>/dev/null; then
sudo zypper install -y "$@"
elif hash apt-get 2>/dev/null; then
export DEBIAN_FRONTEND=noninteractive
sudo apt-get -y -qq --allow-change-held-packages --ignore-missing install "$@"
else
echo -e $YELLOW "This system does not seem to have a software managment system" $ENDCOLOR
_cleanup
exit 1
fi
}
function setup_minecraft_root (){
# Setup and initiate the location of the mincraft server.
# For us i likes to live in /home/$USER/bin/minecraft.server/Minecraft.$VERSION
# This tests if the script files are available and downloades them if not.
mkdir -p $MINECRAFT_HOME
for i in start.sh announce.py server.properties eula.txt
do
if [ ! -f "$MINECRAFT_HOME/$i" ]; then
$i $MINECRAFT_HOME
fi
done
if [ ! -f "$MINECRAFT_HOME/$MINECRAFT_JAR" ]; then
minecraft_update
fi
}
function run_minecraft_server_manager (){
# Start byobu multi screen app...
byobu new-session -d -s $USER
# status screen
byobu rename-window -t $USER:0 'Minecraft Server Manager'
byobu send-keys "bash $MINECRAFT_HOME/start.sh" C-m
# Create new pane vertically and display htop
byobu split-window -v
byobu send-keys "htop " C-m
# Split the "htop" window in 2, and start spedometer there.
byobu split-window -h
byobu send-keys "speedometer -r $NETINT -t $NETINT" C-m
# Create new window...
byobu new-window -t $USER:1 -n 'Anouncing on Network (Press F4 to switch between windows)'
byobu send-keys "python3 $MINECRAFT_HOME/announce.py" C-m
# Set default window as the dev split plane
byobu select-window -t $USER:0
byobu-tmux select-pane -t 0
# Attach to the session you just created
byobu attach-session -t $USER
}
function check_java (){
~/bin/java/bin/java --version >/dev/null
if [ $? -eq 0 ]; then
echo "java of some version is present. I respect your choice... and will try to run! --- OK!"
else
echo Java is now present where expected... will now download:
wget https://download.oracle.com/java/19/latest/jdk-19_linux-x64_bin.tar.gz
mkdir ~/bin/java && tar xzvf jdk-19_linux-x64_bin.tar.gz --directory ~/bin/java/ --strip 1
#check_dependencies default-jre
fi
}
function find_active_newtork_interface (){
# Detect the name of the active network interface.
NETINT=$(ip addr | awk '/state UP/ {print $2}' |grep -v "br*" |grep -v "wlp*" | head --bytes -2 )
echo "Active network interface is: $NETINT"
export NETINT
}
function minecraft_update() {
# Get minecraft version publication file and get the latest server URL
# This is an unneccessarily complicated way by Mojang!
cd $MINECRAFT_HOME || { echo "Failure change to Minecraft folder. Is it there?"; exit 1; }
wget -q -O $MINECRAFT_HOME/versions --no-check-certificate https://launchermeta.mojang.com/mc/game/version_manifest.json
JSONVERSION=$(cat versions | python3 -c "exec(\"import json,sys\nobj=json.load(sys.stdin)\nversion=obj['latest']['release']\nfor v in obj['versions']:\n if v['id']==version:\n print(v['url'])\")")
wget -q -O $MINECRAFT_HOME/versions --no-check-certificate $JSONVERSION
MC_SERVER_URL=$(cat versions | python3 -c 'import json,sys;obj=json.load(sys.stdin);print(obj["downloads"]["server"]["url"])')
rm $MINECRAFT_HOME/versions
# Download the latest minecraft vesrion and check if it is the same as the one installed
wget -q -O $MINECRAFT_HOME/minecraft_server.jar.update --no-check-certificate $MC_SERVER_URL
if [ -f $MINECRAFT_HOME/minecraft_server.jar.update ] ; then
if diff $MINECRAFT_HOME/$MINECRAFT_JAR $MINECRAFT_HOME/minecraft_server.jar.update >/dev/null ; then
dialog --title 'Minecraft Update' --msgbox "You are already running the latest version of $MINECRAFT_JAR." 0 0
else
mv $MINECRAFT_JAR $MINECRAFT_JAR.previous
mv $MINECRAFT_HOME/minecraft_server.jar.update $MINECRAFT_HOME/$MINECRAFT_JAR
dialog --title 'Minecraft Update' --msgbox "Minecraft successfully updated." 0 0
fi
else
dialog --title 'Minecraft Update' --msgbox "Minecraft update could not be downloaded." 0 0
echo
fi
}
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#:::::::::::::: ::::::::::::::::::::::
#:::::::::::::: Script internal config repository ::::::::::::::::::::::
#:::::::::::::: ::::::::::::::::::::::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# This section contains content to written out to several configuration
# files and external scripts.
announce.py () {
cat >> $1/announce.py <<-'EOF'
#!/bin/python3
# RTD System System Managment Script
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# :: Author: generated by minecraft.server
# :: Version 1.02
# ::
# ::
# :: Purpose: The purpose of the script is to:
# :: 1 - Broadcast service availability to the local network.
# :: This script is used to broadcast the minecraft server on the
# :: same machine (see "servers"). You may have as many minecraft
# :: servers running and broadcast as you like.
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
import socket
import time
import sys
import os
import urllib.request
servers = [
["Local Network - Minecraft Server", 25565],
]
BROADCAST_IP = "255.255.255.255"
BROADCAST_PORT = 4445
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
print("Broadcasting Minecraft servers to LAN on: " + os.environ['NETINT'])
print("Local IP adress: ")
print((([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")] or [[(s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) + ["no IP found"])[0])
print("External IP address visible on the internet: ")
external_ip = urllib.request.urlopen('https://ident.me').read().decode('utf8')
print(external_ip)
while True:
for server in servers:
msg = "[MOTD]%s[/MOTD][AD]%d[/AD]" % (server[0], server[1])
encode = str.encode(msg)
sock.sendto(encode, (BROADCAST_IP, BROADCAST_PORT))
time.sleep(3)
EOF
}
start.sh () {
cat >> $1/start.sh <<-'EOF'
#!/bin/bash
#:: RTD System System Managment Script
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#:: Author: generated by minecraft.server
#:: Version 1.00
#::
#::
#:: Purpose: The purpose of the script is to:
#:: 1 - Launch Minecraft
#:: 2 - Check that Minecraft runs for at least one minute
#:: 3 - If Minecraft crashes or quits after one minute, restart Minecraft
#::
#::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
pushd $(dirname "$0")
MC_SERVER_NAME=`cat server.properties |grep "motd=.*" `
export $MC_SERVER_NAME
while true; do
start_epoch=$(date +%s)
echo Starting Minecraft server $MC_SERVER_NAME ....
~/bin/java/bin/java -Xmx2048M -Xms2048M -jar $(dirname "$0")/server.jar noggui broadcast
# Abort if the application exited too quickly
duration=$(( $(date +%s) - $start_epoch ))
if [[ "$duration" < 60 ]]; then
echo "Program exited too quickly! Aborting Minecraft Launcher...."
exit
fi
done
exit
EOF
}
server.properties () {
cat >> $1/server.properties <<-'EOF'
# Minecraft server properties
# generated by minecraft.server
broadcast-rcon-to-ops=true
view-distance=6
max-build-height=256
server-ip=
rcon.port=25575
level-seed=
allow-nether=true
gamemode=survival
enable-command-block=true
server-port=25565
enable-rcon=false
enable-query=true
op-permission-level=3
prevent-proxy-connections=false
generator-settings=
resource-pack=
player-idle-timeout=0
level-name=world
rcon.password=
motd=\u00A7d CuteWorld
query.port=25565
force-gamemode=true
debug=false
hardcore=false
white-list=false
broadcast-console-to-ops=true
pvp=true
spawn-npcs=true
spawn-animals=true
generate-structures=true
snooper-enabled=false
difficulty=normal
network-compression-threshold=256
level-type=default
max-tick-time=-1
spawn-monsters=true
enforce-whitelist=false
max-players=10
use-native-transport=true
spawn-protection=16
resource-pack-sha1=
online-mode=true
allow-flight=false
max-world-size=29999984
EOF
}
eula.txt () {
cat >> $1/eula.txt <<-'EOF'
#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula).
#Sun Feb 26 16:57:58 CET 2017
eula=true
EOF
}
# File write template. Takes one argument "where to put the file"
filetowrite.txt () {
cat >> $1/filetowrite.txt <<-'EOF'
EOF
}
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#:::::::::::::: ::::::::::::::::::::::
#:::::::::::::: Script Executive ::::::::::::::::::::::
#:::::::::::::: ::::::::::::::::::::::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Check that the software components of this script are available.
# iff they are not, attempt to install them.
for i in byobu speedometer htop python3 dialog
do
check_dependencies $i
done
if echo "$@" | grep "update" ;then
dialog --title "Update to latest Minecraft?" --yesno "You have requested that I try to get the latest verion of Minecraft from Mojang. This means that the people playing in the server also will need the latest version. Is this OK?" 0 0
case $? in
0)
minecraft_update ;;
1)
echo "Proceeding without update...";;
255)
echo "ESC pressed.";;
esac
fi
# Basic check to see that the Minecraft folder is present and
# if not crate it and download the scripts and the server jar file
# to enable the server.
setup_minecraft_root
check_java
# Start the server and monitor the server performance
find_active_newtork_interface
run_minecraft_server_manager