3737import net .wurstclient .util .RegionPos ;
3838import net .wurstclient .util .RenderUtils ;
3939import net .wurstclient .util .RotationUtils ;
40+ import net .wurstclient .util .ShaderUtils ; // ### ADDED ###
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 ; // ### ADDED ###
75+ private int buildGeneration ; // ### ADDED ###
76+ private int currentBuildGeneration ; // ### ADDED ###
7377
7478 private EasyVertexBuffer vertexBuffer ;
7579 private RegionPos bufferRegion ;
@@ -92,9 +96,17 @@ protected void onEnable()
9296 notify = true ;
9397
9498 forkJoinPool = new ForkJoinPool ();
99+ shaderSafeMode = ShaderUtils .refreshShadersActive (); // ### ADDED ###
100+ buildGeneration = 0 ; // ### ADDED ###
101+ currentBuildGeneration = 0 ; // ### ADDED ###
95102
96103 bufferUpToDate = false ;
97-
104+ if (shaderSafeMode ) // ### ADDED ###
105+ ChatUtils
106+ .message ("Shaders detected - using safe mode for CaveFinder." ); // ###
107+ // ADDED
108+ // ###
109+
98110 EVENTS .add (UpdateListener .class , this );
99111 EVENTS .add (PacketInputListener .class , coordinator );
100112 EVENTS .add (RenderListener .class , this );
@@ -120,6 +132,25 @@ protected void onDisable()
120132 @ Override
121133 public void onUpdate ()
122134 {
135+ boolean currentShaderSafeMode = ShaderUtils .refreshShadersActive (); // ###
136+ // ADDED
137+ // ###
138+ if (currentShaderSafeMode != shaderSafeMode ) // ### ADDED ###
139+ {
140+ shaderSafeMode = currentShaderSafeMode ; // ### MODIFIED ###
141+ stopBuildingBuffer (); // ### MODIFIED ###
142+ if (shaderSafeMode ) // ### ADDED ###
143+ ChatUtils .message (
144+ "Shaders detected - using safe mode for CaveFinder." ); // ###
145+ // ADDED
146+ // ###
147+ else
148+ ChatUtils .message (
149+ "Shaders disabled - returning CaveFinder to normal mode." ); // ###
150+ // ADDED
151+ // ###
152+ }
153+
123154 boolean searchersChanged = coordinator .update ();
124155
125156 if (searchersChanged )
@@ -136,6 +167,12 @@ public void onUpdate()
136167 notify = true ;
137168 }
138169
170+ if (shaderSafeMode ) // ### ADDED ###
171+ {
172+ buildBufferSafeMode (); // ### ADDED ###
173+ return ; // ### ADDED ###
174+ }
175+
139176 // build the buffer
140177
141178 if (getMatchingBlocksTask == null )
@@ -176,6 +213,7 @@ public void onRender(PoseStack matrixStack, float partialTicks)
176213
177214 private void stopBuildingBuffer ()
178215 {
216+ buildGeneration ++; // ### ADDED ###
179217 if (getMatchingBlocksTask != null )
180218 {
181219 getMatchingBlocksTask .cancel (true );
@@ -196,6 +234,7 @@ private void startGetMatchingBlocksTask()
196234 BlockPos eyesPos = BlockPos .containing (RotationUtils .getEyesPos ());
197235 Comparator <BlockPos > comparator =
198236 Comparator .comparingInt (pos -> eyesPos .distManhattan (pos ));
237+ currentBuildGeneration = buildGeneration ; // ### ADDED ###
199238
200239 getMatchingBlocksTask = forkJoinPool .submit (() -> coordinator
201240 .getMatches ().parallel ().map (ChunkSearcher .Result ::pos )
@@ -205,6 +244,12 @@ private void startGetMatchingBlocksTask()
205244
206245 private void startCompileVerticesTask ()
207246 {
247+ if (currentBuildGeneration != buildGeneration ) // ### ADDED ###
248+ {
249+ stopBuildingBuffer (); // ### ADDED ###
250+ return ; // ### ADDED ###
251+ }
252+
208253 HashSet <BlockPos > matchingBlocks = getMatchingBlocksTask .join ();
209254
210255 if (matchingBlocks .size () < limit .getValueLog ())
@@ -221,9 +266,56 @@ else if(notify)
221266 .submit (() -> BlockVertexCompiler .compile (matchingBlocks ));
222267 }
223268
269+ private void buildBufferSafeMode () // ### ADDED ###
270+ {
271+ if (bufferUpToDate ) // ### ADDED ###
272+ return ; // ### ADDED ###
273+
274+ if (getMatchingBlocksTask != null || compileVerticesTask != null ) // ###
275+ // ADDED
276+ // ###
277+ stopBuildingBuffer (); // ### ADDED ###
278+
279+ BlockPos eyesPos = BlockPos .containing (RotationUtils .getEyesPos ());
280+ Comparator <BlockPos > comparator =
281+ Comparator .comparingInt (pos -> eyesPos .distManhattan (pos ));
282+ java .util .ArrayList <ChunkSearcher .Result > matches =
283+ coordinator .getMatches ().collect (
284+ java .util .stream .Collectors .toCollection (ArrayList ::new ));
285+ HashSet <BlockPos > matchingBlocks =
286+ matches .stream ().map (ChunkSearcher .Result ::pos ).sorted (comparator )
287+ .limit (limit .getValueLog ())
288+ .collect (Collectors .toCollection (HashSet ::new ));
289+
290+ if (matchingBlocks .size () < limit .getValueLog ())
291+ notify = true ;
292+ else if (notify )
293+ {
294+ ChatUtils .warning ("CaveFinder found \u00a7 lA LOT\u00a7 r of blocks!"
295+ + " To prevent lag, it will only show the closest \u00a7 6"
296+ + limit .getValueString () + "\u00a7 r results." );
297+ notify = false ;
298+ }
299+
300+ ArrayList <int []> vertices = BlockVertexCompiler .compile (matchingBlocks );
301+ setBufferFromVertices (vertices );
302+ }
303+
224304 private void setBufferFromTask ()
225305 {
306+ if (currentBuildGeneration != buildGeneration ) // ### ADDED ###
307+ {
308+ stopBuildingBuffer (); // ### ADDED ###
309+ return ; // ### ADDED ###
310+ }
311+
226312 ArrayList <int []> vertices = compileVerticesTask .join ();
313+ setBufferFromVertices (vertices ); // ### MODIFIED ###
314+ }
315+
316+ private void setBufferFromVertices (ArrayList <int []> vertices ) // ### ADDED
317+ // ###
318+ {
227319 RegionPos region = RenderUtils .getCameraRegion ();
228320
229321 if (vertexBuffer != null )
0 commit comments