Skip to content

Instantly share code, notes, and snippets.

@nfitzen
Last active September 10, 2021 12:17
Show Gist options
  • Save nfitzen/c2acc6406435db94d173212846700549 to your computer and use it in GitHub Desktop.
Save nfitzen/c2acc6406435db94d173212846700549 to your computer and use it in GitHub Desktop.
Generates the default Minecraft datapack and optionally gets resources (if client JAR's downloaded)
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2020-2021 Nathaniel Fitzenrider <https://github.com/nfitzen>
#
# SPDX-License-Identifier: CC0-1.0
# This program requires a copy of the Minecraft server software
# and its corresponding client version downloaded through the Launcher.
# save `server.jar` in the current working directory.
# Get version. This can only be done by running the server to my knowledge.
# Very hacky.
# If there's another way to force the server to terminate, then that's great.
# I just didn't want to screw with GNU/Linux processes.
# Plus, it might not work on GNU/kWindows if I do so.
if [ -n $1 ]; then
generator_args="$*"
else
generator_args="--server"
fi
rm -rf server/
mkdir server
echo "eula=true" > server/eula.txt
echo "server-port=25566" > server/server.properties
cd server
nc -l 25566 &
java -jar ../server.jar nogui > /dev/null
mc_version_line="$( grep -i "minecraft server version" logs/latest.log 2> /dev/null )"
mc_version="${mc_version_line:67}"
cd ..
echo "Minecraft version $mc_version"
# Back to not-so-hacky stuff
os="$( ( (uname -o || uname -s) 2> /dev/null ) | tr '[:upper:]' '[:lower:]' )"
echo "your OS is: $os"
case "$os" in
*win* | msys) _MC_DIR=%APPDATA%/.minecraft/;; # unsure how MSYS env variables work
darwin | macos) _MC_DIR=~/Library/"Application Support"/minecraft/;;
*) _MC_DIR=~/.minecraft/;;
esac
echo "detected .minecraft dir: $_MC_DIR"
if ( command -v unzip > /dev/null ); then
UNZIP="unzip -ud generated/"
else
UNZIP="7z x -ogenerated"
fi
echo "Using arguments $generator_args"
rm -r generated
$UNZIP $_MC_DIR/versions/$mc_version/$mc_version.jar assets/* pack.mcmeta pack.png > /dev/null 2>&1
if [ $? -ge 1 ]; then
echo "Couldn't extract resources."
fi
java -cp server.jar net.minecraft.data.Main $generator_args > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment