Skip to content

Commit 63b1036

Browse files
committed
fix CustomModelData and WrittenBookContent datacomponents
1 parent c285fad commit 63b1036

File tree

7 files changed

+126
-6
lines changed

7 files changed

+126
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

azalea-buf/src/read.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub enum BufReadError {
2121
CouldNotReadBytes,
2222
#[error("The received encoded string buffer length is longer than maximum allowed ({length} > {max_length})")]
2323
StringLengthTooLong { length: u32, max_length: u32 },
24+
#[error("The received Vec length is longer than maximum allowed ({length} > {max_length})")]
25+
VecLengthTooLong { length: u32, max_length: u32 },
2426
#[error("{source}")]
2527
Io {
2628
#[from]
@@ -183,7 +185,7 @@ impl AzaleaRead for UnsizedByteArray {
183185
}
184186
}
185187

186-
impl<T: AzaleaRead + Send> AzaleaRead for Vec<T> {
188+
impl<T: AzaleaRead> AzaleaRead for Vec<T> {
187189
default fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
188190
let length = u32::azalea_read_var(buf)? as usize;
189191
// we limit the capacity to not get exploited into allocating a bunch
@@ -194,6 +196,23 @@ impl<T: AzaleaRead + Send> AzaleaRead for Vec<T> {
194196
Ok(contents)
195197
}
196198
}
199+
impl<T: AzaleaRead> AzaleaReadLimited for Vec<T> {
200+
fn azalea_read_limited(buf: &mut Cursor<&[u8]>, limit: usize) -> Result<Self, BufReadError> {
201+
let length = u32::azalea_read_var(buf)? as usize;
202+
if length > limit {
203+
return Err(BufReadError::VecLengthTooLong {
204+
length: length as u32,
205+
max_length: limit as u32,
206+
});
207+
}
208+
209+
let mut contents = Vec::with_capacity(usize::min(length, 65536));
210+
for _ in 0..length {
211+
contents.push(T::azalea_read(buf)?);
212+
}
213+
Ok(contents)
214+
}
215+
}
197216

198217
impl<K: AzaleaRead + Send + Eq + Hash, V: AzaleaRead + Send> AzaleaRead for HashMap<K, V> {
199218
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
@@ -343,6 +362,16 @@ impl<T: AzaleaReadVar> AzaleaReadVar for Option<T> {
343362
})
344363
}
345364
}
365+
impl<T: AzaleaReadLimited> AzaleaReadLimited for Option<T> {
366+
fn azalea_read_limited(buf: &mut Cursor<&[u8]>, limit: usize) -> Result<Self, BufReadError> {
367+
let present = bool::azalea_read(buf)?;
368+
Ok(if present {
369+
Some(T::azalea_read_limited(buf, limit)?)
370+
} else {
371+
None
372+
})
373+
}
374+
}
346375

347376
// [String; 4]
348377
impl<T: AzaleaRead, const N: usize> AzaleaRead for [T; N] {

azalea-core/src/filterable.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use std::io::Cursor;
2+
3+
use azalea_buf::{AzaleaRead, AzaleaReadLimited, AzaleaReadVar, AzaleaWrite};
4+
5+
/// Used for written books.
6+
pub struct Filterable<T> {
7+
pub raw: T,
8+
pub filtered: Option<T>,
9+
}
10+
11+
impl<T: AzaleaWrite> azalea_buf::AzaleaWrite for Filterable<T> {
12+
fn azalea_write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> {
13+
self.raw.azalea_write(buf)?;
14+
self.filtered.azalea_write(buf)?;
15+
Ok(())
16+
}
17+
}
18+
impl<T: AzaleaRead> azalea_buf::AzaleaRead for Filterable<T> {
19+
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
20+
let raw = AzaleaRead::azalea_read(buf)?;
21+
let filtered = AzaleaRead::azalea_read(buf)?;
22+
Ok(Self { raw, filtered })
23+
}
24+
}
25+
impl<T: AzaleaReadLimited> azalea_buf::AzaleaReadLimited for Filterable<T> {
26+
fn azalea_read_limited(
27+
buf: &mut Cursor<&[u8]>,
28+
limit: usize,
29+
) -> Result<Self, azalea_buf::BufReadError> {
30+
let raw = AzaleaReadLimited::azalea_read_limited(buf, limit)?;
31+
let filtered = AzaleaReadLimited::azalea_read_limited(buf, limit)?;
32+
Ok(Self { raw, filtered })
33+
}
34+
}
35+
impl<T: AzaleaReadVar> azalea_buf::AzaleaReadVar for Filterable<T> {
36+
fn azalea_read_var(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
37+
let raw = AzaleaReadVar::azalea_read_var(buf)?;
38+
let filtered = AzaleaReadVar::azalea_read_var(buf)?;
39+
Ok(Self { raw, filtered })
40+
}
41+
}
42+
43+
impl<T: Clone> Clone for Filterable<T> {
44+
fn clone(&self) -> Self {
45+
Self {
46+
raw: self.raw.clone(),
47+
filtered: self.filtered.clone(),
48+
}
49+
}
50+
}
51+
impl<T: PartialEq> PartialEq for Filterable<T> {
52+
fn eq(&self, other: &Self) -> bool {
53+
self.raw == other.raw && self.filtered == other.filtered
54+
}
55+
}

azalea-core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod data_registry;
99
pub mod delta;
1010
pub mod difficulty;
1111
pub mod direction;
12+
pub mod filterable;
1213
pub mod game_type;
1314
pub mod math;
1415
pub mod objectives;

azalea-inventory/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ azalea-registry = { path = "../azalea-registry", version = "0.11.0" }
1717
indexmap.workspace = true
1818

1919
simdnbt.workspace = true
20+
tracing.workspace = true
2021
uuid.workspace = true

azalea-inventory/src/components.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ use std::{any::Any, collections::HashMap, io::Cursor};
33

44
use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError};
55
use azalea_chat::FormattedText;
6-
use azalea_core::{position::GlobalPos, resource_location::ResourceLocation};
6+
use azalea_core::{
7+
filterable::Filterable, position::GlobalPos, resource_location::ResourceLocation,
8+
};
79
use azalea_registry::{
810
Attribute, Block, ConsumeEffectKind, DataComponentKind, Enchantment, EntityKind, HolderSet,
911
Item, MobEffect, Potion, SoundEvent, TrimMaterial, TrimPattern,
1012
};
1113
use simdnbt::owned::{Nbt, NbtCompound};
14+
use tracing::trace;
1215
use uuid::Uuid;
1316

1417
use crate::ItemStack;
@@ -54,6 +57,8 @@ pub fn from_kind(
5457
// if this is causing a compile-time error, look at DataComponents.java in the
5558
// decompiled vanilla code to see how to implement new components
5659

60+
trace!("Reading data component {kind}");
61+
5762
// note that this match statement is updated by genitemcomponents.py
5863
Ok(match kind {
5964
DataComponentKind::CustomData => Box::new(CustomData::azalea_read(buf)?),
@@ -328,8 +333,10 @@ impl DataComponent for AttributeModifiers {
328333

329334
#[derive(Clone, PartialEq, AzBuf)]
330335
pub struct CustomModelData {
331-
#[var]
332-
pub value: i32,
336+
pub floats: Vec<f32>,
337+
pub flags: Vec<bool>,
338+
pub strings: Vec<String>,
339+
pub colors: Vec<i32>,
333340
}
334341
impl DataComponent for CustomModelData {
335342
const KIND: DataComponentKind = DataComponentKind::CustomModelData;
@@ -535,13 +542,15 @@ impl DataComponent for WritableBookContent {
535542

536543
#[derive(Clone, PartialEq, AzBuf)]
537544
pub struct WrittenBookContent {
538-
pub title: String,
545+
#[limit(32)]
546+
pub title: Filterable<String>,
539547
pub author: String,
540548
#[var]
541549
pub generation: i32,
542-
pub pages: Vec<FormattedText>,
550+
pub pages: Vec<Filterable<FormattedText>>,
543551
pub resolved: bool,
544552
}
553+
545554
impl DataComponent for WrittenBookContent {
546555
const KIND: DataComponentKind = DataComponentKind::WrittenBookContent;
547556
}

azalea-protocol/src/packets/game/c_container_set_slot.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,27 @@ pub struct ClientboundContainerSetSlot {
1111
pub slot: u16,
1212
pub item_stack: ItemStack,
1313
}
14+
15+
#[cfg(test)]
16+
mod test {
17+
use std::io::Cursor;
18+
19+
use azalea_buf::AzaleaRead;
20+
21+
use crate::packets::game::ClientboundContainerSetSlot;
22+
23+
#[test]
24+
fn test_read_container_set_slot() {
25+
tracing_subscriber::fmt::try_init().ok();
26+
27+
#[rustfmt::skip]
28+
let contents = [
29+
0, 2, 0, 39, 1, 246, 8, 3, 0, 8, 1, 10, 9, 0, 5, 101, 120, 116, 114, 97, 8, 0, 0, 0, 1, 0, 47, 89, 111, 117, 114, 32, 106, 111, 117, 114, 110, 97, 108, 44, 32, 99, 111, 110, 116, 97, 105, 110, 105, 110, 103, 32, 97, 108, 108, 32, 113, 117, 101, 115, 116, 32, 105, 110, 102, 111, 114, 109, 97, 116, 105, 111, 110, 115, 8, 0, 4, 116, 101, 120, 116, 0, 0, 0, 14, 1, 191, 128, 0, 0, 0, 0, 0, 44, 7, 74, 111, 117, 114, 110, 97, 108, 0, 12, 65, 115, 116, 114, 97, 108, 99, 104, 114, 111, 109, 97, 0, 2, 10, 9, 0, 5, 101, 120, 116, 114, 97, 10, 0, 0, 0, 20, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 1, 0, 10, 117, 110, 100, 101, 114, 108, 105, 110, 101, 100, 0, 1, 0, 4, 98, 111, 108, 100, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 9, 100, 97, 114, 107, 95, 98, 108, 117, 101, 1, 0, 10, 111, 98, 102, 117, 115, 99, 97, 116, 101, 100, 0, 1, 0, 13, 115, 116, 114, 105, 107, 101, 116, 104, 114, 111, 117, 103, 104, 0, 8, 0, 4, 116, 101, 120, 116, 0, 1, 45, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 98, 108, 117, 101, 8, 0, 4, 116, 101, 120, 116, 0, 12, 81, 117, 101, 115, 116, 32, 83, 116, 97, 116, 117, 115, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 9, 100, 97, 114, 107, 95, 98, 108, 117, 101, 8, 0, 4, 116, 101, 120, 116, 0, 1, 45, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 5, 98, 108, 97, 99, 107, 8, 0, 4, 116, 101, 120, 116, 0, 3, 10, 32, 32, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 103, 114, 97, 121, 8, 0, 4, 116, 101, 120, 116, 0, 9, 76, 111, 99, 107, 101, 100, 10, 32, 32, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 10, 100, 97, 114, 107, 95, 103, 114, 101, 101, 110, 8, 0, 4, 116, 101, 120, 116, 0, 17, 67, 97, 110, 32, 98, 101, 32, 83, 116, 97, 114, 116, 101, 100, 10, 32, 32, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 103, 111, 108, 100, 8, 0, 4, 116, 101, 120, 116, 0, 10, 83, 116, 97, 114, 116, 101, 100, 10, 32, 32, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 9, 100, 97, 114, 107, 95, 103, 114, 97, 121, 8, 0, 4, 116, 101, 120, 116, 0, 19, 70, 105, 110, 105, 115, 104, 101, 100, 47, 67, 111, 111, 108, 100, 111, 119, 110, 10, 10, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 9, 100, 97, 114, 107, 95, 98, 108, 117, 101, 8, 0, 4, 116, 101, 120, 116, 0, 1, 45, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 98, 108, 117, 101, 8, 0, 4, 116, 101, 120, 116, 0, 10, 81, 117, 101, 115, 116, 32, 76, 105, 115, 116, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 9, 100, 97, 114, 107, 95, 98, 108, 117, 101, 8, 0, 4, 116, 101, 120, 116, 0, 1, 45, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 8, 100, 97, 114, 107, 95, 114, 101, 100, 8, 0, 4, 116, 101, 120, 116, 0, 3, 10, 32, 32, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 9, 100, 97, 114, 107, 95, 103, 114, 97, 121, 8, 0, 4, 116, 101, 120, 116, 0, 15, 67, 104, 101, 116, 104, 32, 67, 111, 108, 108, 101, 99, 116, 111, 114, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 8, 100, 97, 114, 107, 95, 114, 101, 100, 8, 0, 4, 116, 101, 120, 116, 0, 1, 10, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 9, 100, 97, 114, 107, 95, 98, 108, 117, 101, 8, 0, 4, 116, 101, 120, 116, 0, 1, 45, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 98, 108, 117, 101, 8, 0, 4, 116, 101, 120, 116, 0, 17, 82, 101, 112, 101, 97, 116, 97, 98, 108, 101, 32, 81, 117, 101, 115, 116, 115, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 9, 100, 97, 114, 107, 95, 98, 108, 117, 101, 8, 0, 4, 116, 101, 120, 116, 0, 1, 45, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 8, 100, 97, 114, 107, 95, 114, 101, 100, 8, 0, 4, 116, 101, 120, 116, 0, 3, 10, 32, 32, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 10, 100, 97, 114, 107, 95, 103, 114, 101, 101, 110, 8, 0, 4, 116, 101, 120, 116, 0, 16, 68, 97, 105, 108, 121, 32, 67, 111, 109, 109, 105, 115, 115, 105, 111, 110, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 8, 100, 97, 114, 107, 95, 114, 101, 100, 8, 0, 4, 116, 101, 120, 116, 0, 1, 10, 0, 8, 0, 4, 116, 101, 120, 116, 0, 0, 0, 0, 10, 9, 0, 5, 101, 120, 116, 114, 97, 10, 0, 0, 0, 12, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 98, 108, 117, 101, 8, 0, 4, 116, 101, 120, 116, 0, 11, 49, 48, 46, 48, 50, 46, 50, 48, 50, 53, 32, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 103, 111, 108, 100, 8, 0, 4, 116, 101, 120, 116, 0, 6, 49, 55, 58, 48, 48, 10, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 9, 100, 97, 114, 107, 95, 98, 108, 117, 101, 8, 0, 4, 116, 101, 120, 116, 0, 1, 45, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 98, 108, 117, 101, 8, 0, 4, 116, 101, 120, 116, 0, 16, 68, 97, 105, 108, 121, 32, 67, 111, 109, 109, 105, 115, 115, 105, 111, 110, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 9, 100, 97, 114, 107, 95, 98, 108, 117, 101, 8, 0, 4, 116, 101, 120, 116, 0, 1, 45, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 5, 98, 108, 97, 99, 107, 8, 0, 4, 116, 101, 120, 116, 0, 11, 10, 32, 32, 84, 97, 108, 107, 32, 116, 111, 32, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 10, 100, 97, 114, 107, 95, 103, 114, 101, 101, 110, 8, 0, 4, 116, 101, 120, 116, 0, 11, 65, 113, 117, 97, 10, 75, 121, 114, 101, 108, 108, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 5, 98, 108, 97, 99, 107, 8, 0, 4, 116, 101, 120, 116, 0, 4, 32, 97, 116, 32, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 10, 100, 97, 114, 107, 95, 103, 114, 101, 101, 110, 8, 0, 4, 116, 101, 120, 116, 0, 22, 91, 75, 114, 105, 111, 58, 10, 52, 52, 56, 57, 44, 32, 51, 54, 44, 32, 49, 51, 49, 52, 93, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 5, 98, 108, 97, 99, 107, 8, 0, 4, 116, 101, 120, 116, 0, 7, 46, 10, 32, 32, 10, 32, 32, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 10, 100, 97, 114, 107, 95, 103, 114, 101, 101, 110, 8, 0, 4, 116, 101, 120, 116, 0, 9, 82, 101, 119, 97, 114, 100, 115, 58, 32, 0, 1, 0, 6, 105, 116, 97, 108, 105, 99, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 5, 98, 108, 97, 99, 107, 8, 0, 4, 116, 101, 120, 116, 0, 6, 36, 53, 48, 48, 48, 10, 0, 8, 0, 4, 116, 101, 120, 116, 0, 0, 0, 0, 0
30+
];
31+
let mut buf = Cursor::new(contents.as_slice());
32+
let packet = ClientboundContainerSetSlot::azalea_read(&mut buf).unwrap();
33+
println!("{:?}", packet);
34+
35+
assert_eq!(buf.position(), contents.len() as u64);
36+
}
37+
}

0 commit comments

Comments
 (0)