Skip to content

Commit

Permalink
Optimize how ChunkSearcher results are assigned to PortalESP groups
Browse files Browse the repository at this point in the history
Removed the ForkJoinPool and made sure the groups are only updated when
necessary.
  • Loading branch information
Alexander01998 committed Sep 9, 2023
1 parent d56fb44 commit 71096c8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 59 deletions.
92 changes: 35 additions & 57 deletions src/main/java/net/wurstclient/hacks/PortalEspHack.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
package net.wurstclient.hacks;

import java.awt.Color;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.Collectors;

import org.lwjgl.opengl.GL11;
Expand All @@ -23,7 +26,6 @@
import net.minecraft.block.Blocks;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.chunk.Chunk;
import net.wurstclient.Category;
Expand All @@ -43,7 +45,6 @@
import net.wurstclient.util.ChunkUtils;
import net.wurstclient.util.MinPriorityThreadFactory;
import net.wurstclient.util.RenderUtils;
import net.wurstclient.util.RotationUtils;

public final class PortalEspHack extends Hack implements UpdateListener,
PacketInputListener, CameraTransformViewBobbingListener, RenderListener
Expand Down Expand Up @@ -85,10 +86,9 @@ public final class PortalEspHack extends Hack implements UpdateListener,
new HashMap<>();
private final Set<ChunkPos> chunksToUpdate =
Collections.synchronizedSet(new HashSet<>());
private ExecutorService pool1;
private ExecutorService threadPool;

private ForkJoinPool pool2;
private ForkJoinTask<ArrayList<Result>> getMatchingBlocksTask;
private boolean groupsUpToDate;

public PortalEspHack()
{
Expand All @@ -104,8 +104,8 @@ public PortalEspHack()
@Override
public void onEnable()
{
pool1 = MinPriorityThreadFactory.newFixedThreadPool();
pool2 = new ForkJoinPool();
threadPool = MinPriorityThreadFactory.newFixedThreadPool();
groupsUpToDate = false;

EVENTS.add(UpdateListener.class, this);
EVENTS.add(PacketInputListener.class, this);
Expand All @@ -123,12 +123,8 @@ public void onDisable()
EVENTS.remove(CameraTransformViewBobbingListener.class, this);
EVENTS.remove(RenderListener.class, this);

stopPool2Tasks();
pool1.shutdownNow();
pool2.shutdownNow();

threadPool.shutdownNow();
chunksToUpdate.clear();

groups.forEach(PortalEspBlockGroup::clear);
PortalEspRenderer.closeBuffers();
}
Expand All @@ -153,28 +149,22 @@ public void onCameraTransformViewBobbing(
@Override
public void onUpdate()
{
ArrayList<Block> currentBlockList =
groups.parallelStream().map(PortalEspBlockGroup::getBlock)
ArrayList<Block> blockList =
groups.stream().map(PortalEspBlockGroup::getBlock)
.collect(Collectors.toCollection(ArrayList::new));

BlockPos eyesPos = BlockPos.ofFloored(RotationUtils.getEyesPos());
int dimensionId = MC.world.getRegistryKey().toString().hashCode();

addSearchersInRange(currentBlockList, dimensionId);
addSearchersInRange(blockList, dimensionId);
removeSearchersOutOfRange(dimensionId);
replaceSearchersWithDifferences(currentBlockList, dimensionId);
replaceSearchersWithChunkUpdate(currentBlockList, dimensionId);
replaceSearchersWithDifferences(blockList, dimensionId);
replaceSearchersWithChunkUpdate(blockList, dimensionId);

if(!areAllChunkSearchersDone())
return;

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

if(!getMatchingBlocksTask.isDone())
return;

getMatchingBlocksFromTask();
if(!groupsUpToDate)
updateGroupBoxes();
}

@Override
Expand Down Expand Up @@ -280,29 +270,20 @@ private void replaceSearchersWithChunkUpdate(
private void addSearcher(Chunk chunk, ArrayList<Block> blockList,
int dimensionId)
{
groupsUpToDate = false;
ChunkSearcherMulti searcher =
new ChunkSearcherMulti(chunk, blockList, dimensionId);
searchers.put(chunk.getPos(), searcher);
searcher.startSearching(pool1);
searcher.startSearching(threadPool);
}

private void removeSearcher(ChunkSearcherMulti searcher)
{
stopPool2Tasks();

groupsUpToDate = false;
searchers.remove(searcher.getPos());
searcher.cancelSearching();
}

private void stopPool2Tasks()
{
if(getMatchingBlocksTask != null)
{
getMatchingBlocksTask.cancel(true);
getMatchingBlocksTask = null;
}
}

private boolean areAllChunkSearchersDone()
{
for(ChunkSearcherMulti searcher : searchers.values())
Expand All @@ -312,26 +293,23 @@ private boolean areAllChunkSearchersDone()
return true;
}

private void startGetMatchingBlocksTask(BlockPos eyesPos)
private void updateGroupBoxes()
{
Callable<ArrayList<Result>> task =
() -> searchers.values().parallelStream()
.flatMap(searcher -> searcher.getMatchingBlocks().stream())
.sorted(Comparator.comparingInt(
result -> eyesPos.getManhattanDistance(result.getPos())))
.collect(Collectors.toCollection(ArrayList::new));
groups.forEach(PortalEspBlockGroup::clear);
searchers.values().stream()
.flatMap(ChunkSearcherMulti::getMatchingBlocks)
.forEachOrdered(this::addToGroupBoxes);

getMatchingBlocksTask = pool2.submit(task);
groupsUpToDate = true;
}

private void getMatchingBlocksFromTask()
private void addToGroupBoxes(Result result)
{
groups.forEach(PortalEspBlockGroup::clear);

for(Result result : getMatchingBlocksTask.join())
groups.parallelStream()
.filter(group -> group.getBlock().getClass() == result
.getBlock().getClass())
.forEach(group -> group.add(result.getPos()));
for(PortalEspBlockGroup group : groups)
if(result.getBlock() == group.getBlock())
{
group.add(result.getPos());
break;
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/net/wurstclient/util/ChunkSearcherMulti.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 @@ -125,9 +126,9 @@ public int getDimensionId()
return dimensionId;
}

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

public ChunkSearcherMulti.Status getStatus()
Expand Down

0 comments on commit 71096c8

Please sign in to comment.