aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs
blob: 6743c3af2ff51410ea9d9bd51db4b8f8883f8946 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use super::GamePacket;
use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable};
use azalea_core::resource_location::ResourceLocation;
use std::{
    hash::Hash,
    io::{Read, Write},
};

#[derive(Hash, Clone, Debug)]
pub struct ClientboundDeclareCommandsPacket {
    pub entries: Vec<BrigadierNodeStub>,
    pub root_index: i32,
}

impl ClientboundDeclareCommandsPacket {
    pub fn get(self) -> GamePacket {
        GamePacket::ClientboundDeclareCommandsPacket(self)
    }

    pub fn write(&self, _buf: &mut impl Write) -> Result<(), std::io::Error> {
        panic!("ClientboundDeclareCommandsPacket::write not implemented")
    }

    pub fn read<T: Read>(buf: &mut T) -> Result<GamePacket, String> {
        let node_count = buf.read_varint()?;
        let mut nodes = Vec::with_capacity(node_count as usize);
        for _ in 0..node_count {
            let node = BrigadierNodeStub::read_into(buf)?;
            nodes.push(node);
        }
        let root_index = buf.read_varint()?;
        Ok(GamePacket::ClientboundDeclareCommandsPacket(
            ClientboundDeclareCommandsPacket {
                entries: nodes,
                root_index,
            },
        ))
    }
}

#[derive(Hash, Debug, Clone)]
pub struct BrigadierNodeStub {}

#[derive(Debug, Clone)]
pub struct BrigadierNumber<T> {
    min: Option<T>,
    max: Option<T>,
}
impl<T: McBufReadable> McBufReadable for BrigadierNumber<T> {
    fn read_into(buf: &mut impl Read) -> Result<Self, String> {
        let flags = buf.read_byte()?;
        let min = if flags & 0x01 != 0 {
            Some(T::read_into(buf)?)
        } else {
            None
        };
        let max = if flags & 0x02 != 0 {
            Some(T::read_into(buf)?)
        } else {
            None
        };
        Ok(BrigadierNumber { min, max })
    }
}
impl<T: McBufWritable> McBufWritable for BrigadierNumber<T> {
    fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
        let mut flags = 0;
        if self.min.is_some() {
            flags |= 0x01;
        }
        if self.max.is_some() {
            flags |= 0x02;
        }
        buf.write_byte(flags)?;
        if let Some(min) = &self.min {
            min.write_into(buf)?;
        }
        if let Some(max) = &self.max {
            max.write_into(buf)?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Copy)]
pub enum BrigadierString {
    /// Reads a single word
    SingleWord = 0,
    // If it starts with a ", keeps reading until another " (allowing escaping with \). Otherwise behaves the same as SINGLE_WORD
    QuotablePhrase = 1,
    // Reads the rest of the content after the cursor. Quotes will not be removed.
    GreedyPhrase = 2,
}

impl McBufReadable for BrigadierString {
    fn read_into(buf: &mut impl Read) -> Result<Self, String> {
        let id = buf.read_byte()?;
        Ok(match id {
            0 => BrigadierString::SingleWord,
            1 => BrigadierString::QuotablePhrase,
            2 => BrigadierString::GreedyPhrase,
            _ => panic!("Unknown BrigadierString id: {}", id),
        })
    }
}
impl McBufWritable for BrigadierString {
    fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
        buf.write_byte(*self as u8)?;
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub enum BrigadierParser {
    Bool,
    Double(BrigadierNumber<f64>),
    Float(BrigadierNumber<f32>),
    Integer(BrigadierNumber<i32>),
    Long(BrigadierNumber<i64>),
    String(BrigadierString),
    Entity { single: bool, players_only: bool },
    GameProfile,
    BlockPos,
    ColumnPos,
    Vec3,
    Vec2,
    BlockState,
    BlockPredicate,
    ItemStack,
    ItemPredicate,
    Color,
    Component,
    Message,
    Nbt,
    NbtPath,
    Objective,
    ObjectiveCriteira,
    Operation,
    Particle,
    Rotation,
    Angle,
    ScoreboardSlot,
    ScoreHolder { allows_multiple: bool },
    Swizzle,
    Team,
    ItemSlot,
    ResourceLocation,
    MobEffect,
    Function,
    EntityAnchor,
    Range { decimals_allowed: bool },
    IntRange,
    FloatRange,
    ItemEnchantment,
    EntitySummon,
    Dimension,
    Uuid,
    NbtTag,
    NbtCompoundTag,
    Time,
    ResourceOrTag { registry_key: ResourceLocation },
    Resource { registry_key: ResourceLocation },
}

impl McBufReadable for BrigadierParser {
    fn read_into(buf: &mut impl Read) -> Result<Self, String> {
        let parser = buf.read_resource_location()?;

        if parser == ResourceLocation::new("brigadier:bool")? {
            Ok(BrigadierParser::Bool)
        } else if parser == ResourceLocation::new("brigadier:double")? {
            Ok(BrigadierParser::Double(BrigadierNumber::read_into(buf)?))
        } else if parser == ResourceLocation::new("brigadier:float")? {
            Ok(BrigadierParser::Float(BrigadierNumber::read_into(buf)?))
        } else if parser == ResourceLocation::new("brigadier:integer")? {
            Ok(BrigadierParser::Integer(BrigadierNumber::read_into(buf)?))
        } else if parser == ResourceLocation::new("brigadier:long")? {
            Ok(BrigadierParser::Long(BrigadierNumber::read_into(buf)?))
        } else if parser == ResourceLocation::new("brigadier:string")? {
            Ok(BrigadierParser::String(BrigadierString::read_into(buf)?))
        } else if parser == ResourceLocation::new("minecraft:entity")? {
            let flags = buf.read_byte()?;
            Ok(BrigadierParser::Entity {
                single: flags & 0x01 != 0,
                players_only: flags & 0x02 != 0,
            })
        } else if parser == ResourceLocation::new("minecraft:game_profile")? {
            Ok(BrigadierParser::GameProfile)
        } else if parser == ResourceLocation::new("minecraft:block_pos")? {
            Ok(BrigadierParser::BlockPos)
        } else if parser == ResourceLocation::new("minecraft:column_pos")? {
            Ok(BrigadierParser::ColumnPos)
        } else if parser == ResourceLocation::new("minecraft:vec3")? {
            Ok(BrigadierParser::Vec3)
        } else if parser == ResourceLocation::new("minecraft:vec2")? {
            Ok(BrigadierParser::Vec2)
        } else if parser == ResourceLocation::new("minecraft:block_state")? {
            Ok(BrigadierParser::BlockState)
        } else if parser == ResourceLocation::new("minecraft:block_predicate")? {
            Ok(BrigadierParser::BlockPredicate)
        } else if parser == ResourceLocation::new("minecraft:item_stack")? {
            Ok(BrigadierParser::ItemStack)
        } else if parser == ResourceLocation::new("minecraft:item_predicate")? {
            Ok(BrigadierParser::ItemPredicate)
        } else if parser == ResourceLocation::new("minecraft:color")? {
            Ok(BrigadierParser::Color)
        } else if parser == ResourceLocation::new("minecraft:component")? {
            Ok(BrigadierParser::Component)
        } else if parser == ResourceLocation::new("minecraft:message")? {
            Ok(BrigadierParser::Message)
        } else if parser == ResourceLocation::new("minecraft:nbt")? {
            Ok(BrigadierParser::Nbt)
        } else if parser == ResourceLocation::new("minecraft:nbt_path")? {
            Ok(BrigadierParser::NbtPath)
        } else if parser == ResourceLocation::new("minecraft:objective")? {
            Ok(BrigadierParser::Objective)
        } else if parser == ResourceLocation::new("minecraft:objective_criteria")? {
            Ok(BrigadierParser::ObjectiveCriteira)
        } else if parser == ResourceLocation::new("minecraft:operation")? {
            Ok(BrigadierParser::Operation)
        } else if parser == ResourceLocation::new("minecraft:particle")? {
            Ok(BrigadierParser::Particle)
        } else if parser == ResourceLocation::new("minecraft:rotation")? {
            Ok(BrigadierParser::Rotation)
        } else if parser == ResourceLocation::new("minecraft:angle")? {
            Ok(BrigadierParser::Angle)
        } else if parser == ResourceLocation::new("minecraft:scoreboard_slot")? {
            Ok(BrigadierParser::ScoreboardSlot)
        } else if parser == ResourceLocation::new("minecraft:score_holder")? {
            let flags = buf.read_byte()?;
            Ok(BrigadierParser::ScoreHolder {
                allows_multiple: flags & 0x01 != 0,
            })
        } else if parser == ResourceLocation::new("minecraft:swizzle")? {
            Ok(BrigadierParser::Swizzle)
        } else if parser == ResourceLocation::new("minecraft:team")? {
            Ok(BrigadierParser::Team)
        } else if parser == ResourceLocation::new("minecraft:item_slot")? {
            Ok(BrigadierParser::ItemSlot)
        } else if parser == ResourceLocation::new("minecraft:resource_location")? {
            Ok(BrigadierParser::ResourceLocation)
        } else if parser == ResourceLocation::new("minecraft:mob_effect")? {
            Ok(BrigadierParser::MobEffect)
        } else if parser == ResourceLocation::new("minecraft:function")? {
            Ok(BrigadierParser::Function)
        } else if parser == ResourceLocation::new("minecraft:entity_anchor")? {
            Ok(BrigadierParser::EntityAnchor)
        } else if parser == ResourceLocation::new("minecraft:range")? {
            Ok(BrigadierParser::Range {
                decimals_allowed: buf.read_boolean()?,
            })
        } else if parser == ResourceLocation::new("minecraft:int_range")? {
            Ok(BrigadierParser::IntRange)
        } else if parser == ResourceLocation::new("minecraft:float_range")? {
            Ok(BrigadierParser::FloatRange)
        } else if parser == ResourceLocation::new("minecraft:item_enchantment")? {
            Ok(BrigadierParser::ItemEnchantment)
        } else if parser == ResourceLocation::new("minecraft:entity_summon")? {
            Ok(BrigadierParser::EntitySummon)
        } else if parser == ResourceLocation::new("minecraft:dimension")? {
            Ok(BrigadierParser::Dimension)
        } else if parser == ResourceLocation::new("minecraft:uuid")? {
            Ok(BrigadierParser::Uuid)
        } else if parser == ResourceLocation::new("minecraft:nbt_tag")? {
            Ok(BrigadierParser::NbtTag)
        } else if parser == ResourceLocation::new("minecraft:nbt_compound_tag")? {
            Ok(BrigadierParser::NbtCompoundTag)
        } else if parser == ResourceLocation::new("minecraft:time")? {
            Ok(BrigadierParser::Time)
        } else if parser == ResourceLocation::new("minecraft:resource_or_tag")? {
            Ok(BrigadierParser::ResourceOrTag {
                registry_key: buf.read_resource_location()?,
            })
        } else if parser == ResourceLocation::new("minecraft:resource")? {
            Ok(BrigadierParser::Resource {
                registry_key: buf.read_resource_location()?,
            })
        } else {
            panic!("Unknown Brigadier parser: {}", parser)
        }
    }
}

// TODO: BrigadierNodeStub should have more stuff
impl McBufReadable for BrigadierNodeStub {
    fn read_into(buf: &mut impl Read) -> Result<Self, String> {
        let flags = u8::read_into(buf)?;
        if flags > 31 {
            println!(
                "Warning: The flags from a Brigadier node are over 31. This is probably a bug."
            );
        }

        let node_type = flags & 0x03;
        let _is_executable = flags & 0x04 != 0;
        let has_redirect = flags & 0x08 != 0;
        let has_suggestions_type = flags & 0x10 != 0;

        let _children = buf.read_int_id_list()?;
        let _redirect_node = if has_redirect { buf.read_varint()? } else { 0 };

        // argument node
        if node_type == 2 {
            let _name = buf.read_utf()?;
            let _parser = BrigadierParser::read_into(buf)?;
            let _suggestions_type = if has_suggestions_type {
                Some(buf.read_resource_location()?)
            } else {
                None
            };
            return Ok(BrigadierNodeStub {});
        }
        // literal node
        if node_type == 1 {
            let _name = buf.read_utf()?;
            return Ok(BrigadierNodeStub {});
        }
        Ok(BrigadierNodeStub {})
        // return Err("Unknown node type".to_string());
    }
}