add giveitem command

This commit is contained in:
pestilence 2019-10-24 16:05:10 -05:00 committed by timokoesters
parent a6faffca4e
commit 52dbfde510
No known key found for this signature in database
GPG Key ID: CD80BE9AAEE78097
2 changed files with 26 additions and 3 deletions

View File

@ -5,7 +5,7 @@
use crate::{Server, StateExt};
use chrono::{NaiveTime, Timelike};
use common::{
comp,
assets, comp,
event::{EventBus, ServerEvent},
msg::ServerMsg,
npc::{get_npc_name, NpcKind},
@ -82,6 +82,12 @@ impl ChatCommand {
lazy_static! {
/// Static list of chat commands available to the server.
pub static ref CHAT_COMMANDS: Vec<ChatCommand> = vec![
ChatCommand::new(
"giveitem",
"{d}",
"/giveitem <name> : Give yourself an item.",
true,
handle_give,),
ChatCommand::new(
"jump",
"{d} {d} {d}",
@ -233,7 +239,23 @@ lazy_static! {
),
];
}
fn handle_give(server: &mut Server, entity: EcsEntity, args: String, _action: &ChatCommand) {
if let Ok(item) = assets::load_cloned(&args) {
server
.state
.ecs()
.write_storage::<comp::Inventory>()
.get_mut(entity)
.map(|inv| inv.push(item));
let _ = server
.state
.ecs()
.write_storage::<comp::InventoryUpdate>()
.insert(entity, comp::InventoryUpdate);
} else {
server.notify_client(entity, ServerMsg::private(String::from("Invalid item!")));
}
}
fn handle_jump(server: &mut Server, entity: EcsEntity, args: String, action: &ChatCommand) {
if let Ok((x, y, z)) = scan_fmt!(&args, action.arg_fmt, f32, f32, f32) {
match server.state.read_component_cloned::<comp::Pos>(entity) {

View File

@ -169,7 +169,8 @@ impl<'a> System<'a> for Sys {
entity,
name,
body,
main: main.and_then(|specifier| assets::load_cloned(&specifier)),
main: main
.and_then(|specifier| assets::load_cloned(&specifier).ok()),
});
}
ClientState::Character => client.error_state(RequestStateError::Already),