Inventory draw: a slot is drawn from a reference, not a copy

draw_slot_item needs neither ownership nor &mut self: it reads the carried
stack only for a drag. Taking `&Option<ItemStack>` and &self drops the copy
every caller made, including the two the creative hotbar made per slot each
frame (and one clone per hovered slot becomes a clone only when hovered).

Co-Authored-By: Hermes Agent (Nous Research) <noreply@nousresearch.com>
This commit is contained in:
Alexander
2026-10-28 10:40:11 +03:00
parent b6c936f82b
commit 626068da3a
3 changed files with 13 additions and 16 deletions
+11 -14
View File
@@ -5766,30 +5766,28 @@ impl Game<'_> {
/// highlight (its count yellow when the slot caps it), and hides while
/// it is the drag's only slot.
fn draw_slot_item(
&mut self,
&self,
batch: &mut HudBatch,
drag: Option<&DragView>,
slot: usize,
stack: Option<ItemStack>,
stack: &Option<ItemStack>,
x: f32,
y: f32,
) {
let carried;
let (stack, capped) = match drag.and_then(|d| d.slots.get(&slot)) {
Some(None) => return,
Some(Some((count, capped))) => {
batch.fill(&self.gui.atlas, x, y, 16.0, 16.0, rgba(255, 255, 255, 128));
let carried = self
.carried
.clone()
.map(|c| ItemStack { count: *count, ..c });
(carried, *capped)
carried = self.carried.clone().map(|c| ItemStack { count: *count, ..c });
(carried.as_ref(), *capped)
}
None => (stack, false),
None => (stack.as_ref(), false),
};
if let Some(stack) = stack {
self.draw_item_model(batch, &stack, x, y);
self.draw_item_model(batch, stack, x, y);
let text = capped.then(|| format!("\u{a7}e{}", stack.count));
self.draw_item_decorations(batch, &stack, x, y, text);
self.draw_item_decorations(batch, stack, x, y, text);
}
}
/// `extractCarriedItem`: the carried stack at the cursor, showing what a
@@ -6407,7 +6405,7 @@ impl Game<'_> {
);
}
let stack = self.slot_view(i);
self.draw_slot_item(batch, drag.as_ref(), i, stack, ox + sx, oy + sy);
self.draw_slot_item(batch, drag.as_ref(), i, &stack, ox + sx, oy + sy);
}
}
fn draw_inventory(&mut self, batch: &mut HudBatch) {
@@ -11200,15 +11198,14 @@ impl Game<'_> {
let drag = self.drag_view();
for c in 0..9 {
let (x, y) = (ox + 9.0 + c as f32 * 18.0, oy + 112.0);
let stack = self.inventory[c].clone();
self.draw_slot_item(batch, drag.as_ref(), c, stack.clone(), x, y);
self.draw_slot_item(batch, drag.as_ref(), c, &self.inventory[c], x, y);
if self.cursor.0 >= x
&& self.cursor.0 < x + 16.0
&& self.cursor.1 >= y
&& self.cursor.1 < y + 16.0
{
batch.fill(&self.gui.atlas, x, y, 16.0, 16.0, rgba(255, 255, 255, 128));
hovered = stack;
hovered = self.inventory[c].clone();
}
}
let scrollable = self.creative_screen.rows(&self.pack.names) > 5;
+1 -1
View File
@@ -1643,7 +1643,7 @@ impl Game<'_> {
continue;
};
let stack = self.slot_mut(i).clone();
self.draw_slot_item(batch, drag.as_ref(), i, stack, ox + sx, oy + sy);
self.draw_slot_item(batch, drag.as_ref(), i, &stack, ox + sx, oy + sy);
}
if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) {
let (sx, sy) = self.slot_position(slot);
+1 -1
View File
@@ -783,7 +783,7 @@ impl Game<'_> {
for i in slots {
let (sx, sy) = self.slot_position(i);
let stack = self.slot_mut(i).clone();
self.draw_slot_item(batch, drag.as_ref(), i, stack, ox + sx, oy + sy);
self.draw_slot_item(batch, drag.as_ref(), i, &stack, ox + sx, oy + sy);
}
if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) {
let (sx, sy) = self.slot_position(slot);