-
Notifications
You must be signed in to change notification settings - Fork 35
/
minecraft_chunk.rs
119 lines (104 loc) · 2.7 KB
/
minecraft_chunk.rs
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
use super::resources::{CHUNK_RAW, CHUNK_RAW_WITH_ENTITIES};
use crate::{from_bytes, Value};
use serde::Deserialize;
#[test]
fn unit_variant_enum_for_chunk_status() {
// From https://minecraft.wiki/w/Chunk_format
//
// Status: Defines the world generation status of this chunk. It is always
// one of the following: empty, structure_starts, structure_references,
// biomes, noise, surface, carvers, liquid_carvers, features, light, spawn,
// or heightmaps.[1]
#[derive(Deserialize)]
struct Chunk {
#[serde(rename = "Level")]
level: Level,
}
#[derive(Deserialize)]
struct Level {
#[serde(rename = "Status")]
status: Status,
}
#[derive(Deserialize, PartialEq, Debug)]
#[serde(rename_all = "snake_case")]
enum Status {
Empty,
StructureStarts,
StructureReferences,
Biomes,
Noise,
Surface,
Carvers,
LiquidCarvers,
Features,
Light,
Spawn,
Heightmaps,
Full,
}
let chunk: Chunk = from_bytes(CHUNK_RAW).unwrap();
assert_eq!(Status::Full, chunk.level.status)
}
#[test]
fn chunk_to_value() {
from_bytes::<Value>(CHUNK_RAW).unwrap();
}
#[test]
fn tile_entities() {
#[derive(Deserialize)]
struct Chunk {
#[serde(rename = "Level")]
level: Level,
}
#[derive(Deserialize)]
struct Level {
#[serde(rename = "Entities")]
entities: Vec<Entity>,
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum Entity {
Known(KnownEntity),
Unknown(Value),
}
#[derive(Deserialize, Debug)]
#[serde(tag = "id")]
enum KnownEntity {
#[serde(rename = "minecraft:bat")]
Bat {
#[allow(dead_code)]
#[serde(rename = "BatFlags")]
bat_flags: i8,
},
#[serde(rename = "minecraft:creeper")]
Creeper {
#[allow(dead_code)]
ignited: i8,
},
}
let chunk: Chunk = from_bytes(CHUNK_RAW_WITH_ENTITIES).unwrap();
let entities = chunk.level.entities;
println!("{:#?}", entities);
}
#[test]
fn avoiding_alloc_with_chunk() {
#[derive(Deserialize)]
struct Chunk<'a> {
#[serde(rename = "Level")]
#[serde(borrow)]
_level: Level<'a>,
}
#[derive(Deserialize)]
struct Level<'a> {
#[serde(rename = "Sections")]
#[serde(borrow)]
pub _sections: Option<Vec<Section<'a>>>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct Section<'a> {
#[serde(borrow)]
pub block_states: Option<&'a [u8]>,
}
let _chunk: Chunk = from_bytes(CHUNK_RAW).unwrap();
}