@@ -17,9 +17,9 @@ pub const EntityDataClass = struct {
17
17
18
18
const VTable = struct {
19
19
onLoadClient : * const fn (pos : Vec3i , chunk : * Chunk ) void ,
20
- onUnloadClient : * const fn (pos : Vec3i , chunk : * Chunk ) void ,
20
+ onUnloadClient : * const fn (dataIndex : u32 ) void ,
21
21
onLoadServer : * const fn (pos : Vec3i , chunk : * Chunk ) void ,
22
- onUnloadServer : * const fn (pos : Vec3i , chunk : * Chunk ) void ,
22
+ onUnloadServer : * const fn (dataIndex : u32 ) void ,
23
23
onPlaceClient : * const fn (pos : Vec3i , chunk : * Chunk ) void ,
24
24
onBreakClient : * const fn (pos : Vec3i , chunk : * Chunk ) void ,
25
25
onPlaceServer : * const fn (pos : Vec3i , chunk : * Chunk ) void ,
@@ -44,14 +44,14 @@ pub const EntityDataClass = struct {
44
44
pub inline fn onLoadClient (self : * EntityDataClass , pos : Vec3i , chunk : * Chunk ) void {
45
45
return self .vtable .onLoadClient (pos , chunk );
46
46
}
47
- pub inline fn onUnloadClient (self : * EntityDataClass , pos : Vec3i , chunk : * Chunk ) void {
48
- return self .vtable .onUnloadClient (pos , chunk );
47
+ pub inline fn onUnloadClient (self : * EntityDataClass , dataIndex : u32 ) void {
48
+ return self .vtable .onUnloadClient (dataIndex );
49
49
}
50
50
pub inline fn onLoadServer (self : * EntityDataClass , pos : Vec3i , chunk : * Chunk ) void {
51
51
return self .vtable .onLoadServer (pos , chunk );
52
52
}
53
- pub inline fn onUnloadServer (self : * EntityDataClass , pos : Vec3i , chunk : * Chunk ) void {
54
- return self .vtable .onUnloadServer (pos , chunk );
53
+ pub inline fn onUnloadServer (self : * EntityDataClass , dataIndex : u32 ) void {
54
+ return self .vtable .onUnloadServer (dataIndex );
55
55
}
56
56
pub inline fn onPlaceClient (self : * EntityDataClass , pos : Vec3i , chunk : * Chunk ) void {
57
57
return self .vtable .onPlaceClient (pos , chunk );
@@ -107,6 +107,20 @@ fn BlockEntityDataStorage(comptime side: enum {client, server}, T: type) type {
107
107
chunk .blockPosToEntityDataMap .put (main .globalAllocator .allocator , blockIndex , @intCast (dataIndex )) catch unreachable ;
108
108
chunk .blockPosToEntityDataMapMutex .unlock ();
109
109
}
110
+ pub fn removeByIndex (dataIndex : u32 ) void {
111
+ main .utils .assertLocked (& mutex );
112
+
113
+ _ = storage .swapRemove (dataIndex );
114
+ if (dataIndex == storage .items .len ) {
115
+ return ;
116
+ }
117
+
118
+ const movedEntry = storage .items [dataIndex ];
119
+ switch (side ) {
120
+ .server = > propagateRemoveServer (movedEntry .absoluteBlockPosition , dataIndex ),
121
+ .client = > propagateRemoveClient (movedEntry .absoluteBlockPosition , dataIndex ),
122
+ }
123
+ }
110
124
pub fn remove (pos : Vec3i , chunk : * Chunk ) void {
111
125
mutex .lock ();
112
126
defer mutex .unlock ();
@@ -122,17 +136,7 @@ fn BlockEntityDataStorage(comptime side: enum {client, server}, T: type) type {
122
136
return ;
123
137
};
124
138
125
- const dataIndex = entry .value ;
126
- _ = storage .swapRemove (dataIndex );
127
- if (dataIndex == storage .items .len ) {
128
- return ;
129
- }
130
-
131
- const movedEntry = storage .items [dataIndex ];
132
- switch (side ) {
133
- .server = > propagateRemoveServer (movedEntry .absoluteBlockPosition , dataIndex ),
134
- .client = > propagateRemoveClient (movedEntry .absoluteBlockPosition , dataIndex ),
135
- }
139
+ removeByIndex (entry .value );
136
140
}
137
141
fn propagateRemoveServer (pos : Vec3i , index : u32 ) void {
138
142
const severChunk = server .world .? .getChunkFromCacheAndIncreaseRefCount (ChunkPosition .initFromWorldPos (pos , 1 )).? ;
@@ -192,9 +196,9 @@ pub const EntityDataClasses = struct {
192
196
}
193
197
194
198
pub fn onLoadClient (_ : Vec3i , _ : * Chunk ) void {}
195
- pub fn onUnloadClient (_ : Vec3i , _ : * Chunk ) void {}
199
+ pub fn onUnloadClient (_ : u32 ) void {}
196
200
pub fn onLoadServer (_ : Vec3i , _ : * Chunk ) void {}
197
- pub fn onUnloadServer (_ : Vec3i , _ : * Chunk ) void {}
201
+ pub fn onUnloadServer (_ : u32 ) void {}
198
202
pub fn onPlaceClient (_ : Vec3i , _ : * Chunk ) void {}
199
203
pub fn onBreakClient (_ : Vec3i , _ : * Chunk ) void {}
200
204
pub fn onPlaceServer (_ : Vec3i , _ : * Chunk ) void {}
@@ -238,12 +242,22 @@ pub const EntityDataClasses = struct {
238
242
}
239
243
240
244
pub fn onLoadClient (_ : Vec3i , _ : * Chunk ) void {}
241
- pub fn onUnloadClient (_ : Vec3i , _ : * Chunk ) void {}
245
+ pub fn onUnloadClient (dataIndex : u32 ) void {
246
+ StorageClient .mutex .lock ();
247
+ defer StorageClient .mutex .unlock ();
248
+ StorageClient .removeByIndex (dataIndex );
249
+ }
242
250
pub fn onLoadServer (_ : Vec3i , _ : * Chunk ) void {}
243
- pub fn onUnloadServer (_ : Vec3i , _ : * Chunk ) void {}
244
- pub fn onPlaceClient (_ : Vec3i , _ : * Chunk ) void {}
245
- pub fn onBreakClient (_ : Vec3i , _ : * Chunk ) void {}
246
- pub fn onPlaceServer (_ : Vec3i , _ : * Chunk ) void {}
251
+ pub fn onUnloadServer (_ : u32 ) void {}
252
+ pub fn onPlaceClient (pos : Vec3i , chunk : * Chunk ) void {
253
+ StorageClient .add (pos , .{.text = "test" }, chunk );
254
+ }
255
+ pub fn onBreakClient (pos : Vec3i , chunk : * Chunk ) void {
256
+ StorageClient .remove (pos , chunk );
257
+ }
258
+ pub fn onPlaceServer (pos : Vec3i , chunk : * Chunk ) void {
259
+ StorageServer .add (pos , .{.text = "test" }, chunk );
260
+ }
247
261
pub fn onBreakServer (pos : Vec3i , chunk : * Chunk ) void {
248
262
StorageServer .remove (pos , chunk );
249
263
}
0 commit comments