Nerd-Code Snippets 3: Bash Minecraft Install Scripts

Nerd-Code Snippets 3: Bash Minecraft Install Scripts

TL;DR - I haven’t posted in a while. I like scripting and I try to script my Ubuntu 20.04 setup whenever I can. While a bit silly, Minecraft, and a few other 3rd party programs that work with Minecraft, find their download/installation/upgrade scripts among my bash-scripted setup files.

Minecraft & 3rd Party Launchers

Yes, this old blog still exists, despite how sparse my contributions. However, now it is mid-2020, I’ve been at Synap for over 4 years, I’m married, have had back surgery, have a 1-year-old daughter, and – most importantly – I have recently upgraded to Ubuntu 20.04.

As a part of upgrading to Ubuntu 20.04, I’ve decided to script as much of my setup and preferences as possible. While not a staple of my day-job at all, I do always have Minecraft installed on the Linux-side of my computer. The latest release, Minecraft 1.16, sees a huge update to the nether dimension. As a result, certain 3rd party programs are updating as well.

Since these programs do not exist in the Ubuntu repositories, I have scripts that make those downloads, installs, and updates easier to perform.

Install Minecraft

First of all, here is my update script for downloading and installing the latest Minecraft launcher. The launcher gives you a notification in the upper right corner that it’s time to update, but when you click that notification (on Linux) it takes you to a manual download page. So performing the download and installation is much faster using a bash script.

#!/usr/bin/env bash
# install-minecraft.bash

DOWNLOAD_DIR="$HOME/Software/Mojang"
FILE="$DOWNLOAD_DIR/minecraft.deb"
URL="https://launcher.mojang.com/download/Minecraft.deb"

mkdir -p "$DOWNLOAD_DIR"
wget -O "$FILE" "$URL"
sudo apt install -y "$FILE"

This script was pretty straight-forward. It downloads the .deb from a permanent link which always points to the latest version of the installer and then installs it after said download.

Install Amidst & MCA Selector

The other programs are slightly trickier because they are distributed just as Java .jar files from GitHub release pages. There is no permanent link that they publish which points to the latest version. I’m more proud of these scripts than of the previous ones for a few reasons:

  1. the scripts have to figure out the latest version released to be used in the download link and desktop file (using git ls-remote on the developer’s public repository)
  2. the scripts create an appropriate .desktop file from a template (using sed) so I can keep an easy launcher in my sidebar or application menu

Below is my installation script, install-amidst.bash.

#!/usr/bin/env bash

SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})

PROG_REPO="https://github.com/toolbox4minecraft/amidst"
PROG_VER=$(git ls-remote --ref --tags --sort="v:refname" ${PROG_REPO} | tail -n1 | awk -F/ '{print $3}')
PROG_DIR="$HOME/Software/Amidst"
PROG_FILE="$PROG_DIR/amidst.jar"
PROG_URL="${PROG_REPO}/releases/download/${PROG_VER}/amidst-${PROG_VER/./-}.jar"

mkdir -p "$PROG_DIR"
wget -O "$PROG_FILE" "$PROG_URL"

APP_DIR="$HOME/.local/share/applications"
APP_LAUNCHER_SRC="$SCRIPT_DIR/amidst.desktop"
APP_LAUNCHER="$APP_DIR/amidst.desktop"
APP_ICON=$(realpath "$SCRIPT_DIR/amidst.png")

cat "$APP_LAUNCHER_SRC" | sed \
    -e "s/<PROG>/${PROG_FILE//\//\\\/}/" \
    -e "s/<ICON>/${APP_ICON//\//\\\/}/" \
    -e "s/<VER>/${PROG_VER}/" \
    > "$APP_LAUNCHER"

Below is my desktop template, admist.desktop.

#!/usr/bin/env xdg-open
[Desktop Entry]
Type=Application
Name=Amidst <VER>
GenericName=Minecraft World Browser
Comment=Generate new worlds from seed or open existing worlds and view the locations of biomes and generated structures
Exec=java -jar <PROG>
Icon=<ICON>
Terminal=false
Categories=Game
StartupWMClass=amidst-Amidst

Below is my installation script, install-mca-selector.bash.

#!/usr/bin/env bash

SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})

# required and not bundled by mca-selector
sudo apt install -y openjfx

PROG_REPO="https://github.com/Querz/mcaselector"
PROG_VER=$(git ls-remote --ref --tags --sort="v:refname" ${PROG_REPO} | tail -n1 | awk -F/ '{print $3}')
PROG_DIR="$HOME/Software/MCA Selector"
PROG_FILE="$PROG_DIR/mca-selector.jar"
PROG_URL="${PROG_REPO}/releases/download/${PROG_VER}/mcaselector-${PROG_VER}.jar"

mkdir -p "$PROG_DIR"
wget -O "$PROG_FILE" "$PROG_URL"

APP_DIR="$HOME/.local/share/applications"
APP_LAUNCHER_SRC="$SCRIPT_DIR/mca-selector.desktop"
APP_LAUNCHER="$APP_DIR/mca-selector.desktop"
APP_ICON=$(realpath "$SCRIPT_DIR/mca-selector.png")

cat "$APP_LAUNCHER_SRC" | sed \
    -e "s/<PROG>/${PROG_FILE//\//\\\/}/" \
    -e "s/<ICON>/${APP_ICON//\//\\\/}/" \
    -e "s/<VER>/${PROG_VER}/" \
    > "$APP_LAUNCHER"

Below is my desktop template, mca-selector.desktop.

#!/usr/bin/env xdg-open
[Desktop Entry]
Type=Application
Name=MCA Selector <VER>
GenericName=Minecraft Chunk Browser
Comment=Browse and edit chunks in an existing minecraft world save
Exec=java --module-path /usr/share/openjfx/lib --add-modules ALL-MODULE-PATH -jar "<PROG>"
Icon=<ICON>
Terminal=false
Categories=Game
StartupWMClass=net.querz.mcaselector.ui.Window

Final Word

These scripts are very simple in the scheme of things. But simple things can bring simple joys. While it can take a little bit to set up initially, in many cases, scripting is better than clicking. Use these scripts at your own risk.

Comments

Popular posts from this blog

Arduino & Tri-Colored LEDs

Solar Cell Characteristics