panicbit / rust-rcon

An RCON implementation in Rust
Apache License 2.0
32 stars 14 forks source link

Tokio Example? #27

Open timelessnesses opened 1 year ago

timelessnesses commented 1 year ago

So I tried using rcon with tokio and looks like there's none but I figured out by myself.
For anyone who wondering what is tokio example looks like, here's example code.

use rcon;
use toml;
use std;
use tokio;

#[tokio::main]
async fn main(){
    let config: toml::Value = toml::from_str(include_str!("../config.toml")).unwrap();

    let mut server = <rcon::Connection<tokio::net::TcpStream>>::builder()
    .enable_minecraft_quirks(true)
    .connect(format!("{}:{}", config["server"]["ip"].as_str().expect("Server IP isn't specified"), config["server"]["port"].as_str().unwrap_or("25575")), config["server"]["password"].as_str().unwrap())
    .await.unwrap();

    println!("Connected to the server! ({})\nRun `exit` command to exit the program or CTRL + C!", config["server"]["ip"].as_str().unwrap());

    loop {
        println!("Summoning A Zombie ");
        let res = server.cmd("summon minecraft:zombie -775.65 26.00 -180.72 ").await;
        if res.is_err() {
            println!("Server Error Out!");
            continue
        }
        println!("Server Response: {}", res.unwrap());
    }
    println!("Bye!")
}
JacobMunoz commented 10 months ago

Are you able to provide the Cargo.toml file for this? I'm struggling to get anything to build in rust, it would be very helpful - thanks.

timelessnesses commented 10 months ago

you can check this repo i made https://github.com/timelessnesses/minecraft-rust-rcon