Support quantity + use Enum for autocompletion + Address comments

This commit is contained in:
Vincent Foulon 2021-04-12 20:07:34 +02:00
parent 0692562533
commit a4bd47f340
3 changed files with 70 additions and 51 deletions

View File

@ -1,29 +1,35 @@
({
"debug": [
"common.items.debug.admin_back",
"common.items.debug.admin_black_hole",
"common.items.debug.admin_stick",
"common.items.debug.admin_sword",
"common.items.debug.admin",
"common.items.debug.cultist_belt",
"common.items.debug.cultist_boots",
"common.items.debug.cultist_chest_blue",
"common.items.debug.cultist_hands_blue",
"common.items.debug.cultist_legs_blue",
"common.items.debug.cultist_shoulder_blue",
"common.items.debug.dungeon_purple",
("common.items.debug.admin_back",1),
("common.items.debug.admin_black_hole",1),
("common.items.debug.admin_stick",1),
("common.items.debug.admin_sword",1),
("common.items.debug.admin",1),
("common.items.debug.cultist_belt",1),
("common.items.debug.cultist_boots",1),
("common.items.debug.cultist_chest_blue",1),
("common.items.debug.cultist_hands_blue",1),
("common.items.debug.cultist_legs_blue",1),
("common.items.debug.cultist_shoulder_blue",1),
("common.items.debug.dungeon_purple",1),
],
"cultist": [
"common.items.armor.cultist.chest",
"common.items.armor.cultist.pants",
"common.items.armor.cultist.hand",
"common.items.armor.cultist.foot",
"common.items.armor.cultist.shoulder",
"common.items.armor.cultist.belt",
"common.items.weapons.hammer.cultist_purp_2h-0",
"common.items.weapons.staff.cultist_staff",
"common.items.weapons.sword.cultist",
"common.items.weapons.bow.velorite",
"common.items.weapons.axe.malachite_axe-0",
("common.items.armor.cultist.chest",1),
("common.items.armor.cultist.pants",1),
("common.items.armor.cultist.hand",1),
("common.items.armor.cultist.foot",1),
("common.items.armor.cultist.shoulder",1),
("common.items.armor.cultist.belt",1),
("common.items.weapons.hammer.cultist_purp_2h-0",1),
("common.items.weapons.staff.cultist_staff",1),
("common.items.weapons.sword.cultist",1),
("common.items.weapons.bow.velorite",1),
("common.items.weapons.axe.malachite_axe-0",1),
("common.items.weapons.sceptre.sceptre_velorite_0",1),
],
"potions": [
("common.items.consumable.potion_minor", 100),
("common.items.consumable.potion_med", 100),
("common.items.consumable.potion_big", 100),
]
})

View File

@ -1,6 +1,8 @@
use crate::{assets, comp, npc, terrain};
use assets::AssetExt;
use hashbrown::HashMap;
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use std::{
fmt::{self, Display},
path::Path,
@ -155,6 +157,14 @@ pub static CHAT_COMMANDS: &[ChatCommand] = &[
ChatCommand::World,
];
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct KitManifest(pub HashMap<String, Vec<(String, u32)>>);
impl assets::Asset for KitManifest {
type Loader = assets::RonLoader;
const EXTENSION: &'static str = "ron";
}
lazy_static! {
pub static ref CHAT_SHORTCUTS: HashMap<char, ChatCommand> = [
('f', ChatCommand::Faction),
@ -224,6 +234,14 @@ lazy_static! {
items.sort();
items
};
static ref KITS: Vec<String> = {
if let Ok(kits) = KitManifest::load("server.manifests.kits") {
kits.read().0.keys().cloned().collect()
} else {
Vec::new()
}
};
}
impl ChatCommand {
@ -358,7 +376,7 @@ impl ChatCommand {
ChatCommand::Kill => cmd(vec![], "Kill yourself", NoAdmin),
ChatCommand::KillNpcs => cmd(vec![], "Kill the NPCs", Admin),
ChatCommand::Kit => cmd(
vec![Any("kit_name", Required)],
vec![Enum("kit_name", KITS.to_vec(), Required)],
"Place a set of items into your inventory.",
Admin,
),

View File

@ -38,10 +38,8 @@ use common_sys::state::{BuildAreaError, BuildAreas};
use core::{convert::TryFrom, ops::Not, time::Duration};
use hashbrown::HashSet;
use rand::Rng;
use serde::{Deserialize, Serialize};
use specs::{Builder, Entity as EcsEntity, Join, WorldExt};
use std::collections::HashMap;
use vek::*;
use world::util::Sampler;
@ -1406,14 +1404,6 @@ fn handle_kill_npcs(
Ok(())
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
struct KitManifest(HashMap<String, Vec<String>>);
impl assets::Asset for KitManifest {
type Loader = assets::RonLoader;
const EXTENSION: &'static str = "ron";
}
fn handle_kit(
server: &mut Server,
_client: EcsEntity,
@ -1423,28 +1413,33 @@ fn handle_kit(
) -> CmdResult<()> {
let kit_name = scan_fmt!(&args, &action.arg_fmt(), String);
if let Ok(name) = kit_name {
if let Ok(kits) = KitManifest::load("server.manifests.kits") {
if let Ok(kits) = common::cmd::KitManifest::load("server.manifests.kits") {
let kits = kits.read();
if let Some(kit) = kits.0.get(&name) {
for item_id in kit.iter() {
if let Ok(item) = comp::Item::new_from_asset(item_id) {
server
.state()
.ecs()
.write_storage::<comp::Inventory>()
.get_mut(target)
.map(|mut inv| inv.push(item));
let _ = server
.state
.ecs()
.write_storage::<comp::InventoryUpdate>()
.insert(
target,
comp::InventoryUpdate::new(comp::InventoryUpdateEvent::Debug),
if let (Some(mut target_inventory), mut target_inv_update) = (
server
.state()
.ecs()
.write_storage::<comp::Inventory>()
.get_mut(target),
server.state.ecs().write_storage::<comp::InventoryUpdate>(),
) {
for (item_id, quantity) in kit.iter() {
if let Ok(mut item) = comp::Item::new_from_asset(item_id) {
let _ = item.set_amount(*quantity);
let (_, _) = (
target_inventory.push(item),
target_inv_update.insert(
target,
comp::InventoryUpdate::new(comp::InventoryUpdateEvent::Debug),
),
);
}
}
Ok(())
} else {
Err("Could not get inventory".to_string())
}
Ok(())
} else {
Err(format!("Kit '{}' not found", name))
}