Skip to content

Commit a4eedad

Browse files
authored
fix(inventory): do not extend slot lifetimes (#884)
1 parent 24fff2e commit a4eedad

1 file changed

Lines changed: 57 additions & 48 deletions

File tree

crates/hyperion/src/simulation/inventory.rs

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{borrow::Cow, mem::transmute};
1+
use std::borrow::Cow;
22

33
use flecs_ecs::{
44
core::{EntityView, EntityViewGet, QueryBuilderImpl, SystemAPI, TermBuilderImpl, World, flecs},
@@ -117,7 +117,7 @@ impl Module for InventoryModule {
117117
world,
118118
&Compose($),
119119
&mut PlayerInventory,
120-
&mut InventoryState,
120+
&InventoryState,
121121
&Position,
122122
&CursorItem,
123123
?&OpenInventory,
@@ -185,61 +185,70 @@ impl Module for InventoryModule {
185185
.unwrap();
186186
}
187187

188-
let mut inventories_mut: Vec<&mut ItemSlot> = Vec::new();
189188
if let Some(open_inventory) = open_inventory {
190189
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+
);
195198
});
196-
}
197-
198-
if inventories_mut.is_empty() {
199-
inventory
200-
.slots_mut()
201-
.iter_mut()
202-
.for_each(|slot| inventories_mut.push(slot));
203199
} 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+
);
208208
}
209+
}
210+
);
211+
}
212+
}
209213

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+
});
228234

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+
}
231240

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();
238243

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();
243252
}
244253
}
245254

0 commit comments

Comments
 (0)