From 43116c54e9163b3f6aa2e3218dcc26a11e9c5bd1 Mon Sep 17 00:00:00 2001 From: kralverde <80051564+kralverde@users.noreply.github.com> Date: Tue, 22 Oct 2024 05:27:40 -0400 Subject: [PATCH] fix tick system (#169) * fix tick system * also async wait on stdin --- Cargo.toml | 1 + pumpkin/src/main.rs | 8 ++++++-- pumpkin/src/server/ticker.rs | 4 +++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d32f8e1a0..eacd58092 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ tokio = { version = "1.40", features = [ "net", "rt-multi-thread", "sync", + "io-std", ] } # Concurrency/Parallelism and Synchronization diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index 74c51e921..d6e5cde48 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -30,6 +30,7 @@ use log::LevelFilter; use client::Client; use server::{ticker::Ticker, Server}; use std::io::{self}; +use tokio::io::{AsyncBufReadExt, BufReader}; // Setup some tokens to allow us to identify which event is for which socket. @@ -140,11 +141,14 @@ async fn main() -> io::Result<()> { if use_console { let server = server.clone(); tokio::spawn(async move { - let stdin = std::io::stdin(); + let stdin = tokio::io::stdin(); + let mut reader = BufReader::new(stdin); loop { let mut out = String::new(); - stdin + + reader .read_line(&mut out) + .await .expect("Failed to read console line"); if !out.is_empty() { diff --git a/pumpkin/src/server/ticker.rs b/pumpkin/src/server/ticker.rs index a8395b6a6..ea7d32847 100644 --- a/pumpkin/src/server/ticker.rs +++ b/pumpkin/src/server/ticker.rs @@ -1,5 +1,7 @@ use std::time::{Duration, Instant}; +use tokio::time::sleep; + use super::Server; pub struct Ticker { @@ -28,7 +30,7 @@ impl Ticker { } else { // Wait for the remaining time until the next tick let sleep_time = self.tick_interval - elapsed; - std::thread::sleep(sleep_time); + sleep(sleep_time).await; } } }