Skip to content

Commit

Permalink
Fix nbt position translation
Browse files Browse the repository at this point in the history
  • Loading branch information
rollrat committed Nov 17, 2024
1 parent ec5069e commit 88506fd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
69 changes: 36 additions & 33 deletions src/nbt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,39 +108,42 @@ fn nbt_block_name(block: &Block) -> (String, String, Option<NBTPaletteProperty>)
let (palette_name, specify_name, property) = match block.kind {
BlockKind::Air => ("air", "air".to_owned(), None),
BlockKind::Cobble { .. } => ("stone_bricks", "stone_bricks".to_owned(), None),
BlockKind::Switch { is_on } => (
"lever",
"lever".to_owned(),
Some(NBTPaletteProperty {
face: match block.direction {
Direction::Bottom => Some("floor".to_owned()),
Direction::Top => Some("ceiling".to_owned()),
Direction::East | Direction::West | Direction::South | Direction::North => {
Some("wall".to_owned())
}
_ => unreachable!(),
},
facing: match block.direction {
Direction::Bottom | Direction::Top => None,
Direction::East => Some("south".to_owned()),
Direction::West => Some("north".to_owned()),
Direction::South => Some("west".to_owned()),
Direction::North => Some("east".to_owned()),
_ => unreachable!(),
},
powered: is_on.then(|| "true".to_owned()),
..Default::default()
}),
),
BlockKind::Switch { is_on } => {
let facing = match block.direction {
Direction::Bottom | Direction::Top => None,
Direction::East => Some("south".to_owned()),
Direction::West => Some("north".to_owned()),
Direction::South => Some("east".to_owned()),
Direction::North => Some("west".to_owned()),
_ => unreachable!(),
};
(
"lever",
format!("lever_{is_on}_{}", facing.clone().unwrap_or_default()),
Some(NBTPaletteProperty {
face: match block.direction {
Direction::Bottom => Some("floor".to_owned()),
Direction::Top => Some("ceiling".to_owned()),
Direction::East | Direction::West | Direction::South | Direction::North => {
Some("wall".to_owned())
}
_ => unreachable!(),
},
facing,
powered: is_on.then(|| "true".to_owned()),
..Default::default()
}),
)
}
BlockKind::Redstone {
state, strength, ..
} => (
"redstone_wire",
format!("redstone_wire_{}_{}", strength, state),
Some(NBTPaletteProperty {
power: Some(strength.to_string()),
east: ((state & RedstoneState::South as usize) > 0).then(|| "side".to_owned()),
west: ((state & RedstoneState::North as usize) > 0).then(|| "side".to_owned()),
east: ((state & RedstoneState::North as usize) > 0).then(|| "side".to_owned()),
west: ((state & RedstoneState::South as usize) > 0).then(|| "side".to_owned()),
south: ((state & RedstoneState::West as usize) > 0).then(|| "side".to_owned()),
north: ((state & RedstoneState::East as usize) > 0).then(|| "side".to_owned()),
..Default::default()
Expand All @@ -151,10 +154,10 @@ fn nbt_block_name(block: &Block) -> (String, String, Option<NBTPaletteProperty>)
("redstone_torch", "redstone_torch".to_owned(), None)
} else {
let facing = match block.direction {
Direction::East => "south",
Direction::West => "north",
Direction::South => "west",
Direction::North => "east",
Direction::East => "north",
Direction::West => "south",
Direction::South => "east",
Direction::North => "west",
_ => unreachable!(),
}
.to_owned();
Expand Down Expand Up @@ -186,7 +189,7 @@ fn nbt_block_name(block: &Block) -> (String, String, Option<NBTPaletteProperty>)

(
"repeater",
"repeater".to_owned(),
format!("repeater_{facing}_{is_on}_{is_locked}_{delay}"),
Some(NBTPaletteProperty {
facing: Some(facing),
delay: Some(delay.to_string()),
Expand Down Expand Up @@ -224,15 +227,15 @@ fn world3d_to_nbt(world: &World3D) -> NBTRoot {

blocks.push(NBTBlock {
state: palette_index[&specify_name] as i32,
pos: (pos.2 as i32, pos.0 as i32, pos.1 as i32),
pos: (pos.1 as i32, pos.2 as i32, pos.0 as i32),
});
}

NBTRoot {
size: (
world.size.1 as i32,
world.size.2 as i32,
world.size.0 as i32,
world.size.1 as i32,
),
blocks,
palette,
Expand Down
2 changes: 1 addition & 1 deletion src/transform/placer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
logic::LogicType,
world::{
block::{Block, BlockKind, Direction, RedstoneState},
position::{self, DimSize, Position},
position::{DimSize, Position},
world::World3D,
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/world/position.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::block::{Direction, RedstoneState, RedstoneStateType};

// 위치
// 위치 (x, y, z)
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct Position(pub usize, pub usize, pub usize);

Expand Down
Binary file modified test/and-gate.nbt
Binary file not shown.

0 comments on commit 88506fd

Please sign in to comment.