Improve anvil block loading performance

This commit is contained in:
TheMode 2021-07-20 23:40:23 +02:00
parent cb21b0baf5
commit bad206d504

View File

@ -88,8 +88,7 @@ public class AnvilLoader implements IChunkLoader {
loadBlocks(chunk, fileChunk); loadBlocks(chunk, fileChunk);
loadTileEntities(chunk, fileChunk); loadTileEntities(chunk, fileChunk);
// Lights // Lights
final var chunkSections = fileChunk.getSections(); for (var chunkSection : fileChunk.getSections()) {
for (var chunkSection : chunkSections) {
Section section = chunk.getSection(chunkSection.getY()); Section section = chunk.getSection(chunkSection.getY());
section.setSkyLight(chunkSection.getSkyLights()); section.setSkyLight(chunkSection.getSkyLights());
section.setBlockLight(chunkSection.getBlockLights()); section.setBlockLight(chunkSection.getBlockLights());
@ -116,23 +115,29 @@ public class AnvilLoader implements IChunkLoader {
} }
private void loadBlocks(Chunk chunk, ChunkColumn fileChunk) { private void loadBlocks(Chunk chunk, ChunkColumn fileChunk) {
for (int x = 0; x < Chunk.CHUNK_SIZE_X; x++) { for (var section : fileChunk.getSections()) {
for (int z = 0; z < Chunk.CHUNK_SIZE_Z; z++) { if (section.getEmpty()) {
for (int y = 0; y < 256; y++) { // TODO don't hardcode height continue;
try { }
final BlockState blockState = fileChunk.getBlockState(x, y, z); final int yOffset = Chunk.CHUNK_SECTION_SIZE * section.getY();
Block block = Block.fromNamespaceId(blockState.getName()); for (int x = 0; x < Chunk.CHUNK_SECTION_SIZE; x++) {
if (block == null) { for (int z = 0; z < Chunk.CHUNK_SECTION_SIZE; z++) {
// Invalid block for (int y = 0; y < Chunk.CHUNK_SECTION_SIZE; y++) {
continue; try {
final BlockState blockState = section.get(x, y, z);
Block block = Block.fromNamespaceId(blockState.getName());
if (block == null) {
// Invalid block
continue;
}
final var properties = blockState.getProperties();
if (!properties.isEmpty()) {
block = block.withProperties(properties);
}
chunk.setBlock(x, y + yOffset, z, block);
} catch (Exception e) {
EXCEPTION_MANAGER.handleException(e);
} }
final var properties = blockState.getProperties();
if (!properties.isEmpty()) {
block = block.withProperties(properties);
}
chunk.setBlock(x, y, z, block);
} catch (Exception e) {
EXCEPTION_MANAGER.handleException(e);
} }
} }
} }