1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::borrow::Cow;
use std::io::Write;

use anyhow::bail;
use valence_ident::Ident;

use crate::{Decode, Encode, Packet, VarInt};

#[derive(Clone, PartialEq, Eq, Debug, Packet)]
pub struct UnlockRecipesS2c<'a> {
    pub action: UpdateRecipeBookAction<'a>,
    pub crafting_recipe_book_open: bool,
    pub crafting_recipe_book_filter_active: bool,
    pub smelting_recipe_book_open: bool,
    pub smelting_recipe_book_filter_active: bool,
    pub blast_furnace_recipe_book_open: bool,
    pub blast_furnace_recipe_book_filter_active: bool,
    pub smoker_recipe_book_open: bool,
    pub smoker_recipe_book_filter_active: bool,
    pub recipe_ids: Vec<Ident<Cow<'a, str>>>,
}

impl<'a> Decode<'a> for UnlockRecipesS2c<'a> {
    fn decode(r: &mut &'a [u8]) -> anyhow::Result<Self> {
        let action_id = VarInt::decode(r)?.0;

        let crafting_recipe_book_open = bool::decode(r)?;
        let crafting_recipe_book_filter_active = bool::decode(r)?;
        let smelting_recipe_book_open = bool::decode(r)?;
        let smelting_recipe_book_filter_active = bool::decode(r)?;
        let blast_furnace_recipe_book_open = bool::decode(r)?;
        let blast_furnace_recipe_book_filter_active = bool::decode(r)?;
        let smoker_recipe_book_open = bool::decode(r)?;
        let smoker_recipe_book_filter_active = bool::decode(r)?;
        let recipe_ids = Vec::decode(r)?;

        Ok(Self {
            action: match action_id {
                0 => UpdateRecipeBookAction::Init {
                    recipe_ids: Vec::decode(r)?,
                },
                1 => UpdateRecipeBookAction::Add,
                2 => UpdateRecipeBookAction::Remove,
                n => bail!("unknown recipe book action of {n}"),
            },
            crafting_recipe_book_open,
            crafting_recipe_book_filter_active,
            smelting_recipe_book_open,
            smelting_recipe_book_filter_active,
            blast_furnace_recipe_book_open,
            blast_furnace_recipe_book_filter_active,
            smoker_recipe_book_open,
            smoker_recipe_book_filter_active,
            recipe_ids,
        })
    }
}

impl Encode for UnlockRecipesS2c<'_> {
    fn encode(&self, _w: impl Write) -> anyhow::Result<()> {
        todo!()
    }
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum UpdateRecipeBookAction<'a> {
    Init {
        recipe_ids: Vec<Ident<Cow<'a, str>>>,
    },
    Add,
    Remove,
}