Possibly doable with debug.sethook: https://forum.minetest.net/viewtopic.php?p=155943#p155943
debug.sethook