Skip to content

Commit

Permalink
feat: add useful methods to OtherChunkAccessibleContext
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 18, 2024
1 parent 59de5b2 commit 9b6de74
Showing 1 changed file with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.allaymc.api.world.generator.context;

import lombok.Getter;
import org.allaymc.api.block.type.BlockState;
import org.allaymc.api.world.chunk.ChunkAccessible;
import org.allaymc.api.world.chunk.UnsafeChunk;

Expand All @@ -18,4 +19,47 @@ public OtherChunkAccessibleContext(UnsafeChunk currentChunk, ChunkAccessible chu
super(currentChunk);
this.chunkAccessor = chunkAccessor;
}

public void setBlockState(int x, int y, int z, BlockState blockState) {
setBlockState(x, y, z, blockState, 0);
}

public void setBlockState(int x, int y, int z, BlockState blockState, int layer) {
if(isInCurrentChunk(x, y, z)) {
currentChunk.setBlockState(x & 15, y, z & 15, blockState, layer);
return;
}
setBlockStateInOtherChunk(x, y, z, blockState, layer);
}

private void setBlockStateInOtherChunk(int x, int y, int z, BlockState blockState, int layer) {
var chunk = chunkAccessor.getChunk(x >> 4, z >> 4);
chunk.setBlockState(x & 15, y, z & 15, blockState, layer);
}

public BlockState getBlockState(int x, int y, int z) {
return getBlockState(x, y, z, 0);
}

public BlockState getBlockState(int x, int y, int z, int layer) {
if(isInCurrentChunk(x, y, z)) {
return currentChunk.getBlockState(x & 15, y, z & 15, layer);
}
return getBlockStateInOtherChunk(x, y, z, layer);
}

private BlockState getBlockStateInOtherChunk(int x, int y, int z, int layer) {
var chunk = chunkAccessor.getChunk(x >> 4, z >> 4);
return chunk.getBlockState(x & 15, y, z & 15);
}

private boolean isInCurrentChunk(int x, int y, int z) {
var currentChunkX = currentChunk.getX();
var currentChunkZ = currentChunk.getZ();
var dimInfo = currentChunk.getDimensionInfo();
return
x >= currentChunkX * 16 && x < currentChunkX * 16 + 16 &&
z >= currentChunkZ * 16 && z < currentChunkZ * 16 + 16 &&
y >= dimInfo.minHeight() && y <= dimInfo.maxHeight();
}
}

0 comments on commit 9b6de74

Please sign in to comment.