Skip to main content

steel_core/command/builtins/execute/
mod.rs

1//! Vanilla command execution context composition.
2
3mod condition;
4mod source;
5mod store;
6
7pub(super) use condition::{ensure_region_chunks_block_ticking, loaded_block_position};
8
9use steel_utils::{Identifier, translations};
10use text_components::TextComponent;
11
12use super::super::{
13    brigadier::{CommandNodeBuilder, CommandSyntaxError, NodeId},
14    execution::{CommandSource, SteelCommandContext, SteelCommandRuntime, literal},
15    registration::CommandRegistration,
16};
17use crate::command::storage::CommandStorage;
18use crate::scoreboard::{Scoreboard, ScoreboardObjective};
19
20pub(super) fn registration() -> CommandRegistration<CommandSource> {
21    CommandRegistration::new(Identifier::vanilla_static("execute"), command)
22}
23
24fn command(dispatcher_root: NodeId) -> CommandNodeBuilder<CommandSource, SteelCommandRuntime> {
25    literal("execute")
26        .then(literal("run").redirects(dispatcher_root))
27        .then(condition::conditionals("if", true))
28        .then(condition::conditionals("unless", false))
29        .then(source::as_operation())
30        .then(source::at_operation())
31        .then(
32            literal("store")
33                .then(store::target("result", true))
34                .then(store::target("success", false)),
35        )
36        .then(source::positioned_operation())
37        .then(source::rotated_operation())
38        .then(source::facing_operation())
39        .then(source::align_operation())
40        .then(source::anchored_operation())
41        .then(source::in_operation())
42        .then(source::summon_operation())
43        .then(source::on_relations())
44}
45
46fn source_scoreboard(
47    context: &SteelCommandContext<CommandSource>,
48) -> Result<&Scoreboard, CommandSyntaxError> {
49    let source = context.source();
50    source
51        .server()
52        .scoreboards
53        .get(source.world().domain())
54        .ok_or_else(|| {
55            CommandSyntaxError::dynamic(format!(
56                "Domain '{}' has no command scoreboard",
57                source.world().domain()
58            ))
59        })
60}
61
62fn source_command_storage(
63    context: &SteelCommandContext<CommandSource>,
64) -> Result<&CommandStorage, CommandSyntaxError> {
65    let source = context.source();
66    source
67        .server()
68        .command_storage
69        .get(source.world().domain())
70        .ok_or_else(|| {
71            CommandSyntaxError::dynamic(format!(
72                "Domain '{}' has no command storage",
73                source.world().domain()
74            ))
75        })
76}
77
78fn objective(
79    context: &SteelCommandContext<CommandSource>,
80    scoreboard: &Scoreboard,
81    name: &str,
82) -> Result<ScoreboardObjective, CommandSyntaxError> {
83    let objective_name = context.objective_name(name)?;
84    scoreboard.objective(objective_name).ok_or_else(|| {
85        let message = translations::ARGUMENTS_OBJECTIVE_NOT_FOUND
86            .message([TextComponent::from(objective_name.to_owned())])
87            .component();
88        CommandSyntaxError::dynamic(message)
89    })
90}