From 8f05fef7030d3e999a07d621ba581ebbb205dadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Sun, 10 Nov 2024 15:42:57 +0100 Subject: [PATCH] fix using other execute variants after if resulting in `run execute` --- CHANGELOG.md | 1 + src/datapack/command/execute/conditional.rs | 33 +++++----- src/datapack/command/execute/mod.rs | 67 ++++++++++++++------- 3 files changed, 61 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebe4673..58c20c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/datapack/command/execute/conditional.rs b/src/datapack/command/execute/conditional.rs index 9268b86..a6ef219 100644 --- a/src/datapack/command/execute/conditional.rs +++ b/src/datapack/command/execute/conditional.rs @@ -251,25 +251,20 @@ 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())), - ), - ) - .into_iter() - .map(|cmd| { - ( - cmd.forbid_prefix(), - cmd.compile(options, global_state, function_state), - ) - }) - .flat_map(|(forbid_prefix, cmds)| { - cmds.into_iter() - .map(move |cmd| (!forbid_prefix, prefix.to_string() + &cmd)) - }) - .collect() + str_cond + .into_iter() + .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) + } + }) + }) + .collect() } } diff --git a/src/datapack/command/execute/mod.rs b/src/datapack/command/execute/mod.rs index fa6e94b..0bc6e03 100644 --- a/src/datapack/command/execute/mod.rs +++ b/src/datapack/command/execute/mod.rs @@ -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,19 +42,23 @@ impl Execute { ) -> Vec { // 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( - String::from("execute "), - false, - options, - global_state, - function_state, - ) - .into_iter() - .map(|(_, cmd)| cmd) - .collect() + 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, + global_state, + function_state, + ) + .into_iter() + .map(|(_, cmd)| cmd) + .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) - .into_iter() - .map(move |c| (forbid_prefix, c)) + 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)) + .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());