valence_protocol/packets/play/
unlock_recipes_s2c.rs

1use std::borrow::Cow;
2use std::io::Write;
3
4use anyhow::bail;
5use valence_ident::Ident;
6
7use crate::{Decode, Encode, Packet, VarInt};
8
9#[derive(Clone, PartialEq, Eq, Debug, Packet)]
10pub struct UnlockRecipesS2c<'a> {
11    pub action: UpdateRecipeBookAction<'a>,
12    pub crafting_recipe_book_open: bool,
13    pub crafting_recipe_book_filter_active: bool,
14    pub smelting_recipe_book_open: bool,
15    pub smelting_recipe_book_filter_active: bool,
16    pub blast_furnace_recipe_book_open: bool,
17    pub blast_furnace_recipe_book_filter_active: bool,
18    pub smoker_recipe_book_open: bool,
19    pub smoker_recipe_book_filter_active: bool,
20    pub recipe_ids: Vec<Ident<Cow<'a, str>>>,
21}
22
23impl<'a> Decode<'a> for UnlockRecipesS2c<'a> {
24    fn decode(r: &mut &'a [u8]) -> anyhow::Result<Self> {
25        let action_id = VarInt::decode(r)?.0;
26
27        let crafting_recipe_book_open = bool::decode(r)?;
28        let crafting_recipe_book_filter_active = bool::decode(r)?;
29        let smelting_recipe_book_open = bool::decode(r)?;
30        let smelting_recipe_book_filter_active = bool::decode(r)?;
31        let blast_furnace_recipe_book_open = bool::decode(r)?;
32        let blast_furnace_recipe_book_filter_active = bool::decode(r)?;
33        let smoker_recipe_book_open = bool::decode(r)?;
34        let smoker_recipe_book_filter_active = bool::decode(r)?;
35        let recipe_ids = Vec::decode(r)?;
36
37        Ok(Self {
38            action: match action_id {
39                0 => UpdateRecipeBookAction::Init {
40                    recipe_ids: Vec::decode(r)?,
41                },
42                1 => UpdateRecipeBookAction::Add,
43                2 => UpdateRecipeBookAction::Remove,
44                n => bail!("unknown recipe book action of {n}"),
45            },
46            crafting_recipe_book_open,
47            crafting_recipe_book_filter_active,
48            smelting_recipe_book_open,
49            smelting_recipe_book_filter_active,
50            blast_furnace_recipe_book_open,
51            blast_furnace_recipe_book_filter_active,
52            smoker_recipe_book_open,
53            smoker_recipe_book_filter_active,
54            recipe_ids,
55        })
56    }
57}
58
59impl Encode for UnlockRecipesS2c<'_> {
60    fn encode(&self, _w: impl Write) -> anyhow::Result<()> {
61        todo!()
62    }
63}
64
65#[derive(Clone, PartialEq, Eq, Debug)]
66pub enum UpdateRecipeBookAction<'a> {
67    Init {
68        recipe_ids: Vec<Ident<Cow<'a, str>>>,
69    },
70    Add,
71    Remove,
72}