After visiting with my best friends for a couple of weeks while taking a break from employment, I decided to get back in to minecraft and play with my bestie who lives 5 hours drive away, as a way to keep in touch and hang/chill. I Spun up a decent Virtual Machine on my proxmox server and threw together a minecraft server for us to play on. (owned multicraft on Ubuntu 18.04.2 – 4 cores @4Ghz – 12 gigs of Ram)
Starting on a brand new world is always fun, and eventually we got some resources but it’s slow going, so I wanted to spend some off my background process gaining me some XP & Loot while I was not playing and busy doing other things. Coming from Win10 around 8 months ago I had used Autohotkey a lot but while there is a port it doesn’t function. I’m currently using Kubuntu 19.04. (I had PopOS 19.04 but 3x it seriously died or became unstable, so tried Kubuntu and been very happy since)
I researched, and using xdotool it’s possible. Dihmz on Reddit had a simple script to pull the window ID out and set right mouse button to always held down in the minecraft window. Thats great, but I found on interruption it stopped but the process continued, so I wrote a mouse button up (stop) script. That worked great, but 2 scripts to maintain.
Now, I’ve integrated it all as a single toggle script. only one script to maintain.
The AFK Bash Script
#!/bin/bash
# +++ AFK fishing - Minecraft +++ (Right Mouse Button) V-1.2
# +++ Version 1.2 - sanity check is minecraft running?
# +++ Version 1.1 - added instructions
# +++ Version 1.0 - Adapted script by Dihmz, toggle added with output
# -- Instructions:
# -- Install 'xdotool': just use your distribution's package manager to install it.
# -$ sudo apt install xdotool
# -$ mkdir ~/bin (make bin dir)
# save script ~/bin/AFKtoggle (nano,vim,etc)
# -$ cd ~/bin
# -$ chmod +x AFKtoggle
# -$ nano ~/.bashrc (edit bash profile to add /bin to global path)
# add at the end: export PATH="/home/YOURUSERNAME/bin:$PATH"
# -$ source ~/.bashrc (reloads bash profile)
# -- Disable autopause:
# open the options.txt file in the Minecraft installation folder (it's usually /home/USER/.minecraft/options.txt)
# look for the line with pauseOnLostFocus and replace true with false
# -- Launch Minecraft
# go in front of your fish farm, equip your fishing rod and aim carefully for the sweet spot
# Alt-Tab out of Minecraft without pausing the game
# -- open terminal
# -$ AFKtoggle <-- turns on/off
#Initialize Variables
mcversion="Minecraft 1.14.4"
#get window id
windowinfo="0"
WindowID="1"
IDreal="2"
# set colours for terminal output
RED='\033[0;31m'
NC='\033[0m' # No Color
BRED='\033[0;91m'
GREEN='\033[0;92m'
YELLOW='\033[0;93m'
# ! -- Check the window title
# ie: Minecraft 1.14.4 [set "mcversion" above - aka: window title]
read windowinfo <<< $( echo $(xwininfo -name "$mcversion" ))
# sanity check is minecraft running?
#[[ ! -z "$windowinfo" ]] && echo "Not empty" || echo "Empty"
if [[ -z "$windowinfo" ]];
then
# Not running, warn & exit
clear
echo -e "\n${YELLOW}Start ${mcversion} First!${NC}\n"
else
# else continue grabbing the window ID in hex
read WindowID <<< $( echo $windowinfo | awk -F ":" '{print $3;}' )
read IDreal <<< $( echo $WindowID | awk -F " " '{print $1;}' )
# Great, now we have the windowID in hex
#The Final command to run the code
TOGGLE=$HOME/.AFKtoggle
if [ ! -e $TOGGLE ]; then
# create file as flag as state (TRUE)
touch $TOGGLE
# Start AFK Mode
$( xdotool mousedown --window $IDreal 3 )
# display "AFK engaged"
clear
echo -e "\n${YELLOW}AFK${NC} (Right click)${GREEN} Engaged${NC}\n "
else
# remove file as flag as state (FALSE)
rm $TOGGLE
# Stop AFK Mode
$( xdotool mouseup --window $IDreal 3 )
# display "AFK mode stopped."
clear
echo -e "\n${YELLOW}AFK${NC} (Right click)${RED} Disabled${NC}\n "
fi
fi