#!/usr/bin/env bash # THe name of the screen in which the proxy server will run PROXY_SCREEN="proxy" # The relative path of the velocity proxy server VELOCITY_DIR="proxy" # The relative path of the bungeecord proxy server BUNGEE_DIR="bungee" # How many seconds to sleep before starting the server again, unless it is explicitly specified DEFAULT_SLEEP_SECONDS=3 function show_help { local cmd=$0 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:" { local fmt=" %s\t%s\t%s\n" printf "$fmt" "-c" "--config " "How to configure the server (see ./configure.sh --help). Possible values for mode are: proxy, bungee, standalone" printf "$fmt" "-t" "--temp" "Apply the configuration only temporary, while the server is running. Requires --config." printf "$fmt" "" "--tmp" "Alias to --temp" printf "$fmt" "-s" "--sleep " "How many seconds to sleep after the server has stopped before starting it again" printf "$fmt" "-n" "--no-loop" "Don't restart the server once it stopped" printf "$fmt" "-h" "--help" "Show this message" printf "$fmt" "" "--proxy" "Temporary configure this server for use with Velocity and start a Velocity proxy in the screen $PROXY_SCREEN while this server is running" printf "$fmt" "" "--bungee" "Temporary configure this server for use with Bungeecord and start a Bungeecord proxy in the screen $PROXY_SCREEN while this server is running" } | column -t -s $'\t' exit } function main { local proxy=false local bungee=false local help=false local sleep_seconds=$DEFAULT_SLEEP_SECONDS local config="" local start_proxy=false local tmp=false local loop=true local cmd=$0 # Parse arguments while [[ $# -gt 0 ]]; do local arg=$1 case "$arg" in "--config" | "-c") local mode=$2 case "$mode" in "proxy") config="velocity" ;; "bungee") config="bungeecord" ;; "standalone") config="standalone" ;; *) err "Invalid mode for --config: $mode" ;; esac if $proxy; then err "--config can't be used together with --proxy" elif $bungee; then err "--config can't be used together with --bungee" fi shift 2 ;; "--temp" | "--tmp" | "-t") tmp=true shift ;; "--sleep" | "-s") sleep_seconds=$2 shift 2 ;; "--no-loop" | "-n") loop=false shift ;; "--proxy") proxy=true if $bungee; then err "--proxy can't be used together with --bungee" elif [[ -n $config ]]; then err "--proxy can't be used together with --config" fi shift ;; "--bungee") bungee=true if $proxy; then err "--bungee can't be used together with --proxy" elif [[ -n $config ]]; then err "--bungee can't be used together with --config" fi shift ;; "--help" | "-h") help=true shift ;; *) err "Unknown argument: $arg" ;; esac done # Late parsing: if $tmp && [[ -z $config ]]; then err "--temp requires --config parameter" fi if $proxy; then start_proxy=true config="velocity" tmp=true elif $bungee; then start_proxy=true config="bungeecord" tmp=true fi if $help; then show_help "$cmd" fi # Apply configuration local previous_config=$(./configure.sh --fetch) echo "Current configuration is $previous_config" if [[ -n $config ]]; then ./configure.sh "$config" fi # Start proxy if $start_proxy; then # create proxy screen if it doesn't exist yet screen -ls | grep -q "$PROXY_SCREEN" || screen -dmS "$PROXY_SCREEN" # start the proxy local proxy_dir="" case "$config" in "velocity") proxy_dir=$VELOCITY_DIR ;; "bungeecord") proxy_dir=$BUNGEE_DIR ;; esac screen -S "$PROXY_SCREEN" -X stuff "cd $(pwd)/$proxy_dir; ./launch.sh\n" echo "The proxy has been started in screen $PROXY_SCREEN" fi function cleanup { if $start_proxy; then # stop the proxy screen -S "$PROXY_SCREEN" -X stuff "end\n" echo "The proxy has been stopped" fi if $tmp; then ./configure.sh "$previous_config" fi exit } trap cleanup SIGINT while :; do echo "Starting server..." ./launch.sh $loop || break echo "Sleeping for $sleep_seconds seconds..." sleep "$sleep_seconds" done cleanup } # Shows an error message and exits the program with code 1 # # Usage: err function err { echo "$1" exit 1 } main "$@"