vintage_schematics/formats/
common.rs

1//! Shared functionality between formats.
2
3use serde::{Deserialize, Serialize};
4
5use crate::Map;
6
7/// List of tuples of old and new block codes.
8/// Each old code should be replaced by the paired new code.
9// data sourced from:
10// - https://minecraft.wiki/w/Java_Edition_Flattening#Block_and_item_IDs
11// - https://web.archive.org/web/1/https://bugs.mojang.com/secure/attachment/151784/the_flattening.txt
12// TODO: all of them :(
13pub static MODERNISED_BLOCKS: &[(&str, &str)] = &[
14	("minecraft:grass", "minecraft:grass_block"),
15	("minecraft:grass_path", "minecraft:dirt_path"),
16	("minecraft:fence", "minecraft:oak_fence"),
17	("minecraft:fence_gate", "minecraft:oak_fence_gate"),
18	("minecraft:noteblock", "minecraft:note_block"),
19	("minecraft:golden_rail", "minecraft:powered_rail"),
20	("minecraft:web", "minecraft:cobweb"),
21	("minecraft:tallgrass", "minecraft:tall_grass"),
22	("minecraft:deadbush", "minecraft:dead_bush"),
23	("minecraft:waterlily", "minecraft:lily_pad"),
24	("minecraft:yellow_flower", "minecraft:dandelion"),
25	("minecraft:red_flower", "minecraft:poppy"),
26	("minecraft:reeds", "minecraft:sugar_cane"),
27	("minecraft:stone_stairs", "minecraft:cobblestone_stairs"),
28	("minecraft:wooden_door", "minecraft:oak_door"),
29	("minecraft:trapdoor", "minecraft:oak_trapdoor"),
30	("minecraft:monster_egg", "minecraft:infested_stone"),
31	("minecraft:double_stone_slab", "minecraft:smooth_stone"),
32	("minecraft:stonebrick", "minecraft:stone_bricks"), // TODO: mossy, cracked, etc
33	("minecraft:lit_furnace", "minecraft:furnace"),
34	("minecraft:slime", "minecraft:slime_block"),
35	("minecraft:magma", "minecraft:magma_block"),
36	("minecraft:flowing_water", "minecraft:water"),
37	("minecraft:flowing_lava", "minecraft:lava"),
38	("minecraft:quartz_ore", "minecraft:nether_quartz_ore"),
39	("minecraft:lit_pumpkin", "minecraft:jack_o_lantern"),
40	("minecraft:wooden_pressure_plate", "minecraft:oak_pressure_plate"),
41	// these two must be done in this order!
42	("minecraft:snow", "minecraft:snow_block"),
43	("minecraft:snow_layer", "minecraft:snow"),
44];
45
46/// The [data version](https://minecraft.wiki/w/Data_version) of Minecraft when
47/// [the flattening](https://minecraft.wiki/w/Java_Edition_Flattening) was implemented.
48pub const FLATTENING_VERSION: i32 = 1519;
49
50/// A 3D coordinate.
51#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
52pub struct Xyz {
53	pub x: i32,
54	pub y: i32,
55	pub z: i32,
56}
57
58impl From<(i32, i32, i32)> for Xyz {
59	fn from((x, y, z): (i32, i32, i32)) -> Self { Self { x, y, z } }
60}
61
62/// The size of a schematic in blocks.
63/// Used by the [`Sponge`](super::sponge::Sponge) and [`Schematic`](super::schematic::Schematic) formats.
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
65#[serde(rename_all = "PascalCase")]
66pub struct Size {
67	/// The size along the X axis in blocks.
68	pub width: u16,
69
70	/// The size along the Y axis in blocks.
71	pub height: u16,
72
73	/// The size along the Z axis in blocks.
74	pub length: u16,
75}
76
77impl Size {
78	/// The number of blocks in the schematic.
79	#[must_use]
80	pub fn blocks(self) -> i32 { i32::from(self.width) * i32::from(self.height) * i32::from(self.length) }
81
82	/// Converts an index into the schematic's blocks array to a position.
83	/// See <https://minecraft.wiki/w/Schematic_file_format#NBT_Structure>.
84	#[inline]
85	#[must_use]
86	pub fn index_position(self, i: i32) -> Xyz {
87		Xyz {
88			x: i % i32::from(self.width),
89			y: i / (i32::from(self.width) * i32::from(self.length)),
90			z: (i / i32::from(self.width)) % i32::from(self.length),
91		}
92	}
93}
94
95impl From<Size> for Xyz {
96	fn from(value: Size) -> Self {
97		Self {
98			x: i32::from(value.width),
99			y: i32::from(value.height),
100			z: i32::from(value.length),
101		}
102	}
103}
104
105/// Minecraft [block entity](https://minecraft.wiki/w/Block_entity_format) data.
106#[derive(Debug, Clone, Deserialize, Serialize)]
107pub struct MinecraftBlockEntity {
108	pub id: String,
109	pub x: i32,
110	pub y: i32,
111	pub z: i32,
112
113	#[serde(flatten, skip_serializing)]
114	pub components: Map<String, mininbt::Value>,
115}
116
117impl MinecraftBlockEntity {
118	#[must_use]
119	pub const fn position(&self) -> Xyz {
120		Xyz {
121			x: self.x,
122			y: self.y,
123			z: self.z,
124		}
125	}
126}
127
128/// Minecraft [block entity](https://minecraft.wiki/w/Block_entity_format) data with no position.
129#[derive(Debug, Clone, Deserialize, Serialize)]
130pub struct MinecraftBlockData {
131	pub id: String,
132	#[serde(flatten, skip_serializing)]
133	pub components: Map<String, mininbt::Value>,
134}