Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.runelite.client.config.ConfigProfile;
import net.runelite.client.plugins.microbot.Microbot;
import net.runelite.client.plugins.microbot.Script;
import net.runelite.client.plugins.microbot.breakhandler.BreakHandlerScript;
import net.runelite.client.plugins.microbot.util.discord.Rs2Discord;
import net.runelite.client.plugins.microbot.util.math.Rs2Random;
import net.runelite.client.plugins.microbot.util.player.Rs2Player;
Expand All @@ -30,6 +31,7 @@
@Singleton
@Slf4j
public class BreakHandlerV2Script extends Script {
private static final long LOCK_DEFERRAL_LOG_INTERVAL_MS = 30_000L;

// Instance tracking for debugging
private static int instanceCounter = 0;
Expand Down Expand Up @@ -72,6 +74,7 @@ public BreakHandlerV2Script() {
// Break duration in milliseconds
private long currentBreakDuration = 0;
private boolean logoutBreakActive = false;
private long lastLockDeferralLogAt = 0L;
private boolean longBreakDue = false;
private boolean megaBreakDue = false;
private volatile boolean currentBreakIsLong = false;
Expand Down Expand Up @@ -236,6 +239,16 @@ private void handleWaitingForBreak() {
* Initiates break based on configuration
*/
private void handleBreakRequested() {
if (shouldDeferRequestedBreak(breakEndTime)) {
long now = System.currentTimeMillis();
if (now - lastLockDeferralLogAt >= LOCK_DEFERRAL_LOG_INTERVAL_MS) {
log.info("[BreakHandlerV2] Break deferred while a plugin lock is active");
lastLockDeferralLogAt = now;
}
return;
}

lastLockDeferralLogAt = 0L;
stopConfiguredPluginIfNeeded();

// If breakEndTime is already set, we're in a no-logout break waiting for it to end
Expand Down Expand Up @@ -267,6 +280,14 @@ private void handleBreakRequested() {
}
}

/**
* Defers only a new break request. A non-null end time represents an active
* no-logout break whose completion must continue to be processed.
*/
static boolean shouldDeferRequestedBreak(Instant activeBreakEndTime) {
return activeBreakEndTime == null && BreakHandlerScript.isLockState();
}

/**
* Handle INITIATING_BREAK state
* Performs safety checks before logout with backoff retry
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.runelite.client.plugins.microbot.breakhandler.breakhandlerv2;

import net.runelite.client.plugins.microbot.breakhandler.BreakHandlerScript;
import org.junit.After;
import org.junit.Test;

import java.time.Instant;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class BreakHandlerV2DeferralTest {
@After
public void clearPluginLock() {
BreakHandlerScript.setLockState(false);
}

@Test
public void defersRequestedBreakWhilePluginLockIsHeld() {
BreakHandlerScript.setLockState(true);

assertTrue(BreakHandlerV2Script.shouldDeferRequestedBreak(null));
}

@Test
public void doesNotDeferNewBreakWhenPluginLockIsReleased() {
BreakHandlerScript.setLockState(false);

assertFalse(BreakHandlerV2Script.shouldDeferRequestedBreak(null));
}

@Test
public void doesNotDeferActiveNoLogoutBreakWhenPluginLockIsHeld() {
BreakHandlerScript.setLockState(true);

assertFalse(BreakHandlerV2Script.shouldDeferRequestedBreak(Instant.now()));
}
}
Loading