-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathScanController.java
More file actions
122 lines (108 loc) · 3.66 KB
/
ScanController.java
File metadata and controls
122 lines (108 loc) · 3.66 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
114
115
116
117
118
119
120
121
122
package pro.mikey.fabric.xray;
import net.minecraft.core.BlockPos;
import net.minecraft.core.SectionPos;
import net.minecraft.world.level.ChunkPos;
import pro.mikey.fabric.xray.render.RenderOutlines;
import pro.mikey.fabric.xray.storage.SettingsStore;
import pro.mikey.fabric.xray.tasks.*;
import java.util.concurrent.*;
public class ScanController {
static ThreadPoolExecutor executor = new ThreadPoolExecutor(
StateSettings.getRadius(), StateSettings.getRadius(),
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>()
);
/**
* This function sets the ThreadPool up properly.
* Technically the threads set their own Priority to one anyways, so a normal Threapool would be sufficient
*/
public static void setup(){
executor.setThreadFactory(r -> {
Thread thread = new Thread(r);
thread.setPriority(Thread.MIN_PRIORITY);
return thread;
});
}
private static void submitTask(Runnable worker){
if(!executor.isShutdown()){
executor.execute(worker);
}
}
/**
* Just a security measure to force-close the ThreadPool to make sure it doesn't linger in the background
*/
public static void closeGame() {
executor.shutdownNow();
}
/**
* This function rebuilds the ChunkCache completly and reloads all Chunks
* without flashing the already rendered Chunks instantly away
*/
public static void reBuildCache(boolean force) {
class RebuildThread extends Thread {
@Override
public synchronized void run() {
RenderOutlines.clearChunks(force);
executor.shutdownNow();
while (!executor.isTerminated()) {
try {
currentThread().wait(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
executor = new ThreadPoolExecutor(
StateSettings.getRadius(), StateSettings.getRadius(),
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>()
);
setup();
Runnable worker = new ReBuildCache();
executor.execute(worker);
}
}
Thread r = new RebuildThread();
r.start();
}
/**
* This function clears the Cache on a seperate Thread
*/
public static void clearCache(boolean force) {
class ClearThread extends Thread {
@Override
public synchronized void run() {
RenderOutlines.clearChunks(force);
}
}
Thread r = new ClearThread();
r.start();
}
public static void reBuildCache() {
reBuildCache(false);
}
/**
* This function updates a Chunk based on Pos
*/
public static void updateChunk(ChunkPos pos) {
if (SettingsStore.getInstance().get().isActive()) {
Runnable worker = new UpdateChunkTask(pos);
submitTask(worker);
}
}
/**
* This function updates a Chunk based on Pos
*/
public static void updateChunk(BlockPos pos){
int chunkx = SectionPos.blockToSectionCoord(pos.getX());
int chunkz = SectionPos.blockToSectionCoord(pos.getZ());
ChunkPos chunkPos = new ChunkPos(chunkx,chunkz);
updateChunk(chunkPos);
}
/**
* This function removes a chunk from the Rendering
*/
public static void removeChunk(ChunkPos pos){
Runnable worker = new RemoveChunkTask(pos);
submitTask(worker);
}
}