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]
### Added
- support for commands using macros
### Changed
- use "return" command for conditionals instead of data storage when using supported pack format

View File

@ -251,23 +251,18 @@ fn compile_since_20_format(
})
.collect()
} else {
combine_conditions_commands_concat(
str_cond,
&Command::Concat(
Box::new(Command::Raw("run ".to_string())),
Box::new(Command::Execute(then.clone())),
),
)
str_cond
.into_iter()
.map(|cmd| {
(
cmd.forbid_prefix(),
cmd.compile(options, global_state, function_state),
)
.flat_map(|cond| {
then.compile_internal(String::new(), false, options, global_state, function_state)
.into_iter()
.map(move |(require_prefix, cmd)| {
if require_prefix {
(true, prefix.to_string() + &cond.compile() + " " + &cmd)
} else {
(false, cmd)
}
})
.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 crate::util::{
@ -42,10 +42,14 @@ impl Execute {
) -> Vec<String> {
// Directly compile the command if it is a run command, skipping the execute part
// Otherwise, compile the execute command using internal function
if let Self::Run(cmd) = self {
cmd.compile(options, global_state, function_state)
} else {
self.compile_internal(
match self {
Self::Run(cmd) => cmd.compile(options, global_state, function_state),
Self::Runs(cmds) => cmds
.iter()
.flat_map(|c| c.compile(options, global_state, function_state))
.collect(),
_ => self
.compile_internal(
String::from("execute "),
false,
options,
@ -54,7 +58,7 @@ impl Execute {
)
.into_iter()
.map(|(_, cmd)| cmd)
.collect()
.collect(),
}
}
@ -78,8 +82,7 @@ impl Execute {
| Self::On(arg, next)
| Self::Positioned(arg, next)
| Self::Rotated(arg, next)
| Self::Store(arg, next)
| Self::Summon(arg, next) => next.compile_internal(
| Self::Store(arg, next) => next.compile_internal(
format!(
"{prefix}{op} {arg} ",
op = self.variant_name(),
@ -109,7 +112,18 @@ impl Execute {
global_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(
prefix,
require_grouping,
@ -127,11 +141,22 @@ impl Execute {
.iter()
.flat_map(|c| {
let forbid_prefix = c.forbid_prefix();
c.compile(options, global_state, function_state)
match c {
Command::Execute(ex) => ex.compile_internal(
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))
.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(),
Self::Runs(commands) => {
let group = Command::Group(commands.clone());