mc-perfect/mc-install.sh
2026-01-28 10:54:05 +01:00

222 lines
6.5 KiB
Bash
Executable File

#!/bin/bash
printf "Better to run as root, because of permisions handeling.\n"
printf "Idealy frome git directory. \n"
sleep 2
home=$(pwd)
# TODO add check if i am inside git repository
### RAM ###
printf "How much of RAM are you willing to allocate in GB (Max) [4G/?G]: \n"
read maxRam
printf "How little you want to let RAM go down to in GB (Min) [1G/?G]: \n"
read minRam
### Proxy ###
printf "Are you planing to use Velocity proxy to acces this server? [Y/n]: \n"
read ansVeloProxy
if [[ "$ansVeloProxy" =~ ("y"|"Y") || -z $ansVeloProxy ]]; then
printf "Do you wish to set up moder forwarding? [Y/n]: \n"
read ansModernFarward
fi
printf "Do you want to set up SkinRestorer [Y/n]: \n"
read ansSkinRestorer
printf "Do you want to set up PlayerSync [Y/n]: \n"
read ansPlayerSync
if [[ "$ansPlayerSync" =~ ("y"|"Y") || -z $ansPlayerSync ]]; then
printf "What username you want to use? (leave blank for default):\n"
read user_name
printf "What password you want to use? (leave blank for default):\n"
read password
fi
sleep 2
### Update ###
printf "\n+ Starting to upgrade base system\n\n"
apt update
apt upgrade -y
apt autoremove --purge -y
### Installing depandencies ###
printf "\n+ Installing depandencies \n\n"
apt install openjdk-21-jre-headless screen p7zip-full vim jq wget -y
### User and groupe managment ###
printf "\n+ Creating user minecraft and basic file structure"
groupadd minecraft
useradd --system --shell /usr/sbin/nologin --home /opt/minecraft -g minecraft minecraft
### File strukture ###
mkdir /opt/minecraft
cd /opt/minecraft/
### Download forge server ###
printf "+ Downloading and installing Forge 1.21.1 \n \n"
wget https://maven.neoforged.net/releases/net/neoforged/neoforge/21.1.217/neoforge-21.1.217-installer.jar
mv *neoforge*.jar NeoForge.jar
### Installing server ###
java -Xms512M -Xmx2048M -jar NeoForge.jar --installServer >> /dev/null
### RAM configuration ###
if [[ "$minRam" > "$maxRam" ]]; then
pritf "\n You allocated too low max ram or too much min ram. Manual intervention at the end will be needed\n"
echo "-Xmx?G # in place of ? put number of RAM you want to set ass maximum"
$maxRam="Error"
elif [[ "$maxRam" =~ ("G"|"M") ]]; then
printf "\n+ Allocating $maxRam as RAM maximum."
echo "-Xmx$maxRam" >> /opt/minecraft/user_jvm_args.txt
elif [[ -z $maxRam ]]; then
printf "\n+ Allocating $MaxRam as RAM maximum"
echo "-Xmx4G" >> user_jvm_args.txt
elif [[ ! "$maxRam" =~ ("G"|"MB") ]]; then
printf "\n+ Allocating ${maxRam}G as RAM maximum"
echo "-Xmx${maxRam}G" >> /opt/minecraft/user_jvm_args.txt
else
pritnf "\n \n!!! Sorry I have no idea what are you trying to do :/ Manual intervention will be needed at the end \n"
$maxRam="Error"
echo "-Xmx?G # in place of ? put number of RAM you want to set ass maximum"
fi
if [[ "$minRam" > "$maxRam" ]]; then
echo "-Xms?G # in place of ? put number of RAM you want to se ass minimum"
elif [[ "$minRam" =~ ("G"|"M") ]]; then
printf "\n+ Allocating $minRam as RAM minimum."
echo "-Xms$minRam" >> /opt/minecraft/user_jvm_args.txt
elif [[ -z $minRam ]]; then
printf "\n+ Allocating $minRam as RAM minimum"
echo "-Xms1G" >> /opt/minecraft/user_jvm_args.txt
elif [[ ! "$minRam" =~ ("G"|"MB") ]]; then
printf "Allocating ${minRam}G as RAM maximum"
echo "-Xms${minRam}G" >> /opt/minecraft/user_jvm_args.txt
else
pritnf "\n!!! Sorry I have no idea what are you trying to do :/ Manual intervention will be needed at the end \n"
$minRam="Error"
echo "-Xms?G # in place of ? put number of RAM you want to set ass minimum"
fi
### Other configuraton ###
echo eula=true > eula.txt
mv $home/data/server-icon.png $home/data/server.properties /opt/minecraft/
### Mods handling ###
mkdir /opt/minecraft/mods
mkdir /opt/minecraft/config
while IFS= read -r line; do
if [[ $(echo $line | jq -r '.env.server') == "required" ]]; then
dw_mr=$(echo $line | jq -r '.downloads[0]')
wget mods/ $dw_mr
fi
done < <(jq -c '.files[]' 'data/mr.index.json')
while IFS= read -r line; do
if [[ $(echo $line | jq -r '.env.server') == "required" ]]; then
dw_cf=$(echo $line | jq -r '.downloads[0]')
wget mods/ $dw_cf
fi
done < <(jq -c '.files[]' 'data/cf.index.json')
# adds mode to help comunicate with VeloProxy
if [[ "$ansVeloProxy" =~ ["y"|"Y"] || -z $ansVeloProxy ]]; then
wget https://cdn.modrinth.com/data/vDyrHl8l/versions/82bwNhmQ/proxy-compatible-forge-1.2.1.jar
mv proxy-compatible-forge*.jar /opt/minecraft/mods/
#if [[ "$ansModernFarward" =~ ["y"|"Y"] || -z $ansModernFarward ]]; then
# mkdir /opt/minecraft/config
# mv data/pcf-common.toml /opt/minecraft/config/ #TODO Research this shit
# # TODO Modern forwardig? https://cdn.modrinth.com/data/RT18TcxA/versions/fMMrBXPc/neovelocity-neoforge-1.2.4.jar
#fi
fi
# adds aditional mods
if [[ "$ansSkinRestorer" =~ ["y"|"Y"] || -z $ansSkinRestorer ]]; then
wget https://cdn.modrinth.com/data/ghrZDhGW/versions/NNAoiEfu/skinrestorer-2.4.3%2B1.21-neoforge.jar
mv skinrestorer*.jar /opt/minecraft/mods/
fi
if [[ "$ansPlayerSync" =~ ["y"|"Y"] || -z $ansPlayerSync ]]; then
wget https://cdn.modrinth.com/data/4pmkajBP/versions/mK3hQ8Vs/playersync-1.21.1-2.1.5.jar
mv playersync*.jar /opt/minecraft/mods
mv $home/data/playersync-common.toml /opt/minecraft/config/
rndr=$((RANDOM % (32765-10000+1)+10000))
sed -i "s/1234567890/$rndr$rndr/g" config/playersync-common.toml #gens uniqe id
if [[ -n $password ]]; then
sed -i "s/KurevskeHesloMain/$password/g" config/playersync-common.toml
fi
if [[ -n $user_name ]]; then
sed -i "s/mainUser/$user_name/g" config/playersync-common.toml
fi
fi
### Permisions ###
printf "\n+ Permision handeling\n"
chown -R minecraft:minecraft /opt/minecraft
### SystemD service ###
printf "+ Preparign systemD service\n"
mv $home/data/mc-forge.service /etc/systemd/system/
systemctl daemon-reload
### Error handeling ###
if [[ "$minRam" = "Error" || "$maxRam" = "Error" ]]; then
printf "\n Encountred some eror in Ram settings"
printf "\n Manual configuration needed"
printf "\n Open /opt/minecraft/user_jvm_args.txt in your favorite editor. File contains more instructions"
else
printf "Do you want to start server right now? [Y/n]: \n"
read startServer
if [[ "$startServer" =~ ("y"|"Y") || -z $startServer ]]; then
systemctl start mc-forge
fi
systemctl enable mc-forge
fi
printf "\n+ Done, thanks for your time. It is goode idea to chack if everythink is alright by running:"
printf "\nsystemctl status mc-forge"
printf "\nsu minecraft -s /bin/bash"
printf "\n+ it is good idea to have some fun with screen before trying to open minecraft conesole 'screen -r' while loged wia minecraft user"