Skip to content

Commit

Permalink
Make chunksToUpdate a Set<ChunkPos>
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Sep 6, 2023
1 parent 7219ae7 commit 27c4cdd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 40 deletions.
32 changes: 12 additions & 20 deletions src/main/java/net/wurstclient/hacks/CaveFinderHack.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@
import net.minecraft.block.Blocks;
import net.minecraft.client.gl.ShaderProgram;
import net.minecraft.client.gl.VertexBuffer;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.BufferBuilder.BuiltBuffer;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexFormat;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.ChunkDataS2CPacket;
Expand Down Expand Up @@ -87,7 +85,7 @@ public final class CaveFinderHack extends Hack
private boolean notify;

private final HashMap<Chunk, ChunkSearcher> searchers = new HashMap<>();
private final Set<Chunk> chunksToUpdate =
private final Set<ChunkPos> chunksToUpdate =
Collections.synchronizedSet(new HashSet<>());
private ExecutorService pool1;

Expand Down Expand Up @@ -144,34 +142,26 @@ public void onDisable()
@Override
public void onReceivedPacket(PacketInputEvent event)
{
ClientPlayerEntity player = MC.player;
ClientWorld world = MC.world;
if(player == null || world == null)
return;

Packet<?> packet = event.getPacket();
Chunk chunk;
ChunkPos chunkPos;

if(packet instanceof BlockUpdateS2CPacket change)
{
BlockPos pos = change.getPos();
chunk = world.getChunk(pos);

}else if(packet instanceof ChunkDeltaUpdateS2CPacket change)
chunkPos = new ChunkPos(change.getPos());
else if(packet instanceof ChunkDeltaUpdateS2CPacket change)
{
ArrayList<BlockPos> changedBlocks = new ArrayList<>();
change.visitUpdates((pos, state) -> changedBlocks.add(pos));
if(changedBlocks.isEmpty())
return;

chunk = world.getChunk(changedBlocks.get(0));
chunkPos = new ChunkPos(changedBlocks.get(0));

}else if(packet instanceof ChunkDataS2CPacket chunkData)
chunk = world.getChunk(chunkData.getX(), chunkData.getZ());
chunkPos = new ChunkPos(chunkData.getX(), chunkData.getZ());
else
return;

chunksToUpdate.add(chunk);
chunksToUpdate.add(chunkPos);
}

@Override
Expand Down Expand Up @@ -289,17 +279,19 @@ private void replaceSearchersWithChunkUpdate(Block currentBlock,
int dimensionId)
{
// get the chunks to update and remove them from the set
Chunk[] chunks;
ChunkPos[] chunks;
synchronized(chunksToUpdate)
{
chunks = chunksToUpdate.toArray(Chunk[]::new);
chunks = chunksToUpdate.toArray(ChunkPos[]::new);
chunksToUpdate.clear();
}

// update the chunks separately so the synchronization
// doesn't have to wait for that
for(Chunk chunk : chunks)
for(ChunkPos chunkPos : chunks)
{
Chunk chunk = MC.world.getChunk(chunkPos.x, chunkPos.z);

ChunkSearcher oldSearcher = searchers.get(chunk);
if(oldSearcher == null)
continue;
Expand Down
32 changes: 12 additions & 20 deletions src/main/java/net/wurstclient/hacks/SearchHack.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@
import net.minecraft.block.Block;
import net.minecraft.client.gl.ShaderProgram;
import net.minecraft.client.gl.VertexBuffer;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.BufferBuilder.BuiltBuffer;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexFormat;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.ChunkDataS2CPacket;
Expand Down Expand Up @@ -77,7 +75,7 @@ public final class SearchHack extends Hack
private boolean notify;

private final HashMap<Chunk, ChunkSearcher> searchers = new HashMap<>();
private final Set<Chunk> chunksToUpdate =
private final Set<ChunkPos> chunksToUpdate =
Collections.synchronizedSet(new HashSet<>());
private ExecutorService pool1;

Expand Down Expand Up @@ -143,34 +141,26 @@ public void onDisable()
@Override
public void onReceivedPacket(PacketInputEvent event)
{
ClientPlayerEntity player = MC.player;
ClientWorld world = MC.world;
if(player == null || world == null)
return;

Packet<?> packet = event.getPacket();
Chunk chunk;
ChunkPos chunkPos;

if(packet instanceof BlockUpdateS2CPacket change)
{
BlockPos pos = change.getPos();
chunk = world.getChunk(pos);

}else if(packet instanceof ChunkDeltaUpdateS2CPacket change)
chunkPos = new ChunkPos(change.getPos());
else if(packet instanceof ChunkDeltaUpdateS2CPacket change)
{
ArrayList<BlockPos> changedBlocks = new ArrayList<>();
change.visitUpdates((pos, state) -> changedBlocks.add(pos));
if(changedBlocks.isEmpty())
return;

chunk = world.getChunk(changedBlocks.get(0));
chunkPos = new ChunkPos(changedBlocks.get(0));

}else if(packet instanceof ChunkDataS2CPacket chunkData)
chunk = world.getChunk(chunkData.getX(), chunkData.getZ());
chunkPos = new ChunkPos(chunkData.getX(), chunkData.getZ());
else
return;

chunksToUpdate.add(chunk);
chunksToUpdate.add(chunkPos);
}

@Override
Expand Down Expand Up @@ -282,17 +272,19 @@ private void replaceSearchersWithChunkUpdate(Block currentBlock,
int dimensionId)
{
// get the chunks to update and remove them from the set
Chunk[] chunks;
ChunkPos[] chunks;
synchronized(chunksToUpdate)
{
chunks = chunksToUpdate.toArray(Chunk[]::new);
chunks = chunksToUpdate.toArray(ChunkPos[]::new);
chunksToUpdate.clear();
}

// update the chunks separately so the synchronization
// doesn't have to wait for that
for(Chunk chunk : chunks)
for(ChunkPos chunkPos : chunks)
{
Chunk chunk = MC.world.getChunk(chunkPos.x, chunkPos.z);

ChunkSearcher oldSearcher = searchers.get(chunk);
if(oldSearcher == null)
continue;
Expand Down

0 comments on commit 27c4cdd

Please sign in to comment.