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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use nbt::{Blob, Value};
use crate::{block::Block, region::Region};
use std::{cmp, collections::HashMap};
/// A simple representation of a Minecraft Chunk
#[derive(Clone)]
pub struct Chunk {
/// All of the chunk data
pub data: Box<Blob>,
/// The region x
pub x: u32,
/// The region z
pub z: u32,
}
impl Chunk {
/// Returns the chunk at an x,z coordinate within a Region.
///
/// # Arguments
///
/// * `region` - The Region from which to get the Chunk
/// * `chunk_x` - The x coordinate within the Region of the Chunk
/// * `chunk_z` - The z coordinate within the Region of the Chunk
pub fn from_region(region: &Region, chunk_x: u32, chunk_z: u32) -> Option<Chunk> {
match region.chunk_data(chunk_x, chunk_z) {
Some(data) => {
return Some(Chunk{ data, x: chunk_x, z: chunk_z });
}
None => None,
}
}
/// Returns a string representing the current generation state of the Chunk. 'full' is completely generated.
///
/// # Examples
///
/// ```rust,no_run
/// use simple_anvil::region::Region;
/// let region = Region::from_file("r.0.0.mca");
/// let chunk = region.get_chunk(0, 0).unwrap();
/// if chunk.get_status() == "full" {
/// println!("Fully Generated!");
/// }
/// ```
pub fn get_status(&self) -> &String {
if let Value::String(s) = self.data.get("Status").unwrap() {
s
} else {
panic!("Value should be a string?")
}
}
/// Returns an i64 (equivalent of Java long) of the last tick at which the chunk updated.
///
/// # Examples
///
/// ```rust,no_run
/// use simple_anvil::region::Region;
/// let region = Region::from_file("r.0.0.mca");
/// let chunk = region.get_chunk(0, 0).unwrap();
/// println!("{}", chunk.get_last_update());
/// ```
pub fn get_last_update(&self) -> &i64 {
if let Value::Long(l) = self.data.get("LastUpdate").unwrap() {
l
} else {
panic!("Value should be a i64")
}
}
/// Returns a heightmap of the Chunk. If the Chunk is not fully generated then a None is returned.
///
/// # Arguments
///
/// * `ignore_water` - Determines which heightmap to return, if true then a heightmap that does not take into account the water is returned (OCEAN_FLOOR), if false then the water is accounted for (WORLD_SURFACE).
///
/// # Examples
///
/// ```rust,no_run
/// use simple_anvil::region::Region;
/// let region = Region::from_file("r.0.0.mca");
/// let chunk = region.get_chunk(0, 0).unwrap();
/// let heightmap = chunk.get_heightmap(false);
/// ```
pub fn get_heightmap(&self, ignore_water: bool) -> Option<Vec<i32>> {
if self.get_status() == "full" {
let height_maps = if let Value::Compound(hm) = self.data.get("Heightmaps").unwrap() {
hm
} else {
panic!()
};
let map = if ignore_water {
"OCEAN_FLOOR"
} else {
"WORLD_SURFACE"
};
let surface = if let Value::LongArray(la) = height_maps.get(map).unwrap() {
la
} else {
panic!("no ocean?")
};
let surface_binary: Vec<String> = surface.iter().map(|n| format!("{:b}", n)).map(|n| "0".repeat(63 - n.len()) + &n).collect();
let mut all = Vec::new();
// let mut hmm = Vec::new();
for num in surface_binary {
let num_chars = num.chars().collect::<Vec<_>>();
let mut sub_nums = num_chars.chunks(9).collect::<Vec<&[char]>>();
sub_nums.reverse();
for num in sub_nums {
let test = num.iter().collect::<String>();
if test != "000000000" {
all.push(test.clone());
}
}
}
let mut heights = Vec::new();
for num in all {
let n = usize::from_str_radix(num.as_str(), 2).unwrap();
heights.push(n as i32 - 64 - 1);
}
return Some(heights);
} else {
None
}
}
/// Returns a vertical section of a Chunk
///
/// # Arguments
///
/// * `y` - The y index of the section.
fn get_section(&self, y: i8) -> Option<HashMap<String, Value>> {
if y < -4 || y > 19 {
panic!("Y value out of range")
}
let sections = if let Value::List(s) = self.data.get("sections").unwrap() {
s
} else {
panic!("Value should be a list?")
};
for section in sections {
let section = if let Value::Compound(s) = section {
s
} else {
panic!("should be a compound")
};
let section_y = if let Value::Byte(sec_y) = section.get("Y").unwrap() {
sec_y
} else {
panic!("Failed to get y")
};
if *section_y == y {
let cloned = section.clone();
return Some(cloned);
}
}
None
}
/// Returns the String representation of the biome for a Chunk. Chunks can have different biomes at different vertical sections so use a heightmap to determine the top section if you only want the surface biome.
///
/// # Arguments
///
/// * `y` - The y section of the chunk to get the biome of.
///
/// # Examples
///
/// ```rust,no_run
/// use simple_anvil::region::Region;
/// let region = Region::from_file("r.0.0.mca");
/// let chunk = region.get_chunk(0, 0).unwrap();
/// let heightmap = chunk.get_heightmap(false);
/// let y = if let Some(heights) = heightmap {
/// heights.get(0).unwrap()
/// } else {
/// panic!("Chunk not fully generated");
/// }
/// let section_y = ((y + 64) / 16 - 4) as i8
/// let biome = chunk.get_biome(section_y);
/// ```
///
/// ```rust,no_run
/// use simple_anvil::region::Region;
/// let region = Region::from_file("r.0.0.mca");
/// let chunk = region.get_chunk(0, 0).unwrap();
/// let biome = chunk.get_biome(-3);
/// ```
pub fn get_biome(&self, y: i32) -> String {
let sections = if let Value::List(s) = self.data.get("sections").unwrap() {
s
} else {
panic!("Value should be a list?")
};
for section in sections {
let section = if let Value::Compound(s) = section {
s
} else {
panic!("Should be a compound?")
};
let current_y = if let Value::Byte(val) = section.get("Y").unwrap() {
val
} else {
panic!("invalid height found")
};
if current_y == &(((y + 64) / 16 - 4) as i8) {
let biomes = if let Value::Compound(c) = section.get("biomes").unwrap() {
c
} else {
panic!("biomes not found")
};
let pallete = if let Value::List(l) = biomes.get("palette").unwrap() {
l
} else {
panic!("pallete not found")
};
let biome = if let Value::String(s) = &pallete[0] {
s
} else {
panic!("failed to get string")
};
return biome.to_string();
}
};
return String::from("minecraft:ocean")
}
/// Returns the block at a particular x, y, z coordinate within a chunk. x and z should be the coordinates within the Chunk (0-15).
///
/// # Examples
///
/// ```rust,no_run
/// use simple_anvil::region::Region;
/// let region = Region::from_file("r.0.0.mca");
/// let chunk = region.get_chunk(0, 0).unwrap();
/// let block = chunk.get_block(5, -12, 11);
/// println!("{}", block.id);
/// ```
pub fn get_block(&self, x: i32, mut y: i32, z: i32) -> Block {
let section = self.get_section(((y + 64) / 16 - 4) as i8);
if section == None {
return Block::from_name(String::from("minecraft:air"), Some((self.x as i32 * 32 + x, y, self.z as i32 * 32 + z)), None);
}
let section = section.unwrap();
y = y.rem_euclid(16);
let block_states = if let Some(Value::Compound(bs)) = section.get("block_states") {
Some(bs)
} else {
None
};
if block_states == None {
return Block::from_name(String::from("minecraft:air"), Some((self.x as i32 * 32 + x, y, self.z as i32 * 32 + z)), None);
}
let palette = if let Value::List(p) = block_states.unwrap().get("palette").unwrap() {
p
} else {
panic!("Palette should be a list")
};
match block_states {
Some(bs) => {
let bits = cmp::max(self.bit_length(palette.len() - 1), 4);
let index = y * 16 * 16 + z * 16 + x;
match bs.get("data") {
Some(data) => {
let states = if let Value::LongArray(la) = data {
la
} else {
panic!("something here")
};
let state = index as usize / (64 / bits as usize);
let data = states[state];
let mut d = 0;
let mut modified = false;
if data < 0 {
d = data as u64;
modified = true;
}
let shifted_data = (if modified { d as usize } else { data as usize }) >> (index as usize % (64 / bits as usize) * bits as usize);
let palette_id = shifted_data & (2u32.pow(bits) - 1) as usize;
let block = &palette[palette_id];
// let props =
let props = if let Value::Compound(c) = block {
match c.get("Properties") {
Some(p_val) => {
let properties = if let Value::Compound(p) = p_val {
p
} else {
panic!("Properties should be a compound")
};
Some(properties.iter().map(|f| (f.0.to_owned(), if let Value::String(s) = f.1 {
s.to_owned()
} else {
panic!("Should be a string?")
})).collect::<Vec<_>>())
},
None => None,
}
} else {
panic!("block should be a compound")
};
return Block::from_palette(block, Some((self.x as i32 * 32 + x, y, self.z as i32 * 32 + z)), props);
},
None => return Block::from_name(String::from("minecraft:air"), Some((self.x as i32 * 32 + x, y, self.z as i32 * 32 + z)), None)
}
},
None => {
return Block::from_name(String::from("minecraft:air"), Some((self.x as i32 * 32 + x, y, self.z as i32 * 32 + z)), None);
},
}
}
/// Returns the bitlength of a usize value
fn bit_length(&self, num: usize) -> u32 {
// The number of bits that the number consists of, this is an integer and we don't care about signs or leading 0's
// 0001 and 1 have the same return value
// I think the lowest number that could come in is -1?
// usize is always returned from the len function so I think that it will only be usize?
if num == 0 {
return 0;
}
// Convert the number to a string version of the binary representation
// Get the number of leading 0's
let _leading = num.leading_zeros();
// Place the number into binary
let s_num = format!("{:b}", num);
// Remove leading 0's
// let s = &s_num[leading as usize..];
// Return the length
// Leading zeros appear to be removed when changed to bits
return s_num.len() as u32;
}
}