]> Witch of Git - minecraft-eda/blob - mc-mask/src/bin/litho.rs
Add romgen, a tool to generate ROMs for litho
[minecraft-eda] / mc-mask / src / bin / litho.rs
1 use rcon::{self, Connection};
2 use regex::Regex;
3 use std::{
4 error::Error,
5 io::{self, BufRead},
6 env,
7 };
8
9 fn block_below_player(conn: &mut Connection, user: &str)
10 -> Result<(i32, i32, i32), Box<dyn Error>>
11 {
12 let cmd = format!("execute at {user} run tp {user} ~ ~ ~", user=user);
13 let text = conn.cmd(&cmd)?;
14 let re = Regex::new(r"Teleported .* to (.*), (.*), (.*)").unwrap();
15 if let Some(cap) = re.captures(&text) {
16 let x = (cap[1].parse::<f32>()? + 0.0) as i32;
17 let y = (cap[2].parse::<f32>()? - 1.0) as i32;
18 let z = (cap[3].parse::<f32>()? - 1.0) as i32;
19 Ok((x, y, z))
20 } else {
21 Err(text)?
22 }
23 }
24
25 fn main() -> Result<(), Box<dyn Error>> {
26 if let [_, addr, pass, user] = &env::args().collect::<Vec<_>>()[..] {
27 let mut conn = Connection::connect(&addr, &pass)?;
28 let (x0, y0, z0) = block_below_player(&mut conn, &user)?;
29 println!("Placing design at player {}, at {} {} {}", user, x0, y0, z0);
30 println!("Reading commands on stdin");
31 for (i, line) in io::stdin().lock().lines().enumerate() {
32 let line = line?;
33 let mut parts = line.split_whitespace();
34 let x = parts.next().ok_or("Missing x")?.parse::<i32>()?;
35 let y = parts.next().ok_or("Missing y")?.parse::<i32>()?;
36 let z = parts.next().ok_or("Missing z")?.parse::<i32>()?;
37 let block = parts.next().ok_or("Missing block name")?;
38 if let Some(txt) = parts.next() {
39 println!("Warning: Unexpected trailing {} on line {}", txt, i);
40 }
41 // We shuffle y and z in input coordinates in order to make coordinates good :3
42 conn.cmd(&format!("setblock {} {} {} {}", x0 + x, y0 + z, z0 + y, block))?;
43 }
44 Ok(())
45 } else {
46 println!(
47 "Usage: {} <server> <rcon-password> <user>",
48 env::args().nth(0).unwrap(),
49 );
50 Ok(())
51 }
52 }