55using Sandbox . Game . Entities ;
66using Sandbox . Game . Entities . Cube ;
77using Sandbox . Game . World ;
8+ using SharpDX . Toolkit . Collections ;
89using Torch . Collections ;
910using Torch . Server . ViewModels . Blocks ;
1011using VRage . Game ;
@@ -76,7 +77,7 @@ private void RemoveBlock(MyCubeBlock block)
7677 Blocks . Remove ( block . BlockDefinition ) ;
7778
7879 long ownerId = block . OwnerId ;
79- if ( block . OwnerId == 0 )
80+ if ( block . OwnerId == 0 && block . IDModule != null ) // IDModule shouldn't be null on a terminal block but it has happened.
8081 ownerId = block . IDModule . Owner ;
8182
8283 if ( ownerId != 0 )
@@ -95,26 +96,35 @@ private void RemoveBlock(MyCubeBlock block)
9596
9697 private void AddBlock ( MyTerminalBlock block )
9798 {
98- if ( ! Blocks . TryGetValue ( block . BlockDefinition , out var group ) )
99- group = Blocks [ block . BlockDefinition ] = new MtObservableSortedDictionary < long , BlockViewModel > ( ) ;
100- group . Add ( block . EntityId , new BlockViewModel ( block , Tree ) ) ;
99+ try
100+ {
101+ if ( ! Blocks . TryGetValue ( block . BlockDefinition , out var group ) )
102+ group = Blocks [ block . BlockDefinition ] = new MtObservableSortedDictionary < long , BlockViewModel > ( ) ;
103+ group . Add ( block . EntityId , new BlockViewModel ( block , Tree ) ) ;
101104
102- long ownerId = block . OwnerId ;
103- if ( block . OwnerId == 0 )
104- ownerId = block . IDModule . Owner ;
105+ long ownerId = block . OwnerId ;
106+ if ( block . OwnerId == 0 && block . IDModule != null )
107+ ownerId = block . IDModule . Owner ;
105108
106- if ( ownerId != 0 )
107- {
108- var playerIdent = MySession . Static . Players . TryGetIdentity ( ownerId ) ;
109- if ( playerIdent != null )
109+ if ( ownerId != 0 )
110110 {
111- var name = playerIdent . DisplayName ;
112- if ( ! Owners . TryGetValue ( name , out int count ) )
113- Owners [ name ] = 1 ;
114- else
115- Owners [ name ] = count + 1 ;
111+ var playerIdent = MySession . Static . Players . TryGetIdentity ( ownerId ) ;
112+ if ( playerIdent != null )
113+ {
114+ var name = playerIdent . DisplayName ;
115+ if ( ! Owners . TryGetValue ( name , out int count ) )
116+ Owners [ name ] = 1 ;
117+ else
118+ Owners [ name ] = count + 1 ;
119+ }
116120 }
117121 }
122+ catch ( Exception )
123+ {
124+ // Used MtObservableSortedDictionary for Owners collection, which threw errors on some blocks.
125+ // Switched to ObservableDictionary and left the try/catch just in case...
126+ }
127+
118128 }
119129
120130 public int BlockCount
@@ -127,7 +137,7 @@ public int BlockCount
127137 }
128138 }
129139
130- public MtObservableSortedDictionary < string , int > Owners { get ; } = new MtObservableSortedDictionary < string , int > ( ) ;
140+ public ObservableDictionary < string , int > Owners { get ; } = new ObservableDictionary < string , int > ( ) ;
131141
132142 private void Grid_OnBlockAdded ( MySlimBlock obj )
133143 {
0 commit comments