Minecraft Multiplayer Server

by ultrabenosaurus
72 deployments · 21 still active · last rev. 1 year ago

Original: jsmith/Minecraft <https://www.linode.com/stackscripts/view/232>

The form for jsmith's wouldn't let me choose a distribution to deploy on, so I've copied-and-pasted the script into my account in order to work on it.

I've got it working and added a bunch of options, tested on Ubuntu 14.04 LTS with the latest versions of both the Vanilla and Tekkit servers.

While it's preferable to avoid running Minecraft as root, sometimes the Tekkit server refuses to run as the minecraft user and will only work under root.

Compatible with: Ubuntu 14.04 LTS, Ubuntu 12.04 LTS
						#!/bin/bash

#
# Minecraft Stackscript
# Prepares the system, installs Java, installs Minecraft, and starts the server.
# Original by Jed Smith <jed@jedsmith.org>
# Updated by Dan Bennett <me@danb.co.uk>
#
# <udf name="mcflavour" label="Server Type" default="Vanilla" oneOf="Vanilla,Tekkit" example="Which flavour of Minecraft the server should run">
# <udf name="mcopname" label="Admin Nickname(s)" example="Nickname(s) to give initial operator status, comma-delimited if multiple">
# <udf name="mclevelname" label="Level Name" default="world" example="Name for the level">
# <udf name="mconline" label="Online Mode" default="yes" oneOf="yes,no" example="Whether the server will verify usernames with minecraft.net">
# <udf name="mcflight" label="Ennable Flight" default="yes" oneOf="yes,no" example="Whether the server should allow flight or not">
# <udf name="mcmonsters" label="Spawn Monsters" default="yes" oneOf="yes,no">
# <udf name="mcmaxplayers" label="Maximum Players" default="20">
#
# Once this script is completed, you can start the Minecraft server by typing the following:
#
#    # su - minecraft -s /opt/minecraft/run.sh
#
# This StackScript installs screen as well, so you can assign a password to the minecraft user,
# log in as the minecraft user, and run minecraft in a screen (if you so desire).
#
# While it's preferable to avoid running Minecraft as root,
# sometimes the Tekkit server refuses to run as the minecraft user
#

# which distro are we on?
distro=`grep DISTRIB_CODENAME /etc/lsb-release | cut -d'=' -f 2`

# add source for java
cat >>/etc/apt/sources.list <<EOF

# for java, added by minecraft stackscript
deb http://archive.canonical.com/ubuntu $distro partner
deb-src http://archive.canonical.com/ubuntu $distro partner
EOF

# update and install packages
apt-get -y update
apt-get -y upgrade

# install java
apt-get -y install default-jre screen

# add the minecraft user
adduser --system --group --disabled-login --disabled-password --home /opt/minecraft minecraft
cd /opt/minecraft

# find where to get minecraft
if [ "$MCFLAVOUR" == "Vanilla" ] ; then
    # taken from: http://gaming.stackexchange.com/questions/83263/find-out-the-latest-version-of-minecraft
    m_version=`curl -s http://mcupdate.tumblr.com/rss | egrep -o -e 'le>Minecraft [0-9]\.[0-9]\.[0-9]|le>Minecraft [0-9]\.[0-9]' | head -1 | egrep -o -e '[0-9]\.[0-9]\.[0-9]|[0-9]\.[0-9]'`
    wget -O minecraft_server.jar "https://s3.amazonaws.com/Minecraft.Download/versions/$m_version/minecraft_server.$m_version.jar"
else
    t_url=`curl -s http://technicpack.net/modpack/tekkitmain.552547 | egrep -o -e '<a href="(http://mirror.technicpack.net/Technic/servers/tekkitmain[^"]*)' | egrep -o -e 'http.*'`
    wget -O server.zip "$t_url"
    unzip server.zip
    unlink server.zip
fi

# convert configuration variables
[ "$MCONLINE" == "yes" ] && MCONLINE=true
[ "$MCONLINE" == "true" ] || MCONLINE=false
[ "$MCMONSTERS" == "yes" ] && MCMONSTERS=true
[ "$MCMONSTERS" == "true" ] || MCMONSTERS=false
[ "$MCFLIGHT" == "yes" ] && MCFLIGHT=true
[ "$MCFLIGHT" == "true" ] || MCFLIGHT=false

# write the configuration
cat >server.properties <<EOF
# Minecraft server properties
# Created by Minecraft StackScript `date`

# StackScript variables
online-mode=$MCONLINE
monsters=$MCMONSTERS
max-players=$MCMAXPLAYERS
level-name=$MCLEVELNAME
allow-flight=$MCFLIGHT

# StackScript hard-coded
server-ip=
server-port=25565
generate-structures=true
view-distance=10
pvp=true
difficulty=1
gamemode=0
player-idle-timeout=0
enable-query=false
server-port=25565
level-type=DEFAULT
enable-rcon=false
force-gamemode=false
level-seed=
server-ip=
max-build-height=256
spawn-npcs=true
white-list=false
spawn-animals=true
texture-pack=
hardcore=false
snooper-enabled=true
generator-settings=
op-permission-level=4
allow-nether=true
EOF

# initial op user(s)
touch ops.txt
ops=${MCOPNAME//,/$'\r\n'}
for word in $ops; do echo "$word" >>ops.txt; done
touch banned-players.txt
touch banned-ips.txt

# accept EULA
# http://www.minecraftforum.net/forums/support/server-support/2202336-updated-1-8-cannot-connect-to-server-how-to-fix
unlink eula.txt
cat >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).
#`date`
eula=true
EOF

# write the run script
if [ "$MCFLAVOUR" == "Vanilla" ] ; then
	cat >run.sh <<EOF
#!/bin/sh
java -Xmx1024M -Xms1024M -jar /opt/minecraft/minecraft_server.jar nogui
EOF

else
	mv launch.sh run.sh
fi

# clean up
chown minecraft:minecraft *
chmod a+x run.sh

# do a motd too
cat >/etc/motd.tail <<EOF

This $MCFLAVOUR server has been deployed to Linode from Jed Smith & Dan Bennett's Minecraft StackScript.
EOF