From e8f7ef98763e672f04c9f23ca049eed2ddac3d7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Tue, 2 Apr 2024 19:24:40 +0200 Subject: [PATCH] Fix bugs in execute if and group command --- src/datapack/command/execute.rs | 50 +++++++++++++++++++++------------ src/datapack/command/mod.rs | 8 ++++-- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/datapack/command/execute.rs b/src/datapack/command/execute.rs index 9a54d51..dfbab5b 100644 --- a/src/datapack/command/execute.rs +++ b/src/datapack/command/execute.rs @@ -44,6 +44,9 @@ impl Execute { global_state, function_state, ) + .into_iter() + .map(|(_, cmd)| cmd) + .collect() } } fn compile_internal( @@ -53,7 +56,7 @@ impl Execute { options: &CompileOptions, global_state: &MutCompilerState, function_state: &FunctionCompilerState, - ) -> Vec { + ) -> Vec<(bool, String)> { match self { Self::Align(align, next) => format_execute( prefix, @@ -183,18 +186,18 @@ impl Execute { command => command .compile(options, global_state, function_state) .into_iter() - .map(|c| prefix.clone() + "run " + &c) + .map(|c| (true, prefix.clone() + "run " + &c)) .collect(), }, Self::Runs(commands) if !require_grouping => commands .iter() .flat_map(|c| c.compile(options, global_state, function_state)) - .map(|c| prefix.clone() + "run " + &c) + .map(|c| (true, prefix.clone() + "run " + &c)) .collect(), Self::Runs(commands) => Command::Group(commands.clone()) .compile(options, global_state, function_state) .into_iter() - .map(|c| prefix.clone() + "run " + &c) + .map(|c| (true, prefix.clone() + "run " + &c)) .collect(), } } @@ -208,7 +211,7 @@ fn format_execute( options: &CompileOptions, global_state: &MutCompilerState, function_state: &FunctionCompilerState, -) -> Vec { +) -> Vec<(bool, String)> { next.compile_internal( prefix + new, require_grouping, @@ -226,7 +229,7 @@ fn compile_if_cond( options: &CompileOptions, global_state: &MutCompilerState, function_state: &FunctionCompilerState, -) -> Vec { +) -> Vec<(bool, String)> { let str_cond = cond.clone().compile(options, global_state, function_state); let require_grouping = el.is_some() || str_cond.len() > 1; let then = if require_grouping { @@ -241,7 +244,7 @@ fn compile_if_cond( Command::Group(group_cmd) .compile(options, global_state, function_state) .iter() - .map(|s| "run ".to_string() + s) + .map(|s| (true, "run ".to_string() + s)) .collect() } else { then.compile_internal( @@ -267,33 +270,44 @@ fn compile_if_cond( ); combine_conditions_commands(else_cond, el) .into_iter() - .map(|cmd| (true, cmd)) .chain(std::iter::once(( false, "data remove storage shulkerbox:cond if_success".to_string(), ))) - .map(|(use_prefix, cmd)| { - if use_prefix { - prefix.clone() + &cmd - } else { - cmd - } - }) .collect::>() }) .unwrap_or_default(); then_commands .into_iter() - .map(|cmd| prefix.clone() + &cmd) .chain(el) + .map(|(use_prefix, cmd)| { + let cmd = if use_prefix { + prefix.clone() + &cmd + } else { + cmd + }; + (use_prefix, cmd) + }) .collect() } -fn combine_conditions_commands(conditions: Vec, commands: Vec) -> Vec { +fn combine_conditions_commands( + conditions: Vec, + commands: Vec<(bool, String)>, +) -> Vec<(bool, String)> { conditions .into_iter() - .flat_map(|cond| commands.iter().map(move |cmd| cond.clone() + " " + cmd)) + .flat_map(|cond| { + commands.iter().map(move |(use_prefix, cmd)| { + let cmd = if *use_prefix { + cond.clone() + " " + cmd + } else { + cmd.clone() + }; + (*use_prefix, cmd) + }) + }) .collect() } diff --git a/src/datapack/command/mod.rs b/src/datapack/command/mod.rs index 4ddd1c4..313b2b0 100644 --- a/src/datapack/command/mod.rs +++ b/src/datapack/command/mod.rs @@ -89,11 +89,13 @@ fn compile_group( }; let function_path = { - let pre_hash_path = - function_state.path().to_owned() + ":" + &generated_functions.to_string(); + let function_path = function_state.path(); + let function_path = function_path.strip_prefix("sb/").unwrap_or(function_path); + + let pre_hash_path = function_path.to_owned() + ":" + &generated_functions.to_string(); let hash = md5::hash(pre_hash_path).to_hex_lowercase(); - "sb/".to_string() + function_state.path() + "/" + &hash[..16] + "sb/".to_string() + function_path + "/" + &hash[..16] }; let namespace = function_state.namespace();