Skip to content

Commit

Permalink
Ignore world state when rendering debug worlds
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Nov 12, 2023
1 parent d9081b4 commit f9dd1ee
Showing 1 changed file with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import it.unimi.dsi.fastutil.ints.Int2ReferenceOpenHashMap;
import me.jellysquid.mods.sodium.client.world.ReadableContainerExtended;
import me.jellysquid.mods.sodium.client.world.WorldSlice;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.math.BlockBox;
Expand All @@ -14,10 +16,8 @@
import net.minecraft.world.LightType;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.chunk.ChunkNibbleArray;
import net.minecraft.world.chunk.ChunkSection;
import net.minecraft.world.chunk.ReadableContainer;
import net.minecraft.world.chunk.WorldChunk;
import net.minecraft.world.chunk.*;
import net.minecraft.world.gen.chunk.DebugChunkGenerator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -51,7 +51,11 @@ public ClonedChunkSection(World world, WorldChunk chunk, @Nullable ChunkSection

if (section != null) {
if (!section.isEmpty()) {
blockData = ReadableContainerExtended.clone(section.getBlockStateContainer());
if (!world.isDebugWorld()) {
blockData = ReadableContainerExtended.clone(section.getBlockStateContainer());
} else {
blockData = constructDebugWorldContainer(pos);
}
blockEntityMap = copyBlockEntities(chunk, pos);

if (blockEntityMap != null) {
Expand All @@ -71,6 +75,34 @@ public ClonedChunkSection(World world, WorldChunk chunk, @Nullable ChunkSection
this.lightDataArrays = copyLightData(world, pos);
}

/**
* Construct a fake PalettedContainer whose contents match those of the debug world. This is needed to
* match vanilla's odd approach of short-circuiting getBlockState calls inside its render region class.
*/
@NotNull
private static PalettedContainer<BlockState> constructDebugWorldContainer(ChunkSectionPos pos) {
var container = new PalettedContainer<>(Block.STATE_IDS, Blocks.AIR.getDefaultState(), PalettedContainer.PaletteProvider.BLOCK_STATE);
// We use swapUnsafe to avoid going through the lock when we own the container
for (int y = 0; y < 16; y++) {
int worldY = ChunkSectionPos.getOffsetPos(pos.getY(), y);
if (worldY == 60) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
BlockState state = Blocks.BARRIER.getDefaultState();
container.swapUnsafe(x, y, z, state);
}
}
} else if (worldY == 70) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
container.swapUnsafe(x, y, z, DebugChunkGenerator.getBlockState(ChunkSectionPos.getOffsetPos(pos.getX(), x), ChunkSectionPos.getOffsetPos(pos.getZ(), z)));
}
}
}
}
return container;
}

@NotNull
private static ChunkNibbleArray[] copyLightData(World world, ChunkSectionPos pos) {
var arrays = new ChunkNibbleArray[2];
Expand Down

0 comments on commit f9dd1ee

Please sign in to comment.