Skip to content

Commit e191a08

Browse files
authored
Improve getBlockState performance (#4727)
1 parent e60aa18 commit e191a08

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/main/java/com/gregtechceu/gtceu/core/mixins/LevelMixin.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414
import net.minecraft.world.level.chunk.ChunkAccess;
1515
import net.minecraft.world.level.chunk.LevelChunk;
1616

17-
import org.jetbrains.annotations.Nullable;
1817
import org.spongepowered.asm.mixin.Final;
1918
import org.spongepowered.asm.mixin.Mixin;
2019
import org.spongepowered.asm.mixin.Shadow;
21-
import org.spongepowered.asm.mixin.Unique;
2220
import org.spongepowered.asm.mixin.injection.At;
2321
import org.spongepowered.asm.mixin.injection.Inject;
2422
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@@ -38,27 +36,31 @@ public abstract class LevelMixin implements LevelAccessor {
3836
@Final
3937
private Thread thread;
4038

41-
@Unique
42-
private @Nullable ChunkAccess gtceu$maybeGetChunkAsync(int chunkX, int chunkZ) {
43-
if (this.isClientSide) return null;
44-
if (Thread.currentThread() == this.thread) return null;
45-
if (!MultiblockWorldSavedData.isThreadService() && !AsyncThreadData.isThreadService()) return null;
46-
if (!this.getChunkSource().hasChunk(chunkX, chunkZ)) return null;
47-
48-
return this.getChunkSource().getChunkNow(chunkX, chunkZ);
49-
}
50-
5139
@Inject(method = "getBlockEntity", at = @At(value = "HEAD"), cancellable = true)
5240
private void gtceu$getBlockEntityOffThread(BlockPos pos, CallbackInfoReturnable<BlockEntity> cir) {
53-
ChunkAccess chunk = gtceu$maybeGetChunkAsync(pos.getX() >> 4, pos.getZ() >> 4);
41+
if (Thread.currentThread() == this.thread) return;
42+
if (this.isClientSide) return;
43+
if (!MultiblockWorldSavedData.isThreadService() && !AsyncThreadData.isThreadService()) return;
44+
45+
int chunkX = pos.getX() >> 4, chunkZ = pos.getZ() >> 4;
46+
if (!this.getChunkSource().hasChunk(chunkX, chunkZ)) return;
47+
48+
ChunkAccess chunk = this.getChunkSource().getChunkNow(chunkX, chunkZ);
5449
if (chunk instanceof LevelChunk levelChunk) {
5550
cir.setReturnValue(levelChunk.getBlockEntities().get(pos));
5651
}
5752
}
5853

5954
@Inject(method = "getBlockState", at = @At(value = "HEAD"), cancellable = true)
6055
private void gtceu$getBlockStateOffThread(BlockPos pos, CallbackInfoReturnable<BlockState> cir) {
61-
ChunkAccess chunk = gtceu$maybeGetChunkAsync(pos.getX() >> 4, pos.getZ() >> 4);
56+
if (Thread.currentThread() == this.thread) return;
57+
if (this.isClientSide) return;
58+
if (!MultiblockWorldSavedData.isThreadService() && !AsyncThreadData.isThreadService()) return;
59+
60+
int chunkX = pos.getX() >> 4, chunkZ = pos.getZ() >> 4;
61+
if (!this.getChunkSource().hasChunk(chunkX, chunkZ)) return;
62+
63+
ChunkAccess chunk = this.getChunkSource().getChunkNow(chunkX, chunkZ);
6264
if (chunk != null) {
6365
cir.setReturnValue(chunk.getBlockState(pos));
6466
}

0 commit comments

Comments
 (0)