3737import net .wurstclient .util .RegionPos ;
3838import net .wurstclient .util .RenderUtils ;
3939import net .wurstclient .util .RotationUtils ;
40+ import net .wurstclient .util .ShaderUtils ;
4041import net .wurstclient .util .chunk .ChunkSearcher ;
4142import net .wurstclient .util .chunk .ChunkSearcherCoordinator ;
4243
@@ -70,6 +71,9 @@ public final class CaveFinderHack extends Hack
7071 private ForkJoinPool forkJoinPool ;
7172 private ForkJoinTask <HashSet <BlockPos >> getMatchingBlocksTask ;
7273 private ForkJoinTask <ArrayList <int []>> compileVerticesTask ;
74+ private boolean shaderSafeMode ;
75+ private int buildGeneration ;
76+ private int currentBuildGeneration ;
7377
7478 private EasyVertexBuffer vertexBuffer ;
7579 private RegionPos bufferRegion ;
@@ -92,9 +96,14 @@ protected void onEnable()
9296 notify = true ;
9397
9498 forkJoinPool = new ForkJoinPool ();
99+ shaderSafeMode = ShaderUtils .refreshShadersActive ();
100+ buildGeneration = 0 ;
101+ currentBuildGeneration = 0 ;
95102
96103 bufferUpToDate = false ;
97-
104+ if (shaderSafeMode )
105+ ChatUtils
106+ .message ("Shaders detected - using safe mode for CaveFinder." );
98107 EVENTS .add (UpdateListener .class , this );
99108 EVENTS .add (PacketInputListener .class , coordinator );
100109 EVENTS .add (RenderListener .class , this );
@@ -120,6 +129,19 @@ protected void onDisable()
120129 @ Override
121130 public void onUpdate ()
122131 {
132+ boolean currentShaderSafeMode = ShaderUtils .refreshShadersActive ();
133+ if (currentShaderSafeMode != shaderSafeMode )
134+ {
135+ shaderSafeMode = currentShaderSafeMode ;
136+ stopBuildingBuffer ();
137+ if (shaderSafeMode )
138+ ChatUtils .message (
139+ "Shaders detected - using safe mode for CaveFinder." );
140+ else
141+ ChatUtils .message (
142+ "Shaders disabled - returning CaveFinder to normal mode." );
143+ }
144+
123145 boolean searchersChanged = coordinator .update ();
124146
125147 if (searchersChanged )
@@ -136,6 +158,12 @@ public void onUpdate()
136158 notify = true ;
137159 }
138160
161+ if (shaderSafeMode )
162+ {
163+ buildBufferSafeMode ();
164+ return ;
165+ }
166+
139167 // build the buffer
140168
141169 if (getMatchingBlocksTask == null )
@@ -176,6 +204,7 @@ public void onRender(PoseStack matrixStack, float partialTicks)
176204
177205 private void stopBuildingBuffer ()
178206 {
207+ buildGeneration ++;
179208 if (getMatchingBlocksTask != null )
180209 {
181210 getMatchingBlocksTask .cancel (true );
@@ -196,6 +225,7 @@ private void startGetMatchingBlocksTask()
196225 BlockPos eyesPos = BlockPos .containing (RotationUtils .getEyesPos ());
197226 Comparator <BlockPos > comparator =
198227 Comparator .comparingInt (pos -> eyesPos .distManhattan (pos ));
228+ currentBuildGeneration = buildGeneration ;
199229
200230 getMatchingBlocksTask = forkJoinPool .submit (() -> coordinator
201231 .getMatches ().parallel ().map (ChunkSearcher .Result ::pos )
@@ -205,6 +235,12 @@ private void startGetMatchingBlocksTask()
205235
206236 private void startCompileVerticesTask ()
207237 {
238+ if (currentBuildGeneration != buildGeneration )
239+ {
240+ stopBuildingBuffer ();
241+ return ;
242+ }
243+
208244 HashSet <BlockPos > matchingBlocks = getMatchingBlocksTask .join ();
209245
210246 if (matchingBlocks .size () < limit .getValueLog ())
@@ -221,9 +257,53 @@ else if(notify)
221257 .submit (() -> BlockVertexCompiler .compile (matchingBlocks ));
222258 }
223259
260+ private void buildBufferSafeMode ()
261+ {
262+ if (bufferUpToDate )
263+ return ;
264+
265+ if (getMatchingBlocksTask != null || compileVerticesTask != null )
266+ stopBuildingBuffer ();
267+
268+ BlockPos eyesPos = BlockPos .containing (RotationUtils .getEyesPos ());
269+ Comparator <BlockPos > comparator =
270+ Comparator .comparingInt (pos -> eyesPos .distManhattan (pos ));
271+ java .util .ArrayList <ChunkSearcher .Result > matches =
272+ coordinator .getMatches ().collect (
273+ java .util .stream .Collectors .toCollection (ArrayList ::new ));
274+ HashSet <BlockPos > matchingBlocks =
275+ matches .stream ().map (ChunkSearcher .Result ::pos ).sorted (comparator )
276+ .limit (limit .getValueLog ())
277+ .collect (Collectors .toCollection (HashSet ::new ));
278+
279+ if (matchingBlocks .size () < limit .getValueLog ())
280+ notify = true ;
281+ else if (notify )
282+ {
283+ ChatUtils .warning ("CaveFinder found \u00a7 lA LOT\u00a7 r of blocks!"
284+ + " To prevent lag, it will only show the closest \u00a7 6"
285+ + limit .getValueString () + "\u00a7 r results." );
286+ notify = false ;
287+ }
288+
289+ ArrayList <int []> vertices = BlockVertexCompiler .compile (matchingBlocks );
290+ setBufferFromVertices (vertices );
291+ }
292+
224293 private void setBufferFromTask ()
225294 {
295+ if (currentBuildGeneration != buildGeneration )
296+ {
297+ stopBuildingBuffer ();
298+ return ;
299+ }
300+
226301 ArrayList <int []> vertices = compileVerticesTask .join ();
302+ setBufferFromVertices (vertices );
303+ }
304+
305+ private void setBufferFromVertices (ArrayList <int []> vertices )
306+ {
227307 RegionPos region = RenderUtils .getCameraRegion ();
228308
229309 if (vertexBuffer != null )
@@ -240,3 +320,6 @@ private void setBufferFromTask()
240320 bufferRegion = region ;
241321 }
242322}
323+
324+
325+
0 commit comments