fix using other execute variants after if resulting in `run execute`

This commit is contained in:
Moritz Hölting 2024-11-10 15:42:57 +01:00
parent 897e85c2d7
commit 8f05fef703
3 changed files with 61 additions and 40 deletions

View File

@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added ### Added
- support for commands using macros
### Changed ### Changed
- use "return" command for conditionals instead of data storage when using supported pack format - use "return" command for conditionals instead of data storage when using supported pack format

View File

@ -251,25 +251,20 @@ fn compile_since_20_format(
}) })
.collect() .collect()
} else { } else {
combine_conditions_commands_concat( str_cond
str_cond, .into_iter()
&Command::Concat( .flat_map(|cond| {
Box::new(Command::Raw("run ".to_string())), then.compile_internal(String::new(), false, options, global_state, function_state)
Box::new(Command::Execute(then.clone())), .into_iter()
), .map(move |(require_prefix, cmd)| {
) if require_prefix {
.into_iter() (true, prefix.to_string() + &cond.compile() + " " + &cmd)
.map(|cmd| { } else {
( (false, cmd)
cmd.forbid_prefix(), }
cmd.compile(options, global_state, function_state), })
) })
}) .collect()
.flat_map(|(forbid_prefix, cmds)| {
cmds.into_iter()
.map(move |cmd| (!forbid_prefix, prefix.to_string() + &cmd))
})
.collect()
} }
} }

View File

@ -1,4 +1,4 @@
use std::{collections::HashSet, ops::RangeInclusive}; use std::{collections::HashSet, ops::RangeInclusive, string::ToString};
use super::Command; use super::Command;
use crate::util::{ use crate::util::{
@ -42,19 +42,23 @@ impl Execute {
) -> Vec<String> { ) -> Vec<String> {
// Directly compile the command if it is a run command, skipping the execute part // Directly compile the command if it is a run command, skipping the execute part
// Otherwise, compile the execute command using internal function // Otherwise, compile the execute command using internal function
if let Self::Run(cmd) = self { match self {
cmd.compile(options, global_state, function_state) Self::Run(cmd) => cmd.compile(options, global_state, function_state),
} else { Self::Runs(cmds) => cmds
self.compile_internal( .iter()
String::from("execute "), .flat_map(|c| c.compile(options, global_state, function_state))
false, .collect(),
options, _ => self
global_state, .compile_internal(
function_state, String::from("execute "),
) false,
.into_iter() options,
.map(|(_, cmd)| cmd) global_state,
.collect() function_state,
)
.into_iter()
.map(|(_, cmd)| cmd)
.collect(),
} }
} }
@ -78,8 +82,7 @@ impl Execute {
| Self::On(arg, next) | Self::On(arg, next)
| Self::Positioned(arg, next) | Self::Positioned(arg, next)
| Self::Rotated(arg, next) | Self::Rotated(arg, next)
| Self::Store(arg, next) | Self::Store(arg, next) => next.compile_internal(
| Self::Summon(arg, next) => next.compile_internal(
format!( format!(
"{prefix}{op} {arg} ", "{prefix}{op} {arg} ",
op = self.variant_name(), op = self.variant_name(),
@ -109,7 +112,18 @@ impl Execute {
global_state, global_state,
function_state, function_state,
), ),
Self::Run(command) => match &**command { Self::Summon(arg, next) => next.compile_internal(
format!(
"{prefix}{op} {arg} ",
op = self.variant_name(),
arg = arg.compile()
),
true,
options,
global_state,
function_state,
),
Self::Run(command) => match command.as_ref() {
Command::Execute(ex) => ex.compile_internal( Command::Execute(ex) => ex.compile_internal(
prefix, prefix,
require_grouping, require_grouping,
@ -127,11 +141,22 @@ impl Execute {
.iter() .iter()
.flat_map(|c| { .flat_map(|c| {
let forbid_prefix = c.forbid_prefix(); let forbid_prefix = c.forbid_prefix();
c.compile(options, global_state, function_state) match c {
.into_iter() Command::Execute(ex) => ex.compile_internal(
.map(move |c| (forbid_prefix, c)) prefix.clone(),
require_grouping,
options,
global_state,
function_state,
),
command => command
.compile(options, global_state, function_state)
.into_iter()
.map(move |c| (!forbid_prefix, c))
.collect(),
}
}) })
.map(|(forbid_prefix, c)| map_run_cmd(forbid_prefix, c, &prefix)) .map(|(require_prefix, c)| map_run_cmd(!require_prefix, c, &prefix))
.collect(), .collect(),
Self::Runs(commands) => { Self::Runs(commands) => {
let group = Command::Group(commands.clone()); let group = Command::Group(commands.clone());