22using System . Collections . Generic ;
33using System . Linq ;
44using System . Text ;
5+ using System . Threading ;
56
67namespace MinecraftClient . Mapping
78{
@@ -15,6 +16,11 @@ public class World
1516 /// </summary>
1617 private Dictionary < int , Dictionary < int , ChunkColumn > > chunks = new Dictionary < int , Dictionary < int , ChunkColumn > > ( ) ;
1718
19+ /// <summary>
20+ /// Lock for thread safety
21+ /// </summary>
22+ private readonly ReaderWriterLockSlim chunksLock = new ReaderWriterLockSlim ( ) ;
23+
1824 /// <summary>
1925 /// Read, set or unload the specified chunk column
2026 /// </summary>
@@ -25,34 +31,50 @@ public class World
2531 {
2632 get
2733 {
28- //Read a chunk
29- if ( chunks . ContainsKey ( chunkX ) )
30- if ( chunks [ chunkX ] . ContainsKey ( chunkZ ) )
31- return chunks [ chunkX ] [ chunkZ ] ;
32- return null ;
34+ chunksLock . EnterReadLock ( ) ;
35+ try
36+ {
37+ //Read a chunk
38+ if ( chunks . ContainsKey ( chunkX ) )
39+ if ( chunks [ chunkX ] . ContainsKey ( chunkZ ) )
40+ return chunks [ chunkX ] [ chunkZ ] ;
41+ return null ;
42+ }
43+ finally
44+ {
45+ chunksLock . ExitReadLock ( ) ;
46+ }
3347 }
3448 set
3549 {
36- if ( value != null )
37- {
38- //Update a chunk column
39- if ( ! chunks . ContainsKey ( chunkX ) )
40- chunks [ chunkX ] = new Dictionary < int , ChunkColumn > ( ) ;
41- chunks [ chunkX ] [ chunkZ ] = value ;
42- }
43- else
50+ chunksLock . EnterWriteLock ( ) ;
51+ try
4452 {
45- //Unload a chunk column
46- if ( chunks . ContainsKey ( chunkX ) )
53+ if ( value != null )
4754 {
48- if ( chunks [ chunkX ] . ContainsKey ( chunkZ ) )
55+ //Update a chunk column
56+ if ( ! chunks . ContainsKey ( chunkX ) )
57+ chunks [ chunkX ] = new Dictionary < int , ChunkColumn > ( ) ;
58+ chunks [ chunkX ] [ chunkZ ] = value ;
59+ }
60+ else
61+ {
62+ //Unload a chunk column
63+ if ( chunks . ContainsKey ( chunkX ) )
4964 {
50- chunks [ chunkX ] . Remove ( chunkZ ) ;
51- if ( chunks [ chunkX ] . Count == 0 )
52- chunks . Remove ( chunkX ) ;
65+ if ( chunks [ chunkX ] . ContainsKey ( chunkZ ) )
66+ {
67+ chunks [ chunkX ] . Remove ( chunkZ ) ;
68+ if ( chunks [ chunkX ] . Count == 0 )
69+ chunks . Remove ( chunkX ) ;
70+ }
5371 }
5472 }
5573 }
74+ finally
75+ {
76+ chunksLock . ExitWriteLock ( ) ;
77+ }
5678 }
5779 }
5880
@@ -117,7 +139,7 @@ public List<Location> FindBlock(Location from, Material block, int radiusx, int
117139 {
118140 Location doneloc = new Location ( x , y , z ) ;
119141 Block doneblock = GetBlock ( doneloc ) ;
120- Material blockType = GetBlock ( doneloc ) . Type ;
142+ Material blockType = doneblock . Type ;
121143 if ( blockType == block )
122144 {
123145 list . Add ( doneloc ) ;
@@ -150,7 +172,15 @@ public void SetBlock(Location location, Block block)
150172 /// </summary>
151173 public void Clear ( )
152174 {
153- chunks = new Dictionary < int , Dictionary < int , ChunkColumn > > ( ) ;
175+ chunksLock . EnterWriteLock ( ) ;
176+ try
177+ {
178+ chunks = new Dictionary < int , Dictionary < int , ChunkColumn > > ( ) ;
179+ }
180+ finally
181+ {
182+ chunksLock . ExitWriteLock ( ) ;
183+ }
154184 }
155185
156186 /// <summary>
0 commit comments