#!/bin/bash # Minecraft Server Installation Script - James A. Chambers - https://jamesachambers.com # More information at https://jamesachambers.com/raspberry-pi-minecraft-server-script-with-startup-service/ # GitHub Repository: https://github.com/TheRemote/RaspberryPiMinecraft # To run the setup script use: # curl https://raw.githubusercontent.com/TheRemote/RaspberryPiMinecraft/master/SetupMinecraft.sh | bash # Minecraft server version Version="1.20.4" # Set to AllowLocalCopy="1" if you make changes to the script otherwise any changes will be discarded and the latest online version will run AllowLocalCopy="0" # Custom Directory # You can change this to a custom directory -- it is meant to be the root directory that contains everything (not including the "minecraft" folder part) DirName=$(readlink -e ~) if [ -z "$DirName" ]; then DirName=~ fi cd "$DirName" UserName=$(whoami) # Terminal colors BLACK=$(tput setaf 0) RED=$(tput setaf 1) GREEN=$(tput setaf 2) YELLOW=$(tput setaf 3) LIME_YELLOW=$(tput setaf 190) BLUE=$(tput setaf 4) MAGENTA=$(tput setaf 5) CYAN=$(tput setaf 6) WHITE=$(tput setaf 7) BRIGHT=$(tput bold) NORMAL=$(tput sgr0) BLINK=$(tput blink) REVERSE=$(tput smso) UNDERLINE=$(tput smul) # Function to read input from user with a prompt function read_with_prompt() { variable_name="$1" prompt="$2" default="${3-}" unset $variable_name while [[ ! -n ${!variable_name} ]]; do read -p "$prompt: " $variable_name &1 | head -1 | cut -d '"' -f 2 | cut -d '.' -f 1) if [[ $CurrentJava -lt 18 ]]; then Print_Style "Required OpenJDK version 18 could not be installed." "$RED" exit 1 else Print_Style "OpenJDK installation completed." "$GREEN" fi else rm -rf "$DirName/minecraft/jre" Print_Style "Required OpenJDK version 18 could not be installed." "$RED" exit 1 fi } Update_Sudoers() { if [ -d /etc/sudoers.d ]; then sudoline="$UserName ALL=(ALL) NOPASSWD: /bin/bash $DirName/minecraft/fixpermissions.sh, /bin/systemctl start minecraft, /bin/bash $DirName/minecraft/start.sh, /sbin/reboot" if [ -e /etc/sudoers.d/minecraft ]; then AddLine=$(sudo grep -qxF "$sudoline" /etc/sudoers.d/minecraft || echo "$sudoline" | sudo tee -a /etc/sudoers.d/minecraft) else AddLine=$(echo "$sudoline" | sudo tee /etc/sudoers.d/minecraft) fi else echo "/etc/sudoers.d was not found on your system. Please add this line to sudoers using sudo visudo: $sudoline" fi } Fix_Permissions() { echo "Setting server file permissions..." sudo ./fixpermissions.sh -a >/dev/null } ################################################################################################# Print_Style "Minecraft Server installation script by James A. Chambers - https://jamesachambers.com/" "$MAGENTA" Print_Style "Version $Version will be installed. To change this, open SetupMinecraft.sh and change the \"Version\" variable to the version you want to install." "$MAGENTA" Print_Style "Latest version is always available at https://github.com/TheRemote/RaspberryPiMinecraft" "$MAGENTA" Print_Style "Don't forget to set up port forwarding on your router! The default port is 25565" "$MAGENTA" cd "$DirName" if [[ -e "SetupMinecraft.sh" && "$AllowLocalCopy" -ne "1" ]]; then rm -f "SetupMinecraft.sh" echo "Local copy of SetupMinecraft.sh running. Exiting and running online version. To override set AllowLocalCopy=\"1\" at the top of the script." curl https://raw.githubusercontent.com/TheRemote/RaspberryPiMinecraft/master/SetupMinecraft.sh | bash exit 1 fi # Check to make sure we aren't running as root if [[ $(id -u) = 0 ]]; then echo "This script is not meant to run as root or sudo. Please run as a normal user with ./SetupMinecraft.sh. Exiting..." exit 1 fi # Install dependencies needed to run minecraft in the background Print_Style "Installing screen, sudo, net-tools, curl..." "$YELLOW" if [ ! -n "$(which sudo)" ]; then apt-get update && apt-get install sudo -y fi sudo apt-get update sudo apt-get install net-tools -y if [ ! -n "$(which screen)" ]; then sudo apt-get install screen -y fi if [ ! -n "$(which curl)" ]; then sudo apt-get install curl -y fi if [ ! -n "$(which pigz)" ]; then sudo apt-get install pigz -y fi # Check to see if Minecraft directory already exists, if it does then reconfigure existing scripts if [ -d "$DirName/minecraft" ]; then Print_Style "Directory minecraft already exists! Updating scripts and configuring service ..." "$YELLOW" # Get Home directory path and username cd "$DirName/minecraft" if [ -d "$DirName/minecraft/jre" ]; then if [ -e "$DirName/minecraft/jre/bin/java" ]; then CurrentJava=$($DirName/minecraft/jre/bin/java -version 2>&1 | head -1 | cut -d '"' -f 2 | cut -d '.' -f 1) if [[ $CurrentJava -lt 18 ]]; then Print_Style "Updating Java..." "$YELLOW" rm -rf "$DirName/minecraft/jre" Install_Java fi else Print_Style "Updating Java..." "$YELLOW" rm -rf "$DirName/minecraft/jre" Install_Java fi else Install_Java fi # Ask user for amount of memory they want to dedicate to the Minecraft server Get_ServerMemory # Update Minecraft server scripts Update_Scripts # Service configuration Update_Service # Sudoers configuration Update_Sudoers # Fix server files/folders permissions Fix_Permissions # Configure automatic start on boot Configure_Reboot Print_Style "Minecraft installation scripts have been updated to the latest version!" "$GREEN" exit 0 fi # Create server directory Print_Style "Creating minecraft server directory..." "$YELLOW" mkdir -p "$DirName/minecraft" cd "$DirName/minecraft" mkdir backups mkdir logs # Install Java Install_Java # Get total system memory Get_ServerMemory # Retrieve latest build of Paper minecraft server Print_Style "Getting latest Paper Minecraft server..." "$YELLOW" BuildJSON=$(curl -H "Accept-Encoding: identity" -H "Accept-Language: en" -L -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4.212 Safari/537.36" https://papermc.io/api/v2/projects/paper/versions/$Version) Build=$(echo "$BuildJSON" | rev | cut -d, -f 1 | cut -d']' -f 2 | cut -d'[' -f 2 | rev) Build=$(($Build + 0)) curl -H "Accept-Encoding: identity" -H "Accept-Language: en" -L -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4.212 Safari/537.36" -o paperclip.jar "https://papermc.io/api/v2/projects/paper/versions/$Version/builds/$Build/downloads/paper-$Version-$Build.jar" # Run the Minecraft server for the first time which will build the modified server and exit saying the EULA needs to be accepted Print_Style "Building the Minecraft server..." "$YELLOW" $DirName/minecraft/jre/bin/java -jar -Xms400M -Xmx"$MemSelected"M paperclip.jar # Accept the EULA Print_Style "Accepting the EULA..." "$GREEN" echo eula=true >eula.txt # Update Minecraft server scripts Update_Scripts # Server configuration Print_Style "Enter a name for your server..." "$MAGENTA" read -p 'Server Name: ' servername >server.properties echo "motd=$servername" >>server.properties # Service configuration Update_Service # Configure automatic start on boot Configure_Reboot # Sudoers configuration Update_Sudoers # Fix server files/folders permissions Fix_Permissions # Finished! Print_Style "Setup is complete. Starting Minecraft server..." "$GREEN" sudo systemctl start minecraft.service # Wait up to 30 seconds for server to start StartChecks=0 while [ $StartChecks -lt 30 ]; do if screen -list | grep -q "\.minecraft"; then sleep 5 screen -r minecraft break fi sleep 1 StartChecks=$((StartChecks + 1)) done if [[ $StartChecks == 30 ]]; then Print_Style "Server has failed to start after 30 seconds." "$RED" else screen -r minecraft fi