Skip to main content

azalea_protocol/packets/game/
c_merchant_offers.rs

1use std::fmt::Debug;
2
3use azalea_buf::AzBuf;
4use azalea_protocol_macros::ClientboundGamePacket;
5use azalea_registry::{
6    builtin::ItemKind,
7    item::{
8        ItemStack, ItemStackData,
9        components::{DataComponent, patch::DataComponentPatch},
10    },
11};
12
13#[derive(AzBuf, ClientboundGamePacket, Clone, Debug, PartialEq)]
14pub struct ClientboundMerchantOffers {
15    #[var]
16    pub container_id: i32,
17    pub offers: Vec<MerchantOffer>,
18    #[var]
19    pub villager_level: u32,
20    #[var]
21    pub villager_xp: u32,
22    pub show_progress: bool,
23    pub can_restock: bool,
24}
25
26#[derive(AzBuf, Clone, Debug, PartialEq)]
27pub struct MerchantOffer {
28    pub base_cost_a: ItemCost,
29    pub result: ItemStack,
30    pub cost_b: Option<ItemCost>,
31    pub out_of_stock: bool,
32    pub uses: i32,
33    pub max_uses: i32,
34    pub xp: i32,
35    pub special_price_diff: i32,
36    pub price_multiplier: f32,
37    pub demand: i32,
38}
39
40/// An item that a merchant can buy.
41///
42/// This can be converted into an [`ItemStackData`] with
43/// [`Self::into_item_stack`].
44#[derive(AzBuf, Clone, Debug, PartialEq)]
45pub struct ItemCost {
46    pub item: ItemKind,
47    #[var]
48    pub count: i32,
49    pub components: DataComponentExactPredicate,
50}
51impl ItemCost {
52    pub fn into_item_stack(self) -> ItemStackData {
53        let mut component_patch = DataComponentPatch::default();
54
55        for component in self.components.expected {
56            component_patch.insert_component(component.kind(), Some(component));
57        }
58        // TODO: add a fast way to iterate over default components, and insert
59        // the ones that aren't present as None
60
61        ItemStackData {
62            kind: self.item,
63            count: self.count,
64            component_patch,
65        }
66    }
67}
68
69/// Similar to [`DataComponentPatch`], but it's only additive, meaning that
70/// there are no `None` values.
71///
72/// If you got this from [`ItemCost`], consider using
73/// [`ItemCost::into_item_stack`] for a better API instead.
74#[derive(AzBuf, Clone, Debug, PartialEq)]
75pub struct DataComponentExactPredicate {
76    pub expected: Vec<DataComponent>,
77}