public Chunk GetChunk(int x, int z, ChunkStatus status, bool nonnull) { if (Thread.CurrentThread != _mainThread) { throw new Exception("Wrong Thread"); } var positionHash = ChunkPos.AsLong(x, z); for (int i = 0; i < 4; i++) { if (_recentPositions[i] != positionHash || _recentStatuses[i] != status) { continue; } var recentChunk = _recentChunks[i]; if (recentChunk != null || !nonnull) { return(recentChunk); } } var task = ProvideChunk(x, z, status, nonnull); var chunk = task.GetAwaiter().GetResult(); if (chunk == null && nonnull) { throw new Exception("Chunk not there when requested"); } UpdateCache(positionHash, chunk, status); return(chunk); }