nucleation 0.3.16

A high-performance Minecraft schematic parser and utility library
Documentation
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use crate::BlockState;

/// Axis enum for transformations
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Axis {
    X,
    Y,
    Z,
}

/// Direction mapping for Minecraft blocks
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
    North,
    South,
    East,
    West,
    Up,
    Down,
}

impl Direction {
    pub fn from_str(s: &str) -> Option<Self> {
        match s {
            "north" => Some(Direction::North),
            "south" => Some(Direction::South),
            "east" => Some(Direction::East),
            "west" => Some(Direction::West),
            "up" => Some(Direction::Up),
            "down" => Some(Direction::Down),
            _ => None,
        }
    }

    pub fn to_str(&self) -> &'static str {
        match self {
            Direction::North => "north",
            Direction::South => "south",
            Direction::East => "east",
            Direction::West => "west",
            Direction::Up => "up",
            Direction::Down => "down",
        }
    }

    /// Apply flip transformation to direction
    pub fn flip(&self, axis: Axis) -> Direction {
        match axis {
            Axis::X => match self {
                Direction::East => Direction::West,
                Direction::West => Direction::East,
                _ => *self,
            },
            Axis::Y => match self {
                Direction::Up => Direction::Down,
                Direction::Down => Direction::Up,
                _ => *self,
            },
            Axis::Z => match self {
                Direction::North => Direction::South,
                Direction::South => Direction::North,
                _ => *self,
            },
        }
    }

    /// Rotate direction around Y axis (horizontal plane)
    pub fn rotate_y(&self, degrees: i32) -> Direction {
        let rotations = degrees.rem_euclid(360) / 90;

        let mut current = *self;
        for _ in 0..rotations {
            current = match current {
                Direction::North => Direction::East,
                Direction::East => Direction::South,
                Direction::South => Direction::West,
                Direction::West => Direction::North,
                _ => current, // Up and Down don't change
            };
        }
        current
    }

    /// Rotate direction around X axis
    pub fn rotate_x(&self, degrees: i32) -> Direction {
        let rotations = degrees.rem_euclid(360) / 90;

        let mut current = *self;
        for _ in 0..rotations {
            current = match current {
                Direction::Up => Direction::South,
                Direction::South => Direction::Down,
                Direction::Down => Direction::North,
                Direction::North => Direction::Up,
                _ => current, // East and West don't change
            };
        }
        current
    }

    /// Rotate direction around Z axis
    pub fn rotate_z(&self, degrees: i32) -> Direction {
        let rotations = degrees.rem_euclid(360) / 90;

        let mut current = *self;
        for _ in 0..rotations {
            current = match current {
                Direction::Up => Direction::West,
                Direction::West => Direction::Down,
                Direction::Down => Direction::East,
                Direction::East => Direction::Up,
                _ => current, // North and South don't change
            };
        }
        current
    }
}

/// Transform block state properties based on flip operation
pub fn transform_block_state_flip(block: &BlockState, axis: Axis) -> BlockState {
    let mut new_block = block.clone();

    // Transform 'facing' property
    if let Some(facing) = block.get_property("facing") {
        if let Some(dir) = Direction::from_str(facing) {
            let new_dir = dir.flip(axis);
            new_block.set_property("facing".to_string(), new_dir.to_str().to_string());
        }
    }

    // Transform 'axis' property
    if let Some(axis_val) = block.get_property("axis") {
        let new_axis = match (axis, axis_val.as_str()) {
            (Axis::X, "x") => "x", // X flip doesn't change x axis
            (Axis::Y, "y") => "y", // Y flip doesn't change y axis
            (Axis::Z, "z") => "z", // Z flip doesn't change z axis
            _ => axis_val.as_str(),
        };
        new_block.set_property("axis".to_string(), new_axis.to_string());
    }

    // Transform 'rotation' property (0-15, used by standing signs, banners, etc.)
    if let Some(rotation) = block.get_property("rotation") {
        if let Ok(rot_val) = rotation.parse::<i32>() {
            let new_rotation = match axis {
                Axis::Y => rot_val, // Y flip doesn't change rotation around Y
                Axis::X | Axis::Z => {
                    // Mirror the rotation
                    (16 - rot_val) % 16
                }
            };
            new_block.set_property("rotation".to_string(), new_rotation.to_string());
        }
    }

    // Transform directional properties for specific blocks
    transform_special_block_properties(&mut new_block, axis, false, 0);

    new_block
}

/// Transform block state properties based on rotation operation
pub fn transform_block_state_rotate(block: &BlockState, axis: Axis, degrees: i32) -> BlockState {
    let mut new_block = block.clone();

    // Transform 'facing' property
    if let Some(facing) = block.get_property("facing") {
        if let Some(dir) = Direction::from_str(facing) {
            let new_dir = match axis {
                Axis::Y => dir.rotate_y(degrees),
                Axis::X => dir.rotate_x(degrees),
                Axis::Z => dir.rotate_z(degrees),
            };
            new_block.set_property("facing".to_string(), new_dir.to_str().to_string());
        }
    }

    // Transform 'axis' property (logs, pillars, etc.)
    if let Some(axis_val) = block.get_property("axis") {
        let new_axis = rotate_axis_property(axis_val, axis, degrees);
        new_block.set_property("axis".to_string(), new_axis);
    }

    // Transform 'rotation' property (0-15, used by standing signs, banners, etc.)
    if let Some(rotation) = block.get_property("rotation") {
        if let Ok(rot_val) = rotation.parse::<i32>() {
            let rotations = degrees.rem_euclid(360) / 90;
            let new_rotation = match axis {
                Axis::Y => (rot_val + rotations * 4) % 16,
                Axis::X | Axis::Z => rot_val, // Rotation around these axes doesn't change standing rotation
            };
            new_block.set_property("rotation".to_string(), new_rotation.to_string());
        }
    }

    // Transform directional properties for specific blocks
    transform_special_block_properties(&mut new_block, axis, true, degrees);

    new_block
}

/// Rotate axis property value
fn rotate_axis_property(axis_val: &str, rotation_axis: Axis, degrees: i32) -> String {
    let rotations = degrees.rem_euclid(360) / 90;

    if rotations == 0 || rotations == 2 {
        // 0° or 180° rotation
        return axis_val.to_string();
    }

    // 90° or 270° rotation
    let result = match rotation_axis {
        Axis::Y => match axis_val {
            "x" => "z",
            "z" => "x",
            "y" => "y",
            _ => axis_val,
        },
        Axis::X => match axis_val {
            "y" => "z",
            "z" => "y",
            "x" => "x",
            _ => axis_val,
        },
        Axis::Z => match axis_val {
            "x" => "y",
            "y" => "x",
            "z" => "z",
            _ => axis_val,
        },
    };
    result.to_string()
}

/// Handle special block properties (stairs, slabs, redstone, etc.)
fn transform_special_block_properties(
    block: &mut BlockState,
    axis: Axis,
    is_rotation: bool,
    degrees: i32,
) {
    // Handle stair shapes
    if let Some(shape) = block.get_property("shape") {
        if is_rotation && axis == Axis::Y {
            let new_shape = rotate_stair_shape(shape, degrees);
            block.set_property("shape".to_string(), new_shape);
        }
    }

    // Handle redstone wire connections
    let mut wire_updates: Vec<(&'static str, String)> = Vec::new();
    let mut wire_removals: Vec<&'static str> = Vec::new();

    for direction in &["north", "south", "east", "west"] {
        if let Some(connection) = block.get_property(direction) {
            let connection_value = connection.clone();
            if is_rotation && axis == Axis::Y {
                let dir = Direction::from_str(direction).unwrap();
                let new_dir = dir.rotate_y(degrees);
                // Move the connection value to the new direction
                wire_removals.push(direction);
                wire_updates.push((new_dir.to_str(), connection_value.to_string()));
            } else if !is_rotation {
                let dir = Direction::from_str(direction).unwrap();
                let new_dir = dir.flip(axis);
                if new_dir.to_str() != *direction {
                    // Swap connection values
                    wire_removals.push(direction);
                    wire_updates.push((new_dir.to_str(), connection_value.to_string()));
                }
            }
        }
    }

    // Apply updates and removals
    for dir in wire_removals {
        block.remove_property(dir);
    }
    for (dir, value) in wire_updates {
        block.set_property(dir.to_string(), value);
    }

    // Handle door hinges (left/right)
    if let Some(hinge) = block.get_property("hinge") {
        if !is_rotation && (axis == Axis::X || axis == Axis::Z) {
            let new_hinge = match hinge.as_str() {
                "left" => "right",
                "right" => "left",
                _ => hinge.as_str(),
            };
            block.set_property("hinge".to_string(), new_hinge.to_string());
        }
    }
}

/// Rotate stair shape
fn rotate_stair_shape(shape: &str, degrees: i32) -> String {
    let rotations = degrees.rem_euclid(360) / 90;

    if rotations == 0 || rotations == 2 {
        return shape.to_string();
    }

    // For 90° or 270° rotations
    let result = match shape {
        "inner_left" => "inner_right",
        "inner_right" => "inner_left",
        "outer_left" => "outer_right",
        "outer_right" => "outer_left",
        _ => shape,
    };
    result.to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use smol_str::SmolStr;

    #[test]
    fn test_direction_flip_x() {
        assert_eq!(Direction::East.flip(Axis::X), Direction::West);
        assert_eq!(Direction::West.flip(Axis::X), Direction::East);
        assert_eq!(Direction::North.flip(Axis::X), Direction::North);
        assert_eq!(Direction::Up.flip(Axis::X), Direction::Up);
    }

    #[test]
    fn test_direction_flip_y() {
        assert_eq!(Direction::Up.flip(Axis::Y), Direction::Down);
        assert_eq!(Direction::Down.flip(Axis::Y), Direction::Up);
        assert_eq!(Direction::North.flip(Axis::Y), Direction::North);
    }

    #[test]
    fn test_direction_flip_z() {
        assert_eq!(Direction::North.flip(Axis::Z), Direction::South);
        assert_eq!(Direction::South.flip(Axis::Z), Direction::North);
        assert_eq!(Direction::East.flip(Axis::Z), Direction::East);
    }

    #[test]
    fn test_direction_rotate_y() {
        assert_eq!(Direction::North.rotate_y(90), Direction::East);
        assert_eq!(Direction::East.rotate_y(90), Direction::South);
        assert_eq!(Direction::South.rotate_y(90), Direction::West);
        assert_eq!(Direction::West.rotate_y(90), Direction::North);
        assert_eq!(Direction::North.rotate_y(180), Direction::South);
        assert_eq!(Direction::Up.rotate_y(90), Direction::Up);
    }

    #[test]
    fn test_transform_facing_flip() {
        let mut block = BlockState::new("minecraft:lever".to_string());
        block.set_property("facing".to_string(), "east".to_string());

        let transformed = transform_block_state_flip(&block, Axis::X);
        assert_eq!(
            transformed.get_property("facing"),
            Some(&SmolStr::from("west"))
        );
    }

    #[test]
    fn test_transform_facing_rotate() {
        let mut block = BlockState::new("minecraft:lever".to_string());
        block.set_property("facing".to_string(), "north".to_string());

        let transformed = transform_block_state_rotate(&block, Axis::Y, 90);
        assert_eq!(
            transformed.get_property("facing"),
            Some(&SmolStr::from("east"))
        );

        let transformed_180 = transform_block_state_rotate(&block, Axis::Y, 180);
        assert_eq!(
            transformed_180.get_property("facing"),
            Some(&SmolStr::from("south"))
        );
    }

    #[test]
    fn test_rotate_axis_property() {
        assert_eq!(rotate_axis_property("x", Axis::Y, 90), "z".to_string());
        assert_eq!(rotate_axis_property("z", Axis::Y, 90), "x".to_string());
        assert_eq!(rotate_axis_property("y", Axis::Y, 90), "y".to_string());
        assert_eq!(rotate_axis_property("x", Axis::Y, 180), "x".to_string());
    }

    #[test]
    fn test_rotation_property() {
        let mut block = BlockState::new("minecraft:standing_sign".to_string());
        block.set_property("rotation".to_string(), "0".to_string());

        let transformed = transform_block_state_rotate(&block, Axis::Y, 90);
        assert_eq!(
            transformed.get_property("rotation"),
            Some(&SmolStr::from("4"))
        );

        let transformed_180 = transform_block_state_rotate(&block, Axis::Y, 180);
        assert_eq!(
            transformed_180.get_property("rotation"),
            Some(&SmolStr::from("8"))
        );
    }
}