Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/default_commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod credits;
pub mod echo;
pub mod gamemode;
mod kill;
mod list;
mod motion;
pub mod op;
mod stop;
Expand Down
55 changes: 55 additions & 0 deletions src/default_commands/src/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use bevy_ecs::prelude::{Entity, Query, Res, With};
use temper_command_infra::{CommandHandler, CommandResult, CommandSource};
use temper_components::entity_identity::Identity;
use temper_components::player::player_marker::PlayerMarker;
use temper_macros::Command;
use temper_state::GlobalStateResource;

#[derive(Command)]
#[command(name = "list")]
enum ListCommand {
Normal,
#[literal("uuid")]
Uuid,
}

impl CommandHandler for ListCommand {
type SystemParam<'w, 's> = (
Res<'w, GlobalStateResource>,
Query<'w, 's, (Entity, &'static Identity), With<PlayerMarker>>,
);

fn handle(
self,
source: CommandSource,
params: &mut Self::SystemParam<'_, '_>,
) -> CommandResult {
let &mut (ref state, query) = params;

let player_list = query
.into_iter()
.map(|(_, identity)| {
let Some(ref player_name) = identity.name else {
return Err("player entity does not have a name");
};

Ok(match self {
ListCommand::Normal => player_name.clone(),
ListCommand::Uuid => format!("{player_name} ({})", identity.uuid),
})
})
.collect::<Result<Vec<String>, _>>()?;

source.send_message(
format!(
"There are {} of a max of {} players online: {}",
player_list.len(),
state.0.config.max_players,
player_list.join(", "),
)
.into(),
);

Ok(())
}
}
Loading