GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Remove chunk data serialization workaround to use serde serialization… · BuggStream/feather@f0b0054

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Cargo.lock
  • core/anvil
    • Cargo.toml
    • src
      • block_entity.rs
      • entity.rs
      • lib.rs
      • region.rs
      • region
        • blob.rs
      • serialization_helper.rs
  • server
    • chunk/src
      • chunk_manager.rs
      • chunk_worker.rs
    • src
      • systems.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Cargo.lock‎

Copy file name to clipboardExpand all lines: Cargo.lock
+15-4Lines changed: 15 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎core/anvil/Cargo.toml‎

Copy file name to clipboardExpand all lines: core/anvil/Cargo.toml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ thiserror = "1.0"
1616
tokio = { version = "0.2", features = ["full"] }
1717
serde = "1.0.112" # >= 1.0.112 needed for a fix in #[serde(flatten)]
1818
uuid = "0.8"
19-
hematite-nbt = "0.4"
19+
hematite-nbt = { git = "https://github.com/PistonDevelopers/hematite_nbt", rev="41124ff" } # needed for NBT array serialization
2020
byteorder = "1.3"
2121
bitvec = "0.17"
2222
anyhow = "1.0"
Collapse file

‎core/anvil/src/block_entity.rs‎

Copy file name to clipboardExpand all lines: core/anvil/src/block_entity.rs
+7-33Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::player::InventorySlot;
2-
use nbt::Value;
3-
use serde::{Deserialize, Serialize};
4-
use std::collections::HashMap;
2+
use serde::ser::Error;
3+
use serde::{Deserialize, Serialize, Serializer};
54

65
/// A block entity loaded or saved to the Anvil format.
76
/// Should be serialized using NBT.
@@ -15,35 +14,6 @@ pub struct BlockEntityData {
1514
pub kind: BlockEntityKind,
1615
}
1716

18-
impl BlockEntityData {
19-
pub fn into_nbt_value(self) -> Value {
20-
let mut compound = HashMap::new();
21-
22-
compound.insert(String::from("x"), Value::Int(self.base.x));
23-
compound.insert(String::from("y"), Value::Int(self.base.y));
24-
compound.insert(String::from("z"), Value::Int(self.base.z));
25-
26-
let id = match self.kind {
27-
BlockEntityKind::Chest { items, .. } => {
28-
compound.insert(
29-
String::from("Items"),
30-
Value::List(
31-
items
32-
.into_iter()
33-
.map(InventorySlot::into_nbt_value)
34-
.collect(),
35-
),
36-
);
37-
"minecraft:chest"
38-
}
39-
_ => todo!("implement block entity into_nbt_value"),
40-
};
41-
compound.insert(String::from("id"), Value::String(String::from(id)));
42-
43-
Value::Compound(compound)
44-
}
45-
}
46-
4717
/// Data common to all block entities.
4818
#[derive(Debug, Clone, Serialize, Deserialize)]
4919
pub struct BlockEntityBase {
@@ -158,11 +128,15 @@ pub enum BlockEntityKind {
158128
Jukebox { record_item: InventorySlot },
159129
// TODO: a few more
160130
/// Fallback type for unknown block entities
161-
#[serde(other)]
131+
#[serde(other, serialize_with = "BlockEntityKind::serialize_unknown")]
162132
Unknown,
163133
}
164134

165135
impl BlockEntityKind {
136+
pub(crate) fn serialize_unknown<S: Serializer>(_serializer: S) -> Result<S::Ok, S::Error> {
137+
Err(S::Error::custom("cannot serialize unknown block entities"))
138+
}
139+
166140
pub fn variant(&self) -> BlockEntityVariant {
167141
match self {
168142
BlockEntityKind::Beacon { .. } => BlockEntityVariant::Beacon,
Collapse file

‎core/anvil/src/entity.rs‎

Copy file name to clipboardExpand all lines: core/anvil/src/entity.rs
+5-99Lines changed: 5 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use arrayvec::ArrayVec;
22
use feather_items::Item;
33
use feather_util::{vec3, Position, Vec3d};
4-
use nbt::Value;
5-
use serde::{Deserialize, Serialize};
6-
use std::collections::HashMap;
4+
use serde::ser::Error;
5+
use serde::{Deserialize, Serialize, Serializer};
76
use thiserror::Error;
87

98
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
@@ -72,53 +71,13 @@ pub enum EntityData {
7271
Donkey(AnimalData),
7372

7473
/// Fallback type for unknown entities
75-
#[serde(other)]
74+
#[serde(other, serialize_with = "EntityData::serialize_unknown")]
7675
Unknown,
7776
}
7877

7978
impl EntityData {
80-
pub fn into_nbt_value(self) -> Value {
81-
let mut map = HashMap::new();
82-
83-
map.insert(
84-
String::from("id"),
85-
Value::String(
86-
match self {
87-
EntityData::Item(_) => "minecraft:item",
88-
EntityData::Arrow(_) => "minecraft:arrow",
89-
EntityData::Cow(_) => "minecraft:cow",
90-
EntityData::Pig(_) => "minecraft:pig",
91-
EntityData::Chicken(_) => "minecraft:chicken",
92-
EntityData::Sheep(_) => "minecraft:sheep",
93-
EntityData::Horse(_) => "minecraft:horse",
94-
EntityData::Llama(_) => "minecraft:llama",
95-
EntityData::Mooshroom(_) => "minecraft:mooshroom",
96-
EntityData::Rabbit(_) => "minecraft:rabbit",
97-
EntityData::Squid(_) => "minecraft:squid",
98-
EntityData::Donkey(_) => "minecraft:donkey",
99-
EntityData::Unknown => panic!("Cannot write unknown entities"),
100-
}
101-
.to_string(),
102-
),
103-
);
104-
105-
match self {
106-
EntityData::Item(data) => data.write_to_map(&mut map),
107-
EntityData::Arrow(data) => data.write_to_map(&mut map),
108-
EntityData::Cow(data) => data.write_to_map(&mut map),
109-
EntityData::Pig(data) => data.write_to_map(&mut map),
110-
EntityData::Chicken(data) => data.write_to_map(&mut map),
111-
EntityData::Sheep(data) => data.write_to_map(&mut map),
112-
EntityData::Horse(data) => data.write_to_map(&mut map),
113-
EntityData::Llama(data) => data.write_to_map(&mut map),
114-
EntityData::Mooshroom(data) => data.write_to_map(&mut map),
115-
EntityData::Rabbit(data) => data.write_to_map(&mut map),
116-
EntityData::Squid(data) => data.write_to_map(&mut map),
117-
EntityData::Donkey(data) => data.write_to_map(&mut map),
118-
EntityData::Unknown => unreachable!(),
119-
}
120-
121-
Value::Compound(map)
79+
pub(crate) fn serialize_unknown<S: Serializer>(_serializer: S) -> Result<S::Ok, S::Error> {
80+
Err(S::Error::custom("cannot serialize unknown entities"))
12281
}
12382
}
12483

@@ -133,23 +92,6 @@ pub struct BaseEntityData {
13392
pub velocity: ArrayVec<[f64; 3]>,
13493
}
13594

136-
impl BaseEntityData {
137-
fn write_to_map(self, map: &mut HashMap<String, Value>) {
138-
map.insert(
139-
String::from("Pos"),
140-
Value::List(self.position.iter().copied().map(Value::Double).collect()),
141-
);
142-
map.insert(
143-
String::from("Rotation"),
144-
Value::List(self.rotation.iter().copied().map(Value::Float).collect()),
145-
);
146-
map.insert(
147-
String::from("Motion"),
148-
Value::List(self.velocity.iter().copied().map(Value::Double).collect()),
149-
);
150-
}
151-
}
152-
15395
#[derive(Error, Debug, PartialEq, Eq)]
15496
pub enum EntityLoadError {
15597
#[error("missing position/rotation/velocity data")]
@@ -210,13 +152,6 @@ pub struct AnimalData {
210152
pub health: f32,
211153
}
212154

213-
impl AnimalData {
214-
fn write_to_map(self, map: &mut HashMap<String, Value>) {
215-
self.base.write_to_map(map);
216-
map.insert(String::from("Health"), Value::Float(self.health));
217-
}
218-
}
219-
220155
impl AnimalData {
221156
/// Creates an `AnimalData` from its parameters.
222157
pub fn new(base: BaseEntityData, health: f32) -> Self {
@@ -242,13 +177,6 @@ pub struct ItemData {
242177
pub item: String,
243178
}
244179

245-
impl ItemData {
246-
fn write_to_map(self, map: &mut HashMap<String, Value>) {
247-
map.insert(String::from("Count"), Value::Byte(self.count));
248-
map.insert(String::from("id"), Value::String(self.item));
249-
}
250-
}
251-
252180
impl Default for ItemData {
253181
fn default() -> Self {
254182
Self {
@@ -276,20 +204,6 @@ pub struct ItemEntityData {
276204
pub health: i16,
277205
}
278206

279-
impl ItemEntityData {
280-
fn write_to_map(self, map: &mut HashMap<String, Value>) {
281-
self.entity.write_to_map(map);
282-
283-
let mut item = HashMap::new();
284-
self.item.write_to_map(&mut item);
285-
map.insert(String::from("Item"), Value::Compound(item));
286-
287-
map.insert(String::from("Age"), Value::Short(self.age));
288-
map.insert(String::from("PickupDelay"), Value::Short(self.pickup_delay));
289-
map.insert(String::from("Health"), Value::Short(self.health));
290-
}
291-
}
292-
293207
/// Data for an Arrow entity (`minecraft:arrow`).
294208
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
295209
pub struct ArrowEntityData {
@@ -305,14 +219,6 @@ pub struct ArrowEntityData {
305219
pub critical: i8,
306220
}
307221

308-
impl ArrowEntityData {
309-
fn write_to_map(self, map: &mut HashMap<String, Value>) {
310-
self.entity.write_to_map(map);
311-
312-
map.insert(String::from("crit"), Value::Byte(self.critical));
313-
}
314-
}
315-
316222
#[cfg(test)]
317223
mod tests {
318224
use super::*;
Collapse file

‎core/anvil/src/lib.rs‎

Copy file name to clipboardExpand all lines: core/anvil/src/lib.rs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ pub mod entity;
77
pub mod level;
88
pub mod player;
99
pub mod region;
10+
mod serialization_helper;

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.