1 unstable release

0.1.0 Aug 6, 2020

#812 in Games

MIT license

16KB
206 lines

Serbo

Minecraft Server Manager in Rust

How to use

To manage one or more servers, you must first create a Manager. This struct will allow you to control and manage multiple servers.

A manager requires three arguments: A folder to contain the files of the servers you are managing, a version folder, which contains folders containing server files that correspond to the versions of Minecraft that you are supporting (they should be named as such: 1.16.1, 1.15.2 ...), and a jar_name, which is the name of the jarfile that serbo should execute to start the server.

You call methods on this manager to create, delete, start, stop, change a server's version, and to obtain a reference to a struct that represents an online server called an Instance.

With an Instance, you have access to methods that can access stdout (the server output), send a command to the server (via stdin), or stop that specific server.


lib.rs:

Allows for simple control, and input / output of minecraft servers.

Examples

use serbo;
use std::error::Error;
use std::io;

fn main() -> Result<(), Box<dyn Error>> {
 let mut manager = serbo::Manager::new("servers", "versions","fabric-server-launch.jar");
 let port = 25565;
 let id = "1";
 loop {
   let reader = io::stdin();
   let mut buf = String::new();
   println!("Enter your command.");
   reader.read_line(&mut buf)?;
   match buf.trim() {
     "delete" => {
       match manager.delete(id){
         Ok(_) => println!("Server deleted."),
         Err(e) => println!("{}",e)
       }
     }
     "change_version" => {
       let mut send_buf = String::new();
       println!("Enter the version to change to.");
       reader.read_line(&mut send_buf)?;
       //Remove the newline from read_line
       send_buf = send_buf[..send_buf.chars().count() - 1].to_string();
       manager.change_version(id, &send_buf)?;
     }
     "create" => match manager.create(id, "1.16.1-fabric") {
       Ok(_) => println!("Server Created"),
       Err(e) => println!("{}", e),
     },
     "stop" => {
       //Stops the server
       println!("Server stopping.");
       manager.stop(id)?;
       break Ok(());
     }
     "start" => {
       //Starts the server
       println!("Server starting.");
       match manager.start(id, port) {
         Err(e) => println!("{}", e),
         Ok(_) => println!("Server started!"),
       };
     }
     "send" => {
       //Prompts for a command to send to the server
       let instance = manager.get(id);
       match instance {
         Some(i) => {
           let mut send_buf = String::new();
           println!("Enter the command to send to the server.");
           reader.read_line(&mut send_buf)?;
           //Remove the newline from read_line
           send_buf = send_buf[..send_buf.chars().count() - 1].to_string();
           i.send(send_buf)?;
         }
         None => println!("Server offline."),
       }
     }
     "get" => {
       //Gets the last 5 stdout lines
       let instance: &serbo::Instance = manager.get(id).unwrap();
       let vec = instance.get(0);
       let length = vec.len();
       //Create a vec from the last 5 lines
       let trimmed_vec;
       if length >= 5 {
         trimmed_vec = Vec::from(&vec[length - 5..]);
       } else {
         trimmed_vec = Vec::from(vec);
       }
       for line in trimmed_vec {
         println!("{}", line);
       }
     }
     _ => {
       println!("Unrecognized command");
     }
   }
 }
}

Dependencies

~120–305KB