Skip to content

Commit

Permalink
Merge branch 'master' into pr/DrMaxNix/835
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Sep 9, 2023
2 parents 01b68e0 + 9966b91 commit d56fb44
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 76 deletions.
62 changes: 25 additions & 37 deletions src/main/java/net/wurstclient/hacks/CaveFinderHack.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
Expand Down Expand Up @@ -84,9 +83,9 @@ public final class CaveFinderHack extends Hack
private final HashMap<ChunkPos, ChunkSearcher> searchers = new HashMap<>();
private final Set<ChunkPos> chunksToUpdate =
Collections.synchronizedSet(new HashSet<>());
private ExecutorService pool1;
private ExecutorService threadPool;

private ForkJoinPool pool2;
private ForkJoinPool forkJoinPool;
private ForkJoinTask<HashSet<BlockPos>> getMatchingBlocksTask;
private ForkJoinTask<ArrayList<int[]>> compileVerticesTask;

Expand All @@ -109,8 +108,8 @@ public void onEnable()
prevLimit = limit.getValueI();
notify = true;

pool1 = MinPriorityThreadFactory.newFixedThreadPool();
pool2 = new ForkJoinPool();
threadPool = MinPriorityThreadFactory.newFixedThreadPool();
forkJoinPool = new ForkJoinPool();

bufferUpToDate = false;

Expand All @@ -126,9 +125,9 @@ public void onDisable()
EVENTS.remove(PacketInputListener.class, this);
EVENTS.remove(RenderListener.class, this);

stopPool2Tasks();
pool1.shutdownNow();
pool2.shutdownNow();
stopBuildingBuffer();
threadPool.shutdownNow();
forkJoinPool.shutdownNow();

if(vertexBuffer != null)
vertexBuffer.close();
Expand All @@ -149,7 +148,6 @@ public void onReceivedPacket(PacketInputEvent event)
public void onUpdate()
{
Block blockToFind = Blocks.CAVE_AIR;
BlockPos eyesPos = BlockPos.ofFloored(RotationUtils.getEyesPos());
int dimensionId = MC.world.getRegistryKey().toString().hashCode();

addSearchersInRange(blockToFind, dimensionId);
Expand All @@ -163,7 +161,7 @@ public void onUpdate()
checkIfLimitChanged();

if(getMatchingBlocksTask == null)
startGetMatchingBlocksTask(eyesPos);
startGetMatchingBlocksTask();

if(!getMatchingBlocksTask.isDone())
return;
Expand Down Expand Up @@ -282,22 +280,22 @@ private void replaceSearchersWithChunkUpdate(Block currentBlock,

private void addSearcher(Chunk chunk, Block block, int dimensionId)
{
stopPool2Tasks();
stopBuildingBuffer();

ChunkSearcher searcher = new ChunkSearcher(chunk, block, dimensionId);
searchers.put(chunk.getPos(), searcher);
searcher.startSearching(pool1);
searcher.startSearching(threadPool);
}

private void removeSearcher(ChunkSearcher searcher)
{
stopPool2Tasks();
stopBuildingBuffer();

searchers.remove(searcher.getPos());
searcher.cancelSearching();
}

private void stopPool2Tasks()
private void stopBuildingBuffer()
{
if(getMatchingBlocksTask != null)
{
Expand Down Expand Up @@ -327,26 +325,25 @@ private void checkIfLimitChanged()
{
if(limit.getValueI() != prevLimit)
{
stopPool2Tasks();
stopBuildingBuffer();
notify = true;
prevLimit = limit.getValueI();
}
}

private void startGetMatchingBlocksTask(BlockPos eyesPos)
private void startGetMatchingBlocksTask()
{
Callable<HashSet<BlockPos>> task =
() -> searchers.values().parallelStream()
.flatMap(searcher -> searcher.getMatchingBlocks().stream())
.sorted(Comparator
.comparingInt(pos -> eyesPos.getManhattanDistance(pos)))
.limit(limit.getValueLog())
.collect(Collectors.toCollection(HashSet::new));

getMatchingBlocksTask = pool2.submit(task);
BlockPos eyesPos = BlockPos.ofFloored(RotationUtils.getEyesPos());
Comparator<BlockPos> comparator =
Comparator.comparingInt(pos -> eyesPos.getManhattanDistance(pos));

getMatchingBlocksTask = forkJoinPool.submit(() -> searchers.values()
.parallelStream().flatMap(ChunkSearcher::getMatchingBlocks)
.sorted(comparator).limit(limit.getValueLog())
.collect(Collectors.toCollection(HashSet::new)));
}

private HashSet<BlockPos> getMatchingBlocksFromTask()
private void startCompileVerticesTask()
{
HashSet<BlockPos> matchingBlocks = getMatchingBlocksTask.join();

Expand All @@ -360,21 +357,12 @@ else if(notify)
notify = false;
}

return matchingBlocks;
}

private void startCompileVerticesTask()
{
HashSet<BlockPos> matchingBlocks = getMatchingBlocksFromTask();

BlockPos camPos = RenderUtils.getCameraBlockPos();
int regionX = (camPos.getX() >> 9) * 512;
int regionZ = (camPos.getZ() >> 9) * 512;

Callable<ArrayList<int[]>> task =
() -> BlockVertexCompiler.compile(matchingBlocks, regionX, regionZ);

compileVerticesTask = pool2.submit(task);
compileVerticesTask = forkJoinPool.submit(() -> BlockVertexCompiler
.compile(matchingBlocks, regionX, regionZ));
}

private void setBufferFromTask()
Expand Down
62 changes: 25 additions & 37 deletions src/main/java/net/wurstclient/hacks/SearchHack.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
Expand Down Expand Up @@ -74,9 +73,9 @@ public final class SearchHack extends Hack
private final HashMap<ChunkPos, ChunkSearcher> searchers = new HashMap<>();
private final Set<ChunkPos> chunksToUpdate =
Collections.synchronizedSet(new HashSet<>());
private ExecutorService pool1;
private ExecutorService threadPool;

private ForkJoinPool pool2;
private ForkJoinPool forkJoinPool;
private ForkJoinTask<HashSet<BlockPos>> getMatchingBlocksTask;
private ForkJoinTask<ArrayList<int[]>> compileVerticesTask;

Expand Down Expand Up @@ -105,8 +104,8 @@ public void onEnable()
prevLimit = limit.getValueI();
notify = true;

pool1 = MinPriorityThreadFactory.newFixedThreadPool();
pool2 = new ForkJoinPool();
threadPool = MinPriorityThreadFactory.newFixedThreadPool();
forkJoinPool = new ForkJoinPool();

bufferUpToDate = false;

Expand All @@ -122,9 +121,9 @@ public void onDisable()
EVENTS.remove(PacketInputListener.class, this);
EVENTS.remove(RenderListener.class, this);

stopPool2Tasks();
pool1.shutdownNow();
pool2.shutdownNow();
stopBuildingBuffer();
threadPool.shutdownNow();
forkJoinPool.shutdownNow();

if(vertexBuffer != null)
{
Expand All @@ -148,7 +147,6 @@ public void onReceivedPacket(PacketInputEvent event)
public void onUpdate()
{
Block currentBlock = block.getBlock();
BlockPos eyesPos = BlockPos.ofFloored(RotationUtils.getEyesPos());
int dimensionId = MC.world.getRegistryKey().toString().hashCode();

addSearchersInRange(currentBlock, dimensionId);
Expand All @@ -162,7 +160,7 @@ public void onUpdate()
checkIfLimitChanged();

if(getMatchingBlocksTask == null)
startGetMatchingBlocksTask(eyesPos);
startGetMatchingBlocksTask();

if(!getMatchingBlocksTask.isDone())
return;
Expand Down Expand Up @@ -275,22 +273,22 @@ private void replaceSearchersWithChunkUpdate(Block currentBlock,

private void addSearcher(Chunk chunk, Block block, int dimensionId)
{
stopPool2Tasks();
stopBuildingBuffer();

ChunkSearcher searcher = new ChunkSearcher(chunk, block, dimensionId);
searchers.put(chunk.getPos(), searcher);
searcher.startSearching(pool1);
searcher.startSearching(threadPool);
}

private void removeSearcher(ChunkSearcher searcher)
{
stopPool2Tasks();
stopBuildingBuffer();

searchers.remove(searcher.getPos());
searcher.cancelSearching();
}

private void stopPool2Tasks()
private void stopBuildingBuffer()
{
if(getMatchingBlocksTask != null)
{
Expand Down Expand Up @@ -320,26 +318,25 @@ private void checkIfLimitChanged()
{
if(limit.getValueI() != prevLimit)
{
stopPool2Tasks();
stopBuildingBuffer();
notify = true;
prevLimit = limit.getValueI();
}
}

private void startGetMatchingBlocksTask(BlockPos eyesPos)
private void startGetMatchingBlocksTask()
{
Callable<HashSet<BlockPos>> task =
() -> searchers.values().parallelStream()
.flatMap(searcher -> searcher.getMatchingBlocks().stream())
.sorted(Comparator
.comparingInt(pos -> eyesPos.getManhattanDistance(pos)))
.limit(limit.getValueLog())
.collect(Collectors.toCollection(HashSet::new));

getMatchingBlocksTask = pool2.submit(task);
BlockPos eyesPos = BlockPos.ofFloored(RotationUtils.getEyesPos());
Comparator<BlockPos> comparator =
Comparator.comparingInt(pos -> eyesPos.getManhattanDistance(pos));

getMatchingBlocksTask = forkJoinPool.submit(() -> searchers.values()
.parallelStream().flatMap(ChunkSearcher::getMatchingBlocks)
.sorted(comparator).limit(limit.getValueLog())
.collect(Collectors.toCollection(HashSet::new)));
}

private HashSet<BlockPos> getMatchingBlocksFromTask()
private void startCompileVerticesTask()
{
HashSet<BlockPos> matchingBlocks = getMatchingBlocksTask.join();

Expand All @@ -353,21 +350,12 @@ else if(notify)
notify = false;
}

return matchingBlocks;
}

private void startCompileVerticesTask()
{
HashSet<BlockPos> matchingBlocks = getMatchingBlocksFromTask();

BlockPos camPos = RenderUtils.getCameraBlockPos();
int regionX = (camPos.getX() >> 9) * 512;
int regionZ = (camPos.getZ() >> 9) * 512;

Callable<ArrayList<int[]>> task =
() -> BlockVertexCompiler.compile(matchingBlocks, regionX, regionZ);

compileVerticesTask = pool2.submit(task);
compileVerticesTask = forkJoinPool.submit(() -> BlockVertexCompiler
.compile(matchingBlocks, regionX, regionZ));
}

private void setBufferFromTask()
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/wurstclient/util/ChunkSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.stream.Stream;

import net.minecraft.block.Block;
import net.minecraft.client.world.ClientWorld;
Expand Down Expand Up @@ -123,9 +124,9 @@ public int getDimensionId()
return dimensionId;
}

public ArrayList<BlockPos> getMatchingBlocks()
public Stream<BlockPos> getMatchingBlocks()
{
return matchingBlocks;
return matchingBlocks.stream();
}

public ChunkSearcher.Status getStatus()
Expand Down

0 comments on commit d56fb44

Please sign in to comment.