Initial MC Scripts

This commit is contained in:
Price Hiller 2021-07-31 13:45:43 -05:00
parent 88fa5537ca
commit a34c00960b
3 changed files with 200 additions and 0 deletions

View File

@ -0,0 +1,10 @@
#!/bin/bash --posix
### Minecraft Ports ###
# Game ports 12000 - 12099
# Query ports 12100 - 12199
# RCON ports 12200 - 12299
firewall-cmd --zone=public --add-port=12000-12299/tcp --permanent
firewall-cmd --reload

View File

@ -0,0 +1,189 @@
#!/bin/bash
### CONSTANTS ###
#gigabytes
MAX_RAM=8
#gigabytes
INITIAL_MEM=2
log() {
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
#
# Arguments:
# level <type: string> <position: 1> <required: true>
# - The log level, defined within a case check in this function
# message <type: string> <position: 2> <required: true>
# - The info message
# line_number <type: int> <position: 3> <required: false>
# - The line number of the calling function (${LINNO})
#
# Usage:
# log "info" "Could not find that directory"
#
# POSIX Compliant:
# Yes
#
# Set debug status depending if a global debug variable has been set to either 1 or 0
local debug
if [ ${DEBUG} ]; then
debug=${DEBUG}
else
debug=0
fi
local FORMAT
FORMAT="[$(date +%Y-%m-%dT%H:%M:%S%z)]"
# Convert the level to uppercase
local level
level=$(echo "${1}" | tr '[:lower:]' '[:upper:]')
local message
message="${2}"
case "${level}" in
INFO)
# Output all info log levels to stdout
printf "${FORMAT}[INFO] %s\n" "${message}" >&1
return 0
;;
DEBUG)
[[ ${debug} == 0 ]] && return 0
printf "${FORMAT}[DEBUG] %s\n" "${message}" >&1
return 0
;;
ERROR)
# Output all error log levels to stderr
printf "${FORMAT}[ERROR] %s\n" "${message}" >&2
return 0
;;
# Further log levels can be added by extending this switch statement with more comparisons
*) # Default case, no matches
# Returns non-zero code as an improper log option was passed, this helps with using `set -e`
log "ERROR" "Invalid log level passed, received level \"${level}\" with message \"${message}\""
return 1
esac
}
usage() {
# Print out usage instructions for the local script
#
# Arguments:
# None
#
# Usage:
# usage
#
# POSIX Compliant:
# Yes
#
printf "Usage: %s%s\n" \
"$(basename ${0}) " \
"
-i \"this is some input\" -t \"this is some more example input\"
--input <string> | -i <string>
Example:
--input \"this is an example input\"
--test <string> | -t <string>
Example:
--test \"this is more example input\""
}
server_id=""
minecraft_jar=""
parse_args() {
# Parse input arguments
#
# Arguments:
# Consult the `usage` function
#
# Usage:
# parse_args "$@"
# - All arguments should be ingested by parse_args first for variable setting
#
# POSIX Compliant:
# Yes
#
while :; do
case ${1} in
-h | -\? | --help)
usage # Display a usage synopsis.
exit
;;
--) # End of all options.
break
;;
-s | --server)
shift
server_id="${1}"
;;
-j | --jar)
shift
minecraft_jar="${1}"
;;
-?*)
printf 'Unknown option: %s\n' "$1" >&2
usage
;;
*) # Default case: No more options, so break out of the loop.
break ;;
esac
shift
done
}
parse_args "$@"
# Check and see that the correct variables were set, some may not be expanded as if they're empty they're useless
# anyhow
[[ -z "${server_id}" ]] && log "error" "No server ID provided" && exit 1
[[ "${server_id}" =~ [0-9] ]] || (log "error" "Server ID must be a number, received: ${server_id}" && exit 1)
minecraft_directory=~/Minecraft/Server-"${server_id}"
mkdir -p "${minecraft_directory}"
cd "${minecraft_directory}" || (log "error" "Unable to change directory to ${minecraft_directory}" && exit 1)
if [ ! -z "${minecraft_jar}" ]; then
[[ -d "${minecraft_jar}" ]] || (log "error" "Could not find a minecraft server jar at \"${minecraft_jar}\"" && exit 1)
minecraft_jar_name="$(basename ${minecraft_jar})"
mv "${minecraft_jar}" "${minecraft_directory}/server.jar"
java -jar "${minecraft_directory}/${minecraft_jar_name}"
elif [ ! -d "${minecraft_directory}" ]; then
log "error" "The minecraft server \"${minecraft_directory}\" did not exist, to create it please pass the \"--jar\" flag..."
exit 1
fi
server_port=$(("12000" + "${server_id}"))
query_port=$(("12100" + "${server_id}"))
rcon_port=$(("12200" + "${server_id}"))
## Set the correct ports
sed -i "s/server-port=.+/server-port=${server_port}/g" server.properties \
&& log "info" "Set the server port to ${server_port}"
sed -i "s/query.port=.+/query.port=${query_port}/g" server.properties \
sed -i "s/rcon.port=.+/rcon.port=${rcon_port}/g" server.properties
## Turn RCON On
# set the rcon password to the default
log "info" "Setting the RCON password..."
default_rcon_password=""
[[ -z "${default_rcon_password}" ]] \
&& log "error" "No RCON password has been defined within $(basename "${0}") -- exiting!" \
&& exit 1
sed -i "s/rcon.password=.+/rcon.password=${default_rcon_password}/g" server.properties \
&& log "info" "RCON password set"

View File

@ -0,0 +1 @@
Minecraft Server Uses Spigot