mirror of
https://github.com/italicsjenga/valence.git
synced 2024-12-23 22:41:30 +11:00
add swap_slot
to Inventory
(#168)
This effectively exposes the slice `swap` method: https://doc.rust-lang.org/std/primitive.slice.html#method.swap
This commit is contained in:
parent
ae7eaf15fc
commit
8b50e93b0e
|
@ -140,6 +140,21 @@ impl<C: Config> Inventory<C> {
|
||||||
mem::replace(old, new)
|
mem::replace(old, new)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn swap_slot(&mut self, idx_a: u16, idx_b: u16) {
|
||||||
|
assert!(idx_a < self.slot_count(), "slot index out of range");
|
||||||
|
assert!(idx_b < self.slot_count(), "slot index out of range");
|
||||||
|
|
||||||
|
if idx_a == idx_b || self.slots[idx_a as usize] == self.slots[idx_b as usize] {
|
||||||
|
// Nothing to do here, ignore.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.modified |= 1 << idx_a;
|
||||||
|
self.modified |= 1 << idx_b;
|
||||||
|
|
||||||
|
self.slots.swap(idx_a as usize, idx_b as usize);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn slot_count(&self) -> u16 {
|
pub fn slot_count(&self) -> u16 {
|
||||||
self.slots.len() as u16
|
self.slots.len() as u16
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue