Allow specifiying prefix for AssetPath command argument

This commit is contained in:
Syniis 2024-01-27 15:44:03 +01:00
parent d46dbb9708
commit b97e27df9a
2 changed files with 22 additions and 9 deletions

View File

@ -537,7 +537,7 @@ impl ServerChatCommand {
),
ServerChatCommand::GiveItem => cmd(
vec![
AssetPath("item", ITEM_SPECS.clone(), Required),
AssetPath("item", "common.items", ITEM_SPECS.clone(), Required),
Integer("num", 1, Optional),
],
"Give yourself some items.\nFor an example or to auto complete use Tab.",
@ -647,7 +647,12 @@ impl ServerChatCommand {
),
ServerChatCommand::MakeNpc => cmd(
vec![
AssetPath("entity_config", ENTITY_CONFIGS.clone(), Required),
AssetPath(
"entity_config",
"common.entity",
ENTITY_CONFIGS.clone(),
Required,
),
Integer("num", 1, Optional),
],
"Spawn entity from config near you.\nFor an example or to auto complete use Tab.",
@ -1079,7 +1084,7 @@ impl ServerChatCommand {
ArgumentSpec::Message(_) => "{/.*/}",
ArgumentSpec::SubCommand => "{} {/.*/}",
ArgumentSpec::Enum(_, _, _) => "{}",
ArgumentSpec::AssetPath(_, _, _) => "{}",
ArgumentSpec::AssetPath(_, _, _, _) => "{}",
ArgumentSpec::Boolean(_, _, _) => "{}",
ArgumentSpec::Flag(_) => "{}",
})
@ -1149,9 +1154,10 @@ pub enum ArgumentSpec {
Enum(&'static str, Vec<String>, Requirement),
/// The argument is an asset path. The associated values are
/// * label
/// * Path prefix shared by all assets
/// * List of all asset paths as strings for completion
/// * whether it's optional
AssetPath(&'static str, Vec<String>, Requirement),
AssetPath(&'static str, &'static str, Vec<String>, Requirement),
/// The argument is likely a boolean. The associated values are
/// * label
/// * suggested tab-completion
@ -1228,7 +1234,7 @@ impl ArgumentSpec {
format!("[{}]", label)
}
},
ArgumentSpec::AssetPath(label, _, req) => {
ArgumentSpec::AssetPath(label, _, _, req) => {
if &Requirement::Required == req {
format!("<{}>", label)
} else {
@ -1259,7 +1265,7 @@ impl ArgumentSpec {
| ArgumentSpec::Command(r)
| ArgumentSpec::Message(r)
| ArgumentSpec::Enum(_, _, r)
| ArgumentSpec::AssetPath(_, _, r)
| ArgumentSpec::AssetPath(_, _, _, r)
| ArgumentSpec::Boolean(_, _, r) => *r,
ArgumentSpec::Flag(_) => Requirement::Optional,
ArgumentSpec::SubCommand => Requirement::Required,

View File

@ -101,7 +101,7 @@ impl ClientChatCommand {
ArgumentSpec::Message(_) => "{/.*/}",
ArgumentSpec::SubCommand => "{} {/.*/}",
ArgumentSpec::Enum(_, _, _) => "{}",
ArgumentSpec::AssetPath(_, _, _) => "{}",
ArgumentSpec::AssetPath(_, _, _, _) => "{}",
ArgumentSpec::Boolean(_, _, _) => "{}",
ArgumentSpec::Flag(_) => "{}",
})
@ -221,6 +221,9 @@ fn preproccess_command(
} else if matches!(cmd_args.last(), Some(ArgumentSpec::SubCommand)) {
could_be_entity_target = true;
}
if let Some(ArgumentSpec::AssetPath(_, prefix, _, _)) = cmd_args.get(i) {
*arg = prefix.to_string() + "." + arg;
}
if could_be_entity_target && arg.starts_with(ClientEntityTarget::PREFIX) {
let target_str = arg.trim_start_matches(ClientEntityTarget::PREFIX);
let target = ClientEntityTarget::iter()
@ -593,11 +596,15 @@ impl TabComplete for ArgumentSpec {
.filter(|string| string.starts_with(part))
.map(|c| c.to_string())
.collect(),
ArgumentSpec::AssetPath(_, paths, _) => {
ArgumentSpec::AssetPath(_, prefix, paths, _) => {
let depth = part.split('.').count();
paths
.iter()
.map(|path| path.as_str().split('.').take(depth).join("."))
.filter_map(|path| {
path.as_str()
.strip_prefix(&(prefix.to_string() + "."))
.map(|stripped| stripped.split('.').take(depth).join("."))
})
.dedup()
.filter(|string| string.starts_with(part))
.map(|c| c.to_string())