Skip to content

Instantly share code, notes, and snippets.

@DerEnderKeks
Created November 15, 2020 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DerEnderKeks/bd5f3864e53c03d0e079220f1e019c28 to your computer and use it in GitHub Desktop.
Save DerEnderKeks/bd5f3864e53c03d0e079220f1e019c28 to your computer and use it in GitHub Desktop.
Script to print a specific Minecraft statistic for all players
#!/usr/bin/env bash
# This script prints the uuid, username and value of a minecraft statistic from the json files (located at world/stats/*.json).
PROFILE_URL="https://sessionserver.mojang.com/session/minecraft/profile"
usage() { echo "Usage: $0 [-d <stats directory>] [-1 <first json path, default 'custom'>] [-2 <second json path, default 'deaths'>]" 1>&2; exit 1; }
while getopts ":d:1:2:" o; do
case "${o}" in
d)
dir=${OPTARG}
;;
1)
prop1=${OPTARG}
;;
2)
prop2=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
dir=$([[ -z "$dir" ]] && echo "$PWD" || echo "$dir")
function addPrefix {
if [[ $1 =~ ":" ]]; then
echo $1
else
echo "minecraft:$1"
fi
}
stats_dir=$([[ -z "$3" ]] && echo "custom" || echo "$1")
prop1=$(addPrefix $([[ -z "$prop1" ]] && echo "custom" || echo "$prop1"))
prop2=$(addPrefix $([[ -z "$prop2" ]] && echo "deaths" || echo "$prop2"))
pad=$(printf '%0.1s' " "{1..20})
for f in $dir/*.json; do
filename=$(basename "$f")
uuid=${filename%.*}
name=$(curl -sL "$PROFILE_URL/$uuid" | jq .name)
property=$(cat "$f" | jq ".stats[\"$prop1\"][\"$prop2\"]")
printf "%s %s %*.*s %s\n" "$uuid" "$name" 0 $((18 - ${#name})) "$pad" "$property"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment