Minecraft Script

Post gaming related scripts
User avatar
Datapoint
Posts: 296
Joined: 18 Mar 2018, 17:06

Minecraft Script

Post by Datapoint » 12 Oct 2023, 23:35

These are some simple macros that I use in Minecraft, mainly to keep my hands from cramping up or getting a repetitive stress injury.
Many/most of the hotkeys are "toggle" hotkeys. For toggle hotkeys, press the hotkey once to enable and press it again to disable.

Hotkeys
List of hotkeys and their functions. See the script for more info.
General purpose hotkeys:
  • Ctrl + Esc - Quit the script.
  • Alt + Esc - Cancel any held down keys and reload.
  • F10 - Auto attack. (Toggle)
  • F11 - Hold down Use (RButton). (Toggle)
  • F12 - Hold down Attack (LButton). (Toggle)
  • Up - Pitch the camera up by a small amount.
  • Down - Pitch the camera down by a small amount.
  • Left - Rotate the camera left by a small amount.
  • Right - Rotate the camera right by a small amount.
  • Alt + C - Hold down Shift to crouch. (Toggle, or just press Shift to cancel)
Macros for more specific/temporary tasks:
Note: Some of the macros assume that you will be crouching.
  • F6 - Hold down RButton, S, and A for bridging out into the void. (Mainly for Skyblock.) (Toggle)
  • F7 - Hold down RButton, S, and D. (Same as F6 hotkey, but move right instead of left.) (Toggle)
  • F8 - Similar to F6 and F7 hotkeys, but move straight back holding RButton and S. Also useful for general block placement, tilling dirt, etc. (Toggle)
  • F9 - Plant crops, bonemeal, harvest, repeat. (Toggle)
  • Delete - Hold down RButton and spin in a circle. (Useful for piglin trading.) (Toggle)
  • Ins - Crafting/villager trading macro. Move the mouse right, click, move left, click, repeat. (Hold down the hotkey) This is probably one of the most useful macros in this script, but it will require some cutomization for each specific task. Change the variables dx and dy to adjust how much the mouse moves up/down and left/right.
  • Ctrl + F6 - Hold down Lbutton, W and A. (Toggle)
  • Ctrl + F7 - Hold down Lbutton, W and D. (Toggle)
  • Alt + F8 - Hold down Use (Rbutton) and move forward (W). (Toggle)
Up, Down, Left, Right, and Delete are used as hotkeys which prevents their normal function. Hold Ctrl while pressing the key to use the normal function.

Code: Select all

; Minecraft script
#Requires AutoHotkey v2.0
#SingleInstance Force

; Customize settings here.
PeriodicAttack := 620 ; Speed of attack for F10 hotkey below.
PeriodicHoldAttack := 100 ; Time to hold attack for F10 hotkey below.
BonemealDelay := 90 ; Delay between actions for F9 hotkey below.

; Ctrl + Esc to quit.
^Esc::
+^Esc:: {
	ExitApp
}

; Alt + Esc to cancel any held down keys and reload the script. Useful if you hit the
; wrong hotkeys and can't turn them off.
!Esc::
+!Esc:: {
	Send "{LButton Up}{RButton Up}{w Up}{a Up}{s Up}{d  Up}{Shift Up}"
	Reload
}

#HotIf WinActive("Minecraft ahk_class GLFW30") || WinActive("Minecraft ahk_class ApplicationFrameWindow")

; Pitch camera up by a small amount.
$Up::
$+Up::MouseEvent(0x0000, -0x0001)

; Pitch camera down by a small amount.
$Down::
$+Down::MouseEvent(0x0000, 0x0001)

; Rotate camera left by a small amount.
$Left::
$+Left::MouseEvent(-0x0001, 0x0000)

; Rotate camera right by a small amount.
$Right::
$+Right::MouseEvent(0x0001, 0x0000)

; The arrow keys and Delete are used as hotkeys which prevents their normal function. Hold Ctrl while pressing
; the key to use the normal function. A usage example would be naming something at an anvil.
^Up::Send("{Up}")
^Down::Send("{Down}")
^Left::Send("{Left}")
^Right::Send("{Right}")
^Delete::Send("{Delete}")

MouseEvent(dx, dy) {
	static MOUSEEVENTF_MOVE := 0x0001

	DllCall("mouse_event", "UInt", MOUSEEVENTF_MOVE, "Int", dx, "Int", dy)
}

; Hold down Shift to crouch. Press again to cancel, or just press Shift.
!c::
+!c:: {
	global CrouchToggle := IsSet(CrouchToggle) ? !CrouchToggle : 1

	CrouchToggle ? Send("{Shift Down}") : Send("{Shift Up}")
}

~$Shift Up:: {
	global CrouchToggle := 0
}

; F6 holds down RButton, S, and A for bridging out into the void (Skyblock). Press again to disable.
; Crouch (you can enable crouch "toggle" ON in Minecraft options or use the Alt + C hotkey in this script),
;  and face toward one of the following.
; (45.0, 75.0), (-45.0, 75.0), (135.0, 75.0), or (-135.0, 75.0) - Press F3 to show the direction you are facing.
; Facing towards 45.0, -45.0, 135.0, or -135.0 should be exact because they control the direction you move.
; Up/down facing can be +/- ~0.5 and still work. For example, facing (45.0, 75.3) works fine.
F6::
+F6:: {
	static toggle := 0

	(toggle := !toggle) ?
	Send("{RButton Down}{s Down}{a Down}") :
	Send("{RButton Up}{s Up}{a Up}")
}

; F7 holds down RButton, S, and D for bridging out into the void. Press again to disable.
; Same as F6 hotkey, but this holds D instead of A to move right instead of left.
F7::
+F7:: {
	static toggle := 0

	(toggle := !toggle) ?
	Send("{RButton Down}{s Down}{d Down}") :
	Send("{RButton Up}{s Up}{d Up}")
}

; This holds down RButton and S. Useful for bridging out into the void, tilling dirt with a hoe, general building etc..
; For bridging out into the void it's the same as the F6 hotkey but face toward one of the following:
; (0.0, 79.0), (-0.0, 79.0), (90.0, 79.0), or (-90.0, 79.0)
F8::
+F8:: {
	static toggle := 0

	(toggle := !toggle) ?
	Send("{RButton Down}{s Down}") :
	Send("{RButton Up}{s Up}")
}

; Auto attack.
; To change the speed, change "PeriodicAttack" and "PeriodicHoldAttack" at the top of this script.
F10::
+F10:: {
	global AttackToggle := IsSet(AttackToggle) ? !AttackToggle : 1

	SetTimer SwingWeapon, AttackToggle ? -10 : 0
}

SwingWeapon() {
	global AttackToggle, PeriodicAttack, PeriodicHoldAttack

	Send "{LButton Down}"
	Sleep PeriodicHoldAttack
	Send "{LButton Up}"
	SetTimer SwingWeapon, AttackToggle ? -PeriodicAttack : 0
}

; Hold down Use (RButton).
F11::
+F11:: {
	static toggle := 0

	(toggle := !toggle) ? Send("{RButton Down}") : Send("{RButton Up}")
}

; Hold down Attack (LButton).
F12::
+F12:: {
	static toggle := 0

	(toggle := !toggle) ? Send("{LButton Down}") : Send("{LButton Up}")
}

;==============================================================================
; Below this are macros that were created for temporary and/or specific tasks.
; Yes, much of this can be replaced with redstone eventually. But until you get there...

; Plant crops, bonemeal, harvest, repeat.
; Carrots, potatoes, wheat, etc. must be in hotbar position 8, and bonemeal is in hotbar position 7.
; Stand over the crop then face towards the ground and enable the hotkey. Press the hotkey again to disable.
F9::
+F9:: {
	global BonemealToggle := IsSet(BonemealToggle) ? !BonemealToggle : 1

	SetTimer PlantAndBonemeal, BonemealToggle ? -10 : 0
}

PlantAndBonemeal() {
	global BonemealToggle, BonemealDelay

	Send "8"
	Sleep BonemealDelay
	Send "{RButton}"
	Sleep BonemealDelay
	Send "7"
	Sleep BonemealDelay
	Send "{RButton 4}"
	Sleep BonemealDelay
	Send "{LButton}"
	SetTimer PlantAndBonemeal, BonemealToggle ? -BonemealDelay : 0
}

; Hold RButton and spin in a circle. (Useful for piglin trading.)
; Stand in the center of a circle of piglins (in boats) and spin around to trade gold.
$Delete::
$+Delete:: {
	global RotateToggle := IsSet(RotateToggle) ? !RotateToggle : 1

	RotateToggle ? Send("{RButton Down}") : Send("{RButton Up}")
	SetTimer Rotate, RotateToggle ? -1 : 0
}

Rotate() {
	global RotateToggle

	MouseEvent(0x0012, 0x0000)
	SetTimer Rotate, RotateToggle ? -16 : 0
}

; This is a macro that was created for mass trading / crafting. It moves the mouse right, then clicks the mouse,
; then another press of the hotkey moves the mouse left and clicks the mouse.
; Just hold down the hotkey while trading and release when done.
; Note: If you want to use this you will probably need to adjust the static variables "dx" and "dy" for your
; needs (use trial and error). dx is horizontal distance and dy is vertical distance.
Ins::Trade(0x01DD, 0x0040) ; Cleric, rotten flesh - first click should be center of the emerald.
^Ins::Trade(0x02CB, 0x0000) ; Craft paper
;!Ins::Trade(0x01EE, 0x0000) ; Craft sand - a Skyblock map I play has added the ability to craft sand.

Trade(dx, dy) {
	static LeftRight := 0, Tick := 0

	; If it has been more than 4 seconds since this hotkey was fired then automatically set the direction
	; (LeftRight) to 1, else set it to the opposite.
	if (A_TickCount - Tick) > 4000
		LeftRight := 1
	else
		LeftRight := !LeftRight
	Tick := A_TickCount

	; The first time you press this hotkey it seems to move a different amount compared to the next time,
	; so just moving the mouse by 1 will get that out of the way. (there must be a better way)
	MouseEvent(0x0001, 0x0000)
	MouseEvent(-0x0001, 0x0000)

	; Move the mouse right/left and click
	MouseEvent((LeftRight ? 1 : -1) * dx, (LeftRight ? 1 : -1) * dy)
	Sleep 100
	Send "+{LButton}" ; Shift click to trade all items
}

; Hold down Attack (LButton), W and A.
^F6::
+^F6:: {
	static toggle := 0

	(toggle := !toggle) ?
	Send("{LButton Down}{w Down}{a Down}") :
	Send("{LButton Up}{w Up}{a Up}")
}

; Hold down Attack (LButton), W and D.
^F7::
+^F7:: {
	static toggle := 0

	(toggle := !toggle) ?
	Send("{LButton Down}{w Down}{d Down}") :
	Send("{LButton Up}{w Up}{d Up}")
}

; Click Use (Rbutton) and move forward (w).
; Example usage would be using a hoe to till dirt wile moving forward.
!F8::
+!F8:: {
	static toggle := 0

	(toggle := !toggle) ?
	Send("{RButton Down}{w Down}") :
	Send("{RButton Up}{w Up}")
}

#HotIf
Last edited by Datapoint on 15 Jan 2024, 21:03, edited 12 times in total.

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Minecraft Script

Post by DuckingQuack » 08 Nov 2023, 16:05

Sorry if this is a stupid question, but I was curious if this works for both Bedrock and Java and also which one you wrote this script for. Thanks!
Best of Luck,
The Duck

User avatar
Datapoint
Posts: 296
Joined: 18 Mar 2018, 17:06

Re: Minecraft Script

Post by Datapoint » 09 Nov 2023, 11:14

I play on Java edition, so it's only tested on that currently. If someone wants to test Bedrock edition that would be great. The script is not doing anything complicated, just sending keys and doing simple mouse movements, so it may work on Bedrock.

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Minecraft Script

Post by DuckingQuack » 10 Nov 2023, 17:25

I tried it in Bedrock and... as is... does not work, however, all I had to do to test it was comment out line 22: #HotIf WinActive("ahk_class GLFW30") and it worked somewhat as intended. (No crouch toggle)
I tested out a couple things and this is what I landed on to work with Bedrock: #HotIf WinActive("Minecraft").
That just leaves one issue that I'm aware of... Bedrock lacks a way toggle crouch when using mouse/keyboard. Simple fix is to add {Shift Down} and {Shift Up} to the Send commands as necessary, but then you would also need wildcards added to each hotkey and it starts getting messy.
I wonder how we could implement a toggle for crouch that doesn't upend the whole script to let Bedrock make use of this great script...

You made a wonderful script and I love it! Good Job!
Best of Luck,
The Duck

User avatar
Datapoint
Posts: 296
Joined: 18 Mar 2018, 17:06

Re: Minecraft Script

Post by Datapoint » 11 Nov 2023, 11:13

I'm glad you like the script. Thanks for the comments and testing.
DuckingQuack wrote:
10 Nov 2023, 17:25
I tested out a couple things and this is what I landed on to work with Bedrock: #HotIf WinActive("Minecraft").
I've added that to the script for now. However, just "Minecraft" is pretty general. For example, the script is enabled in my AHK editor because the name of the file has the word Minecraft in it. Using the class might work. If you run this on Bedrock, what does the MsgBox say? F1::MsgBox WinGetClass("A")
DuckingQuack wrote:
10 Nov 2023, 17:25
That just leaves one issue that I'm aware of... Bedrock lacks a way toggle crouch when using mouse/keyboard. Simple fix is to add {Shift Down} and {Shift Up} to the Send commands as necessary, but then you would also need wildcards added to each hotkey and it starts getting messy.
I wonder how we could implement a toggle for crouch that doesn't upend the whole script to let Bedrock make use of this great script...
I didn't reply to your post immediately because I was trying to think of the best way to do this. I've only tested it briefly, but it seems to be working now. I added a new hotkey, Alt+C. It will hold down Shift until you press Alt+C again, or just pressing Shift will cancel it too.
I added a wildcard to the other hotkeys too; that seems to be the easiest way to allow for Shift being held down.

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Minecraft Script

Post by DuckingQuack » 11 Nov 2023, 11:30

Datapoint wrote:
11 Nov 2023, 11:13
If you run this on Bedrock, what does the MsgBox say? F1::MsgBox WinGetClass("A")
ApplicationFrameWindow

Image
Attachments
image.png
image.png (834.96 KiB) Viewed 2433 times
Best of Luck,
The Duck

User avatar
Datapoint
Posts: 296
Joined: 18 Mar 2018, 17:06

Re: Minecraft Script

Post by Datapoint » 11 Nov 2023, 11:34

DuckingQuack wrote:
11 Nov 2023, 11:30
ApplicationFrameWindow
Added to the script. Thanks!

User avatar
Datapoint
Posts: 296
Joined: 18 Mar 2018, 17:06

Re: Minecraft Script

Post by Datapoint » 02 Dec 2023, 05:51

I removed the hotkey wildcards because it was impacting performance. The wildcards were added because Shift could be held down (due to the Alt + C hotkey). Instead, now I've added extra hotkeys that allow for shift being held.

The arrow keys and Delete are used as hotkeys which prevents their normal function. Now you can hold Ctrl while pressing the key to use the normal function. A usage example would be naming something at an anvil.

Added the function MouseEvent(dx, dy) to wrap the mouse_event DllCall.

Post Reply

Return to “Gaming”