mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-04-20 03:25:09 +02:00
world/text_value: add support for new NBT text serialization
Starting with DataVersion 4290, text is stored as NBT instead of JSON. The structure remains the same.
This commit is contained in:
parent
ba6e4bae7f
commit
5f84ec8ed2
4 changed files with 35 additions and 11 deletions
|
@ -41,10 +41,15 @@ pub struct Sign {
|
||||||
|
|
||||||
impl Sign {
|
impl Sign {
|
||||||
/// Processes a [de::BlockEntitySign] into a [Sign]
|
/// Processes a [de::BlockEntitySign] into a [Sign]
|
||||||
fn new(sign: &de::BlockEntitySign, kind: SignKind, material: Option<String>) -> Sign {
|
fn new(
|
||||||
|
sign: &de::BlockEntitySign,
|
||||||
|
kind: SignKind,
|
||||||
|
material: Option<String>,
|
||||||
|
data_version: u32,
|
||||||
|
) -> Sign {
|
||||||
let (front_text, back_text) = sign.text();
|
let (front_text, back_text) = sign.text();
|
||||||
let front_text = front_text.decode();
|
let front_text = front_text.decode(data_version);
|
||||||
let back_text = back_text.decode();
|
let back_text = back_text.decode(data_version);
|
||||||
Sign {
|
Sign {
|
||||||
kind,
|
kind,
|
||||||
material,
|
material,
|
||||||
|
@ -78,7 +83,11 @@ pub struct BlockEntity {
|
||||||
|
|
||||||
impl BlockEntity {
|
impl BlockEntity {
|
||||||
/// Processes a [de::BlockEntity] into a [BlockEntity]
|
/// Processes a [de::BlockEntity] into a [BlockEntity]
|
||||||
pub fn new(entity: &de::BlockEntity, block_type: Option<&BlockType>) -> Option<Self> {
|
pub fn new(
|
||||||
|
entity: &de::BlockEntity,
|
||||||
|
block_type: Option<&BlockType>,
|
||||||
|
data_version: u32,
|
||||||
|
) -> Option<Self> {
|
||||||
let wall_sign = block_type
|
let wall_sign = block_type
|
||||||
.map(|block_type| block_type.block_color.is(BlockFlag::WallSign))
|
.map(|block_type| block_type.block_color.is(BlockFlag::WallSign))
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
@ -92,7 +101,7 @@ impl BlockEntity {
|
||||||
let material = block_type
|
let material = block_type
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|block_type| block_type.sign_material.as_ref());
|
.and_then(|block_type| block_type.sign_material.as_ref());
|
||||||
let data = BlockEntityData::Sign(Sign::new(sign, kind, material.cloned()));
|
let data = BlockEntityData::Sign(Sign::new(sign, kind, material.cloned(), data_version));
|
||||||
|
|
||||||
Some(BlockEntity {
|
Some(BlockEntity {
|
||||||
x: entity.x,
|
x: entity.x,
|
||||||
|
|
|
@ -58,6 +58,8 @@ pub struct Chunk<'a> {
|
||||||
inner: ChunkInner<'a>,
|
inner: ChunkInner<'a>,
|
||||||
/// Unprocessed block entities
|
/// Unprocessed block entities
|
||||||
block_entities: &'a Vec<de::BlockEntity>,
|
block_entities: &'a Vec<de::BlockEntity>,
|
||||||
|
/// Chunk data version
|
||||||
|
data_version: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Chunk<'a> {
|
impl<'a> Chunk<'a> {
|
||||||
|
@ -87,6 +89,7 @@ impl<'a> Chunk<'a> {
|
||||||
Chunk {
|
Chunk {
|
||||||
inner,
|
inner,
|
||||||
block_entities,
|
block_entities,
|
||||||
|
data_version,
|
||||||
},
|
},
|
||||||
has_unknown,
|
has_unknown,
|
||||||
))
|
))
|
||||||
|
@ -292,7 +295,11 @@ impl<'a> Chunk<'a> {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|block_entity| {
|
.map(|block_entity| {
|
||||||
let block_type = self.block_type_at_block_entity(block_entity)?;
|
let block_type = self.block_type_at_block_entity(block_entity)?;
|
||||||
Ok(BlockEntity::new(block_entity, block_type))
|
Ok(BlockEntity::new(
|
||||||
|
block_entity,
|
||||||
|
block_type,
|
||||||
|
self.data_version,
|
||||||
|
))
|
||||||
})
|
})
|
||||||
.collect::<Result<_>>()?;
|
.collect::<Result<_>>()?;
|
||||||
Ok(entities.into_iter().flatten().collect())
|
Ok(entities.into_iter().flatten().collect())
|
||||||
|
|
|
@ -49,7 +49,7 @@ static DYE_COLORS: phf::Map<&'static str, Color> = phf::phf_map! {
|
||||||
|
|
||||||
impl RawSignText<'_> {
|
impl RawSignText<'_> {
|
||||||
/// Decodes the [RawSignText] into a [SignText]
|
/// Decodes the [RawSignText] into a [SignText]
|
||||||
pub fn decode(&self) -> SignText {
|
pub fn decode(&self, data_version: u32) -> SignText {
|
||||||
let color = self
|
let color = self
|
||||||
.color
|
.color
|
||||||
.map(|c| DYE_COLORS.get(c).copied().unwrap_or(DEFAULT_COLOR));
|
.map(|c| DYE_COLORS.get(c).copied().unwrap_or(DEFAULT_COLOR));
|
||||||
|
@ -60,7 +60,7 @@ impl RawSignText<'_> {
|
||||||
SignText(
|
SignText(
|
||||||
self.messages
|
self.messages
|
||||||
.iter()
|
.iter()
|
||||||
.map(|message| message.deserialize().linearize(&parent))
|
.map(|message| message.deserialize(data_version).linearize(&parent))
|
||||||
.collect(),
|
.collect(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -171,12 +171,20 @@ impl Default for DeserializedText {
|
||||||
|
|
||||||
/// Minecraft raw text value
|
/// Minecraft raw text value
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct TextValue(pub String);
|
pub struct TextValue(pub fastnbt::Value);
|
||||||
|
|
||||||
impl TextValue {
|
impl TextValue {
|
||||||
/// Deserializes a [TextValue] into a [DeserializedText]
|
/// Deserializes a [TextValue] into a [DeserializedText]
|
||||||
pub fn deserialize(&self) -> DeserializedText {
|
pub fn deserialize(&self, data_version: u32) -> DeserializedText {
|
||||||
serde_json::from_str(&self.0).unwrap_or_default()
|
if data_version < 4290 {
|
||||||
|
if let fastnbt::Value::String(json) = &self.0 {
|
||||||
|
if let Ok(content) = serde_json::from_str(json) {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fastnbt::from_value(&self.0).unwrap_or_default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue