steel_core/command/builtins/
list.rs1use steel_utils::{
2 Identifier,
3 translations::{COMMANDS_LIST_NAME_AND_ID, COMMANDS_LIST_PLAYERS},
4};
5
6use super::super::{
7 brigadier::{CommandNodeBuilder, CommandSyntaxError},
8 execution::{CommandSource, SteelCommandContext, SteelCommandRuntime, literal},
9 registration::CommandRegistration,
10};
11
12pub(super) fn registration() -> CommandRegistration<CommandSource> {
13 CommandRegistration::new(Identifier::vanilla_static("list"), |_| command()).default_access()
14}
15
16fn command() -> CommandNodeBuilder<CommandSource, SteelCommandRuntime> {
17 literal("list")
18 .executes(|context| list_players(context, false))
19 .then(literal("uuids").executes(|context| list_players(context, true)))
20}
21
22fn list_players(
23 context: &SteelCommandContext<CommandSource>,
24 show_uuids: bool,
25) -> Result<i32, CommandSyntaxError> {
26 let player_count = context.source().server().player_count();
27 let Ok(result) = i32::try_from(player_count) else {
28 return Err(CommandSyntaxError::dynamic(
29 "Online player count exceeds the command result range",
30 ));
31 };
32 let max_players = context.source().server().config.max_players;
33 let formatted_players = context
34 .source()
35 .server()
36 .get_players()
37 .iter()
38 .map(|player| {
39 if show_uuids {
40 COMMANDS_LIST_NAME_AND_ID
41 .message([
42 player.gameprofile.name.clone(),
43 player.gameprofile.id.to_string(),
44 ])
45 .component()
46 .to_string()
47 } else {
48 player.gameprofile.name.clone()
49 }
50 })
51 .collect::<Vec<_>>()
52 .join(", ");
53 let message = COMMANDS_LIST_PLAYERS
54 .message([
55 player_count.to_string(),
56 max_players.to_string(),
57 formatted_players,
58 ])
59 .component();
60 context.source().send_success(&message, false);
61 Ok(result)
62}