Fix bugs in execute if and group command

This commit is contained in:
Moritz Hölting 2024-04-02 19:24:40 +02:00
parent f0d7f7499f
commit e8f7ef9876
2 changed files with 37 additions and 21 deletions

View File

@ -44,6 +44,9 @@ impl Execute {
global_state, global_state,
function_state, function_state,
) )
.into_iter()
.map(|(_, cmd)| cmd)
.collect()
} }
} }
fn compile_internal( fn compile_internal(
@ -53,7 +56,7 @@ impl Execute {
options: &CompileOptions, options: &CompileOptions,
global_state: &MutCompilerState, global_state: &MutCompilerState,
function_state: &FunctionCompilerState, function_state: &FunctionCompilerState,
) -> Vec<String> { ) -> Vec<(bool, String)> {
match self { match self {
Self::Align(align, next) => format_execute( Self::Align(align, next) => format_execute(
prefix, prefix,
@ -183,18 +186,18 @@ impl Execute {
command => command command => command
.compile(options, global_state, function_state) .compile(options, global_state, function_state)
.into_iter() .into_iter()
.map(|c| prefix.clone() + "run " + &c) .map(|c| (true, prefix.clone() + "run " + &c))
.collect(), .collect(),
}, },
Self::Runs(commands) if !require_grouping => commands Self::Runs(commands) if !require_grouping => commands
.iter() .iter()
.flat_map(|c| c.compile(options, global_state, function_state)) .flat_map(|c| c.compile(options, global_state, function_state))
.map(|c| prefix.clone() + "run " + &c) .map(|c| (true, prefix.clone() + "run " + &c))
.collect(), .collect(),
Self::Runs(commands) => Command::Group(commands.clone()) Self::Runs(commands) => Command::Group(commands.clone())
.compile(options, global_state, function_state) .compile(options, global_state, function_state)
.into_iter() .into_iter()
.map(|c| prefix.clone() + "run " + &c) .map(|c| (true, prefix.clone() + "run " + &c))
.collect(), .collect(),
} }
} }
@ -208,7 +211,7 @@ fn format_execute(
options: &CompileOptions, options: &CompileOptions,
global_state: &MutCompilerState, global_state: &MutCompilerState,
function_state: &FunctionCompilerState, function_state: &FunctionCompilerState,
) -> Vec<String> { ) -> Vec<(bool, String)> {
next.compile_internal( next.compile_internal(
prefix + new, prefix + new,
require_grouping, require_grouping,
@ -226,7 +229,7 @@ fn compile_if_cond(
options: &CompileOptions, options: &CompileOptions,
global_state: &MutCompilerState, global_state: &MutCompilerState,
function_state: &FunctionCompilerState, function_state: &FunctionCompilerState,
) -> Vec<String> { ) -> Vec<(bool, String)> {
let str_cond = cond.clone().compile(options, global_state, function_state); let str_cond = cond.clone().compile(options, global_state, function_state);
let require_grouping = el.is_some() || str_cond.len() > 1; let require_grouping = el.is_some() || str_cond.len() > 1;
let then = if require_grouping { let then = if require_grouping {
@ -241,7 +244,7 @@ fn compile_if_cond(
Command::Group(group_cmd) Command::Group(group_cmd)
.compile(options, global_state, function_state) .compile(options, global_state, function_state)
.iter() .iter()
.map(|s| "run ".to_string() + s) .map(|s| (true, "run ".to_string() + s))
.collect() .collect()
} else { } else {
then.compile_internal( then.compile_internal(
@ -267,33 +270,44 @@ fn compile_if_cond(
); );
combine_conditions_commands(else_cond, el) combine_conditions_commands(else_cond, el)
.into_iter() .into_iter()
.map(|cmd| (true, cmd))
.chain(std::iter::once(( .chain(std::iter::once((
false, false,
"data remove storage shulkerbox:cond if_success".to_string(), "data remove storage shulkerbox:cond if_success".to_string(),
))) )))
.map(|(use_prefix, cmd)| {
if use_prefix {
prefix.clone() + &cmd
} else {
cmd
}
})
.collect::<Vec<_>>() .collect::<Vec<_>>()
}) })
.unwrap_or_default(); .unwrap_or_default();
then_commands then_commands
.into_iter() .into_iter()
.map(|cmd| prefix.clone() + &cmd)
.chain(el) .chain(el)
.map(|(use_prefix, cmd)| {
let cmd = if use_prefix {
prefix.clone() + &cmd
} else {
cmd
};
(use_prefix, cmd)
})
.collect() .collect()
} }
fn combine_conditions_commands(conditions: Vec<String>, commands: Vec<String>) -> Vec<String> { fn combine_conditions_commands(
conditions: Vec<String>,
commands: Vec<(bool, String)>,
) -> Vec<(bool, String)> {
conditions conditions
.into_iter() .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() .collect()
} }

View File

@ -89,11 +89,13 @@ fn compile_group(
}; };
let function_path = { let function_path = {
let pre_hash_path = let function_path = function_state.path();
function_state.path().to_owned() + ":" + &generated_functions.to_string(); 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(); 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(); let namespace = function_state.namespace();