Skip to content

Commit dad55e7

Browse files
committed
Fix error when getting out of bounds block state
- Increased region range - Refactor
1 parent 4c11df7 commit dad55e7

File tree

1 file changed

+57
-34
lines changed

1 file changed

+57
-34
lines changed

src/main/java/net/vulkanmod/render/chunk/build/RenderRegion.java

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ public class RenderRegion implements BlockAndTintGetter {
2727
public static final int WIDTH = 3;
2828
public static final int SIZE = WIDTH * WIDTH * WIDTH;
2929

30-
public static final int REGION_WIDTH = 16 + 2;
31-
public static final int BLOCKS = REGION_WIDTH * REGION_WIDTH * REGION_WIDTH;
30+
public static final int BOUNDARY_BLOCK_WIDTH = 2;
31+
public static final int REGION_BLOCK_WIDTH = 16 + BOUNDARY_BLOCK_WIDTH * 2;
32+
public static final int BLOCK_COUNT = REGION_BLOCK_WIDTH * REGION_BLOCK_WIDTH * REGION_BLOCK_WIDTH;
33+
34+
public static final BlockState AIR_BLOCK_STATE = Blocks.AIR.defaultBlockState();
3235

3336
private final int minSecX, minSecY, minSecZ;
3437
private final int minX, minY, minZ;
38+
private final int maxX, maxY, maxZ;
3539
private final Level level;
3640
private final int blendRadius;
3741

@@ -51,49 +55,48 @@ public class RenderRegion implements BlockAndTintGetter {
5155
this.minSecX = x - 1;
5256
this.minSecY = y - 1;
5357
this.minSecZ = z - 1;
54-
this.minX = (minSecX << 4) + 15;
55-
this.minZ = (minSecZ << 4) + 15;
56-
this.minY = (minSecY << 4) + 15;
58+
this.minX = (minSecX << 4) + 16 - BOUNDARY_BLOCK_WIDTH;
59+
this.minZ = (minSecZ << 4) + 16 - BOUNDARY_BLOCK_WIDTH;
60+
this.minY = (minSecY << 4) + 16 - BOUNDARY_BLOCK_WIDTH;
61+
this.maxX = minX + REGION_BLOCK_WIDTH;
62+
this.maxZ = minZ + REGION_BLOCK_WIDTH;
63+
this.maxY = minY + REGION_BLOCK_WIDTH;
5764

5865
this.blockDataContainers = blockData;
5966
this.lightData = lightData;
6067
this.blockEntityMap = blockEntityMap;
6168

62-
this.blockData = new BlockState[BLOCKS];
69+
this.blockData = new BlockState[BLOCK_COUNT];
6370

6471
this.blockStateGetter = level.isDebug() ? this::debugBlockState : this::defaultBlockState;
6572

6673
this.blendRadius = Minecraft.getInstance().options.biomeBlendRadius().get();
6774
}
6875

6976
public void loadBlockStates() {
70-
int maxSecX = minSecX + 2;
71-
int maxSecY = minSecY + 2;
72-
int maxSecZ = minSecZ + 2;
73-
74-
int maxX = (maxSecX << 4);
75-
int maxZ = (maxSecZ << 4);
76-
int maxY = (maxSecY << 4);
77-
7877
Arrays.fill(blockData, Blocks.AIR.defaultBlockState());
7978

80-
for(int x = minSecX; x <= maxSecX; ++x) {
81-
for(int z = minSecZ; z <= maxSecZ; ++z) {
82-
for(int y = minSecY; y <= maxSecY; ++y) {
83-
final int idx = getSectionIdx((x - minSecX), (y - minSecY), (z - minSecZ));
79+
for(int x = 0; x <= 2; ++x) {
80+
for(int z = 0; z <= 2; ++z) {
81+
for(int y = 0; y <= 2; ++y) {
82+
final int idx = getSectionIdx(x, y, z);
8483

8584
PalettedContainer<BlockState> container = blockDataContainers[idx];
8685

8786
if(container == null)
8887
continue;
8988

90-
int tMinX = Math.max(minX, x << 4);
91-
int tMinY = Math.max(minY, y << 4);
92-
int tMinZ = Math.max(minZ, z << 4);
89+
int absBlockX = (x + minSecX) << 4;
90+
int absBlockY = (y + minSecY) << 4;
91+
int absBlockZ = (z + minSecZ) << 4;
9392

94-
int tMaxX = Math.min(maxX, (x << 4) + 15);
95-
int tMaxY = Math.min(maxY, (y << 4) + 15);
96-
int tMaxZ = Math.min(maxZ, (z << 4) + 15);
93+
int tMinX = Math.max(minX, absBlockX);
94+
int tMinY = Math.max(minY, absBlockY);
95+
int tMinZ = Math.max(minZ, absBlockZ);
96+
97+
int tMaxX = Math.min(maxX, absBlockX + 16);
98+
int tMaxY = Math.min(maxY, absBlockY + 16);
99+
int tMaxZ = Math.min(maxZ, absBlockZ + 16);
97100

98101
loadSectionBlockStates(container, blockData,
99102
tMinX, tMinY, tMinZ, tMaxX, tMaxY, tMaxZ);
@@ -106,10 +109,10 @@ public void loadBlockStates() {
106109
void loadSectionBlockStates(PalettedContainer<BlockState> container, BlockState[] blockStates,
107110
int minX, int minY, int minZ, int maxX, int maxY, int maxZ) {
108111

109-
for(int x = minX; x <= maxX; ++x) {
110-
for(int z = minZ; z <= maxZ; ++z) {
111-
for(int y = minY; y <= maxY; ++y) {
112-
final int idx = getBlockIdx(x, y, z);
112+
for (int y = minY; y < maxY; ++y) {
113+
for (int z = minZ; z < maxZ; ++z) {
114+
for (int x = minX; x < maxX; ++x) {
115+
final int idx = getBlockIdx(x - this.minX, y - this.minY, z - this.minZ);
113116

114117
blockStates[idx] = container != null ?
115118
container.get(x & 15, y & 15, z & 15)
@@ -129,7 +132,7 @@ public BlockState getBlockState(BlockPos blockPos) {
129132
}
130133

131134
public FluidState getFluidState(BlockPos blockPos) {
132-
return blockStateGetter.apply(blockPos).getFluidState();
135+
return this.getBlockState(blockPos).getFluidState();
133136
}
134137

135138
public float getShade(Direction direction, boolean bl) {
@@ -141,16 +144,23 @@ public LevelLightEngine getLightEngine() {
141144
}
142145

143146
public int getBrightness(LightLayer lightLayer, BlockPos blockPos) {
147+
if (outsideRegion(blockPos.getX(), blockPos.getY(), blockPos.getZ())) {
148+
return 0;
149+
}
150+
144151
int secX = SectionPos.blockToSectionCoord(blockPos.getX()) - this.minSecX;
145152
int secY = SectionPos.blockToSectionCoord(blockPos.getY()) - this.minSecY;
146153
int secZ = SectionPos.blockToSectionCoord(blockPos.getZ()) - this.minSecZ;
147154

148155
DataLayer dataLayer = this.lightData[getSectionIdx(secX, secY, secZ)][lightLayer.ordinal()];
149156
return dataLayer == null ? 0 : dataLayer.get(blockPos.getX() & 15, blockPos.getY() & 15, blockPos.getZ() & 15);
150-
151157
}
152158

153159
public int getRawBrightness(BlockPos blockPos, int i) {
160+
if (outsideRegion(blockPos.getX(), blockPos.getY(), blockPos.getZ())) {
161+
return 0;
162+
}
163+
154164
int secX = SectionPos.blockToSectionCoord(blockPos.getX()) - this.minSecX;
155165
int secY = SectionPos.blockToSectionCoord(blockPos.getY()) - this.minSecY;
156166
int secZ = SectionPos.blockToSectionCoord(blockPos.getZ()) - this.minSecZ;
@@ -190,14 +200,27 @@ public int getSectionIdx(int secX, int secY, int secZ) {
190200
}
191201

192202
public int getBlockIdx(int x, int y, int z) {
203+
return REGION_BLOCK_WIDTH * ((REGION_BLOCK_WIDTH * y) + z) + x;
204+
}
205+
206+
public boolean outsideRegion(int x, int y, int z) {
207+
return x < minX || x >= maxX || y < minY || y >= maxY || z < minZ || z >= maxZ;
208+
}
209+
210+
public BlockState defaultBlockState(BlockPos blockPos) {
211+
int x = blockPos.getX();
212+
int y = blockPos.getY();
213+
int z = blockPos.getZ();
214+
215+
if (outsideRegion(x, y, z)) {
216+
return AIR_BLOCK_STATE;
217+
}
218+
193219
x -= minX;
194220
y -= minY;
195221
z -= minZ;
196-
return REGION_WIDTH * ((REGION_WIDTH * y) + z) + x;
197-
}
198222

199-
public BlockState defaultBlockState(BlockPos blockPos) {
200-
return blockData[getBlockIdx(blockPos.getX(), blockPos.getY(), blockPos.getZ())];
223+
return blockData[getBlockIdx(x, y, z)];
201224
}
202225

203226
public BlockState debugBlockState(BlockPos blockPos) {

0 commit comments

Comments
 (0)