|
1 | | -use std::{borrow::Cow, mem::transmute}; |
| 1 | +use std::borrow::Cow; |
2 | 2 |
|
3 | 3 | use flecs_ecs::{ |
4 | 4 | core::{EntityView, EntityViewGet, QueryBuilderImpl, SystemAPI, TermBuilderImpl, World, flecs}, |
@@ -117,7 +117,7 @@ impl Module for InventoryModule { |
117 | 117 | world, |
118 | 118 | &Compose($), |
119 | 119 | &mut PlayerInventory, |
120 | | - &mut InventoryState, |
| 120 | + &InventoryState, |
121 | 121 | &Position, |
122 | 122 | &CursorItem, |
123 | 123 | ?&OpenInventory, |
@@ -185,61 +185,70 @@ impl Module for InventoryModule { |
185 | 185 | .unwrap(); |
186 | 186 | } |
187 | 187 |
|
188 | | - let mut inventories_mut: Vec<&mut ItemSlot> = Vec::new(); |
189 | 188 | if let Some(open_inventory) = open_inventory { |
190 | 189 | open_inventory.entity.entity_view(world).get::<&mut Inventory>(|open_inv| { |
191 | | - (unsafe { transmute::<&mut Inventory, &mut Inventory>(open_inv) }) |
192 | | - .slots_mut() |
193 | | - .iter_mut() |
194 | | - .for_each(|slot| inventories_mut.push(slot)); |
| 190 | + update_player_inventory_inner( |
| 191 | + compose, |
| 192 | + stream_id, |
| 193 | + system, |
| 194 | + inv_state, |
| 195 | + cursor_item, |
| 196 | + open_inv.slots_mut().iter_mut().chain(inventory.slots_inventory_mut().iter_mut()), |
| 197 | + ); |
195 | 198 | }); |
196 | | - } |
197 | | - |
198 | | - if inventories_mut.is_empty() { |
199 | | - inventory |
200 | | - .slots_mut() |
201 | | - .iter_mut() |
202 | | - .for_each(|slot| inventories_mut.push(slot)); |
203 | 199 | } else { |
204 | | - inventory |
205 | | - .slots_inventory_mut() |
206 | | - .iter_mut() |
207 | | - .for_each(|slot| inventories_mut.push(slot)); |
| 200 | + update_player_inventory_inner( |
| 201 | + compose, |
| 202 | + stream_id, |
| 203 | + system, |
| 204 | + inv_state, |
| 205 | + cursor_item, |
| 206 | + inventory.slots_mut().iter_mut(), |
| 207 | + ); |
208 | 208 | } |
| 209 | + } |
| 210 | + ); |
| 211 | + } |
| 212 | +} |
209 | 213 |
|
210 | | - let mut bundle = DataBundle::new(compose, system); |
211 | | - let mut changed_slots = false; |
212 | | - let window_id = i8::try_from(inv_state.window_id()).unwrap(); |
213 | | - for (idx, slot) in inventories_mut.iter_mut().enumerate() { |
214 | | - if slot.changed { |
215 | | - let idx = i16::try_from(idx).unwrap(); |
216 | | - let packet = &(play::ScreenHandlerSlotUpdateS2c { |
217 | | - window_id, |
218 | | - state_id: VarInt(inv_state.state_id()), |
219 | | - slot_idx: idx, |
220 | | - slot_data: Cow::Borrowed(&slot.stack), |
221 | | - }); |
222 | | - |
223 | | - bundle.add_packet(packet).unwrap(); |
224 | | - slot.changed = false; |
225 | | - changed_slots = true; |
226 | | - } |
227 | | - } |
| 214 | +fn update_player_inventory_inner<'a>( |
| 215 | + compose: &Compose, |
| 216 | + stream_id: ConnectionId, |
| 217 | + system: EntityView<'_>, |
| 218 | + inv_state: &InventoryState, |
| 219 | + cursor_item: &CursorItem, |
| 220 | + inventories_mut: impl Iterator<Item = &'a mut ItemSlot>, |
| 221 | +) { |
| 222 | + let mut bundle = DataBundle::new(compose, system); |
| 223 | + let mut changed_slots = false; |
| 224 | + let window_id = i8::try_from(inv_state.window_id()).unwrap(); |
| 225 | + for (idx, slot) in inventories_mut.enumerate() { |
| 226 | + if slot.changed { |
| 227 | + let idx = i16::try_from(idx).unwrap(); |
| 228 | + let packet = &(play::ScreenHandlerSlotUpdateS2c { |
| 229 | + window_id, |
| 230 | + state_id: VarInt(inv_state.state_id()), |
| 231 | + slot_idx: idx, |
| 232 | + slot_data: Cow::Borrowed(&slot.stack), |
| 233 | + }); |
228 | 234 |
|
229 | | - if changed_slots { |
230 | | - bundle.unicast(stream_id).unwrap(); |
| 235 | + bundle.add_packet(packet).unwrap(); |
| 236 | + slot.changed = false; |
| 237 | + changed_slots = true; |
| 238 | + } |
| 239 | + } |
231 | 240 |
|
232 | | - let packet = &(play::ScreenHandlerSlotUpdateS2c { |
233 | | - window_id: -1, |
234 | | - state_id: VarInt(inv_state.state_id()), |
235 | | - slot_idx: -1, |
236 | | - slot_data: Cow::Borrowed(&cursor_item.0), |
237 | | - }); |
| 241 | + if changed_slots { |
| 242 | + bundle.unicast(stream_id).unwrap(); |
238 | 243 |
|
239 | | - compose.unicast(packet, stream_id, system).unwrap(); |
240 | | - } |
241 | | - } |
242 | | - ); |
| 244 | + let packet = &(play::ScreenHandlerSlotUpdateS2c { |
| 245 | + window_id: -1, |
| 246 | + state_id: VarInt(inv_state.state_id()), |
| 247 | + slot_idx: -1, |
| 248 | + slot_data: Cow::Borrowed(&cursor_item.0), |
| 249 | + }); |
| 250 | + |
| 251 | + compose.unicast(packet, stream_id, system).unwrap(); |
243 | 252 | } |
244 | 253 | } |
245 | 254 |
|
|
0 commit comments