server bindings and eula util

This commit is contained in:
2025-12-08 00:26:07 +01:00
parent e2cbd16dc8
commit 7e7bfbc576
2 changed files with 50 additions and 3 deletions

View File

@@ -91,6 +91,11 @@ pub enum ServerError {
#[error("Failed to write to stdin")]
StdinWriteFailed,
#[error("Failed to open eula.txt")]
NoEULA,
#[error("Failed to write eula.txt")]
WriteEULAFailed,
}
#[cfg(feature = "events")]

View File

@@ -3,13 +3,14 @@ use std::{ops::RangeInclusive, path::PathBuf, str::FromStr};
use tokio::{
fs::{File, create_dir},
io::{self, AsyncWriteExt},
sync::RwLock,
sync::{RwLock, watch},
};
use tokio_stream::wrappers::BroadcastStream;
use uuid::Uuid;
use crate::{
config::{MinecraftType, MinecraftVersion, Version},
error::CreationError,
config::{MinecraftType, MinecraftVersion, StreamSource, Version, stream::InstanceEvent},
error::{CreationError, ServerError, SubscribeError},
instance::InstanceHandle,
manifests::vanilla::{VanillaManifestV2, VanillaManifestV2Version, VanillaReleaseManifest},
server,
@@ -120,4 +121,45 @@ impl MineGuardServer {
Ok(server)
}
pub async fn start(&self) -> Result<(), ServerError> {
let mut handle_w = self.handle.write().await;
let res = handle_w.start().await;
res
}
pub async fn kill(&self) -> Result<(), ServerError> {
let mut handle_w = self.handle.write().await;
let res = handle_w.kill().await;
res
}
pub async fn stop(&self) -> Result<(), ServerError> {
let mut handle_w = self.handle.write().await;
let res = handle_w.stop().await;
res
}
pub async fn subscribe(
&self,
stream: StreamSource,
) -> Result<BroadcastStream<InstanceEvent>, SubscribeError> {
let handle_r = self.handle.read().await;
let res = handle_r.subscribe(stream);
res
}
pub async fn accept_eula(&self) -> Result<(), ServerError> {
let config_r = self.config.read().await;
let eula_path = config_r.server_dir.join("eula.txt");
let mut out = File::create(eula_path)
.await
.map_err(|_| ServerError::NoEULA)?;
out.write_all(b"#Generated by MineGuard\neula=true\n")
.await
.map_err(|_| ServerError::WriteEULAFailed)?;
Ok(())
}
}