Automatically switch Touch Bar to function keys for Minecraft only
Open Terminal and run the following commands from ravbug.com/tutorials/mc-touchbar:
defaults write com.apple.touchbar.agent PresentationModePerApp -dict-add net.java.openjdk.cmd functionKeys
sudo killall TouchBarServer NowPlayingTouchUI ControlStrip

You will probably need to enter your password.
The first line edits the necessary config file manually, bypassing System Preferences, and the second causes the Touch Bar to reload its configuration.
After doing this, editing the function keys list using System Preferences will (a) fail to show java in the list, even though it’s there, and (b) delete java from the list if you make any changes. In that case you can repeat the above process to re-add it.

Workaround 1: Go to System Preferences → Keyboard → Keyboard and change the “Touch Bar shows” value manually when switching to and from the game.
Workaround 2: Go to System Preferences → Keyboard → Keyboard and change “Touch Bar shows” to always show the function keys. Since you’ll lose shortcuts to adjust brightness and volume, you probably want to then click on the control center icon in the menu bar and drag commonly used controls into the menu bar.