minecraft-bash-scripts/start.sh

125 lines
3.0 KiB
Bash
Raw Normal View History

2025-09-18 16:15:35 +02:00
#!/usr/bin/env bash
2025-12-24 18:56:39 +01:00
# THe name of the screen in which the proxy server will run
2025-09-18 16:15:35 +02:00
PROXY_SCREEN="proxy"
2025-12-24 18:56:39 +01:00
# How many seconds to sleep before starting the server again, unless it is explicitly specified
DEFAULT_SLEEP_SECONDS=3
function main {
local proxy=false
local bungee=false
local help=false
local sleep_seconds=$DEFAULT_SLEEP_SECONDS
2025-12-24 23:24:44 +01:00
local config=""
2025-12-24 18:56:39 +01:00
local cmd=$0
2025-12-24 23:24:44 +01:00
# TODO --config is incompatible with --proxy, --bungee and standalone
2025-12-24 18:56:39 +01:00
while [ $# -gt 0 ]; do
local arg=$1
case "$arg" in
2025-12-24 23:24:44 +01:00
"--config" | "-c")
config=$2 #TODO ensure mode is valid
shift 2
;;
2025-12-24 18:56:39 +01:00
"--sleep" | "-s")
sleep_seconds=$2
shift 2
;;
"--proxy")
proxy=true
if $bungee; then
err "--proxy can't be used together with --bungee"
fi
shift
;;
"--bungee")
bungee=true
if $proxy; then
err "--bungee can't be used together with --proxy"
fi
shift
;;
"--help" | "-h")
help=true
shift
;;
*)
err "Unknown argument: $arg"
;;
esac
done
if $help; then
echo "Create a start loop for the minecraft server, additionally behind a velocity proxy or bungeecord proxy"
echo ""
echo "Usage $cmd [OPTIONS]"
echo ""
echo "OPTIONS:"
2025-12-24 23:24:44 +01:00
echo " -c --config <mode> How to configure the server. Possible values for mode are: proxy, bungee, standalone"
echo " -s --sleep <secs> How many seconds to sleep after the server has stopped before starting it again"
echo " -h --help Show help"
echo " --proxy Start the server behind a velocity proxy"
echo " --bungee Start the server behind a bungeecord proxy"
2025-12-24 18:56:39 +01:00
exit
fi
2025-12-24 23:24:44 +01:00
# TODO perform configuration according to config parameter
2025-12-24 18:56:39 +01:00
if $proxy; then
./configure.sh velocity
# create proxy screen if it doesn't exist yet
screen -ls | grep -q "$PROXY_SCREEN" || screen -dmS "$PROXY_SCREEN"
# start the proxy
screen -S "$PROXY_SCREEN" -X stuff "cd $(pwd)/proxy; ./launch.sh\n"
echo "The velocity proxy has been started in the screen '$PROXY_SCREEN'"
elif $bungee; then
./configure.sh bungeecord
2025-09-18 16:15:35 +02:00
2025-12-24 18:56:39 +01:00
# create proxy screen if it doesn't exist yet
screen -ls | grep -q "$PROXY_SCREEN" || screen -dmS "$PROXY_SCREEN"
# start the proxy
screen -S "$PROXY_SCREEN" -X stuff "cd $(pwd)/bungee; ./launch.sh\n"
echo "The bungeecord proxy has been started in the screen '$PROXY_SCREEN'"
else
./configure.sh standalone
2025-09-18 16:15:35 +02:00
fi
2025-12-24 18:56:39 +01:00
trap cleanup SIGINT
cleanup() {
if $proxy || $bungee; then
# stop the proxy
screen -S "$PROXY_SCREEN" -X stuff "end\n"
echo "The proxy has been stopped"
fi
exit
}
while true
do
echo starting server...
./launch.sh
echo sleeping...
sleep "$sleep_seconds"
done
}
# Shows ans error message and exits the program with code 1
#
# Usage: err <msg>
function err {
echo "$1"
exit 1
2025-09-18 16:15:35 +02:00
}
2025-12-24 18:56:39 +01:00
main "$@"
2025-09-18 16:15:35 +02:00