Minecraft - Latest One-Click

by linode
55 deployments · 54 still active · last rev. 1 month ago

Compatible with: Debian 9
						#!/bin/bash
#
#<UDF name="pubkey" Label="SSH Key" example="ssh-rsa..." default="" />
# Game config options:
#https://minecraft.gamepedia.com/Server.properties

#<UDF name="levelname" label="World Name" default="world" />
#<UDF name="motd" label="Message of the Day" default="Powered by Linode!" />
#<UDF name="allowflight" label="Flight Enabled" oneOf="true,false" default="false" />
#<UDF name="allownether" label="Nether World Enabled" oneOf="true,false" default="true" />
#<UDF name="announceplayerachievements" label="Player Achievements Enabled" oneOf="true,false" default="true" />
#<UDF name="maxplayers" label="Maximum Players" oneOf="1,25,50,100,500,1000," default="25" />
#<UDF name="playeridletimeout" label="Player Idle Timeout Limit" oneOf="Disabled,15,30,45,60" default="Disabled" />
#<UDF name="difficulty" label="Difficulty Level" oneOF="Peaceful,Easy,Normal,Hard" default="Easy" />
#<UDF name="hardcore" label="Hardcore Mode Enabled" oneOf="true,false" default="false" />
#<UDF name="pvp" label="PvP Enabled" oneOf="true,false" default="true" />
#<UDF name="forcegamemode" label="Force Game Mode Enabled" oneOf="true,false" default="false" />
#<UDF name="leveltype" label="World Type" oneOf="DEFAULT,FLAT,LEGACY"default="DEFAULT" />
#<UDF name="levelseed" label="World Seed" default="" />
#<UDF name="spawnanimals" label="Spawn Animals Enabled" oneOf="true,false" default="true" />
#<UDF name="spawnmonsters" label="Spawn Monsters Enabled" oneOf="true,false" default="true" />
#<UDF name="spawnnpcs" label="Spawn NPCs Enabled" oneOf="true,false" default="true" />
#<UDF name="gamemode" label="Game Mode" oneOf="Survival,Creative,Adventure,Spectator" default="Survival" />
#<UDF name="generatestructures" label="Structure Generation Enabled" oneOf="true,false" default="true" />
#<UDF name="maxbuildheight" label="Maximum Build Height" oneOf="50,100,200,256" default="256" />
#<UDF name="maxworldsize" label="Maximum World Size" oneOf="100,1000,10000,100000,1000000,10000000,29999984" default="29999984" />
#<UDF name="viewdistance" label="View Distance" oneOf="2,5,10,15,25,32" default="10" />
#<UDF name="enablecommandblock" label="Command Block Enabled" oneOf="true,false" default="false" />
#<UDF name="enablequery" label="Querying Enabled" oneOf="true,false" default="true" />
#<UDF name="enablercon" label="Enable RCON" oneOf="true,false" default="false" />
#<UDF name="rconpassword" label="RCON Password" default="" />
#<UDF name="rconport" label="RCON Port" default="25575" />
#<UDF name="maxticktime" label="Maximum Tick Time" default="60000" />
#<UDF name="networkcompressionthreshold" label="Network Compression Threshold" default="256" />
#<UDF name="oppermissionlevel" label="Op-permission Level" oneOf="1,2,3,4" default="4" />
#<UDF name="port" label="Port Number" default="25565" />
#<UDF name="snooperenabled" label="Snooper Enabled" oneOf="true,false" default="true" />
#<UDF name="usenativetransport" label="Use Native Transport Enabled" oneOf="true,false" default="true" />

#Non-MVP Options
#name="resourcepack" label="resourcepack" default=""
#name="whitelist" label="whitelist" oneOf="true,false" default="false"
#name="generatorsettings" label="generatorsettings" default=""


source <ssinclude StackScriptID="401711">
source <ssinclude StackScriptID="401712">

exec 1> >(tee -a "/var/log/stackscript.log") 2>&1

GAMESERVER="mcserver"


### UDFs to configs

#Difficulty
if [[ "$DIFFICULTY" = "Peaceful" ]]; then
  DIFFICULTY=0
elif [[ "$DIFFICULTY" = "Easy" ]]; then
  DIFFICULTY=1
elif [[ "$DIFFICULTY" = "Normal" ]]; then
  DIFFICULTY=2
elif [[ "$DIFFICULTY" = "Hard" ]]; then
  DIFFICULTY=3
fi

#Gamemode
if [[ "$GAMEMODE" = "Survival" ]]; then
  GAMEMODE=0
elif [[ "$GAMEMODE" = "Creative" ]]; then
  GAMEMODE=1
elif [[ "$GAMEMODE" = "Adventure" ]]; then
  GAMEMODE=2
elif [[ "$GAMEMODE" = "Spectator" ]]; then
  GAMEMODE=3
fi

#Player Idle Timeout

if [[ "$PLAYERIDLETIMEOUT" = "Disabled" ]]; then
  PLAYERIDLETIMEOUT=0
fi

# Server config

set_hostname
apt_setup_update

if [[ "$PUBKEY" != "" ]]; then
  add_pubkey
fi

# Minecraft specific dependencies
debconf-set-selections <<< "postfix postfix/main_mailer_type string 'No Configuration'"
debconf-set-selections <<< "postfix postfix/mailname string `hostname`"
dpkg --add-architecture i386
apt update
sudo apt -q -y install mailutils postfix curl \
wget file bzip2 gzip unzip bsdmainutils python \
util-linux ca-certificates binutils bc jq tmux default-jre
add-apt-repository -y ppa:webupd8team/java
apt -q -y purge openjdk-11*
apt -q -y install openjdk-8-jre-headless
update-ca-certificates -f

# Install linuxGSM
linuxgsm_install

# Install Minecraft
game_install

# Minecraft config

sed -i s/server-ip=/server-ip="$IP"/ /home/"$GAMESERVER"/serverfiles/server.properties

#Customer config
sed -i s/allow-flight=false/allow-flight="$ALLOWFLIGHT"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/allow-nether=true/allow-nether="$ALLOWNETHER"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/announce-player-achievements=true/announce-player-achievements="$ANNOUNCEPLAYERACHIEVEMENTS"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/difficulty=1/difficulty="$DIFFICULTY"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/enable-command-block=false/enable-command-block="$ENABLECOMMANDBLOCK"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/enable-query=true/enable-query="$ENABLEQUERY"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/force-gamemode=false/force-gamemode="$FORCEGAMEMODE"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/gamemode=0/gamemode="$GAMEMODE"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/generate-structures=true/generate-structures="$GENERATESTRUCTURES"/ /home/"$GAMESERVER"/serverfiles/server.properties
#sed -i s/generator-settings=/generator-settings="$GENERATORSETTINGS"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/hardcore=false/hardcore="$HARDCORE"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/level-name=world/level-name="$LEVELNAME"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/level-seed=/level-seed="$LEVELSEED"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/level-type=DEFAULT/level-type="$LEVELTYPE"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/max-build-height=256/max-build-height="$MAXBUILDHEIGHT"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/max-players=20/max-players="$MAXPLAYERS"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/max-tick-time=60000/max-tick-time="$MAXTICKTIME"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/max-world-size=29999984/max-world-size="$MAXWORLDSIZE"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/motd=.*/motd="$MOTD"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/network-compression-threshold=256/network-compression-threshold="$NETWORKCOMPRESSIONTHRESHOLD"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/op-permission-level=4/op-permission-level="$OPPERMISSIONLEVEL"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/player-idle-timeout=0/player-idle-timeout="$PLAYERIDLETIMEOUT"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/pvp=true/pvp="$PVP"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/resource-pack-sha1=/resource-pack-sha1="$RESOURCEPACKSHA1"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/server-port=25565/server-port="$PORT"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/snooper-enabled=true/snooper-enabled="$SNOOPERENABLED"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/spawn-animals=true/spawn-animals="$SPAWNANIMALS"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/spawn-monsters=true/spawn-monsters="$SPAWNMONSTERS"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/spawn-npcs=true/spawn-npcs="$SPAWNNPCS"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/use-native-transport=true/use-native-transport="$USENATIVETRANSPORT"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/view-distance=10/view-distance="$VIEWDISTANCE"/ /home/"$GAMESERVER"/serverfiles/server.properties
#sed -i s/white-list=false/white-list="$WHITELIST"/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/rcon.password=*/rcon.password="\"$RCONPASSWORD\""/ /home/"$GAMESERVER"/serverfiles/server.properties
sed -i s/enable-rcon=false/enable-rcon=true/ /home/"$GAMESERVER"/serverfiles/server.properties

#Non-MVP configs
#sed -i s/resource-pack=/resource-pack="$RESOURCEPACK"/ /home/"$GAMESERVER"/serverfiles/server.properties

# Setup crons and create systemd service file
service_config

# Start the service and setup firewall
ufw_install
ufw allow "$PORT"
ufw allow "25575"
ufw enable
fail2ban_install
systemctl start "$GAMESERVER".service
systemctl enable "$GAMESERVER".service
stackscript_cleanup