Skip to content

Commit

Permalink
Retrieve BlockState directly from chunk section for random tick
Browse files Browse the repository at this point in the history
This appears to be faster than going directly to the global
palette, even though the global palette should be a simple list
get call.
  • Loading branch information
Spottedleaf committed Jul 15, 2024
1 parent 225d5f9 commit b160a14
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.chunk.PalettedContainer;
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.storage.WritableLevelData;
Expand Down Expand Up @@ -67,6 +68,7 @@ private LevelChunkSection[] optimiseRandomTick(final LevelChunk chunk,
for (int sectionIndex = 0, sectionsLen = sections.length; sectionIndex < sectionsLen; sectionIndex++) {
final int offsetY = (sectionIndex + minSection) << 4;
final LevelChunkSection section = sections[sectionIndex];
final PalettedContainer<BlockState> states = section.states;
if (section == null || !section.isRandomlyTickingBlocks()) {
continue;
}
Expand All @@ -86,14 +88,14 @@ private LevelChunkSection[] optimiseRandomTick(final LevelChunk chunk,
}

final long raw = tickList.getRaw(index);
final BlockState state = IBlockDataList.getBlockDataFromRaw(raw);
final int location = IBlockDataList.getLocationFromRaw(raw);
final int randomX = (location & 15) | offsetX;
final int randomY = ((location >>> (4 + 4)) & 255) | offsetY;
final int randomZ = ((location >>> 4) & 15) | offsetZ;
final int randomX = (location & 15);
final int randomY = ((location >>> (4 + 4)) & 255);
final int randomZ = ((location >>> 4) & 15);
final BlockState state = states.get(randomX | (randomZ << 4) | (randomZ << 8));

// do not use a mutable pos, as some random tick implementations store the input without calling immutable()!
final BlockPos pos = new BlockPos(randomX, randomY, randomZ);
final BlockPos pos = new BlockPos(randomX | offsetX, randomY | offsetY, randomZ | offsetZ);

state.randomTick((ServerLevel)(Object)this, pos, random);
if (tickFluids) {
Expand Down

0 comments on commit b160a14

Please sign in to comment.