Files
Bedwars/rust/src/commands.rs
T
Cortex Builder 16433a893f premier commit
2026-06-11 20:14:58 +02:00

196 lines
8.6 KiB
Rust

use crate::config::{BedwarsConfig, Loc, TeamConfig, GeneratorConfig};
use crate::team::BedwarsTeam;
use crate::game::GameManager;
pub enum BedwarsCommand {
SetLobby,
SetSpawn { team: BedwarsTeam },
SetBed { team: BedwarsTeam },
SetShop { team: BedwarsTeam },
SetUpgrades { team: BedwarsTeam },
SetGenerator { team: BedwarsTeam },
SetEnderchest { team: BedwarsTeam },
AddGenerator { gen_type: String },
Save,
Start,
Help,
}
impl BedwarsCommand {
pub fn parse(args: &[String]) -> Result<Self, String> {
if args.is_empty() {
return Ok(Self::Help);
}
match args[0].to_lowercase().as_str() {
"setlobby" => Ok(Self::SetLobby),
"setspawn" => {
if args.len() < 2 {
return Err("Usage: /bw setspawn <red|blue|purple|yellow>".to_string());
}
let team = BedwarsTeam::from_str(&args[1]).ok_or("Invalid team name")?;
Ok(Self::SetSpawn { team })
}
"setbed" => {
if args.len() < 2 {
return Err("Usage: /bw setbed <red|blue|purple|yellow>".to_string());
}
let team = BedwarsTeam::from_str(&args[1]).ok_or("Invalid team name")?;
Ok(Self::SetBed { team })
}
"setshop" => {
if args.len() < 2 {
return Err("Usage: /bw setshop <red|blue|purple|yellow>".to_string());
}
let team = BedwarsTeam::from_str(&args[1]).ok_or("Invalid team name")?;
Ok(Self::SetShop { team })
}
"setupgrades" => {
if args.len() < 2 {
return Err("Usage: /bw setupgrades <red|blue|purple|yellow>".to_string());
}
let team = BedwarsTeam::from_str(&args[1]).ok_or("Invalid team name")?;
Ok(Self::SetUpgrades { team })
}
"setgenerator" => {
if args.len() < 2 {
return Err("Usage: /bw setgenerator <red|blue|purple|yellow>".to_string());
}
let team = BedwarsTeam::from_str(&args[1]).ok_or("Invalid team name")?;
Ok(Self::SetGenerator { team })
}
"setenderchest" => {
if args.len() < 2 {
return Err("Usage: /bw setenderchest <red|blue|purple|yellow>".to_string());
}
let team = BedwarsTeam::from_str(&args[1]).ok_or("Invalid team name")?;
Ok(Self::SetEnderchest { team })
}
"addgenerator" => {
if args.len() < 2 {
return Err("Usage: /bw addgenerator <diamond|emerald>".to_string());
}
let gen_type = args[1].to_lowercase();
if gen_type != "diamond" && gen_type != "emerald" {
return Err("Generator type must be 'diamond' or 'emerald'".to_string());
}
Ok(Self::AddGenerator { gen_type })
}
"save" => Ok(Self::Save),
"start" => Ok(Self::Start),
"help" => Ok(Self::Help),
_ => Err("Unknown command. Type /bw help for options.".to_string()),
}
}
pub fn execute(
&self,
config: &mut BedwarsConfig,
game_manager: &mut GameManager,
player_loc: Loc,
target_block_loc: Option<Loc>,
) -> Result<String, String> {
match self {
Self::SetLobby => {
config.locations.lobby = player_loc;
Ok("Lobby location updated in config".to_string())
}
Self::SetSpawn { team } => {
let team_name = team.get_name().to_lowercase();
let team_conf = config.locations.teams.entry(team_name.clone()).or_insert_with(|| TeamConfig {
enabled: true,
spawn: player_loc.clone(),
bed: player_loc.clone(),
generator: player_loc.clone(),
shop: player_loc.clone(),
upgrades: player_loc.clone(),
enderchest: player_loc.clone(),
});
team_conf.spawn = player_loc;
team_conf.enabled = true;
game_manager.enable_team(*team);
Ok(format!("Spawn for team {} set and enabled", team.get_name()))
}
Self::SetBed { team } => {
let block_loc = target_block_loc.ok_or("You must look at a bed block")?;
let team_name = team.get_name().to_lowercase();
if let Some(t_conf) = config.locations.teams.get_mut(&team_name) {
t_conf.bed = block_loc;
Ok(format!("Bed for team {} set", team.get_name()))
} else {
Err("Team not configured yet. Set spawn first.".to_string())
}
}
Self::SetShop { team } => {
let team_name = team.get_name().to_lowercase();
if let Some(t_conf) = config.locations.teams.get_mut(&team_name) {
t_conf.shop = player_loc;
Ok(format!("Shop for team {} set", team.get_name()))
} else {
Err("Team not configured yet. Set spawn first.".to_string())
}
}
Self::SetUpgrades { team } => {
let team_name = team.get_name().to_lowercase();
if let Some(t_conf) = config.locations.teams.get_mut(&team_name) {
t_conf.upgrades = player_loc;
Ok(format!("Upgrades for team {} set", team.get_name()))
} else {
Err("Team not configured yet. Set spawn first.".to_string())
}
}
Self::SetGenerator { team } => {
let team_name = team.get_name().to_lowercase();
if let Some(t_conf) = config.locations.teams.get_mut(&team_name) {
t_conf.generator = player_loc;
Ok(format!("Generator for team {} set", team.get_name()))
} else {
Err("Team not configured yet. Set spawn first.".to_string())
}
}
Self::SetEnderchest { team } => {
let team_name = team.get_name().to_lowercase();
if let Some(t_conf) = config.locations.teams.get_mut(&team_name) {
t_conf.enderchest = player_loc;
Ok(format!("Ender Chest for team {} set", team.get_name()))
} else {
Err("Team not configured yet. Set spawn first.".to_string())
}
}
Self::AddGenerator { gen_type } => {
config.locations.generators.push(GeneratorConfig {
x: player_loc.x,
y: player_loc.y,
z: player_loc.z,
generator_type: gen_type.to_uppercase(),
});
Ok(format!("Added a {} generator at your location", gen_type.to_uppercase()))
}
Self::Save => {
// Return string representation to write to file
Ok("Config ready to save".to_string())
}
Self::Start => {
game_manager.start_game()?;
Ok("Match starting now!".to_string())
}
Self::Help => {
let mut help = String::new();
help.push_str("§d§m================ §b§lBEDWARS SETUP (RUST) §d§m================\n");
help.push_str("§d/bw setlobby §7- Set waiting lobby coordinate\n");
help.push_str("§d/bw setspawn <team> §7- Set team player spawn point\n");
help.push_str("§d/bw setbed <team> §7- Register team bed (look at block)\n");
help.push_str("§d/bw setshop <team> §7- Set shop NPC position\n");
help.push_str("§d/bw setupgrades <team> §7- Set upgrades NPC position\n");
help.push_str("§d/bw setgenerator <team> §7- Set base spawner location\n");
help.push_str("§d/bw setenderchest <team> §7- Set team Ender Chest\n");
help.push_str("§d/bw addgenerator <diamond|emerald> §7- Add generator\n");
help.push_str("§d/bw save §7- Save config file\n");
help.push_str("§d/bw start §7- Force start game immediately\n");
help.push_str("§d§m================================================\n");
Ok(help)
}
}
}
}