use new() with primary argument instead of default()

This commit is contained in:
Raptorox 2026-08-13 23:13:54 +02:00
parent 25052c00e0
commit 3a099b72f6
No known key found for this signature in database
GPG key ID: 8B3556FC3ED1F6D8
11 changed files with 53 additions and 64 deletions

View file

@ -1,16 +1,14 @@
use super::{Command, Selector}; use super::{Command, Selector};
#[derive(Default)]
pub struct BanCommand { pub struct BanCommand {
target: Option<Selector>, target: Selector,
reason: Option<String>, reason: Option<String>,
ip: Option<bool>, ip: Option<bool>,
} }
impl BanCommand { impl BanCommand {
pub fn target(mut self, target: Selector) -> Self { pub fn new(target: Selector) -> Self {
self.target = Some(target); Self { target, reason: None, ip: None }
self
} }
pub fn reason(mut self, reason: impl Into<String>) -> Self { pub fn reason(mut self, reason: impl Into<String>) -> Self {
@ -24,9 +22,7 @@ impl BanCommand {
} }
pub fn build(self) -> Command { pub fn build(self) -> Command {
let target = self let target = self.target;
.target
.unwrap_or(Selector::Player("nonexistent".to_string()));
let ip = self.ip.unwrap_or(false); let ip = self.ip.unwrap_or(false);
let mut s = if ip { let mut s = if ip {
format!("ban-ip {target}") format!("ban-ip {target}")

View file

@ -1,7 +1,6 @@
use super::Command; use super::Command;
use std::marker::PhantomData; use std::marker::PhantomData;
#[derive(Default)]
pub struct BanlistCommand<State> { pub struct BanlistCommand<State> {
state: PhantomData<State>, state: PhantomData<State>,
} }

View file

@ -1,18 +1,16 @@
use super::{Command, Gamemode}; use super::{Command, Gamemode};
#[derive(Default)]
pub struct DefaultGamemodeCommand { pub struct DefaultGamemodeCommand {
gamemode: Option<Gamemode>, gamemode: Gamemode,
} }
impl DefaultGamemodeCommand { impl DefaultGamemodeCommand {
pub fn gamemode(mut self, gamemode: Gamemode) -> Self { pub fn new(gamemode: Gamemode) -> Self {
self.gamemode = Some(gamemode); Self { gamemode }
self
} }
pub fn build(self) -> Command { pub fn build(self) -> Command {
let gamemode = self.gamemode.unwrap_or(Gamemode::Survival); let gamemode = self.gamemode;
let s = format!("defaultgamemode {gamemode}"); let s = format!("defaultgamemode {gamemode}");
Command(s) Command(s)
} }

View file

@ -1,18 +1,16 @@
use super::{Command, Selector}; use super::{Command, Selector};
#[derive(Default)]
pub struct DeopCommand { pub struct DeopCommand {
target: Option<Selector>, target: Selector,
} }
impl DeopCommand { impl DeopCommand {
pub fn target(mut self, target: Selector) -> Self { pub fn new(target: Selector) -> Self {
self.target = Some(target); Self { target }
self
} }
pub fn build(self) -> Command { pub fn build(self) -> Command {
let target = self.target.unwrap_or(Selector::Executor); let target = self.target;
let s = format!("deop {target}"); let s = format!("deop {target}");
Command(s) Command(s)
} }

View file

@ -1,18 +1,21 @@
use super::{Command, Difficulty}; use super::{Command, Difficulty};
#[derive(Default)]
pub struct DifficultyCommand { pub struct DifficultyCommand {
difficulty: Option<Difficulty> difficulty: Option<Difficulty>
} }
impl DifficultyCommand { impl DifficultyCommand {
pub fn new() -> Self {
Self { difficulty: None }
}
pub fn difficulty(mut self, difficulty: Difficulty) -> Self { pub fn difficulty(mut self, difficulty: Difficulty) -> Self {
self.difficulty = Some(difficulty); self.difficulty = Some(difficulty);
self self
} }
pub fn build(self) -> Command { pub fn build(self) -> Command {
let mut s = format!("difficulty"); let mut s = "difficulty".to_string();
if let Some(difficulty) = self.difficulty { if let Some(difficulty) = self.difficulty {
s.push_str(&format!(" {difficulty}")) s.push_str(&format!(" {difficulty}"))
}; };

View file

@ -1,15 +1,13 @@
use super::{Command, Gamemode, Selector}; use super::{Command, Gamemode, Selector};
#[derive(Default)]
pub struct GamemodeCommand { pub struct GamemodeCommand {
gamemode: Option<Gamemode>, gamemode: Gamemode,
target: Option<Selector> target: Option<Selector>
} }
impl GamemodeCommand { impl GamemodeCommand {
pub fn gamemode(mut self, gamemode: Gamemode) -> Self { pub fn new(gamemode: Gamemode) -> Self {
self.gamemode = Some(gamemode); Self { gamemode, target: None }
self
} }
pub fn target(mut self, target: Selector) -> Self { pub fn target(mut self, target: Selector) -> Self {
@ -18,7 +16,7 @@ impl GamemodeCommand {
} }
pub fn build(self) -> Command { pub fn build(self) -> Command {
let gamemode = self.gamemode.unwrap_or(Gamemode::Survival); let gamemode = self.gamemode;
let mut s = format!("defaultgamemode {gamemode}"); let mut s = format!("defaultgamemode {gamemode}");
if let Some(target) = self.target { if let Some(target) = self.target {
s.push_str(&format!(" {target}")) s.push_str(&format!(" {target}"))

View file

@ -1,18 +1,16 @@
use super::{Command, Selector}; use super::{Command, Selector};
#[derive(Default)]
pub struct KillCommand { pub struct KillCommand {
target: Option<Selector>, target: Selector,
} }
impl KillCommand { impl KillCommand {
pub fn target(mut self, target: Selector) -> Self { pub fn new(target: Selector) -> Self {
self.target = Some(target); Self { target }
self
} }
pub fn build(self) -> Command { pub fn build(self) -> Command {
let target = self.target.unwrap_or(Selector::Executor); let target = self.target;
let s = format!("kill {target}"); let s = format!("kill {target}");
Command(s) Command(s)
} }

View file

@ -1,11 +1,14 @@
use super::Command; use super::Command;
#[derive(Default)]
pub struct ListCommand { pub struct ListCommand {
uuids: Option<bool>, uuids: Option<bool>,
} }
impl ListCommand { impl ListCommand {
pub fn new() -> Self {
Self { uuids: None }
}
pub fn uuids(mut self) -> Self { pub fn uuids(mut self) -> Self {
self.uuids = Some(true); self.uuids = Some(true);
self self

View file

@ -82,43 +82,43 @@ impl Command {
&self.0 &self.0
} }
pub fn say() -> say::SayCommand { pub fn say(message: impl Into<String>) -> say::SayCommand {
say::SayCommand::default() say::SayCommand::new(message.into())
} }
pub fn list() -> list::ListCommand { pub fn list() -> list::ListCommand {
list::ListCommand::default() list::ListCommand::new()
} }
pub fn kill() -> kill::KillCommand { pub fn kill(target: Selector) -> kill::KillCommand {
kill::KillCommand::default() kill::KillCommand::new(target)
} }
pub fn ban() -> ban::BanCommand { pub fn ban(target: Selector) -> ban::BanCommand {
ban::BanCommand::default() ban::BanCommand::new(target)
} }
pub fn banlist() -> banlist::BanlistCommand<banlist::NoFilter> { pub fn banlist() -> banlist::BanlistCommand<banlist::NoFilter> {
banlist::BanlistCommand::new() banlist::BanlistCommand::new()
} }
pub fn defaultgamemode() -> defaultgamemode::DefaultGamemodeCommand { pub fn defaultgamemode(gamemode: Gamemode) -> defaultgamemode::DefaultGamemodeCommand {
defaultgamemode::DefaultGamemodeCommand::default() defaultgamemode::DefaultGamemodeCommand::new(gamemode)
} }
pub fn op() -> op::OpCommand { pub fn op(target: Selector) -> op::OpCommand {
op::OpCommand::default() op::OpCommand::new(target)
} }
pub fn deop() -> deop::DeopCommand { pub fn deop(target: Selector) -> deop::DeopCommand {
deop::DeopCommand::default() deop::DeopCommand::new(target)
} }
pub fn difficulty() -> difficulty::DifficultyCommand { pub fn difficulty() -> difficulty::DifficultyCommand {
difficulty::DifficultyCommand::default() difficulty::DifficultyCommand::new()
} }
pub fn gamemode() -> gamemode::GamemodeCommand { pub fn gamemode(gamemode: Gamemode) -> gamemode::GamemodeCommand {
gamemode::GamemodeCommand::default() gamemode::GamemodeCommand::new(gamemode)
} }
} }

View file

@ -1,18 +1,16 @@
use super::{Command, Selector}; use super::{Command, Selector};
#[derive(Default)]
pub struct OpCommand { pub struct OpCommand {
target: Option<Selector>, target: Selector,
} }
impl OpCommand { impl OpCommand {
pub fn target(mut self, target: Selector) -> Self { pub fn new(target: Selector) -> Self {
self.target = Some(target); Self { target }
self
} }
pub fn build(self) -> Command { pub fn build(self) -> Command {
let target = self.target.unwrap_or(Selector::Executor); let target = self.target;
let s = format!("op {target}"); let s = format!("op {target}");
Command(s) Command(s)
} }

View file

@ -1,18 +1,16 @@
use super::Command; use super::Command;
#[derive(Default)]
pub struct SayCommand { pub struct SayCommand {
message: Option<String>, message: String,
} }
impl SayCommand { impl SayCommand {
pub fn message(mut self, msg: impl Into<String>) -> Self { pub fn new(message: String) -> Self {
self.message = Some(msg.into()); Self { message }
self
} }
pub fn build(self) -> Command { pub fn build(self) -> Command {
let message = self.message.unwrap_or("Hello, world!".to_string()); let message = self.message;
let s = format!("say {message}"); let s = format!("say {message}");
Command(s) Command(s)
} }