Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Created December 4, 2021 01:35
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 ormaaj/bd40da12408305de91e8ff5cc62fd931 to your computer and use it in GitHub Desktop.
Save ormaaj/bd40da12408305de91e8ff5cc62fd931 to your computer and use it in GitHub Desktop.
minecraft function
# Stupid game.
# minecraft -c | -s [ -p ]
function minecraft {
typeset \
jvm=${JAVA_HOME}/bin/java \
worldPath=~/doc/mcdata2 \
serverPath=~/doc/programs/ServerJarStorage/Release\ 1.8/minecraft_server.jar \
clientPath=~/doc/programs/Minecraft.jar
typeset opt OPTARG OPTIND OPTERR=0
typeset -A mcopts
[[ -x $jvm ]] || return
while getopts :scp: opt; do
case $opt in
\?)
printf 'Unknown option: %s\n' "$OPTARG" >&2
return 1
;;
:)
printf '-%s requires an argument.\n' "$OPTARG" >&2
return 1
;;
*)
mcopts[$opt]=$OPTARG
OPTARG=
esac
done
if ! ${mcopts[s]+'false'}; then # Server
pushd "$worldPath" || return
if ! ${mcopts[p]+'false'}; then # If user supplies a fifo then read from it.
if [[ -p ${mcopts[p]} ]]; then
# Open the fifo without blocking using a temporary fd in case there's no writer.
{
{
#lsof -ap "$BASHPID" -d 0-20 +f G >&2
#cat
"$jvm" -Xmx4096M -jar "$serverPath" nogui
} 3<>"${mcopts[p]}" </dev/fd/3 3>&-
} &
else
printf '%s must be a readable/writable named pipe\n' "${mcopts[p]}" >&2
return 1
fi
else
"$jvm" -Xmx4096M -jar "$serverPath" nogui &
fi
printf 'Minecraft server PID: %d\n' "$!"
wait
popd
elif ! ${mcopts[c]+'false'}; then # Client
"$jvm" -jar "$clientPath"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment