Skip to content

Commit

Permalink
Skip initial call
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 18, 2024
1 parent 11034ce commit 338b1dc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public List<WorldAction> findRouteSync(NodeState from, boolean requiresRepositio
bestNode.node().blockPosition().formatXYZ(),
bestNode.totalRouteScore() - bestNode.sourceCost()
);
}, 1, TimeUnit.SECONDS);
}, 1, TimeUnit.SECONDS, true);
var cleaner = new CallLimiter(() -> {
if (Boolean.getBoolean("sf.pathfinding-no-prune")) {
return;
Expand All @@ -132,7 +132,7 @@ public List<WorldAction> findRouteSync(NodeState from, boolean requiresRepositio
openSet.clear();
openSet.enqueue(bestNode);
routeIndex.clear();
}, 5, TimeUnit.SECONDS);
}, 5, TimeUnit.SECONDS, true);
while (!openSet.isEmpty()) {
if (Thread.currentThread().isInterrupted()) {
stopwatch.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@
public class CallLimiter implements Runnable {
private final Runnable c;
private final long interval;
private final boolean skipInitial;
private volatile long lastCalled;

public CallLimiter(Runnable c, long interval, TimeUnit unit) {
public CallLimiter(Runnable c, long interval, TimeUnit unit, boolean skipInitial) {
this.c = c;
this.interval = unit.toMillis(interval);
this.skipInitial = skipInitial;
}

@Override
public void run() {
if (skipInitial && lastCalled == 0) {
lastCalled = System.currentTimeMillis();
return;
}

if (lastCalled + interval < System.currentTimeMillis()) {
lastCalled = System.currentTimeMillis();
c.run();
Expand Down

0 comments on commit 338b1dc

Please sign in to comment.