Last active
December 3, 2020 05:28
-
-
Save Machine-Maker/7738eabd8e4cafe7ac6b0f90d2a457e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
shopt -s extglob | |
ARCH= | |
VERSION= | |
RAM=6G | |
function confirm () { | |
printf "$1 [Y/n]: " | |
read -n 1 RES | |
if [[ ${RES/Y/y} == "y" ]]; then | |
printf "\n" | |
return | |
else | |
printf "\nCancelling...\n" | |
exit 2 | |
fi | |
} | |
# Parse options | |
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do | |
case $1 in | |
-R | --reset) | |
confirm "Remove all files/folders except .updater/ and run.sh?" | |
echo "Removing..." | |
shopt -s dotglob | |
rm -rf !(run.sh|.updater) | |
shopt -u dotglob | |
echo "Done." | |
exit 2 | |
;; | |
--reset-hard) | |
confirm "Remove all files/folders except run.sh?" | |
echo "Removing..." | |
shopt -s dotglob | |
rm -rf !(run.sh) | |
shopt -u dotglob | |
echo "Done." | |
exit 2 | |
;; | |
-A | --arch) | |
shift | |
ARCH=$1 | |
;; | |
-V | --version) | |
shift | |
VERSION=$1 | |
;; | |
--no-start) | |
NO_START="true" | |
;; | |
-h | --help) | |
echo "========================Help Menu========================" | |
echo "-R,--reset Resets the server directory leaving only" | |
echo " this script and the .updater/ directory" | |
echo "--reset-hard Resets the server directory leaving only" | |
echo " this script" | |
echo "-A,--arch <A> Sets the server architecture" | |
echo " currently one of: paper, spigot, craftbukkit, vanilla" | |
echo "-V,--version <V> Sets the server version" | |
echo " paper, spigot, craftbukkit must have a numerical version number" | |
echo " vanilla supports snapshot alphanumbers" | |
echo "--no-start Does not start the server once the update check process is complete" | |
echo "-h,--help Displays this menu" | |
exit | |
;; | |
*) | |
echo "$1 is an invalid option!" | |
exit | |
;; | |
esac | |
shift | |
done | |
BT_LAST_BUILD="https://hub.spigotmc.org/jenkins/job/BuildTools/lastBuild" | |
HELPER_API="https://mc-server-versions-api.glitch.me/" | |
SHOME=$(pwd) | |
DIR=".updater" | |
BT_DIR="$DIR/BuildTools" | |
function check_setup () { | |
if [[ ! -e "$DIR" ]]; then | |
if [[ -z $ARCH || -z $VERSION ]]; then | |
echo "You must specify an architecture and version either as a command-line argument, or in the script itself!" | |
exit 2 | |
fi | |
mkdir "$DIR" > /dev/null 2>&1 | |
echo "$ARCH" > "$DIR/server.arch" | |
echo "$VERSION" > "$DIR/server.version" | |
echo "Created $DIR directory & files" | |
return | |
fi | |
if [[ ! -e "$DIR/server.arch" ]]; then | |
if [[ -z $ARCH ]]; then | |
echo "Could not find a $DIR/server.arch file and you did not specify an arch though the command-line" | |
exit 2 | |
fi | |
echo "$ARCH" > "$DIR/server.arch" | |
echo "Created $DIR/server.arch file" | |
else | |
ARCH=$(cat "$DIR/server.arch") | |
fi | |
if [[ ! -e "$DIR/server.version" ]]; then | |
if [[ -z $VERSION ]]; then | |
echo "Could not find a $DIR/server.version file and you did not specify an version though the command-line" | |
exit 2 | |
fi | |
echo "$VERSION" > "$DIR/server.version" | |
echo "Created $DIR/server.version file" | |
else | |
VERSION=$(cat "$DIR/server.version") | |
fi | |
} | |
function setup_buildtools () { | |
if [[ ! -e "$BT_DIR" ]]; then | |
mkdir "$BT_DIR" > /dev/null 2>&1 | |
fi | |
# Check for buildtools.build file | |
if [[ ! -e "$BT_DIR/buildtools.build" ]]; then | |
get_latest_build buildtools > "$BT_DIR/buildtools.build" | |
fi | |
# Check for BuildTools.jar | |
if [[ ! -e "$BT_DIR/BuildTools.jar" ]]; then | |
echo "Downloading latest BuildTools.jar..." | |
curl -# -o "$BT_DIR/BuildTools.jar" "$BT_LAST_BUILD/artifact/target/BuildTools.jar" | |
fi | |
} | |
function check_update_buildtools () { | |
local LATEST=$(get_latest_build buildtools) | |
if [[ $LATEST > $(cat "$BT_DIR/buildtools.build") ]]; then | |
echo "You have an outdated BuildTools.jar!" | |
download_version buildtools $LATEST | |
else | |
echo "Your BuildTools.jar is up-to-date!" | |
fi | |
unset LATEST | |
} | |
function version_check () { | |
local BUILD=$(get_latest_build $ARCH $VERSION) | |
if [[ ! -e "$DIR/server.build" ]]; then | |
echo "No server.build file found!" | |
download_version $ARCH $VERSION $BUILD | |
else | |
echo "Found server.build file! Checking for update..." | |
# Compare build numbers | |
if (( $BUILD > `cat "./$DIR/server.build"` )); then | |
echo "Your build is out of date!" | |
printf "Download the update? [Y/n]: " | |
read -n 1 RES | |
printf "\n" | |
if [[ ${RES/Y/y} == "y" ]]; then | |
echo "Downloading..." | |
download_version $ARCH $VERSION $BUILD | |
else | |
echo "Skipping update..." | |
fi | |
else | |
echo "Your build is up to date!" | |
fi | |
fi | |
unset BUILD | |
} | |
function download_version () { | |
case $1 in | |
"paper") | |
echo "Downloading Paper v$2 build #$3..." | |
curl "https://papermc.io/api/v2/projects/$1/versions/$2/builds/$3/downloads/$1-$2-$3.jar" -# -o "$1-$2.jar" | |
echo "Finished downloading $1-$2.jar!" | |
set_eula | |
;; | |
"spigot" | "craftbukkit") | |
setup_buildtools | |
check_update_buildtools | |
echo "Running BuildTools to get $1-$2.jar (this may take a while the first time)" | |
cd "$BT_DIR" | |
java -jar BuildTools.jar --rev $2 --compile $1 | |
cd "$SHOME" | |
cp "$BT_DIR/$1-$2.jar" ./$1-$2.jar | |
echo "Finished compiling $1-$2.jar!" | |
set_eula | |
;; | |
"vanilla") | |
echo "Downloading Vanilla v$2..." | |
curl $(curl -s $HELPER_API/vanilla/dl/$2) -# -o "$1-$2.jar" | |
echo "Finished downloading $1-$2.jar!" | |
set_eula | |
;; | |
"buildtools") | |
echo "Downloading latest BuildTools.jar..." | |
curl -# -o "$BT_DIR/BuildTools.jar" "$BT_LAST_BUILD/artifact/target/BuildTools.jar" | |
echo $2 > "$BT_DIR/buildtools.build" | |
return 1 | |
;; | |
esac | |
echo $3 > "$DIR/server.build" | |
echo "Set build #$3 as server build in $DIR/server.build" | |
} | |
function set_eula () { | |
echo "Creating eula.txt... By using this tool, you are indicating your agreement to Mojang's EULA (https://account.mojang.com/documents/minecraft_eula)" | |
printf "#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula).\n#Thu Mar 12 03:11:10 PDT 2020\neula=true" > eula.txt | |
} | |
function get_latest_build () { | |
case $1 in | |
"paper" | "waterfall") | |
echo $(curl -s https://papermc.io/api/v2/projects/$1/versions/$2 | grep -E -o '[0-9]+' | tail -1) | |
;; | |
"spigot" | "craftbukkit") | |
echo $(curl -s "$HELPER_API/spigot/versions/$2") | |
;; | |
"vanilla") | |
echo -1 | |
;; | |
"buildtools") | |
echo $(curl -s "$BT_LAST_BUILD/buildNumber") | |
;; | |
esac | |
} | |
function trap_ctrlc () { | |
printf "\nCancelling...\n" | |
exit 2 | |
} | |
# Catch ctrl+c | |
trap "trap_ctrlc" 2 | |
check_setup | |
version_check | |
if [[ -z $NO_START ]]; then | |
while true; do | |
echo "Starting server..." | |
java -Xms${RAM} -Xmx${RAM} -jar "${ARCH}-${VERSION}.jar" nogui | |
read -n 1 -e -r -s -p "Press any key to restart server" | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works fine for me. Remember, this is a bash script, not batch. It wont work in Windows Command Prompt.
Plus, if you download it and you set your version/ram, you don't really need to use any of the flags. just a simple
./run.sh
will start everything up.