using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace MinecraftClient.Mapping { /// /// Represent a chunk of terrain in a Minecraft world /// public class Chunk { public const int SizeX = 16; public const int SizeY = 16; public const int SizeZ = 16; /// /// Blocks contained into the chunk /// private readonly Block[,,] blocks = new Block[SizeX, SizeY, SizeZ]; /// /// Lock for thread safety /// private readonly ReaderWriterLockSlim blockLock = new ReaderWriterLockSlim(); /// /// Read, or set the specified block /// /// Block X /// Block Y /// Block Z /// chunk at the given location public Block this[int blockX, int blockY, int blockZ] { get { if (blockX < 0 || blockX >= SizeX) throw new ArgumentOutOfRangeException("blockX", "Must be between 0 and " + (SizeX - 1) + " (inclusive)"); if (blockY < 0 || blockY >= SizeY) throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)"); if (blockZ < 0 || blockZ >= SizeZ) throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)"); blockLock.EnterReadLock(); try { return blocks[blockX, blockY, blockZ]; } finally { blockLock.ExitReadLock(); } } set { if (blockX < 0 || blockX >= SizeX) throw new ArgumentOutOfRangeException("blockX", "Must be between 0 and " + (SizeX - 1) + " (inclusive)"); if (blockY < 0 || blockY >= SizeY) throw new ArgumentOutOfRangeException("blockY", "Must be between 0 and " + (SizeY - 1) + " (inclusive)"); if (blockZ < 0 || blockZ >= SizeZ) throw new ArgumentOutOfRangeException("blockZ", "Must be between 0 and " + (SizeZ - 1) + " (inclusive)"); blockLock.EnterWriteLock(); try { blocks[blockX, blockY, blockZ] = value; } finally { blockLock.ExitWriteLock(); } } } /// /// Get block at the specified location /// /// Location, a modulo will be applied /// The block public Block GetBlock(Location location) { return this[location.ChunkBlockX, location.ChunkBlockY, location.ChunkBlockZ]; } } }