#!/bin/bash
# Installs the Minecraft Server

echo "$(tput setaf 6)Making Minecraft Directory$(tput sgr0)"
mkdir Minecraft
cd Minecraft

echo "$(tput setaf 6)Downloading Minecraft server JAR file$(tput sgr0)"
wget https://cdn.hackaday.io/files/4224180719616/picraft.zip
unzip picraft.zip
rm picraft.zip

echo "$(tput setaf 6)Setting up agreement to EULA for Mojang$(tput sgr0)"
echo "Do you agree to Mojang's EULA? https://account.mojang.com/documents/minecraft_eula"
echo "Type 'true' if you do, for 'false' if you dont"
read eula
	if [[ $eula == "" ]]
		then eula=false
		echo "setting EULA to false since you entered nothing. Change it by hand to enable the server"
	fi
echo "#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula).
eula=$eula" > eula.txt

echo "$(tput setaf 8)Now the fun and interactive part:$(tput sgr0)"
echo "$(tput setaf 6)Enable Nether? Enter 'true' to enable or 'false' to disable.$(tput sgr0)"
echo "$(tput setaf 6)False will run smoother on the Pi. Type your choice and press [Enter]$(tput sgr0)"
read nether
echo "$(tput setaf 6)You entered $nether$(tput sgr0)"
        if [[ $nether == "" ]]
                then nether=false
                echo "setting Nether to false since you entered nothing"
        fi

echo "$(tput setaf 6)Choose your Difficulty settings$(tput sgr0)"
echo "0 is Peaceful, aka pointless"
echo "1 is Easy and for the weak"
echo "2 is Normal and permissible"
echo "3 is for Hard and where boys are forged into men."
echo "$(tput setaf 6)Pick 0-3 and press [Enter]$(tput sgr0)"
read difficulty
        if [[ $difficulty == "" ]]
                then difficulty=2
                echo "setting difficulty to Normal(2) since you entered nothing"
        fi
	if [[ $difficulty -gt 3 ]]
		then difficulty=3
		echo "setting difficutly to Hard(3) since you cant enter a correct number"
	fi

echo "$(tput setaf 6)PvP? Enter 'true' if we can hurt each other or 'false' so we cant.$(tput sgr0)"
read pvp
        if [[ $pvp == "" ]]
                then pvp=true
                echo "setting PVP to true since you entered nothing"
        fi

echo "$(tput setaf 6)How many players can connect at once?$(tput sgr0)"
echo "Put a low number, like 4. Too many and it will lag and crash"
read players
        if [[ $players == "" ]]
                then players=4
                echo "setting players to 4 since you entered nothing"
	fi
	if [[ $players -gt 8 ]]
		then players=8
		echo "settings players to 8 since you entered too high of a number"
        fi

echo "$(tput setaf 6)Do you want to make this world Hardcore mode?$(tput sgr0)"
echo "$(tput setaf 6)You are permanently banned if you die in Hardcore$(tput sgr0)"
echo "Enter 'true' for Hardcore Mode or 'false' to respawn after death"
read hc
        if [[ $hc == "" ]]
                then hc=false
                echo "setting Hardcore mode to false since you entered nothing."
        fi

echo "$(tput setaf 6)Seeds allow you to create the same world over and over again.$(tput sgr0)"
echo "Do you have a seed you would like to enter?"
echo "Just type one in, make one up, or leave it blank and press [Enter]"
read seed

echo "$(tput setaf 6)How big do you want your world to be?.$(tput sgr0)"
echo "Setting a smaller number will keep the server load down."
echo "The number is blocks in each cardinal direction from center. Enter a number over 100 or just press [Enter] for the max default"
read world
    if [[ $world -le 99 ]]
        then
            echo "Going with default World Size"
            world=29999984
    fi

echo "$(tput setaf 6)Last thing. What do you want the server banner to read?$(tput sgr0)"
echo "Put whatever you want, then press [Enter]"
read banner
        if [[ $banner == "" ]]
                then banner="I never filled this in"
                echo "setting the banner to something since you didnt."
        fi

echo "#Minecraft server properties
generator-settings=
use-native-transport=true
op-permission-level=4
resource-pack-hash=
allow-nether=$nether
level-name=world
enable-query=false
allow-flight=false
announce-player-achievements=false
server-port=25565
max-world-size=$world
level-type=DEFAULT
enable-rcon=false
force-gamemode=true
level-seed=$seed
server-ip=
network-compression-threshold=256
max-build-height=128
spawn-npcs=true
white-list=false
spawn-animals=true
snooper-enabled=false
hardcore=$hc
online-mode=false
resource-pack=
pvp=$pvp
difficulty=$difficulty
enable-command-block=false
player-idle-timeout=5
gamemode=0
max-players=$players
spawn-monsters=true
view-distance=8
generate-structures=true
motd=$banner" > server.properties


echo "$(tput setaf 6)Creating startup script...$(tput sgr0)"
echo "#!/bin/bash
# Starts the Minecraft Server

#Uncomment for A or A+
#java -Xms128M -Xmx128M -jar spigot.jar

#Uncomment for B or B+
java -Xmx256M -Xms256M -XX:+AlwaysPreTouch -jar spigot.jar" > start-Pi
chmod +x start-Pi

echo "#!/bin/bash
# Starts the Minecraft Server

java -Xmx512M -Xms512M -XX:+AlwaysPreTouch -server -jar spigot.jar" > start-Pi2
chmod +x start-Pi2

echo "#!/bin/bash
# Starts the Minecraft Server

#Uncomment and adjust RAM if needed.  This should work on any system with 4gb or more RAM
#java -Xmx2048M -Xms2048M -XX:+AlwaysPreTouch -server -jar spigot.jar

# This start script should work for any Linux distribution.  No ram limits have been set.
java -server -jar spigot.jar" > start-generic
chmod +x start-generic

echo "$(tput setaf 6)All done! Type 'cd Minecraft' then enter the './start' script appropriate for your system from the Minecraft directory to start the server.$(tput sgr0)"

