Initialize Minecraft text generator project with basic structure and CLI functionality

This commit is contained in:
2026-03-23 00:56:19 +01:00
commit 55d730d542
20 changed files with 900 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
mod mcfunction;
pub use mcfunction::McFunctionExporter;

View 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"
}
}