Add optional argument to /get_items

This commit is contained in:
CapsizeGlimmer 2020-05-07 18:39:48 +00:00 committed by Songtronix
parent 846c3a9a0b
commit 63826d1a50
3 changed files with 85 additions and 19 deletions

View File

@ -72,6 +72,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- The /give_item command can now specify the amount of items. Syntax is now `/give_item <name> [num]`
- Brighter / higher contrast main-map
- Removed highlighting of non-collectible sprites
- Fixed /give_exp ignoring player argument

View File

@ -123,6 +123,28 @@ impl Item {
}
}
pub fn set_amount(&mut self, give_amount: u32) -> Result<(), assets::Error> {
use ItemKind::*;
match self.kind {
Consumable { ref mut amount, .. } => {
*amount = give_amount;
Ok(())
},
Utility { ref mut amount, .. } => {
*amount = give_amount;
Ok(())
},
Ingredient { ref mut amount, .. } => {
*amount = give_amount;
Ok(())
},
Tool { .. } | Lantern { .. } | Armor { .. } => {
// Tools and armor don't stack
Err(assets::Error::InvalidType)
},
}
}
pub fn name(&self) -> &str { &self.name }
pub fn description(&self) -> &str { &self.description }

View File

@ -95,9 +95,9 @@ lazy_static! {
pub static ref CHAT_COMMANDS: Vec<ChatCommand> = vec![
ChatCommand::new(
"give_item",
"{d}",
"/give_item <path to item>\n\
Example: common/items/debug/boost",
"{} {d}",
"/give_item <path to item> [num]\n\
Example items: common/items/apple, common/items/debug/boost",
true,
handle_give,),
ChatCommand::new(
@ -285,15 +285,52 @@ fn handle_give(
client: EcsEntity,
target: EcsEntity,
args: String,
_action: &ChatCommand,
action: &ChatCommand,
) {
if let Ok(item) = assets::load_cloned(&args) {
if let (Some(item_name), give_amount_opt) = scan_fmt_some!(&args, action.arg_fmt, String, u32) {
let give_amount = give_amount_opt.unwrap_or(1);
if let Ok(item) = assets::load_cloned(&item_name) {
let mut item: Item = item;
if let Ok(()) = item.set_amount(give_amount.min(2000)) {
server
.state
.ecs()
.write_storage::<comp::Inventory>()
.get_mut(target)
.map(|inv| inv.push(item));
.map(|inv| {
if inv.push(item).is_some() {
server.notify_client(
client,
ServerMsg::private(format!(
"Player inventory full. Gave 0 of {} items.",
give_amount
)),
);
}
});
} else {
// This item can't stack. Give each item in a loop.
server
.state
.ecs()
.write_storage::<comp::Inventory>()
.get_mut(target)
.map(|inv| {
for i in 0..give_amount {
if inv.push(item.clone()).is_some() {
server.notify_client(
client,
ServerMsg::private(format!(
"Player inventory full. Gave {} of {} items.",
i, give_amount
)),
);
break;
}
}
});
}
let _ = server
.state
.ecs()
@ -303,7 +340,13 @@ fn handle_give(
comp::InventoryUpdate::new(comp::InventoryUpdateEvent::Given),
);
} else {
server.notify_client(client, ServerMsg::private(String::from("Invalid item!")));
server.notify_client(
client,
ServerMsg::private(format!("Invalid item: {}", item_name)),
);
}
} else {
server.notify_client(client, ServerMsg::private(String::from(action.help_string)));
}
}