-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_minecraft_java.sh
executable file
·105 lines (90 loc) · 3.37 KB
/
install_minecraft_java.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
#!/bin/bash
# Custom Minecraft server install script for Ubuntu
# $1 = Minecraft user name, to be an operator of the server.
# $2 = difficulty, 0 - Peaceful, 1 - Easy, 2 - Normal, 3 - Hard. See http://minecraft.gamepedia.com/Server.properties
# $3 = level-name, Name of your world. No spaces, single quotes, explanation marks, backslashes
# $4 = gamemode, 0 - Survival, 1 - Creative, 2 - Adventure, 3 - Spectator
# $5 = white-list, set this to true to make this invite-only. (Default: False)
# $6 = enable-command-block, if this is true you can create command blocks in the server. (Default: False)
# $7 = spawn-monsters, controls whether monsters show up at night or not. (Default: True)
# $8 = generate-structures, controls whether your world will have temples and villages. (Default: True)
# $9 = level-seed, Leave this blank to use a random seed.
set -eu -o pipefail
# Basic service and API settings
INSTALL_DIR=/srv/minecraft_server_java
USER=minecraft
GROUP=minecraft
UUID_URL=https://api.mojang.com/users/profiles/minecraft/$1
# Screen scrape the server jar location from the Minecraft server download page
SERVER_JAR_URL="curl -L https://minecraft.net/en-us/download/server/ | grep -Eo \"(http|https)://[a-zA-Z0-9./?=_-]*\" | sort | uniq | grep launcher"
apt update
apt install -y software-properties-common jq
apt install -y default-jdk
# Create user and install folder+
printf "Create %s user...\\n" "$USER"
adduser --system --no-create-home --home $INSTALL_DIR $USER
addgroup --system $GROUP
mkdir -pv $INSTALL_DIR
# Download the server jar
printf "Download %s/server.jar...\\n" "$INSTALL_DIR"
wget -q "$(eval "$SERVER_JAR_URL")" -O $INSTALL_DIR/server.jar
# Set permissions on install folder
chown -R $USER $INSTALL_DIR
# Adjust memory usage depending on VM size
totalMem=$(free -m | awk '/Mem:/ { print $2 }')
if [ "$totalMem" -lt 2048 ]; then
memoryAllocs=512m
memoryAllocx=1g
else
memoryAllocs=1g
memoryAllocx=2g
fi
# Create a service
printf "Create service..."
cat <<EOF | tee /etc/systemd/system/minecraft-server-java.service
[Unit]
Description=Minecraft Service
After=rc-local.service
[Service]
WorkingDirectory=$INSTALL_DIR
ExecStart=/usr/bin/java -Xms$memoryAllocs -Xmx$memoryAllocx -jar $INSTALL_DIR/server.jar nogui
ExecReload=/bin/kill -HUP \$MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
Alias=minecraft-server-java.service
EOF
printf "Service create to launch:"
printf "/usr/bin/java -Xms%s -Xmx%s -jar %s/server.jar nogui\\n" "$memoryAllocs" "$memoryAllocx" "$INSTALL_DIR"
# Configure the server
printf "Configure the server..."
echo 'eula=true' | tee $INSTALL_DIR/eula.txt
mojang_output="$(wget -qO- "$UUID_URL")"
rawUUID="$(echo "${mojang_output}" | jq -r '.id')"
UUID=${rawUUID:0:8}-${rawUUID:8:4}-${rawUUID:12:4}-${rawUUID:16:4}-${rawUUID:20:12}
cat <<EOF | tee $INSTALL_DIR/ops.json
[
{
"uuid": "$UUID",
"name": "$1",
"level": 4
}
]
EOF
chown $USER:$GROUP $INSTALL_DIR/ops.json
cat <<EOF | tee $INSTALL_DIR/server.properties
difficulty=$2
level-name=$3
gamemode=$4
white-list=$5
enable-command-block=$6
spawn-monsters=$7
generate-structures=$8
level-seed=$9
motd=Welcome on $3
EOF
chown $USER:$GROUP $INSTALL_DIR/server.properties
systemctl daemon-reload
systemctl enable minecraft-server-java && echo "Enable minecraft-server-java..." || echo "minecraft-server-java already enabled. Skipping..."
systemctl start minecraft-server-java