public void ChunkGetSectionTest() { var chunk = new Chunk(_regionMock.Object, 0, 0, _serviceProvider); IChunkSection section = chunk.GetSection(0); Assert.NotNull(section); Assert.IsType <ChunkSection>(section); Assert.Equal(0, section.Index); }
public IChunkSection GetSection(int sectionIndex) { IChunkSection section = _chunkSections.ElementAtOrDefault(sectionIndex); if (section is null) { throw new InvalidOperationException($"Failed to get section with index '{sectionIndex}'."); } return(section); }
public void Serialize(IMinecraftPacket packet, bool fullChunk = false) { packet.WriteInt32(X); packet.WriteInt32(Z); packet.WriteBoolean(fullChunk); int mask = 0; // if full chunk using var chunkStream = new MinecraftPacket(); for (int i = 0; i < Sections.Count(); i++) { IChunkSection section = Sections.ElementAt(i); if (fullChunk || section.IsDirty) { mask |= 1 << i; section.Serialize(chunkStream); } } packet.WriteVarInt32(mask); // Heightmap serialization var heightmapCompound = new NbtCompound("") { new NbtLongArray("MOTION_BLOCKING", Heightmap.ToArray()), new NbtLongArray("WORLD_SURFACE", WorldSurfaceHeightmap.ToArray()) }; var nbtFile = new NbtFile(heightmapCompound); packet.WriteBytes(nbtFile.GetBuffer()); // Biomes if (fullChunk) { packet.WriteVarInt32(1024); for (int i = 0; i < 1024; i++) { packet.WriteVarInt32(0); } } chunkStream.Position = 0; packet.WriteVarInt32((int)chunkStream.Length); packet.WriteBytes(chunkStream.BaseBuffer); packet.WriteVarInt32(0); // block count // TODO: foreach block in blocks in chunk as NBT }
public IBlock GetBlock(int x, int y, int z) { if (y < 0) { throw new InvalidOperationException($"Cannot get a block with a negative Y value."); } if (y > Height) { throw new InvalidOperationException($"Cannot get a block with a Y value higher than {Height}."); } int sectionIndex = GetSectionIndex(y); IChunkSection section = GetSection(sectionIndex); return(section.GetBlock(x, y % Size, z)); }
public IBlock SetBlock(BlockType blockType, int x, int y, int z) { if (x < 0 || x >= Size) { throw new InvalidOperationException("X position is out of chunk bounds."); } if (z < 0 || z >= Size) { throw new InvalidOperationException("Z position is out of chunk bounds."); } IChunkSection section = GetChunkSection(y); IBlock block = section.SetBlock(blockType, x, y % Size, z); // TODO: improve heightmap generation when placing a block. GenerateHeightMap(); return(block); }