1#[cfg(feature = "v1_8")]
4use ghast_protocol::v1_8::play::{clientbound as cb8, serverbound as sb8};
5#[cfg(feature = "v1_12")]
6use ghast_protocol::v1_12_2::play::{clientbound as cb12, serverbound as sb12};
7#[cfg(feature = "v1_16_5")]
8use ghast_protocol::v1_16_5::play::{clientbound as cb16, serverbound as sb16};
9
10#[allow(unused_imports)]
11use crate::inventory::{ClickPacket, Inventory, Outgoing};
12#[allow(unused_imports)]
13use crate::stack::{from_legacy_slot, from_modern_slot, to_legacy_slot, to_modern_slot};
14#[allow(unused_imports)]
15use crate::window::WindowKind;
16
17impl ClickPacket {
18 #[cfg(feature = "v1_8")]
20 #[must_use]
21 pub fn to_v1_8(&self) -> sb8::WindowClick {
22 sb8::WindowClick {
23 window_id: self.window_id,
24 slot: self.slot,
25 mouse_button: self.button as i8,
26 action: self.action,
27 mode: self.mode as i8,
28 item: to_legacy_slot(self.item.as_ref()),
29 }
30 }
31
32 #[cfg(feature = "v1_12")]
34 #[must_use]
35 pub fn to_v1_12_2(&self) -> sb12::WindowClick {
36 sb12::WindowClick {
37 window_id: self.window_id,
38 slot: self.slot,
39 mouse_button: self.button as i8,
40 action: self.action,
41 mode: self.mode as i8,
42 item: to_legacy_slot(self.item.as_ref()),
43 }
44 }
45
46 #[cfg(feature = "v1_16_5")]
48 #[must_use]
49 pub fn to_v1_16_5(&self) -> sb16::WindowClick {
50 sb16::WindowClick {
51 window_id: self.window_id,
52 slot: self.slot,
53 mouse_button: self.button as i8,
54 action: self.action,
55 mode: self.mode as i8,
56 item: to_modern_slot(self.item.as_ref()),
57 }
58 }
59}
60
61#[cfg(any(feature = "v1_8", feature = "v1_12"))]
63fn mc_string(s: &str) -> bytes::Bytes {
64 let mut out = Vec::with_capacity(s.len() + 5);
65 let mut n = s.len() as u32;
66 loop {
67 let b = (n & 0x7f) as u8;
68 n >>= 7;
69 if n == 0 {
70 out.push(b);
71 break;
72 }
73 out.push(b | 0x80);
74 }
75 out.extend_from_slice(s.as_bytes());
76 bytes::Bytes::from(out)
77}
78
79impl Outgoing {
80 #[cfg(feature = "v1_8")]
83 #[must_use]
84 pub fn to_v1_8(&self) -> Option<sb8::ServerboundPlayPacket> {
85 Some(match self {
86 Outgoing::Click(c) => sb8::ServerboundPlayPacket::WindowClick(c.to_v1_8()),
87 Outgoing::Confirm { window_id, action } => {
88 sb8::ServerboundPlayPacket::Transaction(sb8::Transaction {
89 window_id: *window_id,
90 action: *action,
91 accepted: true,
92 })
93 }
94 Outgoing::CloseWindow(id) => {
95 sb8::ServerboundPlayPacket::CloseWindow(sb8::CloseWindow { window_id: *id })
96 }
97 Outgoing::OpenInventory => {
98 sb8::ServerboundPlayPacket::ClientCommand(sb8::ClientCommand {
99 payload: sb8::ClientCommandPayload(2),
100 })
101 }
102 Outgoing::SelectTrade(i) => {
104 sb8::ServerboundPlayPacket::CustomPayload(sb8::CustomPayload {
105 channel: "MC|TrSel".into(),
106 data: bytes::Bytes::copy_from_slice(&i.to_be_bytes()),
107 })
108 }
109 Outgoing::RenameItem(n) => {
111 sb8::ServerboundPlayPacket::CustomPayload(sb8::CustomPayload {
112 channel: "MC|ItemName".into(),
113 data: mc_string(n),
114 })
115 }
116 Outgoing::ContainerClick(_)
117 | Outgoing::Creative(_)
118 | Outgoing::ButtonClick { .. }
119 | Outgoing::Swing => {
120 return None;
121 }
122 })
123 }
124
125 #[cfg(feature = "v1_12")]
128 #[must_use]
129 pub fn to_v1_12_2(&self) -> Option<sb12::ServerboundPlayPacket> {
130 Some(match self {
131 Outgoing::Click(c) => sb12::ServerboundPlayPacket::WindowClick(c.to_v1_12_2()),
132 Outgoing::Confirm { window_id, action } => {
133 sb12::ServerboundPlayPacket::Transaction(sb12::Transaction {
134 window_id: *window_id,
135 action: *action,
136 accepted: true,
137 })
138 }
139 Outgoing::CloseWindow(id) => {
140 sb12::ServerboundPlayPacket::CloseWindow(sb12::CloseWindow { window_id: *id })
141 }
142 Outgoing::SelectTrade(i) => {
143 sb12::ServerboundPlayPacket::CustomPayload(sb12::CustomPayload {
144 channel: "MC|TrSel".into(),
145 data: bytes::Bytes::copy_from_slice(&i.to_be_bytes()),
146 })
147 }
148 Outgoing::RenameItem(n) => {
150 sb12::ServerboundPlayPacket::CustomPayload(sb12::CustomPayload {
151 channel: "MC|ItemName".into(),
152 data: mc_string(n),
153 })
154 }
155 Outgoing::OpenInventory
156 | Outgoing::ContainerClick(_)
157 | Outgoing::Creative(_)
158 | Outgoing::ButtonClick { .. }
159 | Outgoing::Swing => {
160 return None;
161 }
162 })
163 }
164
165 #[cfg(feature = "v1_16_5")]
168 #[must_use]
169 pub fn to_v1_16_5(&self) -> Option<sb16::ServerboundPlayPacket> {
170 Some(match self {
171 Outgoing::Click(c) => sb16::ServerboundPlayPacket::WindowClick(c.to_v1_16_5()),
172 Outgoing::Confirm { window_id, action } => {
173 sb16::ServerboundPlayPacket::Transaction(sb16::Transaction {
174 window_id: *window_id,
175 action: *action,
176 accepted: true,
177 })
178 }
179 Outgoing::CloseWindow(id) => {
180 sb16::ServerboundPlayPacket::CloseWindow(sb16::CloseWindow { window_id: *id })
181 }
182 Outgoing::SelectTrade(i) => {
183 sb16::ServerboundPlayPacket::SelectTrade(sb16::SelectTrade { slot: *i })
184 }
185 Outgoing::ButtonClick { window_id, button } => {
187 sb16::ServerboundPlayPacket::EnchantItem(sb16::EnchantItem {
188 window_id: *window_id as i8,
189 enchantment: *button as i8,
190 })
191 }
192 Outgoing::RenameItem(n) => sb16::ServerboundPlayPacket::NameItem(sb16::NameItem {
194 name: n.as_str().into(),
195 }),
196 Outgoing::Swing => {
197 sb16::ServerboundPlayPacket::ArmAnimation(sb16::ArmAnimation { hand: 0 })
198 }
199 Outgoing::OpenInventory | Outgoing::ContainerClick(_) | Outgoing::Creative(_) => {
200 return None;
201 }
202 })
203 }
204}
205
206impl Inventory {
207 #[cfg(feature = "v1_8")]
212 pub fn apply_v1_8(&mut self, p: &cb8::ClientboundPlayPacket) -> Option<Outgoing> {
213 use cb8::ClientboundPlayPacket as P;
214 match p {
215 P::Login(_) | P::Respawn(_) => self.reset(),
216 P::OpenWindow(w) => self.on_open_window(
217 i32::from(w.window_id),
218 WindowKind::from_v1_8(&w.inventory_type, w.slot_count),
219 w.window_title.clone(),
220 ),
221 P::CloseWindow(_) => self.on_close_window(),
222 P::WindowItems(w) => self.on_window_items_legacy(i32::from(w.window_id), &w.items),
223 P::SetSlot(s) => {
224 self.on_set_slot(
225 i32::from(s.window_id),
226 s.slot,
227 from_legacy_slot(s.item.clone()).map(crate::Stack::detached),
228 );
229 }
230 P::Transaction(t) => return self.on_transaction(t.window_id, t.action, t.accepted),
231 P::HeldItemSlot(h) => self.on_held_slot(h.slot),
232 P::CraftProgressBar(c) => {
233 self.on_window_property(i32::from(c.window_id), c.property, c.value);
234 }
235 P::CustomPayload(c) if c.channel == "MC|TrList" => self.on_trade_list_legacy(&c.data),
236 P::Experience(x) => self.set_experience_level(x.level),
238 P::Abilities(a) => self.set_creative(a.flags & 0x08 != 0),
239 _ => {}
240 }
241 None
242 }
243
244 #[cfg(feature = "v1_12")]
247 pub fn apply_v1_12_2(&mut self, p: &cb12::ClientboundPlayPacket) -> Option<Outgoing> {
248 use cb12::ClientboundPlayPacket as P;
249 match p {
250 P::Login(_) | P::Respawn(_) => self.reset(),
251 P::OpenWindow(w) => self.on_open_window(
252 i32::from(w.window_id),
253 WindowKind::from_v1_12_2(&w.inventory_type, w.slot_count),
254 w.window_title.clone(),
255 ),
256 P::CloseWindow(_) => self.on_close_window(),
257 P::WindowItems(w) => self.on_window_items_legacy(i32::from(w.window_id), &w.items),
258 P::SetSlot(s) => {
259 self.on_set_slot(
260 i32::from(s.window_id),
261 s.slot,
262 from_legacy_slot(s.item.clone()).map(crate::Stack::detached),
263 );
264 }
265 P::Transaction(t) => return self.on_transaction(t.window_id, t.action, t.accepted),
266 P::HeldItemSlot(h) => self.on_held_slot(h.slot),
267 P::CraftProgressBar(c) => {
268 self.on_window_property(i32::from(c.window_id), c.property, c.value);
269 }
270 P::CustomPayload(c) if c.channel == "MC|TrList" => self.on_trade_list_legacy(&c.data),
271 P::Experience(x) => self.set_experience_level(x.level),
273 P::Abilities(a) => self.set_creative(a.flags & 0x08 != 0),
274 _ => {}
275 }
276 None
277 }
278
279 #[cfg(feature = "v1_16_5")]
282 pub fn apply_v1_16_5(&mut self, p: &cb16::ClientboundPlayPacket) -> Option<Outgoing> {
283 use cb16::ClientboundPlayPacket as P;
284 match p {
285 P::Login(_) | P::Respawn(_) => self.reset(),
286 P::OpenWindow(w) => {
287 let swing = self.carried_drop_swing();
288 self.on_open_window_v1_16_5(w.window_id, w.inventory_type, w.window_title.clone());
289 if swing {
290 return Some(Outgoing::Swing);
291 }
292 }
293 P::OpenHorseWindow(w) => {
294 let swing = self.carried_drop_swing();
295 self.on_open_window(
296 i32::from(w.window_id),
297 u8::try_from(w.nb_slots)
299 .ok()
300 .map(|slots| WindowKind::Horse { slots }),
301 bytestring::ByteString::new(),
302 );
303 if swing {
304 return Some(Outgoing::Swing);
305 }
306 }
307 P::CloseWindow(_) => self.on_close_window(),
308 P::WindowItems(w) => self.on_window_items(
309 i32::from(w.window_id),
310 w.items.iter().cloned().map(from_modern_slot),
311 ),
312 P::SetSlot(s) => {
313 self.on_set_slot(
314 i32::from(s.window_id),
315 s.slot,
316 from_modern_slot(s.item.clone()).map(crate::Stack::detached),
317 );
318 }
319 P::Transaction(t) => return self.on_transaction(t.window_id, t.action, t.accepted),
320 P::HeldItemSlot(h) => self.on_held_slot(h.slot),
321 P::CraftProgressBar(c) => {
322 self.on_window_property(i32::from(c.window_id), c.property, c.value);
323 }
324 P::DeclareRecipes(d) => self.on_declare_recipes(crate::RecipeBook::from_v1_16_5(d)),
325 P::TradeList(t) => self.on_trade_list_v1_16_5(t),
326 P::Tags(t) => self.on_tags_v1_16_5(t),
327 P::Experience(x) => self.set_experience_level(x.level),
328 P::Abilities(a) => self.set_creative(a.flags & 0x08 != 0),
330 _ => {}
331 }
332 None
333 }
334}
335
336#[cfg(modern)]
342macro_rules! modern_packets {
343 ($m:ident, $proto:ident, $defs:ident, $mv:expr, $ty:path) => {
344 modern_packets!($m, $proto, $defs, $mv, $ty, |hand| {
345 Some(sb::ServerboundPlayPacket::ArmAnimation(sb::ArmAnimation {
346 hand: sb::ArmAnimationHand(hand),
347 }))
348 });
349 };
350 ($m:ident, $proto:ident, $defs:ident, $mv:expr, $ty:path, $swing:expr) => {
351 pub(crate) mod $m {
352 use bytes::BytesMut;
353 use ghast_protocol::codec::{Encode, Reader};
354 use ghast_protocol::defs::$defs::types as t;
355 use ghast_protocol::$proto::play::clientbound::{self as cb, ClientboundPlayPacket};
356 use ghast_protocol::$proto::play::serverbound as sb;
357 use ghast_protocol::{DecodeError, RawPacket};
358
359 use crate::inventory::{ContainerClick, CreativePacket, Inventory, Outgoing};
360 use crate::item26::{HashRegistries, Hashed, hashed};
361 use crate::stack::Stack;
362 use crate::tables::Mv;
363
364 const MV: Mv = $mv;
365
366 pub(crate) const PACKET_IDS: &[i32] = &[
368 cb::id::LOGIN,
369 cb::id::RESPAWN,
370 cb::id::OPEN_WINDOW,
371 cb::id::CLOSE_WINDOW,
372 cb::id::WINDOW_ITEMS,
373 cb::id::SET_SLOT,
374 cb::id::SET_CURSOR_ITEM,
375 cb::id::SET_PLAYER_INVENTORY,
376 cb::id::HELD_ITEM_SLOT,
377 cb::id::CRAFT_PROGRESS_BAR,
378 cb::id::DECLARE_RECIPES,
379 cb::id::ABILITIES,
380 cb::id::EXPERIENCE,
381 cb::id::TAGS,
382 cb::id::TRADE_LIST,
383 ];
384
385 #[inline]
386 fn slot(r: &mut Reader<'_>) -> Result<Option<Stack>, DecodeError> {
387 Ok(MV.read_stack(r)?.map(Stack::from_v775))
388 }
389
390 #[allow(dead_code)]
392 fn from_generated(s: &t::Slot) -> Option<Stack> {
393 let mut w = BytesMut::new();
394 s.encode(&mut w).ok()?;
395 let b = w.freeze();
396 MV.read_stack(&mut Reader::new(&b))
397 .ok()?
398 .map(Stack::from_v775)
399 }
400
401 pub(crate) fn hashed_slot(h: Hashed) -> t::HashedSlot {
403 t::HashedSlot {
404 item_id: h.item_id,
405 item_count: h.item_count,
406 components: h
407 .components
408 .into_iter()
409 .map(|(ty, hash)| t::HashedSlotComponents {
410 r#type: $ty(ty),
411 hash,
412 })
413 .collect(),
414 remove_components: h
415 .removed
416 .into_iter()
417 .map(|ty| t::HashedSlotRemoveComponents { r#type: $ty(ty) })
418 .collect(),
419 }
420 }
421
422 fn hashed_opt(s: Option<&Stack>, reg: &HashRegistries) -> Option<t::HashedSlot> {
423 hashed(MV, s, reg).0.map(hashed_slot)
424 }
425
426 pub(crate) fn click(c: &ContainerClick, reg: &HashRegistries) -> sb::WindowClick {
428 sb::WindowClick {
429 window_id: c.window_id,
430 state_id: c.state_id,
431 slot: c.slot,
432 mouse_button: c.button as i8,
433 mode: i32::from(c.mode),
434 changed_slots: c
435 .changed
436 .iter()
437 .map(|(location, s)| sb::WindowClickChangedSlots {
438 location: *location,
439 item: hashed_opt(s.as_ref(), reg),
440 })
441 .collect(),
442 cursor_item: hashed_opt(c.carried.as_ref(), reg),
443 }
444 }
445
446 pub(crate) fn creative(c: &CreativePacket) -> Option<sb::ServerboundPlayPacket> {
450 let swing: fn(i32) -> Option<sb::ServerboundPlayPacket> = $swing;
451 Some(match c {
452 CreativePacket::Swing => return swing(0),
453 CreativePacket::SetSlot { slot, item } => {
454 let item = match item.as_ref().filter(|s| s.count > 0 && s.id != 0) {
455 None => t::UntrustedSlot {
456 item_count: 0,
457 body: None,
458 },
459 Some(s) => {
460 let mut components: Vec<t::UntrustedSlotComponent> = Vec::new();
461 let mut removed: Vec<i32> = Vec::new();
462 if let Some(p) = s.components() {
463 for (ty, b) in p.iter() {
464 match components.iter_mut().find(|c| c.r#type.0 == ty) {
465 Some(c) => c.data = b,
466 None => components.push(t::UntrustedSlotComponent {
467 r#type: $ty(ty),
468 data: b,
469 }),
470 }
471 }
472 removed.extend(p.removed());
473 }
474 components.sort_by_key(|c| c.r#type.0);
475 removed.sort_unstable();
476 removed.dedup();
477 t::UntrustedSlot {
478 item_count: s.count,
479 body: Some(t::UntrustedSlotBodyDefault {
480 item_id: s.id,
481 added_component_count: components.len() as i32,
482 removed_component_count: removed.len() as i32,
483 components,
484 remove_components: removed
485 .into_iter()
486 .map(|ty| t::UntrustedSlotBodyDefaultRemoveComponents {
487 r#type: $ty(ty),
488 })
489 .collect(),
490 }),
491 }
492 }
493 };
494 sb::ServerboundPlayPacket::SetCreativeSlot(sb::SetCreativeSlot {
495 slot: *slot,
496 item,
497 })
498 }
499 })
500 }
501
502 pub(crate) fn outgoing(
505 o: &Outgoing,
506 reg: &HashRegistries,
507 ) -> Option<sb::ServerboundPlayPacket> {
508 Some(match o {
509 Outgoing::ContainerClick(c) => {
510 sb::ServerboundPlayPacket::WindowClick(click(c, reg))
511 }
512 Outgoing::CloseWindow(id) => {
513 sb::ServerboundPlayPacket::CloseWindow(sb::CloseWindow {
514 window_id: i32::from(*id),
515 })
516 }
517 Outgoing::RenameItem(n) => sb::ServerboundPlayPacket::NameItem(sb::NameItem {
518 name: n.as_str().into(),
519 }),
520 Outgoing::ButtonClick { window_id, button } => {
521 sb::ServerboundPlayPacket::EnchantItem(sb::EnchantItem {
522 window_id: *window_id,
523 enchantment: *button,
524 })
525 }
526 Outgoing::SelectTrade(i) => {
527 sb::ServerboundPlayPacket::SelectTrade(sb::SelectTrade { slot: *i })
528 }
529 _ => return None,
530 })
531 }
532
533 pub(crate) fn raw(p: &sb::ServerboundPlayPacket) -> Option<RawPacket> {
535 use ghast_protocol::PacketSet;
536 let mut w = BytesMut::new();
537 p.encode_body(&mut w).ok()?;
538 Some(RawPacket::new(p.id(), w.freeze()))
539 }
540
541 #[allow(dead_code)]
543 pub(crate) fn apply(inv: &mut Inventory, p: &ClientboundPlayPacket) {
544 use ClientboundPlayPacket as P;
545 match p {
546 P::Login(_) => inv.reset(),
547 P::Respawn(_) => inv.reset(),
549 P::OpenWindow(w) => open_window(inv, w),
550 P::CloseWindow(_) => inv.on_close_window(),
551 P::WindowItems(w) => inv.on_window_items_modern(
552 w.window_id,
553 w.state_id,
554 w.items.iter().map(from_generated),
555 from_generated(&w.carried_item),
556 ),
557 P::SetSlot(s) => inv.on_set_slot_modern(
558 s.window_id,
559 s.state_id,
560 s.slot,
561 from_generated(&s.item),
562 ),
563 P::SetCursorItem(c) => inv.on_set_cursor(from_generated(&c.contents)),
564 P::SetPlayerInventory(p) => {
565 inv.on_set_player_inventory(p.slot_id, from_generated(&p.contents))
566 }
567 P::HeldItemSlot(h) => inv.on_held_slot(i8::try_from(h.slot).unwrap_or(-1)),
568 P::CraftProgressBar(c) => {
569 inv.on_window_property(c.window_id, c.property, c.value)
570 }
571 P::DeclareRecipes(d) => declare_recipes(inv, d),
572 P::Abilities(a) => inv.set_creative_modern(a.flags & 0x08 != 0),
573 P::Experience(e) => inv.set_experience_level_modern(e.level),
574 _ => {}
575 }
576 }
577
578 pub(crate) fn ensure_recipes(inv: &mut Inventory) {
581 if let Some(body) = inv.take_recipes_raw_modern()
582 && let Ok(d) = RawPacket::new(cb::id::DECLARE_RECIPES, body)
583 .decode_as::<cb::DeclareRecipes>()
584 {
585 declare_recipes(inv, &d);
586 }
587 }
588
589 fn declare_recipes(inv: &mut Inventory, d: &cb::DeclareRecipes) {
590 use ghast_protocol::types::holder::HolderSet;
591 let mut inputs = Some(Vec::new());
592 for r in &d.stone_cutter_recipes {
593 match (&r.input, &mut inputs) {
594 (HolderSet::Ids(ids), Some(v)) => v.extend_from_slice(ids),
595 _ => inputs = None,
596 }
597 }
598 inv.on_recipes_modern(
599 d.recipes.iter().map(|r| (&*r.name, r.items.as_slice())),
600 inputs,
601 );
602 }
603
604 pub(crate) fn apply_raw(
606 inv: &mut Inventory,
607 raw: &RawPacket,
608 ) -> Result<(), DecodeError> {
609 let mut r = Reader::new(&raw.body);
610 match raw.id {
611 cb::id::WINDOW_ITEMS => {
612 let window = r.read_varint()?;
613 let state = r.read_varint()?;
614 let n = r.read_varint()?;
615 let n = r.check_count(i64::from(n), 1)?;
616 let mut items = Vec::with_capacity(n.min(256));
617 for _ in 0..n {
618 items.push(slot(&mut r)?);
619 }
620 let carried = slot(&mut r)?;
621 inv.on_window_items_modern(window, state, items, carried);
622 }
623 cb::id::SET_SLOT => {
624 let window = r.read_varint()?;
625 let state = r.read_varint()?;
626 let s = r.read_i16()?;
627 let item = slot(&mut r)?.map(Stack::detached);
628 inv.on_set_slot_modern(window, state, s, item);
629 }
630 cb::id::SET_CURSOR_ITEM => {
631 let item = slot(&mut r)?.map(Stack::detached);
632 inv.on_set_cursor(item);
633 }
634 cb::id::SET_PLAYER_INVENTORY => {
635 let k = r.read_varint()?;
636 let item = slot(&mut r)?.map(Stack::detached);
637 inv.on_set_player_inventory(k, item);
638 }
639 cb::id::LOGIN => {
641 raw.decode_as::<cb::Login>()?;
642 inv.reset();
643 }
644 cb::id::RESPAWN => {
645 raw.decode_as::<cb::Respawn>()?;
646 inv.reset();
647 }
648 cb::id::OPEN_WINDOW => open_window(inv, &raw.decode_as::<cb::OpenWindow>()?),
649 cb::id::CLOSE_WINDOW => {
650 raw.decode_as::<cb::CloseWindow>()?;
651 inv.on_close_window();
652 }
653 cb::id::HELD_ITEM_SLOT => {
654 let h = raw.decode_as::<cb::HeldItemSlot>()?;
655 inv.on_held_slot(i8::try_from(h.slot).unwrap_or(-1));
656 }
657 cb::id::CRAFT_PROGRESS_BAR => {
658 let c = raw.decode_as::<cb::CraftProgressBar>()?;
659 inv.on_window_property(c.window_id, c.property, c.value);
660 }
661 cb::id::DECLARE_RECIPES => {
664 inv.set_recipes_raw_modern(ghast_protocol::intern::bytes(&raw.body));
665 }
666 cb::id::TAGS => {
667 inv.on_update_tags_modern(bytes::Bytes::copy_from_slice(&raw.body))
668 }
669 cb::id::TRADE_LIST => inv.on_merchant_offers_modern(&raw.body),
670 cb::id::ABILITIES => {
671 let a = raw.decode_as::<cb::Abilities>()?;
672 inv.set_creative_modern(a.flags & 0x08 != 0);
673 }
674 cb::id::EXPERIENCE => {
675 let e = raw.decode_as::<cb::Experience>()?;
676 inv.set_experience_level_modern(e.level);
677 }
678 _ => {}
679 }
680 Ok(())
681 }
682
683 fn open_window(inv: &mut Inventory, w: &cb::OpenWindow) {
684 inv.on_open_screen_modern(
685 w.window_id,
686 w.inventory_type,
687 ghast_protocol::chat::nbt_to_plain_text(&w.window_title).into(),
688 );
689 }
690 }
691 };
692}
693
694#[cfg(feature = "v26_1")]
695modern_packets!(m775, v26_1, p775, Mv::V26_1, t::SlotComponentType);
696#[cfg(feature = "v26_2")]
697modern_packets!(m776, v26_2, p776, Mv::V26_2, t::SlotComponentType::new);
698#[cfg(feature = "v26_3")]
699modern_packets!(
701 m777,
702 v26_3,
703 p777,
704 Mv::V26_3,
705 t::SlotComponentType::new,
706 |_| None
707);
708
709#[cfg(modern)]
710use crate::tables::Mv;
711
712#[cfg(modern)]
715pub(crate) const fn packet_ids_modern(mv: Mv) -> &'static [i32] {
716 match mv {
717 #[cfg(feature = "v26_1")]
718 Mv::V26_1 => m775::PACKET_IDS,
719 #[cfg(feature = "v26_2")]
720 Mv::V26_2 => m776::PACKET_IDS,
721 #[cfg(feature = "v26_3")]
722 Mv::V26_3 => m777::PACKET_IDS,
723 }
724}
725
726#[cfg(feature = "v26_1")]
728pub const PACKET_IDS_26_1: &[i32] = m775::PACKET_IDS;
729
730#[cfg(feature = "v26_1")]
731pub(crate) use m775::hashed_slot as hashed_775;
732
733#[cfg(feature = "v26_1")]
734mod v26_1_api {
735 use ghast_protocol::v26_1::play::clientbound::ClientboundPlayPacket;
736 use ghast_protocol::v26_1::play::serverbound as sb;
737 use ghast_protocol::{DecodeError, RawPacket};
738
739 use super::m775;
740 use crate::inventory::{ContainerClick, CreativePacket, Inventory, Outgoing};
741 use crate::v26_1::HashRegistries;
742
743 impl ContainerClick {
744 #[must_use]
746 pub fn to_v26_1(&self, reg: &HashRegistries) -> sb::WindowClick {
747 m775::click(self, reg)
748 }
749 }
750
751 impl CreativePacket {
752 #[must_use]
755 pub fn to_v26_1(&self) -> sb::ServerboundPlayPacket {
756 m775::creative(self).unwrap_or_else(|| unreachable!("26.1 has a swing packet"))
757 }
758 }
759
760 impl Outgoing {
761 #[must_use]
765 pub fn to_v26_1(&self, reg: &HashRegistries) -> Option<sb::ServerboundPlayPacket> {
766 m775::outgoing(self, reg)
767 }
768 }
769
770 impl Inventory {
771 #[must_use]
773 pub fn encode_v26_1(&self, out: &Outgoing) -> Option<sb::ServerboundPlayPacket> {
774 out.to_v26_1(self.registries_modern())
775 }
776
777 pub fn apply_v26_1(&mut self, p: &ClientboundPlayPacket) {
783 m775::apply(self, p);
784 }
785
786 pub fn apply_raw_v26_1(&mut self, raw: &RawPacket) -> Result<(), DecodeError> {
791 m775::apply_raw(self, raw)
792 }
793 }
794}
795
796#[cfg(feature = "v26_2")]
799mod v26_2_api {
800 use ghast_protocol::v26_2::play::serverbound as sb;
801
802 use super::m776;
803 use crate::inventory::{CreativePacket, Outgoing};
804 use crate::modern::HashRegistries;
805
806 impl CreativePacket {
807 #[must_use]
809 pub fn to_v26_2(&self) -> sb::ServerboundPlayPacket {
810 m776::creative(self).unwrap_or_else(|| unreachable!("26.2 has a swing packet"))
811 }
812 }
813
814 impl Outgoing {
815 #[must_use]
817 pub fn to_v26_2(&self, reg: &HashRegistries) -> Option<sb::ServerboundPlayPacket> {
818 m776::outgoing(self, reg)
819 }
820 }
821}
822
823#[cfg(feature = "v26_3")]
826mod v26_3_api {
827 use ghast_protocol::v26_3::play::serverbound as sb;
828
829 use super::m777;
830 use crate::inventory::{CreativePacket, Outgoing};
831 use crate::modern::HashRegistries;
832
833 impl CreativePacket {
834 #[must_use]
838 pub fn to_v26_3(&self) -> Option<sb::ServerboundPlayPacket> {
839 m777::creative(self)
840 }
841 }
842
843 impl Outgoing {
844 #[must_use]
846 pub fn to_v26_3(&self, reg: &HashRegistries) -> Option<sb::ServerboundPlayPacket> {
847 m777::outgoing(self, reg)
848 }
849 }
850}
851
852#[cfg(modern)]
853impl Inventory {
854 pub fn apply_raw_modern(
862 &mut self,
863 raw: &ghast_protocol::RawPacket,
864 ) -> Result<(), ghast_protocol::DecodeError> {
865 match Mv::of(self.version()) {
866 #[cfg(feature = "v26_1")]
867 Some(Mv::V26_1) => m775::apply_raw(self, raw),
868 #[cfg(feature = "v26_2")]
869 Some(Mv::V26_2) => m776::apply_raw(self, raw),
870 #[cfg(feature = "v26_3")]
871 Some(Mv::V26_3) => m777::apply_raw(self, raw),
872 None => Ok(()),
873 }
874 }
875
876 #[must_use]
880 pub fn encode_modern(&self, out: &Outgoing) -> Option<ghast_protocol::RawPacket> {
881 match Mv::of(self.version())? {
882 #[cfg(feature = "v26_1")]
883 Mv::V26_1 => m775::raw(&m775::outgoing(out, self.registries_modern())?),
884 #[cfg(feature = "v26_2")]
885 Mv::V26_2 => m776::raw(&m776::outgoing(out, self.registries_modern())?),
886 #[cfg(feature = "v26_3")]
887 Mv::V26_3 => m777::raw(&m777::outgoing(out, self.registries_modern())?),
888 }
889 }
890
891 #[must_use]
894 pub fn encode_creative_modern(
895 &self,
896 p: &crate::CreativePacket,
897 ) -> Option<ghast_protocol::RawPacket> {
898 match Mv::of(self.version())? {
899 #[cfg(feature = "v26_1")]
900 Mv::V26_1 => m775::raw(&m775::creative(p)?),
901 #[cfg(feature = "v26_2")]
902 Mv::V26_2 => m776::raw(&m776::creative(p)?),
903 #[cfg(feature = "v26_3")]
904 Mv::V26_3 => m777::raw(&m777::creative(p)?),
905 }
906 }
907
908 pub(crate) fn ensure_recipes_modern(&mut self) {
910 match Mv::of(self.version()) {
911 #[cfg(feature = "v26_1")]
912 Some(Mv::V26_1) => m775::ensure_recipes(self),
913 #[cfg(feature = "v26_2")]
914 Some(Mv::V26_2) => m776::ensure_recipes(self),
915 #[cfg(feature = "v26_3")]
916 Some(Mv::V26_3) => m777::ensure_recipes(self),
917 None => {}
918 }
919 }
920}