Initialize Minecraft text generator project with basic structure and CLI functionality
This commit is contained in:
10
crates/exporters/Cargo.toml
Normal file
10
crates/exporters/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "exporters"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
lib = { path = "../lib" }
|
||||
anyhow = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
3
crates/exporters/src/lib.rs
Normal file
3
crates/exporters/src/lib.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
mod mcfunction;
|
||||
|
||||
pub use mcfunction::McFunctionExporter;
|
||||
50
crates/exporters/src/mcfunction.rs
Normal file
50
crates/exporters/src/mcfunction.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use lib::{StructureExporter, VoxelType};
|
||||
|
||||
pub struct McFunctionExporter {
|
||||
palette: HashMap<VoxelType, String>,
|
||||
}
|
||||
|
||||
impl McFunctionExporter {
|
||||
pub fn new(body_block: &str, outline_block: &str) -> Self {
|
||||
let mut palette = HashMap::new();
|
||||
|
||||
palette.insert(VoxelType::Body, body_block.to_string());
|
||||
palette.insert(VoxelType::Outline, outline_block.to_string());
|
||||
|
||||
Self { palette }
|
||||
}
|
||||
}
|
||||
|
||||
impl StructureExporter for McFunctionExporter {
|
||||
fn export(&self, grid: &lib::VoxelGrid) -> anyhow::Result<Vec<u8>> {
|
||||
let mut output = String::new();
|
||||
|
||||
output.push_str(&format!("# Generated by Minecraft Text Builder\n"));
|
||||
output.push_str(&format!(
|
||||
"# Dimensions: {}x{}x{}\n\n",
|
||||
grid.width, grid.height, grid.depth
|
||||
));
|
||||
|
||||
for z in 0..grid.depth {
|
||||
for y in 0..grid.height {
|
||||
for x in 0..grid.width {
|
||||
if let Some(voxel_type) = grid.get(x, y, z) {
|
||||
if let Some(block_id) = self.palette.get(&voxel_type) {
|
||||
// ~x ~y ~z generates blocks relative to where the command is executed
|
||||
let command = format!("setblock ~{} ~{} ~{} {}\n", x, y, z, block_id);
|
||||
output.push_str(&command);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(output.into_bytes())
|
||||
}
|
||||
|
||||
fn file_extension(&self) -> &'static str {
|
||||
"mcfunction"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user