async def fetch_chunk(self, chunk_x: int, chunk_z: int) -> Chunk: key = (chunk_x, chunk_z) try: # try to fetch chunk from cache return self._chunk_cache[key] except KeyError: pass try: # try to fetch from disk return self.cache_chunk( await self.server.chunkio.fetch_chunk_async(self.path, *key), key) except FileNotFoundError: # fall back to generate chunk sections = self.server.generator.generate_chunk( self.data["RandomSeed"], self.dimension, chunk_x, chunk_z) chunk = Chunk.new(chunk_x, chunk_z, sections, int(time.time())) return self.cache_chunk(chunk)
def generate_chunk(seed: int, dimension: str, chunk_x: int, chunk_z: int) -> Chunk: chunk = Chunk.new(chunk_x, chunk_z, int(time.time())) chunk.sections[0] = ChunkSection.new(0, DirectPalette) palette = chunk.sections[0].palette if dimension == "minecraft:overworld": chunk.sections[0].block_states[0] = palette.encode( "minecraft:bedrock") chunk.sections[0].block_states[1:3] = palette.encode( "minecraft:dirt") chunk.sections[0].block_states[3] = palette.encode( "minecraft:grass_block", {"snowy": "false"}) chunk.sections[0].block_light[0:4] = 0 chunk.sections[0].sky_light[0:3] = 0 chunk.sections[0].sky_light[3:] = 15 elif dimension == "minecraft:nether": chunk.sections[0].block_states[0] = palette.encode( "minecraft:bedrock") chunk.sections[0].block_states[1:4] = palette.encode( "minecraft:netherrack") chunk.sections[0].block_light[0:4] = 0 chunk.sections[0].sky_light[4:] = 7 elif dimension == "minecraft:the_end": chunk.sections[0].block_states[0:4] = palette.encode( "minecraft:end_stone") chunk.sections[0].block_light[0:4] = 0 chunk.sections[0].sky_light[4:] = 0 else: raise ValueError(f"Unsupported dimension: {dimension}") return chunk