Skip to content

Commit

Permalink
Use sub chunk request system
Browse files Browse the repository at this point in the history
  • Loading branch information
valaphee committed Feb 5, 2023
1 parent 2e9e92c commit b394c13
Showing 1 changed file with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,23 @@ public void translate(GeyserSession session, SubChunkRequestPacket packet) {
subChunkData.setPosition(positionOffset);
subChunkPacket.getSubChunks().add(subChunkData);

// Should never happen, but if no caching is enabled, send undefined result
if (!session.getChunkCache().isCache()) {
subChunkData.setResult(SubChunkRequestResult.UNDEFINED);
subChunkData.setData(new byte[0]);
subChunkData.setHeightMapType(HeightMapDataType.NO_DATA);
continue;
}

// Check dimension
if (packet.getDimension() != DimensionUtils.javaToBedrock(session.getDimension())) {
subChunkData.setResult(SubChunkRequestResult.INVALID_DIMENSION);
subChunkData.setData(new byte[0]);
subChunkData.setHeightMapType(HeightMapDataType.NO_DATA);
continue;
}

// Check if chunk is cached
Vector3i position = centerPosition.add(positionOffset);
GeyserChunk chunk = session.getChunkCache().getChunk(position.getX(), position.getZ());
if (chunk == null) {
Expand All @@ -108,6 +111,7 @@ public void translate(GeyserSession session, SubChunkRequestPacket packet) {
continue;
}

// Check if chunk y index is in range, adjust for Java vs. Bedrock y offset
int sectionY = position.getY() - javaSubChunkOffset;
if (position.getY() < bedrockSubChunkMinY || position.getY() >= bedrockSubChunkMaxY) {
subChunkData.setResult(SubChunkRequestResult.INDEX_OUT_OF_BOUNDS);
Expand All @@ -116,16 +120,21 @@ public void translate(GeyserSession session, SubChunkRequestPacket packet) {
continue;
}

// Ignore if its belows Java Edition min height
if (sectionY < 0) {
subChunkData.setHeightMapType(HeightMapDataType.NO_DATA);
} else {
LightUpdateData lightData = chunk.lightData();
BitSet emptyLightMask = lightData.getEmptySkyYMask();
BitSet lightMask = lightData.getSkyYMask();
List<byte[]> lightData_ = lightData.getSkyUpdates();
// This will calculate a light-blocking height map, based on Java Editions
// sky-light
LightUpdateData lightUpdateData = chunk.lightData();
BitSet emptyLightMask = lightUpdateData.getEmptySkyYMask();
BitSet lightMask = lightUpdateData.getSkyYMask();
List<byte[]> lightData = lightUpdateData.getSkyUpdates();
// Check if its empty (aka. the height map is too high/section is underground)
if (emptyLightMask.get(sectionY + 1)) {
subChunkData.setHeightMapType(HeightMapDataType.TOO_HIGH);
} else if (lightMask.get(sectionY + 1)) {
// If there is light data, get the light data for below the current section or null
byte[] belowLight;
if (lightMask.get(sectionY)) {
int belowSection = 0;
Expand All @@ -134,17 +143,21 @@ public void translate(GeyserSession session, SubChunkRequestPacket packet) {
belowSection++;
}
}
belowLight = lightData_.get(belowSection);
belowLight = lightData.get(belowSection);
} else {
belowLight = null;
}

// Get the light data for the current section
int lightIndex = 0;
for (int i = 0; i < sectionY + 1; i++) {
if (lightMask.get(i)) {
lightIndex++;
}
}
byte[] light = lightData_.get(lightIndex);
byte[] light = lightData.get(lightIndex);

// Get the light data for above the current section or null
byte[] aboveLight;
if (lightMask.get(sectionY + 2)) {
int aboveSection = 0;
Expand All @@ -153,11 +166,12 @@ public void translate(GeyserSession session, SubChunkRequestPacket packet) {
aboveSection++;
}
}
aboveLight = lightData_.get(aboveSection);
aboveLight = lightData.get(aboveSection);
} else {
aboveLight = null;
}

// Iterate through all columns, and get the row where sky-light is blocked
byte[] heightMapData = new byte[16 * 16];
boolean lower = true, higher = true;
xyLoop: for (int i = 0; i < heightMapData.length; i++) {
Expand Down Expand Up @@ -195,6 +209,8 @@ public void translate(GeyserSession session, SubChunkRequestPacket packet) {
}
}
}

// Check if everything is lower, or higher, as there is no need to send the height map data
if (lower) {
subChunkData.setHeightMapType(HeightMapDataType.TOO_LOW);
} else if (higher) {
Expand Down

0 comments on commit b394c13

Please sign in to comment.