Skip to content

Commit 7253e3c

Browse files
committed
Partial implementation for non-cubic-worlds with extended height
1 parent 381eb08 commit 7253e3c

File tree

3 files changed

+78
-11
lines changed

3 files changed

+78
-11
lines changed

src/main/java/io/github/opencubicchunks/cubicchunks/core/asm/mixin/core/client/MixinRenderGlobal.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.github.opencubicchunks.cubicchunks.api.world.ICubicWorld;
3232
import io.github.opencubicchunks.cubicchunks.api.world.IColumn;
3333
import mcp.MethodsReturnNonnullByDefault;
34+
import net.minecraft.client.multiplayer.WorldClient;
3435
import net.minecraft.client.renderer.RenderGlobal;
3536
import net.minecraft.client.renderer.ViewFrustum;
3637
import net.minecraft.client.renderer.chunk.RenderChunk;
@@ -76,6 +77,8 @@ public class MixinRenderGlobal {
7677

7778
@Shadow private ViewFrustum viewFrustum;
7879

80+
@Shadow private WorldClient world;
81+
7982
/*
8083
* This allows to get the Y position of rendered entity by injecting itself directly before call to
8184
* chunk.getEntityLists
@@ -163,7 +166,7 @@ private int getRenderChunkYPos(BlockPos pos) {
163166
if (this.position != null) { // also set from optifine specific mixins
164167
return 0;//must be 0 (or anything between 0 and 15)
165168
}
166-
return pos.getY();
169+
return pos.getY() - ((ICubicWorld) world).getMinHeight();
167170
}
168171

169172
/*

src/main/java/io/github/opencubicchunks/cubicchunks/core/asm/mixin/core/common/MixinChunk_Cubes.java

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import io.github.opencubicchunks.cubicchunks.api.world.ICube;
3535
import io.github.opencubicchunks.cubicchunks.api.world.ICubicWorld;
3636
import io.github.opencubicchunks.cubicchunks.api.world.IHeightMap;
37+
import io.github.opencubicchunks.cubicchunks.api.world.IMinMaxHeight;
3738
import io.github.opencubicchunks.cubicchunks.core.CubicChunksConfig;
3839
import io.github.opencubicchunks.cubicchunks.core.asm.mixin.ICubicWorldInternal;
3940
import io.github.opencubicchunks.cubicchunks.core.world.ClientHeightMap;
@@ -148,7 +149,7 @@ public <T extends World & ICubicWorldInternal> T getWorld() {
148149
@Unique @Nullable
149150
private ExtendedBlockStorage getEBS_CubicChunks(int index) {
150151
if (!isColumn) {
151-
return storageArrays[index];
152+
return storageArrays[index - Coords.blockToCube(getWorld().getMinHeight())];
152153
}
153154
if (cachedCube != null && cachedCube.getY() == index) {
154155
return cachedCube.getStorage();
@@ -163,7 +164,7 @@ private ExtendedBlockStorage getEBS_CubicChunks(int index) {
163164
// setEBS is unlikely to be used extremely frequently, no caching
164165
@Unique private void setEBS_CubicChunks(int index, ExtendedBlockStorage ebs) {
165166
if (!isColumn) {
166-
storageArrays[index] = ebs;
167+
storageArrays[index - Coords.blockToCube(getWorld().getMinHeight())] = ebs;
167168
return;
168169
}
169170
if (index >= 0 && index < 16) {
@@ -221,6 +222,31 @@ private void cubicChunkColumn_construct(World world, int x, int z, CallbackInfo
221222
Arrays.fill(getBiomeArray(), (byte) -1);
222223
}
223224

225+
@ModifyConstant(method = "<init>(Lnet/minecraft/world/World;II)V", constant = @Constant(intValue = 16))
226+
private int modifySectionArrayLength(int sixteen, World worldIn, int x, int z) {
227+
IMinMaxHeight y = (IMinMaxHeight) worldIn;
228+
return Coords.blockToCube(y.getMaxHeight()) - Coords.blockToCube(y.getMinHeight()) + 1;
229+
}
230+
231+
@Redirect(method = "<init>(Lnet/minecraft/world/World;Lnet/minecraft/world/chunk/ChunkPrimer;II)V",
232+
at = @At(
233+
value = "FIELD",
234+
args = "array=get",
235+
target = "Lnet/minecraft/world/chunk/Chunk;storageArrays:[Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;"
236+
))
237+
private ExtendedBlockStorage init_getStorage(ExtendedBlockStorage[] ebs, int y) {
238+
return ebs[y - Coords.blockToCube(((IMinMaxHeight) world).getMinHeight())];
239+
}
240+
241+
@Redirect(method = "<init>(Lnet/minecraft/world/World;Lnet/minecraft/world/chunk/ChunkPrimer;II)V",
242+
at = @At(
243+
value = "FIELD",
244+
args = "array=set",
245+
target = "Lnet/minecraft/world/chunk/Chunk;storageArrays:[Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;"
246+
))
247+
private void getBlockState_getMaxHeight(ExtendedBlockStorage[] ebs, int y, ExtendedBlockStorage val) {
248+
ebs[y - Coords.blockToCube(((IMinMaxHeight) world).getMinHeight())] = val;
249+
}
224250
@ModifyConstant(method = "<init>(Lnet/minecraft/world/World;Lnet/minecraft/world/chunk/ChunkPrimer;II)V",
225251
constant = @Constant(intValue = 16, ordinal = 0), require = 1)
226252
private int getInitChunkLoopEnd(int _16, World world, ChunkPrimer primer, int x, int z) {
@@ -300,6 +326,17 @@ private void generateSkylightMap_CubicChunks_Replace(CallbackInfo cbi) {
300326
}
301327
}
302328

329+
330+
@Nullable
331+
@Redirect(method = "generateSkylightMap", at = @At(
332+
value = "FIELD",
333+
target = "Lnet/minecraft/world/chunk/Chunk;storageArrays:[Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;",
334+
args = "array=get"
335+
))
336+
private ExtendedBlockStorage generateSkylightMapRedirectEBSAccess(ExtendedBlockStorage[] array, int index) {
337+
return getEBS_CubicChunks(index);
338+
}
339+
303340
// ==============================================
304341
// propagateSkylightOcclusion
305342
// ==============================================
@@ -402,7 +439,7 @@ private boolean getBlockLightOpacity_isChunkLoadedCubeRedirect(Chunk chunk, int
402439
constant = @Constant(expandZeroConditions = Constant.Condition.GREATER_THAN_OR_EQUAL_TO_ZERO),
403440
require = 1)
404441
private int getBlockState_getMinHeight(int zero) {
405-
return isColumn ? Integer.MIN_VALUE : 0;
442+
return isColumn ? Integer.MIN_VALUE : getWorld().getMinHeight(); // this one is in block coords, max is in cube coords. Mojang logic.
406443
}
407444

408445
@Redirect(method = "getBlockState(III)Lnet/minecraft/block/state/IBlockState;",
@@ -412,7 +449,7 @@ private int getBlockState_getMinHeight(int zero) {
412449
target = "Lnet/minecraft/world/chunk/Chunk;storageArrays:[Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;"
413450
))
414451
private int getBlockState_getMaxHeight(ExtendedBlockStorage[] ebs) {
415-
return isColumn ? Integer.MAX_VALUE : ebs.length;
452+
return isColumn ? Integer.MAX_VALUE : (ebs.length - Coords.blockToCube(getWorld().getMinHeight()));
416453
}
417454

418455
@Redirect(method = "getBlockState(III)Lnet/minecraft/block/state/IBlockState;",
@@ -421,7 +458,7 @@ private int getBlockState_getMaxHeight(ExtendedBlockStorage[] ebs) {
421458
args = "array=get",
422459
target = "Lnet/minecraft/world/chunk/Chunk;storageArrays:[Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;"
423460
))
424-
private ExtendedBlockStorage getBlockState_getMaxHeight(ExtendedBlockStorage[] ebs, int y) {
461+
private ExtendedBlockStorage getBlockState_getStorage(ExtendedBlockStorage[] ebs, int y) {
425462
return getEBS_CubicChunks(y);
426463
}
427464

@@ -592,7 +629,7 @@ private int addEntity_getMinY(int zero) {
592629
),
593630
require = 2)
594631
private int addEntity_getMaxHeight(ClassInheritanceMultiMap<?>[] entityLists) {
595-
return isColumn ? blockToCube(getWorld().getMaxHeight()) : entityLists.length;
632+
return isColumn ? blockToCube(getWorld().getMaxHeight()) : (entityLists.length - Coords.blockToCube(getWorld().getMinHeight()));
596633
}
597634

598635
@Redirect(method = "addEntity",
@@ -604,7 +641,7 @@ private int addEntity_getMaxHeight(ClassInheritanceMultiMap<?>[] entityLists) {
604641
require = 1)
605642
private ClassInheritanceMultiMap<?> addEntity_getEntityList(ClassInheritanceMultiMap<?>[] entityLists, int idx, Entity entity) {
606643
if (!isColumn) {
607-
return entityLists[idx];
644+
return entityLists[idx - Coords.blockToCube(getWorld().getMinHeight())];
608645
} else if (cachedCube != null && cachedCube.getY() == idx) {
609646
cachedCube.getEntityContainer().addEntity(entity);
610647
return null;
@@ -652,7 +689,7 @@ private int removeEntityAtIndex_getMinY(int zero) {
652689
),
653690
require = 2)
654691
private int removeEntityAtIndex_getMaxHeight(ClassInheritanceMultiMap<?>[] entityLists) {
655-
return isColumn ? blockToCube(getWorld().getMaxHeight()) : entityLists.length;
692+
return isColumn ? blockToCube(getWorld().getMaxHeight()) : (entityLists.length - Coords.blockToCube(getWorld().getMinHeight()));
656693
}
657694

658695
@Redirect(method = "removeEntityAtIndex",
@@ -665,7 +702,7 @@ private int removeEntityAtIndex_getMaxHeight(ClassInheritanceMultiMap<?>[] entit
665702
private ClassInheritanceMultiMap<?> removeEntityAtIndex_getEntityList(ClassInheritanceMultiMap<?>[] entityLists, int idx, Entity entity,
666703
int index) {
667704
if (!isColumn) {
668-
return entityLists[idx];
705+
return entityLists[idx - Coords.blockToCube(getWorld().getMinHeight())];
669706
} else if (cachedCube != null && cachedCube.getY() == idx) {
670707
cachedCube.getEntityContainer().remove(entity);
671708
return null;

src/main/java/io/github/opencubicchunks/cubicchunks/core/asm/mixin/noncritical/client/MixinViewFrustum_VertViewDistance.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,25 @@
2424
*/
2525
package io.github.opencubicchunks.cubicchunks.core.asm.mixin.noncritical.client;
2626

27+
import io.github.opencubicchunks.cubicchunks.api.util.Coords;
2728
import io.github.opencubicchunks.cubicchunks.api.world.ICubicWorld;
2829
import io.github.opencubicchunks.cubicchunks.core.CubicChunksConfig;
2930
import io.github.opencubicchunks.cubicchunks.core.asm.CubicChunksMixinConfig;
3031
import mcp.MethodsReturnNonnullByDefault;
3132
import net.minecraft.client.renderer.RenderGlobal;
3233
import net.minecraft.client.renderer.ViewFrustum;
34+
import net.minecraft.util.math.BlockPos;
3335
import net.minecraft.world.World;
3436
import org.spongepowered.asm.mixin.Final;
3537
import org.spongepowered.asm.mixin.Mixin;
3638
import org.spongepowered.asm.mixin.Shadow;
3739
import org.spongepowered.asm.mixin.injection.At;
3840
import org.spongepowered.asm.mixin.injection.Constant;
3941
import org.spongepowered.asm.mixin.injection.Inject;
42+
import org.spongepowered.asm.mixin.injection.ModifyArg;
4043
import org.spongepowered.asm.mixin.injection.ModifyConstant;
44+
import org.spongepowered.asm.mixin.injection.ModifyVariable;
45+
import org.spongepowered.asm.mixin.injection.Redirect;
4146
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
4247

4348
import javax.annotation.ParametersAreNonnullByDefault;
@@ -61,10 +66,32 @@ private void onSetCountChunks(int renderDistance, CallbackInfo cbi) {
6166
this.renderDistance = (CubicChunksMixinConfig.BoolOptions.VERT_RENDER_DISTANCE.getValue() ?
6267
CubicChunksConfig.verticalCubeLoadDistance : renderDistance) * 2 + 1;
6368
} else {
64-
this.renderDistance = 16;
69+
ICubicWorld world = (ICubicWorld) this.world;
70+
this.renderDistance = Coords.blockToCube(world.getMaxHeight()) - Coords.blockToCube(world.getMinHeight()) + 1;
6571
}
6672
}
6773

74+
@ModifyArg(method = "updateChunkPositions", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/chunk/RenderChunk;setPosition"
75+
+ "(III)V"), index = 1)
76+
private int modifyRenderChunkPosWhenUpdatingPositions(int y) {
77+
return y + ((ICubicWorld) world).getMinHeight();
78+
}
79+
80+
@ModifyVariable(method = "markBlocksForUpdate", at = @At("HEAD"), argsOnly = true, index = 2)
81+
private int modifyMinYForUpdate(int minY) {
82+
return minY - ((ICubicWorld) world).getMinHeight();
83+
}
84+
85+
@ModifyVariable(method = "markBlocksForUpdate", at = @At("HEAD"), argsOnly = true, index = 5)
86+
private int modifyMaxYForUpdate(int maxY) {
87+
return maxY - ((ICubicWorld) world).getMinHeight();
88+
}
89+
90+
@Redirect(method = "getRenderChunk", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/BlockPos;getY()I"))
91+
private int modifyMaxYForUpdate(BlockPos instance) {
92+
return instance.getY() - ((ICubicWorld) world).getMinHeight();
93+
}
94+
6895
@ModifyConstant(method = "setCountChunksXYZ", constant = @Constant(intValue = 16))
6996
private int getYViewDistance(int oldDistance) {
7097
return renderDistance;

0 commit comments

Comments
 (0)