|
1 | 1 | use arrayvec::ArrayVec; |
2 | 2 | use feather_items::Item; |
3 | 3 | 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}; |
7 | 6 | use thiserror::Error; |
8 | 7 |
|
9 | 8 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] |
@@ -72,53 +71,13 @@ pub enum EntityData { |
72 | 71 | Donkey(AnimalData), |
73 | 72 |
|
74 | 73 | /// Fallback type for unknown entities |
75 | | - #[serde(other)] |
| 74 | + #[serde(other, serialize_with = "EntityData::serialize_unknown")] |
76 | 75 | Unknown, |
77 | 76 | } |
78 | 77 |
|
79 | 78 | 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")) |
122 | 81 | } |
123 | 82 | } |
124 | 83 |
|
@@ -133,23 +92,6 @@ pub struct BaseEntityData { |
133 | 92 | pub velocity: ArrayVec<[f64; 3]>, |
134 | 93 | } |
135 | 94 |
|
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 | | - |
153 | 95 | #[derive(Error, Debug, PartialEq, Eq)] |
154 | 96 | pub enum EntityLoadError { |
155 | 97 | #[error("missing position/rotation/velocity data")] |
@@ -210,13 +152,6 @@ pub struct AnimalData { |
210 | 152 | pub health: f32, |
211 | 153 | } |
212 | 154 |
|
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 | | - |
220 | 155 | impl AnimalData { |
221 | 156 | /// Creates an `AnimalData` from its parameters. |
222 | 157 | pub fn new(base: BaseEntityData, health: f32) -> Self { |
@@ -242,13 +177,6 @@ pub struct ItemData { |
242 | 177 | pub item: String, |
243 | 178 | } |
244 | 179 |
|
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 | | - |
252 | 180 | impl Default for ItemData { |
253 | 181 | fn default() -> Self { |
254 | 182 | Self { |
@@ -276,20 +204,6 @@ pub struct ItemEntityData { |
276 | 204 | pub health: i16, |
277 | 205 | } |
278 | 206 |
|
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 | | - |
293 | 207 | /// Data for an Arrow entity (`minecraft:arrow`). |
294 | 208 | #[derive(Debug, Default, Clone, Serialize, Deserialize)] |
295 | 209 | pub struct ArrowEntityData { |
@@ -305,14 +219,6 @@ pub struct ArrowEntityData { |
305 | 219 | pub critical: i8, |
306 | 220 | } |
307 | 221 |
|
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 | | - |
316 | 222 | #[cfg(test)] |
317 | 223 | mod tests { |
318 | 224 | use super::*; |
|