From 48f12a164059726244d04b36c3bef91c90331060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Mon, 5 Aug 2024 20:50:03 +0200 Subject: [PATCH] Fix clicking on clock every X times --- .../java/wtf/beatrice/autosqueal/Main.java | 2 -- .../autosqueal/controls/CursorMover.java | 35 ++++++++++++++----- .../controls/SingleStepMovementTask.java | 24 ++++++++++++- .../beatrice/autosqueal/ui/MainWindow.java | 2 +- .../beatrice/autosqueal/util/RunnerUtil.java | 5 ++- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/main/java/wtf/beatrice/autosqueal/Main.java b/src/main/java/wtf/beatrice/autosqueal/Main.java index dc5598f..9614974 100644 --- a/src/main/java/wtf/beatrice/autosqueal/Main.java +++ b/src/main/java/wtf/beatrice/autosqueal/Main.java @@ -13,8 +13,6 @@ import java.util.Timer; public class Main { - private static final int LOOPS_BEFORE_CLICK = 5; - private static boolean running = true; private static final Logger LOGGER = LogManager.getLogger(Main.class); diff --git a/src/main/java/wtf/beatrice/autosqueal/controls/CursorMover.java b/src/main/java/wtf/beatrice/autosqueal/controls/CursorMover.java index 748f0b5..7043429 100644 --- a/src/main/java/wtf/beatrice/autosqueal/controls/CursorMover.java +++ b/src/main/java/wtf/beatrice/autosqueal/controls/CursorMover.java @@ -2,6 +2,7 @@ package wtf.beatrice.autosqueal.controls; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import wtf.beatrice.autosqueal.util.RunnerUtil; import java.awt.*; import java.security.SecureRandom; @@ -15,14 +16,12 @@ public class CursorMover extends TimerTask private static final Logger LOGGER = LogManager.getLogger(CursorMover.class); private final Random RANDOM; - private final int SCREEN_HEIGHT; - private final int SCREEN_WIDTH; + private static final int LOOPS_BEFORE_CLICK = 5; - public CursorMover(int screenWidth, int screenHeight) { + private int iteration = 0; + + public CursorMover() { RANDOM = new SecureRandom(); - - this.SCREEN_WIDTH = screenWidth; - this.SCREEN_HEIGHT = screenHeight; } @Override @@ -33,13 +32,31 @@ public class CursorMover extends TimerTask LOGGER.info("Starting coordinates: {}, {}", currentX, currentY); - int destX = RANDOM.nextInt(SCREEN_WIDTH); - int destY = RANDOM.nextInt(SCREEN_HEIGHT); + int destX; + int destY; + + SingleStepMovementTask singleStepMovementTask; + + if (iteration == LOOPS_BEFORE_CLICK) { + destX = RunnerUtil.SCREEN_WIDTH - 5; + destY = 5; + + singleStepMovementTask = new SingleStepMovementTask(destX, destY, true); + + iteration = 0; + } else { + destX = RANDOM.nextInt(RunnerUtil.SCREEN_WIDTH); + destY = RANDOM.nextInt(RunnerUtil.SCREEN_HEIGHT); + + singleStepMovementTask = new SingleStepMovementTask(destX, destY, false); + + iteration++; + } LOGGER.info("Destination coordinates: {}, {}", destX, destY); Timer timer = new Timer(); - SingleStepMovementTask singleStepMovementTask = new SingleStepMovementTask(destX, destY); timer.schedule(singleStepMovementTask, 0L, 2L); + } } diff --git a/src/main/java/wtf/beatrice/autosqueal/controls/SingleStepMovementTask.java b/src/main/java/wtf/beatrice/autosqueal/controls/SingleStepMovementTask.java index e643f28..6f06db5 100644 --- a/src/main/java/wtf/beatrice/autosqueal/controls/SingleStepMovementTask.java +++ b/src/main/java/wtf/beatrice/autosqueal/controls/SingleStepMovementTask.java @@ -4,6 +4,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.awt.*; +import java.awt.event.InputEvent; import java.util.TimerTask; public class SingleStepMovementTask extends TimerTask { @@ -20,9 +21,12 @@ public class SingleStepMovementTask extends TimerTask { float stepX = 1; float stepY = 1; boolean isRunning = true; + boolean click; - public SingleStepMovementTask(int destinationX, int destinationY) { + public SingleStepMovementTask(int destinationX, int destinationY, boolean click) { + + this.click = click; currentX = MouseInfo.getPointerInfo().getLocation().x; currentY = MouseInfo.getPointerInfo().getLocation().y; @@ -67,6 +71,24 @@ public class SingleStepMovementTask extends TimerTask { } if(destX == Math.round(currentX) || destY == Math.round(currentY)) { + + if (click) { + try { + Thread.sleep(500); + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); + Thread.sleep(200); + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + Thread.sleep(500); + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); + Thread.sleep(200); + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + Thread.sleep(200); + } catch (InterruptedException e) { + LOGGER.error(e); + return; + } + } + LOGGER.info("Reached destination, stopping mover timer"); LOGGER.info("Dest: [{}, {}], Curr: [{}, {}]", destX, destY, currentX, currentY); isRunning = false; diff --git a/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java b/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java index 02c4bbf..02a43fc 100644 --- a/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java +++ b/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java @@ -67,7 +67,7 @@ public class MainWindow CursorMoveListener cursorMoveListener = new CursorMoveListener(); TIMER_RUNNER.schedule(cursorMoveListener, 0L, 1000L); - cursorMover = new CursorMover(RunnerUtil.SCREEN_WIDTH, RunnerUtil.SCREEN_HEIGHT); + cursorMover = new CursorMover(); TIMER_RUNNER.schedule(cursorMover, 1000L, RunnerUtil.SECONDS_BETWEEN_MOVES * 1000L); button.setLabel("Stop"); diff --git a/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java b/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java index 74090a5..08ad0b0 100644 --- a/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java +++ b/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java @@ -4,10 +4,9 @@ import java.awt.*; public class RunnerUtil { - public static final int SECONDS_BETWEEN_MOVES = 5; + public static final int SECONDS_BETWEEN_MOVES = 10; public static final int SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; public static final int SCREEN_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; - - + }