-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathClientThread.java
More file actions
113 lines (101 loc) · 2.81 KB
/
Copy pathClientThread.java
File metadata and controls
113 lines (101 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package net.runelite.client.callback;
import com.google.inject.Inject;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.BooleanSupplier;
import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
@Singleton
@Slf4j
public class ClientThread {
private final ConcurrentLinkedQueue<BooleanSupplier> invokes = new ConcurrentLinkedQueue<>();
private final ConcurrentLinkedQueue<BooleanSupplier> invokesAtTickEnd = new ConcurrentLinkedQueue<>();
@Inject
private Client client;
/**
* Immediately invokes a runnable onto the client thread
*
* @param r a runnable
*/
public void invoke(Runnable r) {
invoke(() ->
{
r.run();
return true;
});
}
/**
* Will run r on the game thread, at a unspecified point in the future.
* If r returns false, r will be ran again, at a later point
*
* @param r the conditional to validate
*/
public void invoke(BooleanSupplier r) {
if (client.isClientThread()) {
if (!r.getAsBoolean()) {
invokes.add(r);
}
return;
}
invokeLater(r);
}
/**
* Will run r on the game thread after this method returns
* If r returns false, r will be ran again, at a later point
*
* @param r the runnable to invoke on the client thread
*/
public void invokeLater(Runnable r) {
invokeLater(() ->
{
r.run();
return true;
});
}
/**
* Adds a conditional to validate onto the client thread queue
*
* @param r the conditional to add to the client thread queue
*/
public void invokeLater(BooleanSupplier r) {
invokes.add(r);
}
public void invokeAtTickEnd(Runnable r)
{
invokesAtTickEnd.add(() ->
{
r.run();
return true;
});
}
/**
* Invokes queued actions on the client thread
*/
void invoke() {
invokeList(invokes);
}
void invokeTickEnd() {
invokeList(invokesAtTickEnd);
}
private void invokeList(ConcurrentLinkedQueue<BooleanSupplier> invokes) {
assert client.isClientThread();
Iterator<BooleanSupplier> ir = invokes.iterator();
while (ir.hasNext()) {
BooleanSupplier r = ir.next();
boolean remove = true;
try {
remove = r.getAsBoolean();
} catch (ThreadDeath d) {
throw d;
} catch (Throwable e) {
log.error("Exception in invoke", e);
}
if (remove) {
ir.remove();
} else {
log.trace("Deferring task {}", r);
}
}
}
}