feat: use OtherChunkAccessibleContext directly

This commit is contained in:
daoge_cmd
2024-06-18 14:59:42 +08:00
parent 4a3678cea9
commit e68f928e38
2 changed files with 10 additions and 34 deletions
@@ -4,6 +4,7 @@ import com.dfsek.terra.api.util.vector.Vector3;
import org.allaymc.api.world.chunk.ChunkAccessible; import org.allaymc.api.world.chunk.ChunkAccessible;
import org.allaymc.api.world.chunk.UnsafeChunk; import org.allaymc.api.world.chunk.UnsafeChunk;
import org.allaymc.api.world.generator.context.OtherChunkAccessibleContext;
import org.allaymc.terra.allay.Mapping; import org.allaymc.terra.allay.Mapping;
import com.dfsek.terra.api.block.entity.BlockEntity; import com.dfsek.terra.api.block.entity.BlockEntity;
@@ -21,16 +22,16 @@ import com.dfsek.terra.api.world.chunk.generation.ProtoWorld;
* *
* @author daoge_cmd * @author daoge_cmd
*/ */
public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk centerChunk, ChunkAccessible chunkAccessor) implements ProtoWorld { public record AllayProtoWorld(AllayServerWorld allayServerWorld, OtherChunkAccessibleContext context) implements ProtoWorld {
@Override @Override
public int centerChunkX() { public int centerChunkX() {
return centerChunk.getX(); return context.getCurrentChunk().getX();
} }
@Override @Override
public int centerChunkZ() { public int centerChunkZ() {
return centerChunk.getZ(); return context.getCurrentChunk().getZ();
} }
@Override @Override
@@ -40,16 +41,13 @@ public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk cen
@Override @Override
public void setBlockState(int x, int y, int z, BlockState data, boolean physics) { public void setBlockState(int x, int y, int z, BlockState data, boolean physics) {
if(isInCurrentChunk(x, y, z)) { context.setBlockState(x & 15, y, z & 15, ((AllayBlockState)data).allayBlockState());
centerChunk.setBlockState(x & 15, y, z & 15, ((AllayBlockState)data).allayBlockState());
} else {
setBlockStateInOtherChunk(x, y, z, data, physics);
}
} }
private void setBlockStateInOtherChunk(int x, int y, int z, BlockState data, boolean physics) { @Override
var chunk = chunkAccessor.getChunk(x >> 4, z >> 4); public BlockState getBlockState(int x, int y, int z) {
chunk.setBlockState(x & 15, y, z & 15, ((AllayBlockState)data).allayBlockState()); var blockState = context.getBlockState(x & 15, y, z & 15);
return new AllayBlockState(blockState, Mapping.blockStateBeToJe(blockState));
} }
@Override @Override
@@ -57,20 +55,6 @@ public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk cen
return new AllayFakeEntity(Vector3.of(x, y, z), allayServerWorld); return new AllayFakeEntity(Vector3.of(x, y, z), allayServerWorld);
} }
@Override
public BlockState getBlockState(int x, int y, int z) {
if(isInCurrentChunk(x, y, z)) {
var blockState = centerChunk.getBlockState(x & 15, y, z & 15);
return new AllayBlockState(blockState, Mapping.blockStateBeToJe(blockState));
}
return getBlockStateInOtherChunk(x, y, z);
}
private BlockState getBlockStateInOtherChunk(int x, int y, int z) {
var chunk = chunkAccessor.getChunk(x >> 4, z >> 4);
return new AllayBlockState(chunk.getBlockState(x & 15, y, z & 15), Mapping.blockStateBeToJe(chunk.getBlockState(x & 15, y, z & 15)));
}
@Override @Override
public BlockEntity getBlockEntity(int x, int y, int z) { public BlockEntity getBlockEntity(int x, int y, int z) {
// TODO // TODO
@@ -111,11 +95,4 @@ public record AllayProtoWorld(AllayServerWorld allayServerWorld, UnsafeChunk cen
public AllayServerWorld getHandle() { public AllayServerWorld getHandle() {
return allayServerWorld; return allayServerWorld;
} }
private boolean isInCurrentChunk(int x, int y, int z) {
return
x >= centerChunkX() * 16 && x < centerChunkX() * 16 + 16 &&
z >= centerChunkZ() * 16 && z < centerChunkZ() * 16 + 16 &&
y >= getMinHeight() && y <= getMaxHeight();
}
} }
@@ -125,8 +125,7 @@ public class AllayGeneratorWrapper implements GeneratorWrapper {
@Override @Override
public Boolean apply(PopulateContext context) { public Boolean apply(PopulateContext context) {
var chunk = context.getCurrentChunk(); var tmp = new AllayProtoWorld(allayServerWorld, context);
var tmp = new AllayProtoWorld(allayServerWorld, chunk, context.getChunkAccessor());
try { try {
for (var generationStage : configPack.getStages()) { for (var generationStage : configPack.getStages()) {
generationStage.populate(tmp); generationStage.populate(tmp);