Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/main/java/com/gregtechceu/gtceu/core/mixins/LevelMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.LevelChunk;

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -38,27 +36,31 @@ public abstract class LevelMixin implements LevelAccessor {
@Final
private Thread thread;

@Unique
private @Nullable ChunkAccess gtceu$maybeGetChunkAsync(int chunkX, int chunkZ) {
if (this.isClientSide) return null;
if (Thread.currentThread() == this.thread) return null;
if (!MultiblockWorldSavedData.isThreadService() && !AsyncThreadData.isThreadService()) return null;
if (!this.getChunkSource().hasChunk(chunkX, chunkZ)) return null;

return this.getChunkSource().getChunkNow(chunkX, chunkZ);
}

@Inject(method = "getBlockEntity", at = @At(value = "HEAD"), cancellable = true)
private void gtceu$getBlockEntityOffThread(BlockPos pos, CallbackInfoReturnable<BlockEntity> cir) {
ChunkAccess chunk = gtceu$maybeGetChunkAsync(pos.getX() >> 4, pos.getZ() >> 4);
if (Thread.currentThread() == this.thread) return;
if (this.isClientSide) return;
if (!MultiblockWorldSavedData.isThreadService() && !AsyncThreadData.isThreadService()) return;

int chunkX = pos.getX() >> 4, chunkZ = pos.getZ() >> 4;
if (!this.getChunkSource().hasChunk(chunkX, chunkZ)) return;

ChunkAccess chunk = this.getChunkSource().getChunkNow(chunkX, chunkZ);
if (chunk instanceof LevelChunk levelChunk) {
cir.setReturnValue(levelChunk.getBlockEntities().get(pos));
}
}

@Inject(method = "getBlockState", at = @At(value = "HEAD"), cancellable = true)
private void gtceu$getBlockStateOffThread(BlockPos pos, CallbackInfoReturnable<BlockState> cir) {
ChunkAccess chunk = gtceu$maybeGetChunkAsync(pos.getX() >> 4, pos.getZ() >> 4);
if (Thread.currentThread() == this.thread) return;
if (this.isClientSide) return;
if (!MultiblockWorldSavedData.isThreadService() && !AsyncThreadData.isThreadService()) return;

int chunkX = pos.getX() >> 4, chunkZ = pos.getZ() >> 4;
if (!this.getChunkSource().hasChunk(chunkX, chunkZ)) return;

ChunkAccess chunk = this.getChunkSource().getChunkNow(chunkX, chunkZ);
if (chunk != null) {
cir.setReturnValue(chunk.getBlockState(pos));
}
Expand Down
Loading