ghast_inventory/inventory/hands.rs
1//! Client-side changes to the held stacks without a click: consuming, damaging, finishing into
2//! a container, exchanging, equipping, dropping and selecting the hotbar slot.
3
4use super::*;
5
6impl Inventory {
7 /// Client-side use of the held stack (no packet): `n` items off the selected hotbar stack,
8 /// the slot emptied when nothing is left. Vanilla does this when a right click places a block
9 /// or plants seeds (1.8 `BlockItem` / `SeedItem.use`, 1.16.5 `BlockItem.place`) or
10 /// when eating finishes, before the server's `set_slot`; call it then (not in creative,
11 /// where vanilla keeps the count). Returns whether there was a stack.
12 pub fn consume_held(&mut self, n: u8) -> bool {
13 self.consume(usize::from(self.m.selected.min(8)), n)
14 }
15
16 /// Client-side durability loss of the held stack (no packet; 1.8 / 1.12 `ItemStack.damage`
17 /// without Unbreaking, which rolls a random number): the damage goes up by `amount`; past
18 /// the durability the stack breaks (one item less, damage 0). Vanilla does this when a hit
19 /// lands on a living entity (a sword 1, a tool 2), before the server's `set_slot`. Returns
20 /// whether the stack took the damage (1.16.5 / 26.x keep the damage in NBT / components:
21 /// `false`, nothing changes).
22 pub fn damage_held(&mut self, amount: u16) -> bool {
23 if !matches!(self.version, Version::V1_8 | Version::V1_12) {
24 return false;
25 }
26 let v = self.version;
27 let k = usize::from(self.m.selected.min(8));
28 let slot = &mut self.m.player[k];
29 let Some(s) = slot.as_mut() else {
30 return false;
31 };
32 let Some(max) = s.max_damage(v).filter(|&m| m > 0) else {
33 return false;
34 };
35 if !s.is_damageable(v) {
36 return false;
37 }
38 let d = i32::from(s.damage) + i32::from(amount);
39 if d > max {
40 s.count -= 1;
41 s.damage = 0;
42 if s.count <= 0 {
43 *slot = None;
44 }
45 } else {
46 s.damage = d as i16;
47 }
48 self.events.player_changed = true;
49 true
50 }
51
52 /// [`consume_held`](Inventory::consume_held) for the offhand stack (1.12 / 1.16.5; no-op on
53 /// 1.8).
54 pub fn consume_offhand(&mut self, n: u8) -> bool {
55 self.version != Version::V1_8 && self.consume(40, n)
56 }
57
58 /// Client-side end of a drink or a stew (`onItemUseFinish`, no packet): the last item of
59 /// the stack in the main hand (or the off hand, 1.12+) becomes `container` (a glass bottle,
60 /// a bucket, a bowl; one of it); a stack of more loses one item. Returns whether there
61 /// was a stack.
62 pub fn finish_into(&mut self, off_hand: bool, container: i32) -> bool {
63 if off_hand && self.version == Version::V1_8 {
64 return false;
65 }
66 let k = if off_hand {
67 40
68 } else {
69 usize::from(self.m.selected.min(8))
70 };
71 let slot = &mut self.m.player[k];
72 let Some(s) = slot.as_mut() else {
73 return false;
74 };
75 if s.count > 1 {
76 s.count -= 1;
77 } else {
78 *slot = Some(Stack::new(container, 1, 0));
79 }
80 self.events.player_changed = true;
81 true
82 }
83
84 /// 1.16.5 `ItemUsage.exchangeStack` (Yarn `method_30270`) on the client, after an item use
85 /// in the air of the main hand (or the off hand) turned one item into `output` (a bucket
86 /// filled, a bottle filled, a map drawn; no packet): outside creative one item is used up;
87 /// the last one becomes `output`, else `output` goes into the inventory
88 /// (`PlayerInventory.insertStack`). In creative with `creative_keeps` the hand keeps its
89 /// stack and `output` goes in only when no stack of its item is there yet; in creative what
90 /// does not fit is gone. Returns whether `output` did not fit outside creative (vanilla drops
91 /// it: the client swings its main hand).
92 pub fn exchange_hand(
93 &mut self,
94 off_hand: bool,
95 output: Stack,
96 creative: bool,
97 creative_keeps: bool,
98 ) -> bool {
99 let k = if off_hand {
100 40
101 } else {
102 usize::from(self.m.selected.min(8))
103 };
104 self.events.player_changed = true;
105 if creative && creative_keeps {
106 let id = output.id;
107 if !self.m.player.iter().flatten().any(|s| s.id == id) {
108 let _ = self.insert_player(output);
109 }
110 return false;
111 }
112 if !creative {
113 let _ = self.consume(k, 1);
114 }
115 if self.m.player[k].is_none() {
116 self.m.player[k] = Some(output);
117 return false;
118 }
119 self.insert_player(output) > 0 && !creative
120 }
121
122 /// Sets the main hand's (or the off hand's) stack on the client (no packet).
123 pub fn set_hand(&mut self, off_hand: bool, stack: Option<Stack>) {
124 let k = if off_hand {
125 40
126 } else {
127 usize::from(self.m.selected.min(8))
128 };
129 self.m.player[k] = stack;
130 self.events.player_changed = true;
131 }
132
133 /// `PlayerInventory.insertStack` into the player's own inventory; returns the count left.
134 pub(super) fn insert_player(&mut self, s: Stack) -> i32 {
135 let lay = Layout::new(self.version, Screen::Player);
136 let mut sim = Sim {
137 v: self.version,
138 lay,
139 m: &mut self.m,
140 book: &self.book,
141 binding_curse: None,
142 tags: self.tags16.as_deref(),
143 };
144 sim.insert_stack(s)
145 }
146
147 pub(super) fn consume(&mut self, k: usize, n: u8) -> bool {
148 let slot = &mut self.m.player[k];
149 let Some(s) = slot.as_mut() else {
150 return false;
151 };
152 s.count -= i32::from(n);
153 if s.count <= 0 {
154 *slot = None;
155 }
156 self.events.player_changed = true;
157 true
158 }
159
160 /// What a right-click use of the stack in the main hand (or the off hand, 1.12+) does to
161 /// the armor on the client (no packet; vanilla's item `use`): `true` when it equips the
162 /// stack. 1.8 / 1.12.2 / 1.16.5 (`ArmorItem` / `ElytraItem`): only into an empty armor
163 /// slot. 26.x (a swappable `equippable`): also swaps with the piece worn, unless that is
164 /// the same item with the same components or carries the binding curse outside creative.
165 /// `creative` is the game mode.
166 #[must_use]
167 pub fn use_equips(&self, off_hand: bool, creative: bool) -> bool {
168 self.use_equip_plan(off_hand, creative).is_some()
169 }
170
171 /// The hand's player index and the armor index its use equips.
172 pub(super) fn use_equip_plan(&self, off_hand: bool, creative: bool) -> Option<(usize, usize)> {
173 let hand = if off_hand {
174 if self.version == Version::V1_8 {
175 return None;
176 }
177 40
178 } else {
179 usize::from(self.m.selected.min(8))
180 };
181 let s = self.m.player[hand].as_ref().filter(|s| s.count > 0)?;
182 let a = self.version.use_equip_target(s)?;
183 let worn_k = 39 - usize::from(a);
184 match &self.m.player[worn_k] {
185 None => Some((hand, worn_k)),
186 Some(_)
187 if !matches!(
188 self.version,
189 Version::V26_1 | Version::V26_2 | Version::V26_3
190 ) =>
191 {
192 None
193 }
194 // Equippable.swapWithEquipmentSlot: FAIL for the same item or a cursed piece.
195 Some(w) => (!w.same_item(s)
196 && (creative || self.enchantment_level(w, "binding_curse") == 0))
197 .then_some((hand, worn_k)),
198 }
199 }
200
201 /// Applies the right-click equip of [`Inventory::use_equips`] like the vanilla client,
202 /// before the server's slot updates: 1.8 – 1.16.5 the stack moves into the armor slot and
203 /// the hand empties (creative too); 26.x a single item swaps with the worn piece (in
204 /// creative a copy goes on and the hand keeps its stack when the slot was empty), a larger
205 /// stack puts one on and keeps the rest (not predicted when a worn piece would have to go
206 /// into the inventory). Returns whether the use equips (the server's answer then follows).
207 pub fn equip_from_hand(&mut self, off_hand: bool, creative: bool) -> bool {
208 let Some((hand, worn_k)) = self.use_equip_plan(off_hand, creative) else {
209 return false;
210 };
211 let modern = matches!(
212 self.version,
213 Version::V26_1 | Version::V26_2 | Version::V26_3
214 );
215 let worn = self.m.player[worn_k].take();
216 let held = self.m.player[hand].take().expect("planned with a stack");
217 if !modern {
218 self.m.player[worn_k] = Some(held);
219 } else if held.count <= 1 {
220 let to_hand = match (&worn, creative) {
221 (None, true) => Some(held.clone()),
222 _ => worn,
223 };
224 self.m.player[worn_k] = Some(held);
225 self.m.player[hand] = to_hand;
226 } else if worn.is_some() {
227 // Inventory.add of the old piece: left to the server's updates.
228 self.m.player[worn_k] = worn;
229 self.m.player[hand] = Some(held);
230 return true;
231 } else {
232 let mut one = held.clone();
233 one.count = 1;
234 self.m.player[worn_k] = Some(one);
235 let mut rest = held;
236 if !creative {
237 rest.count -= 1;
238 }
239 self.m.player[hand] = Some(rest);
240 }
241 self.events.player_changed = true;
242 true
243 }
244
245 /// Client-side drop of the held stack (no packet): one item off the selected hotbar stack,
246 /// or all of it with `whole_stack`, the slot emptied when nothing is left. The 1.16.5 / 26.1
247 /// drop key does this before the server's `set_slot` (`ClientPlayerEntity.dropSelectedItem`,
248 /// `LocalPlayer.drop`: `removeStack(selected, whole ? count : 1)`); 1.8 / 1.12 do not.
249 /// Returns whether there was a stack (vanilla then swings the arm).
250 pub fn drop_held(&mut self, whole_stack: bool) -> bool {
251 let slot = &mut self.m.player[usize::from(self.m.selected.min(8))];
252 let Some(s) = slot.as_mut() else {
253 return false;
254 };
255 if s.count <= 0 {
256 return false;
257 }
258 if whole_stack || s.count == 1 {
259 *slot = None;
260 } else {
261 s.count -= 1;
262 }
263 self.events.player_changed = true;
264 true
265 }
266
267 /// Selects a hotbar slot (the behaviour's `select_slot`; vanilla's insertStack prefers it).
268 pub fn select(&mut self, slot: u8) {
269 if slot < 9 {
270 self.m.selected = slot;
271 self.m.player.touch();
272 }
273 }
274}