From f8a65ba2f3b3134aff4bdf102b1c5b3580199db3 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 13 Sep 2021 12:18:18 +0200 Subject: [PATCH] Added very basic proof-of-concept --- .gitignore | 5 +++++ Cargo.toml | 12 +++++++++++ src/main.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index 8ffd9f4..d1bb021 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,8 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk + + +# Added by cargo + +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..804a627 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "mc-wrapper" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +# Used for creating tarballs for backups +tar = "0.4.37" +# Used to compress said tarballs using gzip +flate2 = "1.0.21" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e331182 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,59 @@ +use std::io; +use std::io::Write; +use std::process::{Child, Command, Stdio}; + +struct Server { + child: Child, +} + +impl Server { + fn spawn() -> Server { + let child = Command::new("/usr/lib/jvm/java-16-openjdk/bin/java") + .arg("-jar") + .arg("paper-1.17.1-259.jar") + .arg("--universe") + .arg("worlds") + .arg("--nogui") + .stdin(Stdio::piped()) + .spawn() + .expect("Couldn't start child process"); + + Server { child } + } + + pub fn send_command(&mut self, cmd: &str) { + match cmd.trim() { + "stop" | "exit" => self.stop(), + s => self.custom(s), + } + } + + fn custom(&mut self, cmd: &str) { + let mut stdin = self.child.stdin.as_ref().unwrap(); + stdin.write_all(cmd.as_bytes()).unwrap(); + stdin.flush().unwrap(); + } + + pub fn stop(&mut self) { + self.custom("stop"); + self.child.wait(); + } +} + +fn main() { + let mut server = Server::spawn(); + + let stdin = io::stdin(); + let input = &mut String::new(); + + loop { + input.clear(); + stdin.read_line(input); + println!("input: {}", input.trim()); + server.send_command(input); + + if input.trim() == "stop" { + break; + } + } +}