diff --git a/src/command/ban.rs b/src/command/ban.rs index 42e2dcf..8783e2b 100644 --- a/src/command/ban.rs +++ b/src/command/ban.rs @@ -1,14 +1,16 @@ use super::{Command, Selector}; +#[derive(Default)] pub struct BanCommand { - target: Selector, + target: Option, reason: Option, ip: Option, } impl BanCommand { - pub fn new(target: Selector) -> Self { - Self { target, reason: None, ip: None } + pub fn target(mut self, target: Selector) -> Self { + self.target = Some(target); + self } pub fn reason(mut self, reason: impl Into) -> Self { @@ -22,7 +24,9 @@ impl BanCommand { } pub fn build(self) -> Command { - let target = self.target; + let target = self + .target + .unwrap_or(Selector::Player("nonexistent".to_string())); let ip = self.ip.unwrap_or(false); let mut s = if ip { format!("ban-ip {target}") diff --git a/src/command/banlist.rs b/src/command/banlist.rs index 3d746b0..8ceba79 100644 --- a/src/command/banlist.rs +++ b/src/command/banlist.rs @@ -1,6 +1,7 @@ use super::Command; use std::marker::PhantomData; +#[derive(Default)] pub struct BanlistCommand { state: PhantomData, } diff --git a/src/command/defaultgamemode.rs b/src/command/defaultgamemode.rs index 6d89d24..6d2183c 100644 --- a/src/command/defaultgamemode.rs +++ b/src/command/defaultgamemode.rs @@ -1,16 +1,18 @@ use super::{Command, Gamemode}; +#[derive(Default)] pub struct DefaultGamemodeCommand { - gamemode: Gamemode, + gamemode: Option, } impl DefaultGamemodeCommand { - pub fn new(gamemode: Gamemode) -> Self { - Self { gamemode } + pub fn gamemode(mut self, gamemode: Gamemode) -> Self { + self.gamemode = Some(gamemode); + self } pub fn build(self) -> Command { - let gamemode = self.gamemode; + let gamemode = self.gamemode.unwrap_or(Gamemode::Survival); let s = format!("defaultgamemode {gamemode}"); Command(s) } diff --git a/src/command/deop.rs b/src/command/deop.rs index c30943e..05832dd 100644 --- a/src/command/deop.rs +++ b/src/command/deop.rs @@ -1,16 +1,18 @@ use super::{Command, Selector}; +#[derive(Default)] pub struct DeopCommand { - target: Selector, + target: Option, } impl DeopCommand { - pub fn new(target: Selector) -> Self { - Self { target } + pub fn target(mut self, target: Selector) -> Self { + self.target = Some(target); + self } pub fn build(self) -> Command { - let target = self.target; + let target = self.target.unwrap_or(Selector::Executor); let s = format!("deop {target}"); Command(s) } diff --git a/src/command/difficulty.rs b/src/command/difficulty.rs index 2c70da5..4c01e6f 100644 --- a/src/command/difficulty.rs +++ b/src/command/difficulty.rs @@ -1,21 +1,18 @@ use super::{Command, Difficulty}; +#[derive(Default)] pub struct DifficultyCommand { difficulty: Option } impl DifficultyCommand { - pub fn new() -> Self { - Self { difficulty: None } - } - pub fn difficulty(mut self, difficulty: Difficulty) -> Self { self.difficulty = Some(difficulty); self } pub fn build(self) -> Command { - let mut s = "difficulty".to_string(); + let mut s = format!("difficulty"); if let Some(difficulty) = self.difficulty { s.push_str(&format!(" {difficulty}")) }; diff --git a/src/command/gamemode.rs b/src/command/gamemode.rs index 5a4e997..9a6b5d9 100644 --- a/src/command/gamemode.rs +++ b/src/command/gamemode.rs @@ -1,13 +1,15 @@ use super::{Command, Gamemode, Selector}; +#[derive(Default)] pub struct GamemodeCommand { - gamemode: Gamemode, + gamemode: Option, target: Option } impl GamemodeCommand { - pub fn new(gamemode: Gamemode) -> Self { - Self { gamemode, target: None } + pub fn gamemode(mut self, gamemode: Gamemode) -> Self { + self.gamemode = Some(gamemode); + self } pub fn target(mut self, target: Selector) -> Self { @@ -16,7 +18,7 @@ impl GamemodeCommand { } pub fn build(self) -> Command { - let gamemode = self.gamemode; + let gamemode = self.gamemode.unwrap_or(Gamemode::Survival); let mut s = format!("defaultgamemode {gamemode}"); if let Some(target) = self.target { s.push_str(&format!(" {target}")) diff --git a/src/command/kill.rs b/src/command/kill.rs index 5e51a9b..d365fbb 100644 --- a/src/command/kill.rs +++ b/src/command/kill.rs @@ -1,16 +1,18 @@ use super::{Command, Selector}; +#[derive(Default)] pub struct KillCommand { - target: Selector, + target: Option, } impl KillCommand { - pub fn new(target: Selector) -> Self { - Self { target } + pub fn target(mut self, target: Selector) -> Self { + self.target = Some(target); + self } pub fn build(self) -> Command { - let target = self.target; + let target = self.target.unwrap_or(Selector::Executor); let s = format!("kill {target}"); Command(s) } diff --git a/src/command/list.rs b/src/command/list.rs index ad7f7b9..398bdec 100644 --- a/src/command/list.rs +++ b/src/command/list.rs @@ -1,14 +1,11 @@ use super::Command; +#[derive(Default)] pub struct ListCommand { uuids: Option, } impl ListCommand { - pub fn new() -> Self { - Self { uuids: None } - } - pub fn uuids(mut self) -> Self { self.uuids = Some(true); self diff --git a/src/command/mod.rs b/src/command/mod.rs index ce1f188..4ab2054 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -82,43 +82,43 @@ impl Command { &self.0 } - pub fn say(message: impl Into) -> say::SayCommand { - say::SayCommand::new(message.into()) + pub fn say() -> say::SayCommand { + say::SayCommand::default() } pub fn list() -> list::ListCommand { - list::ListCommand::new() + list::ListCommand::default() } - pub fn kill(target: Selector) -> kill::KillCommand { - kill::KillCommand::new(target) + pub fn kill() -> kill::KillCommand { + kill::KillCommand::default() } - pub fn ban(target: Selector) -> ban::BanCommand { - ban::BanCommand::new(target) + pub fn ban() -> ban::BanCommand { + ban::BanCommand::default() } pub fn banlist() -> banlist::BanlistCommand { banlist::BanlistCommand::new() } - pub fn defaultgamemode(gamemode: Gamemode) -> defaultgamemode::DefaultGamemodeCommand { - defaultgamemode::DefaultGamemodeCommand::new(gamemode) + pub fn defaultgamemode() -> defaultgamemode::DefaultGamemodeCommand { + defaultgamemode::DefaultGamemodeCommand::default() } - pub fn op(target: Selector) -> op::OpCommand { - op::OpCommand::new(target) + pub fn op() -> op::OpCommand { + op::OpCommand::default() } - pub fn deop(target: Selector) -> deop::DeopCommand { - deop::DeopCommand::new(target) + pub fn deop() -> deop::DeopCommand { + deop::DeopCommand::default() } pub fn difficulty() -> difficulty::DifficultyCommand { - difficulty::DifficultyCommand::new() + difficulty::DifficultyCommand::default() } - pub fn gamemode(gamemode: Gamemode) -> gamemode::GamemodeCommand { - gamemode::GamemodeCommand::new(gamemode) + pub fn gamemode() -> gamemode::GamemodeCommand { + gamemode::GamemodeCommand::default() } } \ No newline at end of file diff --git a/src/command/op.rs b/src/command/op.rs index 94bb8de..28e3cc2 100644 --- a/src/command/op.rs +++ b/src/command/op.rs @@ -1,16 +1,18 @@ use super::{Command, Selector}; +#[derive(Default)] pub struct OpCommand { - target: Selector, + target: Option, } impl OpCommand { - pub fn new(target: Selector) -> Self { - Self { target } + pub fn target(mut self, target: Selector) -> Self { + self.target = Some(target); + self } pub fn build(self) -> Command { - let target = self.target; + let target = self.target.unwrap_or(Selector::Executor); let s = format!("op {target}"); Command(s) } diff --git a/src/command/say.rs b/src/command/say.rs index acf940d..44e5fe5 100644 --- a/src/command/say.rs +++ b/src/command/say.rs @@ -1,16 +1,18 @@ use super::Command; +#[derive(Default)] pub struct SayCommand { - message: String, + message: Option, } impl SayCommand { - pub fn new(message: String) -> Self { - Self { message } + pub fn message(mut self, msg: impl Into) -> Self { + self.message = Some(msg.into()); + self } pub fn build(self) -> Command { - let message = self.message; + let message = self.message.unwrap_or("Hello, world!".to_string()); let s = format!("say {message}"); Command(s) } diff --git a/src/lib.rs b/src/lib.rs index 76ae7cc..2a184a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,4 @@ mod client; pub use client::{RconClient, RconError, RconResult}; mod command; -pub use command::{Command, Selector, Difficulty, Gamemode}; \ No newline at end of file +pub use command::{Command, Selector}; \ No newline at end of file