Skip to content

Commit

Permalink
fix potential ChunkDataEvent race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
RacoonDog authored and MineGame159 committed Mar 4, 2024
1 parent 1f98e95 commit 484b2cf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,10 @@

package meteordevelopment.meteorclient.events.world;

import meteordevelopment.meteorclient.utils.misc.Pool;
import net.minecraft.world.chunk.WorldChunk;

public class ChunkDataEvent {
private static final Pool<ChunkDataEvent> INSTANCE = new Pool<>(ChunkDataEvent::new);

public WorldChunk chunk;

public static ChunkDataEvent get(WorldChunk chunk) {
ChunkDataEvent event = INSTANCE.get();
event.chunk = chunk;
return event;
}

public static void returnChunkDataEvent(ChunkDataEvent event) {
INSTANCE.free(event);
}
}
/**
* @implNote Shouldn't be put in a {@link meteordevelopment.meteorclient.utils.misc.Pool} to avoid a race-condition, or in a {@link ThreadLocal} as it is shared between threads.
* @author Crosby
*/
public record ChunkDataEvent(WorldChunk chunk) {}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void onPlaySound(PlaySoundS2CPacket packet, CallbackInfo info) {
@Inject(method = "onChunkData", at = @At("TAIL"))
private void onChunkData(ChunkDataS2CPacket packet, CallbackInfo info) {
WorldChunk chunk = client.world.getChunk(packet.getChunkX(), packet.getChunkZ());
MeteorClient.EVENT_BUS.post(ChunkDataEvent.get(chunk));
MeteorClient.EVENT_BUS.post(new ChunkDataEvent(chunk));
}

@Inject(method = "onScreenHandlerSlotUpdate", at = @At("TAIL"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void onActivate() {
}

for (Chunk chunk : Utils.chunks()) {
searchChunk(chunk, null);
searchChunk(chunk);
}

lastDimension = PlayerUtils.getDimension();
Expand Down Expand Up @@ -153,10 +153,10 @@ public void removeGroup(ESPGroup group) {

@EventHandler
private void onChunkData(ChunkDataEvent event) {
searchChunk(event.chunk, event);
searchChunk(event.chunk());
}

private void searchChunk(Chunk chunk, ChunkDataEvent event) {
private void searchChunk(Chunk chunk) {
MeteorExecutor.execute(() -> {
if (!isActive()) return;
ESPChunk schunk = ESPChunk.searchChunk(chunk, blocks.get());
Expand All @@ -173,8 +173,6 @@ private void searchChunk(Chunk chunk, ChunkDataEvent event) {
updateChunk(chunk.getPos().x, chunk.getPos().z + 1);
}
}

if (event != null) ChunkDataEvent.returnChunkDataEvent(event);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ public void onActivate() {
@EventHandler
private void onChunkData(ChunkDataEvent event) {
// Check the distance.
double chunkXAbs = Math.abs(event.chunk.getPos().x * 16);
double chunkZAbs = Math.abs(event.chunk.getPos().z * 16);
double chunkXAbs = Math.abs(event.chunk().getPos().x * 16);
double chunkZAbs = Math.abs(event.chunk().getPos().z * 16);
if (Math.sqrt(chunkXAbs * chunkXAbs + chunkZAbs * chunkZAbs) < minimumDistance.get()) return;

Chunk chunk = new Chunk(event.chunk.getPos());
Chunk chunk = new Chunk(event.chunk().getPos());

for (BlockEntity blockEntity : event.chunk.getBlockEntities().values()) {
for (BlockEntity blockEntity : event.chunk().getBlockEntities().values()) {
if (!storageBlocks.get().contains(blockEntity.getType())) continue;

if (blockEntity instanceof ChestBlockEntity) chunk.chests++;
Expand Down Expand Up @@ -133,8 +133,6 @@ private void onChunkData(ChunkDataEvent event) {
}
}
}

ChunkDataEvent.returnChunkDataEvent(event);
}

@Override
Expand Down

0 comments on commit 484b2cf

Please sign in to comment.