Skip to content

Instantly share code, notes, and snippets.

@Hypro999
Last active February 7, 2024 06:38
Show Gist options
  • Save Hypro999/822ffbd3a1fea40341ae503a5c98b504 to your computer and use it in GitHub Desktop.
Save Hypro999/822ffbd3a1fea40341ae503a5c98b504 to your computer and use it in GitHub Desktop.
Autoclicker for Minecraft Java edition mob farms (Windows).
#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;
}
@Hypro999
Copy link
Author

Hypro999 commented Feb 7, 2024

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