From 2a9336a8a3317e38397a6a0a163159b0bdd23b99 Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Mon, 29 Apr 2019 00:03:26 -0400 Subject: [PATCH] Add "lithography" binary that places blocks Input in form of rows: x y z block Where z is the vertial axis as Euler intended --- mc-mask/src/bin/litho.rs | 52 ++++++++++++++++++++++++++++++++++++++++ mc-mask/src/main.rs | 37 ---------------------------- 2 files changed, 52 insertions(+), 37 deletions(-) create mode 100644 mc-mask/src/bin/litho.rs delete mode 100644 mc-mask/src/main.rs diff --git a/mc-mask/src/bin/litho.rs b/mc-mask/src/bin/litho.rs new file mode 100644 index 0000000..945d7f7 --- /dev/null +++ b/mc-mask/src/bin/litho.rs @@ -0,0 +1,52 @@ +use rcon::{self, Connection}; +use regex::Regex; +use std::{ + error::Error, + io::{self, BufRead}, + env, +}; + +fn block_below_player(conn: &mut Connection, user: &str) + -> Result<(i32, i32, i32), Box> + { + let cmd = format!("execute at {user} run tp {user} ~ ~ ~", user=user); + let text = conn.cmd(&cmd)?; + let re = Regex::new(r"Teleported .* to (.*), (.*), (.*)").unwrap(); + if let Some(cap) = re.captures(&text) { + let x = (cap[1].parse::()? + 0.0) as i32; + let y = (cap[2].parse::()? - 1.0) as i32; + let z = (cap[3].parse::()? - 1.0) as i32; + Ok((x, y, z)) + } else { + Err(text)? + } +} + +fn main() -> Result<(), Box> { + if let [_, addr, pass, user] = &env::args().collect::>()[..] { + let mut conn = Connection::connect(&addr, &pass)?; + let (x0, y0, z0) = block_below_player(&mut conn, &user)?; + println!("Placing design at player {}, at {} {} {}", user, x0, y0, z0); + println!("Reading commands on stdin"); + for (i, line) in io::stdin().lock().lines().enumerate() { + let line = line?; + let mut parts = line.split_whitespace(); + let x = parts.next().ok_or("Missing x")?.parse::()?; + let y = parts.next().ok_or("Missing y")?.parse::()?; + let z = parts.next().ok_or("Missing z")?.parse::()?; + let block = parts.next().ok_or("Missing block name")?; + if let Some(txt) = parts.next() { + println!("Warning: Unexpected trailing {} on line {}", txt, i); + } + // We shuffle y and z in input coordinates in order to make coordinates good :3 + conn.cmd(&format!("setblock {} {} {} {}", x0 + x, y0 + z, z0 + y, block))?; + } + Ok(()) + } else { + println!( + "Usage: {} ", + env::args().nth(0).unwrap(), + ); + Ok(()) + } +} diff --git a/mc-mask/src/main.rs b/mc-mask/src/main.rs deleted file mode 100644 index 1f0cacc..0000000 --- a/mc-mask/src/main.rs +++ /dev/null @@ -1,37 +0,0 @@ -use rcon::{self, Connection}; -use regex::Regex; -use std::error::Error; - -fn block_below_player(conn: &mut Connection, user: &str) - -> Result<(i32, i32, i32), Box> - { - let cmd = format!("execute at {user} run tp {user} ~ ~ ~", user=user); - let text = conn.cmd(&cmd)?; - let re = Regex::new(r"Teleported .* to (.*), (.*), (.*)").unwrap(); - if let Some(cap) = re.captures(&text) { - let x = (cap[1].parse::()? + 0.0) as i32; - let y = (cap[2].parse::()? - 1.0) as i32; - let z = (cap[3].parse::()? - 1.0) as i32; - Ok((x, y, z)) - } else { - Err(text)? - } -} - -fn main() -> Result<(), Box> { - if std::env::args().len() != 4 { - println!( - "Usage: {} ", - std::env::args().nth(0).unwrap(), - ); - return Ok(()); - } - let addr = std::env::args().nth(1).unwrap(); - let password = std::env::args().nth(2).unwrap(); - let user = std::env::args().nth(3).unwrap(); - let mut conn = Connection::connect(&addr, &password)?; - let (x, y, z) = block_below_player(&mut conn, &user)?; - let cmd = format!("setblock {} {} {} gold_block", x, y, z); - conn.cmd(&cmd)?; - Ok(()) -} -- 2.47.0