private void HandleMapChunk(int x, short y, int z, MCBlock[, ,] data) { if (OnMapChunk != null) OnMapChunk(this, new MapChunkEventArgs(x, y, z, data)); }
private void ParseMapChunk(BinaryReader reader) { int x = reader.ReadNetworkInt32(); short y = reader.ReadNetworkInt16(); int z = reader.ReadNetworkInt32(); byte size_x = reader.ReadByte(); byte size_y = reader.ReadByte(); byte size_z = reader.ReadByte(); int size = reader.ReadNetworkInt32(); if (reader.BaseStream.Position + size > reader.BaseStream.Length) // Like omg BinaryReader won't throw exceptions throw new EndOfStreamException("Reader has reached end of stream."); // if you read past the end of the stream :@ byte[] data = Compression.DecompressZLib(reader.ReadBytes(size)); // parse data block size_x++; size_y++; size_z++; // wat to do here... hmm MCBlock[, ,] blockdata = new MCBlock[size_x, size_y, size_z]; BinaryReader parser = new BinaryReader(new MemoryStream(data)); byte[] blocks = parser.ReadBytes(size_x * size_y * size_z); byte[] metadatas = new byte[blocks.Length]; byte[] lights = new byte[blocks.Length]; byte[] skylights = new byte[blocks.Length]; for (int i = 0; i < blocks.Length; i += 2) { byte metadata = parser.ReadByte(); metadatas[i] = (byte)((metadata >> 4) & 0xF); metadatas[i + 1] = (byte)(metadata & 0xF); } for (int i = 0; i < blocks.Length; i += 2) { byte light = parser.ReadByte(); lights[i] = (byte)((light >> 4) & 0xF); lights[i + 1] = (byte)(light & 0xF); } for (int i = 0; i < blocks.Length; i += 2) { byte skylight = parser.ReadByte(); skylights[i] = (byte)((skylight >> 4) & 0xF); skylights[i + 1] = (byte)(skylight & 0xF); } for (int iX = 0; iX < size_x; iX++) { for (int iY = 0; iY < size_y; iY++) { for (int iZ = 0; iZ < size_z; iZ++) { int index = iY + (iZ * size_y) + (iX * size_y * size_z); blockdata[iX, iY, iZ] = new MCBlock(); blockdata[iX, iY, iZ].Type = (MCBlockType)blocks[index]; blockdata[iX, iY, iZ].MetaData = metadatas[index]; blockdata[iX, iY, iZ].Light = lights[index]; blockdata[iX, iY, iZ].SkyLight = skylights[index]; } } } HandleMapChunk(x, y, z, blockdata); }
public MapChunkEventArgs(int x, short y, int z, MCBlock[,,] data) : base() { Position = new Point3D(x, y, z); ChunkData = data; }