Skip to content

Instantly share code, notes, and snippets.

@Kas-tle
Created December 20, 2021 07:13
Show Gist options
  • Save Kas-tle/884e9695712450a6e30d5304e661ec08 to your computer and use it in GitHub Desktop.
Save Kas-tle/884e9695712450a6e30d5304e661ec08 to your computer and use it in GitHub Desktop.
Minecraft Book Head Generator
#!/usr/bin/env bash
: ${1?'Please specify a player name (e.g. ./book_head.sh _Kastle)'}
# Config
title='My Cool Title'
author='_Kastle'
# Get player UUID
uuid=$(curl -s https://api.mojang.com/users/profiles/minecraft/${1} | jq -r .id)
# Exit if a valid username was not provided
[ -z "${uuid}" ] && echo "Please provide a valid username" && exit 1
# Set overlay for image if the user requested
[ -z "${2}" ] && overlay='?overlay'
# Make output file
touch book_commands.txt
echo -n "/minecraft:give @p written_book{title:\"${title}\",author:\"${author}\",pages:['" >> book_commands.txt
# Get image and output
convert https://crafatar.com/avatars/${uuid}${overlay} -resize 8x8 RGB: | xxd -p -g3 -c24 | jq --raw-input --slurp -r -c '
split("\n")
| del(.[-1])
| map(
[
{"text":"|||","color":("#" + .[0:6])},
{"text":"|||","color":("#" + .[6:12])},
{"text":"|||","color":("#" + .[12:18])},
{"text":"|||","color":("#" + .[18:24])},
{"text":"|||","color":("#" + .[24:30])},
{"text":"|||","color":("#" + .[30:36])},
{"text":"|||","color":("#" + .[36:42])},
{"text":"|||\\n","color":("#" + .[42:48])}
]
) | .[0][0].bold |= true | add
' | tr -d \\n >> book_commands.txt
# End command line for book
echo "']} 1" >> book_commands.txt
# Output command to terminal
tail -n 1 book_commands.txt
@Kas-tle
Copy link
Author

Kas-tle commented Dec 26, 2021

Minecraft Book Head Generator

About

This script takes an input Minecraft username and outputs a book with the head of the specified user. The script also supports the ability to exclude the head clothing layer if that is not desired.

2021-12-26_12 58 58

The script works by pulling the given user's head from the Crafatar API. This could easily be modified with ImageMagick to exclusively use the Mojang skin API, but the current implementation has been done for simplicity. ImageMagick is then used to output the the image as a file of pure RGB values. The hexadecimal representations of each pixel can then be easily obtained by using xxd to display a hex dump of the pure RGB file. The flag -c24 is used to dump 24 octets per line, which effectively displays a hexadecimal matrix of color values that correspond to the actual pixels of the image.

The raw input of the hexadecimal matrix is then parsed by jq with new lines treated as splitters. Each head pixel is mapped to three bold pipe characters. This is formatted to proper NBT output by jq. The NBT is then placed in a Minecraft command to give the book to a player.

Usage

./book_head.sh <username> [no_overlay]

Dependencies

The script requires:

If you have trouble installing the latest version of ImageMagick, consider using IMEI.

Editing Books

If you are not comfortable editing the outputted command to add pages and text, I would recommend using the import command feature in MCStacker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment