Skip to content

Commit

Permalink
Edited script
Browse files Browse the repository at this point in the history
  • Loading branch information
spoutn1k committed Jan 5, 2024
1 parent b319e5f commit 6a87436
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions scripts/average.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,72 @@

set -eo pipefail

install_imagemagick() {
SYSTEM=$(uname)
EXTRACTDIR=/tmp/extracted_blocks

macos::install_deps() {
# Script requires imagemagick
if ! command -v convert &>/dev/null; then
if [[ "${UNAME}" == "Darwin" ]] && command -v brew &>/dev/null; then
if [[ "$SYSTEM" == "Darwin" ]] && command -v brew &>/dev/null; then
brew install imagemagick
fi
fi
}

unpack_minecraft_jar() {
if [[ -z "${MINECRAFT_VER}" ]]; then
return
fi
macos::mc_home() {
echo "$HOME/Library/Application Support/minecraft/versions"
}

if [[ -f "extracted_blocks/${MINECRAFT_VER}/assets/minecraft/textures/block/dirt.png" ]]; then
linux::mc_home() {
echo "$HOME/.minecraft/versions"
}

unpack_assets() {
case "$SYSTEM" in
Darwin)
MC_HOME="$(macos::mc_home)"
;;
Linux)
MC_HOME="$(linux::mc_home)"
;;
esac

if [[ -z "$MINECRAFT_VER" ]]; then
return
fi

if [[ "${UNAME}" == "Darwin" ]]; then
jar_file="${HOME}/Library/Application Support/minecraft/versions/${MINECRAFT_VER}/${MINECRAFT_VER}.jar"
fi
JAR="$MC_HOME/$MINECRAFT_VER/$MINECRAFT_VER.jar"

if [[ -n "${jar_file}" ]]; then
mkdir -p "extracted_blocks/${MINECRAFT_VER}"
pushd "extracted_blocks/${MINECRAFT_VER}"
jar xf "${jar_file}" assets/minecraft/textures/block
if [[ -n "$JAR" ]]; then
mkdir -p "$EXTRACTDIR/$MINECRAFT_VER"
pushd "$EXTRACTDIR/$MINECRAFT_VER"
jar xf "$JAR" assets/minecraft/textures/block
popd
fi
}

install_imagemagick
unpack_minecraft_jar
average() {
FILE=$1
EXTRACTED="$EXTRACTDIR/$MINECRAFT_VER/assets/minecraft/textures/block/$1.png"
if [[ -f "$EXTRACTED" ]] ; then
FILE="$EXTRACTED"
fi

COLOR="$(convert "$FILE" -resize 1x1 txt:- \
| grep -o "#[[:xdigit:]]\{6\}" \
| tr A-F a-f)"

printf '%s\t%s\n' \
"$(basename "$FILE")" \
"$COLOR"
}

if [[ "$SYSTEM" == Darwin ]] ; then
macos::install_deps

if [[ ! -d "$EXTRACTDIR/assets/minecraft/textures/" ]] ; then
unpack_assets
fi
fi

printf '%s\t%s\n' \
"$(basename "extracted_blocks/${MINECRAFT_VER}/assets/minecraft/textures/block/${1}.png")" \
"$(convert "extracted_blocks/${MINECRAFT_VER}/assets/minecraft/textures/block/${1}.png" -resize 1x1 txt:- \
| grep -o "#[[:xdigit:]]\{6\}" | tr A-F a-f)"
average $1

0 comments on commit 6a87436

Please sign in to comment.