Skip to content

Instantly share code, notes, and snippets.

@olivia-banks
Created May 11, 2023 02:18
Show Gist options
  • Select an option

  • Save olivia-banks/cb608f51a05175d884a206b8a11c0e91 to your computer and use it in GitHub Desktop.

Select an option

Save olivia-banks/cb608f51a05175d884a206b8a11c0e91 to your computer and use it in GitHub Desktop.
Given a zipfile with a modpack contained inside, it will download Forge, the mods, apply overrides, compile and upload a mod archive for clients, etc...
#!/bin/bash
set -e
if [ "$1" == "" ]; then
echo "Please supply a modpack zipfile as an argument."
exit 1
fi
if [ -d "./mods" ]; then
echo "A modpack is already installed. Please remove everything to provide a clean slate."
fi
echo "Unzipping modpack"
unzip -o $1 > /dev/null
## Check if manifest.json exists, exit if it does not exist
if [ ! -f "manifest.json" ]; then
echo "No manifest.json found in modpack."
exit 1
fi
MANIFEST_JSON=$(cat manifest.json)
OVERRIDES_DIR=$(echo $MANIFEST_JSON | jq -r .overrides)
MINECRAFT_VERSION=$(echo $MANIFEST_JSON | jq -r .minecraft.version)
FORGE_VERSION=$(echo $MANIFEST_JSON | jq -r .minecraft.modLoaders[0].id)
FORGE_VERSION=${FORGE_VERSION//forge-/}
echo "Using minecraft v$MINECRAFT_VERSION"
echo "Using forge v$FORGE_VERSION"
if [ ! -z "${OVERRIDES_DIR}" ] && [ "${OVERRIDES_DIR}" != "null" ] && [ -d "./${OVERRIDES_DIR}" ]; then
echo "Considering all overrides in"
for override in ${OVERRIDES_DIR}/*; do
echo "Considering override $override"
rm -rf $(basename $override)
mv $override $(basename $override)
done
rm -rf ${OVERRIDES_DIR}
fi
## Function to download mod files from manifest.json
function downloadFile {
if [[ $# != 2 ]]; then
echo "Not enough arguments to download file"
exit 1
fi
## Change to mods directory
cd mods
## Get download url for mod file
#MOD_FILE_URL=$(curl -sL "https://addons-ecs.forgesvc.net/api/v2/addon/${1}/file/${2}/download-url")
MOD_FILE_URL="$(curl -sSL https://api.curse.tools/v1/cf/mods/${1}/files/${2}/download-url | jq -r .data)"
MOD_FILE_URL=${MOD_FILE_URL// /%20}
echo "Downloading $MOD_FILE_URL"
curl -sSLOJ "$MOD_FILE_URL"
}
## Make downloadFile function accessible for xargs call
export -f downloadFile
# Create mods directory if it does not exist
[ ! -d "mods" ] && mkdir "mods"
# Download all mod files to mods directory
cat manifest.json | \
jq -r '.files[] | (.projectID|tostring) + " " + (.fileID|tostring)' | \
xargs -P 10 -I {} bash -c 'downloadFile $@' _ {}
FORGE_INSTALLER_FILENAME=forge-${MINECRAFT_VERSION}-${FORGE_VERSION}-installer.jar
echo "Downloading forge installer"
rm -f $FORGE_INSTALLER_FILENAME
curl -sSLOJ https://maven.minecraftforge.net/net/minecraftforge/forge/${MINECRAFT_VERSION}-${FORGE_VERSION}/${FORGE_INSTALLER_FILENAME}
echo "Running forge installer"
java -jar ${FORGE_INSTALLER_FILENAME} --installServer .
rm $FORGE_INSTALLER_FILENAME
echo "Creating initial files"
bash ./run.sh
echo "---- EULA ----"
cat eula.txt
echo "---- END EULA ----"
while true; do
read -r -p "Do you want to agree to the EULA (required to continue)? [y/n] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
echo "Agreeing to EULA!"
break
else
echo "Goodbye!"
exit 0
fi
done
echo "eula=true" > eula.txt
echo "Creating mod archive for distribution."
tar -czf mods.tar.gz mods
echo "Installation complete!"
echo "You may want to change server.properties to customize your server."
echo "There is a mods.tar.gz file in your current directory that contains all the mods needed for a client"
echo "to join this server. You can host this any way you like, and you can also grab it off the internet here:"
echo "(uploading to keep.sh, this might take a moment)"
curl --upload-file mods.tar.gz https://free.keep.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment