ghast_inventory/inventory/
screens.rs1use super::*;
5
6impl Inventory {
7 pub fn on_trade_offers(&mut self, window_id: i32, offers: Vec<crate::merchant::TradeOffer>) {
13 if self.open_id() == window_id
14 && let Some(mer) = self.m.merchant.as_deref_mut()
15 {
16 mer.offers = Some(offers);
17 }
18 }
19
20 pub fn on_open_window_v1_16_5(&mut self, id: i32, menu_type: i32, title: ByteString) {
24 self.on_open_window(id, WindowKind::from_v1_16_5(menu_type), title);
25 if self.open_id() == id {
26 self.m.furnace = match menu_type {
27 9 => 1,
28 21 => 2,
29 _ => 0,
30 };
31 }
32 }
33
34 pub(super) fn station_window_items(&mut self, before: Vec<Option<Stack>>) {
38 let Some(st) = self.m.station.as_deref() else {
39 return;
40 };
41 let inputs: Vec<bool> = (0..before.len()).map(|k| st.is_input(k)).collect();
42 let new = std::mem::replace(&mut self.m.container, before);
43 let lay = self.layout();
44 let mut sim = Sim {
45 v: self.version,
46 lay,
47 m: &mut self.m,
48 book: &self.book,
49 binding_curse: None,
50 tags: self.tags16.as_deref(),
51 };
52 for (k, s) in new.into_iter().enumerate() {
53 if k >= sim.m.container.len() {
54 break;
55 }
56 sim.m.container[k] = s;
57 if inputs[k] {
58 sim.station_touched(Loc::Container(k));
59 sim.station_touched(Loc::Container(k));
60 }
61 }
62 self.m.container_result_known = true;
63 }
64
65 pub(super) fn station_inbound(&mut self, k: Option<usize>) {
68 let Some(st) = self.m.station.as_deref() else {
69 return;
70 };
71 let out = st.output();
72 if k.is_some_and(|k| !st.is_input(k)) {
73 self.m.container_result_known = true;
75 return;
76 }
77 let keep = self.m.container.get(out).cloned();
78 let lay = self.layout();
79 let mut sim = Sim {
80 v: self.version,
81 lay,
82 m: &mut self.m,
83 book: &self.book,
84 binding_curse: None,
85 tags: self.tags16.as_deref(),
86 };
87 sim.station_touched(Loc::Container(0));
89 sim.station_touched(Loc::Container(0));
90 if k.is_none()
91 && let (Some(slot), Some(keep)) = (self.m.container.get_mut(out), keep)
92 {
93 *slot = keep;
94 self.m.container_result_known = true;
95 }
96 }
97
98 pub fn take_screen_packets(&mut self) -> Vec<Outgoing> {
102 std::mem::take(&mut self.m.screen_out)
103 }
104
105 pub fn set_experience_level(&mut self, level: i32) {
108 self.m.xp_level = level;
109 #[cfg(modern)]
110 if self.version.is_modern() {
111 self.set_experience_level_modern(level);
112 }
113 }
114
115 #[must_use]
117 pub fn experience_level(&self) -> i32 {
118 self.m.xp_level
119 }
120
121 pub fn set_creative(&mut self, creative: bool) {
124 self.m.creative = creative;
125 #[cfg(modern)]
126 if self.version.is_modern() {
127 self.set_creative_modern(creative);
128 }
129 }
130
131 #[must_use]
133 pub fn anvil_cost(&self) -> Option<i32> {
134 match self.m.station.as_deref() {
135 Some(crate::station::Station::Anvil(a)) => Some(a.cost),
136 _ => None,
137 }
138 }
139
140 pub fn anvil_rename(&mut self, name: &str) -> Result<Option<Outgoing>, ClickError> {
151 #[cfg(modern)]
152 if self.version.is_modern() {
153 return self.anvil_rename_modern(name);
154 }
155 if !matches!(
156 self.m.station.as_deref(),
157 Some(crate::station::Station::Anvil(_))
158 ) {
159 return Err(ClickError::NoScreen);
160 }
161 let field = if self.version == Version::V1_8 {
162 30
163 } else {
164 35
165 };
166 if name.chars().count() > field
167 || name
168 .chars()
169 .any(|c| c == '\u{a7}' || c < ' ' || c == '\u{7f}')
170 {
171 return Err(ClickError::InvalidSlot);
172 }
173 let editable = matches!(
175 self.m.station.as_deref(),
176 Some(crate::station::Station::Anvil(a)) if a.tracked.is_some()
177 );
178 if (name.is_empty() && self.version == Version::V1_16_5) || !editable {
180 return Ok(None);
181 }
182 let lay = self.layout();
183 let mut m = self.m.clone();
184 let mut sim = Sim {
185 v: self.version,
186 lay,
187 m: &mut m,
188 book: &self.book,
189 binding_curse: None,
190 tags: self.tags16.as_deref(),
191 };
192 sim.anvil_set_name(name.to_owned());
194 if !m.container_result_known {
195 return Err(ClickError::Unpredictable);
196 }
197 let out = std::mem::take(&mut m.screen_out);
198 self.m = m;
199 self.events.window_changed = true;
200 Ok(out.into_iter().last())
201 }
202
203 pub fn click_button(&mut self, index: i32) -> Result<Option<Outgoing>, ClickError> {
210 use crate::station::Station;
211 let id = self.open_id();
212 let lay = self.layout();
213 if matches!(&self.screen, ScreenState::Window(w) if w.kind == WindowKind::Lectern)
216 && self.version == Version::V1_16_5
217 {
218 return Ok(
219 matches!(index, 1..=3 | 100..).then_some(Outgoing::ButtonClick {
220 window_id: id,
221 button: index,
222 }),
223 );
224 }
225 if let Some(Station::Loom { .. }) = self.m.station.as_deref() {
226 let before = self.m.container.clone();
227 let mut sim = Sim {
228 v: self.version,
229 lay,
230 m: &mut self.m,
231 book: &self.book,
232 binding_curse: None,
233 tags: self.tags16.as_deref(),
234 };
235 let ok = sim.loom_button(index);
236 if self.m.container != before {
237 self.events.window_changed = true;
238 }
239 return Ok(ok.then_some(Outgoing::ButtonClick {
240 window_id: id,
241 button: index,
242 }));
243 }
244 let Some(Station::Stonecutter {
245 recipes, selected, ..
246 }) = self.m.station.as_deref_mut()
247 else {
248 return Err(ClickError::NoScreen);
249 };
250 if usize::try_from(index).map_or(true, |k| k >= recipes.len()) {
251 return Ok(None);
252 }
253 *selected = index;
254 let before = self.m.container.clone();
255 let mut sim = Sim {
256 v: self.version,
257 lay,
258 m: &mut self.m,
259 book: &self.book,
260 binding_curse: None,
261 tags: self.tags16.as_deref(),
262 };
263 sim.stonecutter_populate();
264 if self.m.container != before {
265 self.events.window_changed = true;
266 }
267 Ok(Some(Outgoing::ButtonClick {
268 window_id: id,
269 button: index,
270 }))
271 }
272
273 pub fn set_mount_rules(&mut self, rules: Option<crate::mount::MountRules>) {
281 if matches!(&self.screen, ScreenState::Window(w) if matches!(w.kind, WindowKind::Horse { .. }))
282 {
283 self.m.mount = rules;
284 }
285 }
286
287 #[must_use]
289 pub fn mount_rules(&self) -> Option<crate::mount::MountRules> {
290 self.m.mount
291 }
292
293 #[cfg(any(feature = "v1_8", feature = "v1_12"))]
295 pub fn on_trade_list_legacy(&mut self, data: &bytes::Bytes) {
296 if let Some((w, offers)) = crate::merchant::parse_legacy(data) {
297 self.on_trade_offers(w, offers);
298 }
299 }
300
301 #[cfg(feature = "v1_16_5")]
305 pub fn on_tags_v1_16_5(&mut self, p: &ghast_protocol::v1_16_5::play::clientbound::Tags) {
306 self.tags16 = Some(std::sync::Arc::new(
307 crate::tags16::ItemTags16::from_item_tags(
308 self.version,
309 p.item_tags.iter().map(|t| (&*t.tag_name, &t.entries[..])),
310 ),
311 ));
312 }
313
314 #[cfg(feature = "v1_16_5")]
316 pub fn on_trade_list_v1_16_5(
317 &mut self,
318 p: &ghast_protocol::v1_16_5::play::clientbound::TradeList,
319 ) {
320 self.on_trade_offers(p.window_id, crate::merchant::from_v1_16_5(p));
321 }
322
323 #[must_use]
326 pub fn trade_offers(&self) -> Option<&[crate::merchant::TradeOffer]> {
327 self.m.merchant.as_ref()?.offers.as_deref()
328 }
329
330 pub fn select_trade(&mut self, index: i32) -> Result<Outgoing, ClickError> {
341 #[cfg(modern)]
342 if self.version.is_modern() {
343 return self.select_trade_modern(index);
344 }
345 let v = self.version;
346 let Some(mer) = self.m.merchant.as_deref_mut() else {
347 return Err(ClickError::NoScreen);
348 };
349 let n = mer.offers.as_ref().map_or(0, Vec::len);
350 if usize::try_from(index).map_or(true, |k| k >= n) {
351 return Err(ClickError::InvalidSlot);
352 }
353 mer.index = index;
354 crate::merchant::update(v, &mut self.m);
355 if v == Version::V1_16_5 {
356 let lay = self.layout();
357 let before = self.m.clone();
358 let mut sim = Sim {
359 v,
360 lay,
361 m: &mut self.m,
362 book: &self.book,
363 binding_curse: None,
364 tags: self.tags16.as_deref(),
365 };
366 sim.switch_trade(index as usize);
367 if self.m.container != before.container {
368 self.events.window_changed = true;
369 }
370 if self.m.player != before.player {
371 self.events.player_changed = true;
372 }
373 }
374 Ok(Outgoing::SelectTrade(index))
375 }
376}