Last active
February 7, 2024 06:38
-
-
Save Hypro999/822ffbd3a1fea40341ae503a5c98b504 to your computer and use it in GitHub Desktop.
Autoclicker for Minecraft Java edition mob farms (Windows).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <windows.h> | |
#pragma comment(lib, "user32.lib") | |
// How long to wait between clicks (in ms). | |
#define TIME_PERIOD 30000 | |
// How long to wait before doing a quick follow-up click within the same time | |
// period (in ms). | |
// Motivation: In Minecraft mob farms (on Java edition, when using the sword), | |
// after the first sweep, a second follow-up sweep is often needed to actually | |
// clear out a batch of mobs. | |
#define MINI_TIME_PERIOD 2000 | |
/* | |
* Emulate a mouse click whereever the cursor is currently located. | |
* | |
* @returns: Returns TRUE if there was an error, FALSE on success. | |
*/ | |
bool clickInPos() { | |
// First, find where the cursor is currently located. | |
POINT p; | |
bool success = GetCursorPos(&p); | |
if (!success) { | |
fprintf(stderr, "Failed to get cursor position. (%lu).\n", GetLastError()); | |
return TRUE; | |
} | |
fprintf(stdout, "Current cursor position: (%ld, %ld).\n", p.x, p.y); | |
// Now, click the mouse at that same position. | |
INPUT inp; | |
inp.type = INPUT_MOUSE; | |
inp.mi.dx = p.x; | |
inp.mi.dy = p.y; | |
inp.mi.dwFlags = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP; | |
int numSuccess = SendInput(1, &inp, sizeof(inp)); | |
if (numSuccess != 1) { | |
fprintf(stderr, "Failed to click. (%lu).\n", GetLastError()); | |
return TRUE; | |
} | |
fprintf(stdout, "Clicked at position: (%ld, %ld).\n", inp.mi.dx, inp.mi.dy); | |
return FALSE; | |
} | |
int main() { | |
bool err = FALSE; | |
while (TRUE) { | |
Sleep(TIME_PERIOD); | |
err = clickInPos(); | |
if (err) { | |
return EXIT_FAILURE; | |
} | |
Sleep(MINI_TIME_PERIOD); | |
err = clickInPos(); | |
if (err) { | |
return EXIT_FAILURE; | |
} | |
} | |
return EXIT_SUCCESS; | |
} |
If you can't compile this yourself, then here's a link to the pre-built executable.
Demo of the autoclicker in action at an AFK Zombie spawner farm:
click.mp4
If you need a mouse jiggler instead of a mouse clicker:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <windows.h>
#pragma comment(lib, "user32.lib")
#define NUM_EVENTS 1
#define SLEEP_TIME_MS 5000
#define MOUSE_MOVE_DISTANCE_PX 50
int main() {
INPUT inp;
inp.type = INPUT_MOUSE;
inp.mi.dx = MOUSE_MOVE_DISTANCE_PX;
inp.mi.dy = 0;
inp.mi.dwFlags = MOUSEEVENTF_MOVE;
while (true) {
int numSuccess = SendInput(NUM_EVENTS, &inp, sizeof(inp));
if (numSuccess != NUM_EVENTS) {
fprintf(stderr, "Failed to move mouse. (%lu).\n", GetLastError());
exit(EXIT_FAILURE);
}
inp.mi.dx *= -1;
Sleep(SLEEP_TIME_MS);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a simpler alternative for people who have Python installed and don't mind installing pyautogui: