Skip to content

Commit

Permalink
Add bounds checking in WorldSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Nov 20, 2023
1 parent 5e40060 commit 36921a6
Showing 1 changed file with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public final class WorldSlice implements BlockRenderView, BiomeColorView, Render
// The number of bits needed for each local X/Y/Z coordinate.
private static final int LOCAL_XYZ_BITS = 4;

// The default BlockState used if we have not cloned a section
private static final BlockState EMPTY_BLOCK_STATE = Blocks.AIR.getDefaultState();

// The world this slice has copied data from
private final ClientWorld world;

Expand Down Expand Up @@ -224,8 +227,22 @@ public BlockState getBlockState(int x, int y, int z) {
int relY = y - this.originY;
int relZ = z - this.originZ;

return this.blockArrays[getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4)]
[getLocalBlockIndex(relX & 15, relY & 15, relZ & 15)];
int sectionIndex = getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4);

if (sectionIndex < 0 || sectionIndex >= this.blockArrays.length) {
// We never cloned that section
return EMPTY_BLOCK_STATE;
}

BlockState[] blockArray = this.blockArrays[sectionIndex];
BlockState blockState = blockArray[getLocalBlockIndex(relX & 15, relY & 15, relZ & 15)];

if (blockState == null) {
// We didn't clone that part of the given section
return EMPTY_BLOCK_STATE;
}

return blockState;
}

@Override
Expand All @@ -251,7 +268,12 @@ public int getLightLevel(LightType type, BlockPos pos) {
int relY = pos.getY() - this.originY;
int relZ = pos.getZ() - this.originZ;

var lightArray = this.lightArrays[getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4)][type.ordinal()];
int sectionIndex = getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4);
if(sectionIndex < 0 || sectionIndex >= this.lightArrays.length) {
return 0;
}

var lightArray = this.lightArrays[sectionIndex][type.ordinal()];

if (lightArray == null) {
// If the array is null, it means the dimension for the current world does not support that light type
Expand All @@ -267,7 +289,12 @@ public int getBaseLightLevel(BlockPos pos, int ambientDarkness) {
int relY = pos.getY() - this.originY;
int relZ = pos.getZ() - this.originZ;

var lightArrays = this.lightArrays[getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4)];
int sectionIndex = getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4);
if (sectionIndex < 0 || sectionIndex >= this.lightArrays.length) {
return 0;
}

var lightArrays = this.lightArrays[sectionIndex];

var skyLightArray = lightArrays[LightType.SKY.ordinal()];
var blockLightArray = lightArrays[LightType.BLOCK.ordinal()];
Expand All @@ -292,7 +319,13 @@ public BlockEntity getBlockEntity(int x, int y, int z) {
int relY = y - this.originY;
int relZ = z - this.originZ;

var blockEntities = this.blockEntityArrays[getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4)];
int sectionIndex = getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4);

if (sectionIndex < 0 || sectionIndex >= this.blockEntityArrays.length) {
return null;
}

var blockEntities = this.blockEntityArrays[sectionIndex];

if (blockEntities == null) {
return null;
Expand Down Expand Up @@ -327,7 +360,12 @@ public int getColor(BiomeColorSource source, int x, int y, int z) {
int relY = pos.getY() - this.originY;
int relZ = pos.getZ() - this.originZ;

var blockEntityRenderDataMap = this.blockEntityRenderDataArrays[getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4)];
int sectionIndex = getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4);
if (sectionIndex < 0 || sectionIndex >= this.blockEntityRenderDataArrays.length) {
return null;
}

var blockEntityRenderDataMap = this.blockEntityRenderDataArrays[sectionIndex];

if (blockEntityRenderDataMap == null) {
return null;
Expand Down

0 comments on commit 36921a6

Please sign in to comment.