From 7e3ec94207c00736f3b1b143ef974032b0b6af7b Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 8 Feb 2021 13:55:50 -0500 Subject: [PATCH] Inventory manipulations are now only for input from the client, and are no longer directly sent as a server event. Slot manipulations do that instead. --- client/src/lib.rs | 25 ++++++++++------ common/src/comp/controller.rs | 43 ++++++++++++++++++++++------ common/src/comp/mod.rs | 2 +- common/src/event.rs | 2 +- common/sys/src/controller.rs | 2 +- server/src/events/inventory_manip.rs | 14 ++++----- 6 files changed, 61 insertions(+), 27 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 8cf47984b6..c18a1a1bcd 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -600,9 +600,14 @@ impl Client { } pub fn use_slot(&mut self, slot: Slot) { - self.send_msg(ClientGeneral::ControlEvent(ControlEvent::InventoryManip( - InventoryManip::Use(slot), - ))); + match slot { + Slot::Equip(equip) => { + self.control_action(ControlAction::LoadoutManip(LoadoutManip::Use(equip))) + }, + Slot::Inventory(inv) => self.send_msg(ClientGeneral::ControlEvent( + ControlEvent::InventoryManip(InventoryManip::Use(inv)), + )), + } } pub fn swap_slots(&mut self, a: Slot, b: Slot) { @@ -610,9 +615,11 @@ impl Client { (Slot::Equip(equip), slot) | (slot, Slot::Equip(equip)) => { self.control_action(ControlAction::LoadoutManip(LoadoutManip::Swap(equip, slot))) }, - _ => self.send_msg(ClientGeneral::ControlEvent(ControlEvent::InventoryManip( - InventoryManip::Swap(a, b), - ))), + (Slot::Inventory(inv1), Slot::Inventory(inv2)) => { + self.send_msg(ClientGeneral::ControlEvent(ControlEvent::InventoryManip( + InventoryManip::Swap(inv1, inv2), + ))) + }, } } @@ -621,9 +628,9 @@ impl Client { Slot::Equip(equip) => { self.control_action(ControlAction::LoadoutManip(LoadoutManip::Drop(equip))) }, - _ => self.send_msg(ClientGeneral::ControlEvent(ControlEvent::InventoryManip( - InventoryManip::Drop(slot), - ))), + Slot::Inventory(inv) => self.send_msg(ClientGeneral::ControlEvent( + ControlEvent::InventoryManip(InventoryManip::Drop(inv)), + )), } } diff --git a/common/src/comp/controller.rs b/common/src/comp/controller.rs index 72f0dacd65..5b99e695f7 100644 --- a/common/src/comp/controller.rs +++ b/common/src/comp/controller.rs @@ -1,6 +1,6 @@ use crate::{ comp::{ - inventory::slot::{EquipSlot, Slot}, + inventory::slot::{EquipSlot, InvSlotId, Slot}, BuffKind, }, uid::Uid, @@ -17,6 +17,23 @@ pub const DEFAULT_HOLD_DURATION: Duration = Duration::from_millis(200); #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum InventoryManip { + Pickup(Uid), + Collect(Vec3), + Use(InvSlotId), + Swap(InvSlotId, InvSlotId), + Drop(InvSlotId), + CraftRecipe(String), +} + +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +pub enum LoadoutManip { + Use(EquipSlot), + Swap(EquipSlot, Slot), + Drop(EquipSlot), +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum SlotManip { Pickup(Uid), Collect(Vec3), Use(Slot), @@ -25,21 +42,31 @@ pub enum InventoryManip { CraftRecipe(String), } -#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] -pub enum LoadoutManip { - Swap(EquipSlot, Slot), - Drop(EquipSlot), -} - -impl From for InventoryManip { +impl From for SlotManip { fn from(loadout_manip: LoadoutManip) -> Self { match loadout_manip { + LoadoutManip::Use(equip) => Self::Use(Slot::Equip(equip)), LoadoutManip::Swap(equip, slot) => Self::Swap(Slot::Equip(equip), slot), LoadoutManip::Drop(equip) => Self::Drop(Slot::Equip(equip)), } } } +impl From for SlotManip { + fn from(inv_manip: InventoryManip) -> Self { + match inv_manip { + InventoryManip::Pickup(pickup) => Self::Pickup(pickup), + InventoryManip::Collect(collect) => Self::Collect(collect), + InventoryManip::Use(inv) => Self::Use(Slot::Inventory(inv)), + InventoryManip::Swap(inv1, inv2) => { + Self::Swap(Slot::Inventory(inv1), Slot::Inventory(inv2)) + }, + InventoryManip::Drop(inv) => Self::Drop(Slot::Inventory(inv)), + InventoryManip::CraftRecipe(recipe) => Self::CraftRecipe(recipe), + } + } +} + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum GroupManip { Invite(Uid), diff --git a/common/src/comp/mod.rs b/common/src/comp/mod.rs index b36ce811fb..54c3ad67be 100644 --- a/common/src/comp/mod.rs +++ b/common/src/comp/mod.rs @@ -46,7 +46,7 @@ pub use chat::{ }; pub use controller::{ Climb, ControlAction, ControlEvent, Controller, ControllerInputs, GroupManip, Input, - InventoryManip, LoadoutManip, MountState, Mounting, + InventoryManip, LoadoutManip, MountState, Mounting, SlotManip, }; pub use energy::{Energy, EnergyChange, EnergySource}; pub use group::Group; diff --git a/common/src/event.rs b/common/src/event.rs index 68c1d90349..a375898815 100644 --- a/common/src/event.rs +++ b/common/src/event.rs @@ -46,7 +46,7 @@ pub enum ServerEvent { entity: EcsEntity, cause: comp::HealthSource, }, - InventoryManip(EcsEntity, comp::InventoryManip), + InventoryManip(EcsEntity, comp::SlotManip), GroupManip(EcsEntity, comp::GroupManip), Respawn(EcsEntity), Shoot { diff --git a/common/sys/src/controller.rs b/common/sys/src/controller.rs index cd04d9ca6e..93f08e00f1 100644 --- a/common/sys/src/controller.rs +++ b/common/sys/src/controller.rs @@ -99,7 +99,7 @@ impl<'a> System<'a> for Sys { } }, ControlEvent::InventoryManip(manip) => { - server_emitter.emit(ServerEvent::InventoryManip(entity, manip)); + server_emitter.emit(ServerEvent::InventoryManip(entity, manip.into())); }, ControlEvent::GroupManip(manip) => { server_emitter.emit(ServerEvent::GroupManip(entity, manip)) diff --git a/server/src/events/inventory_manip.rs b/server/src/events/inventory_manip.rs index 9816a4d59a..38103ad025 100644 --- a/server/src/events/inventory_manip.rs +++ b/server/src/events/inventory_manip.rs @@ -37,7 +37,7 @@ pub fn snuff_lantern(storage: &mut WriteStorage, entity: Ecs #[allow(clippy::blocks_in_if_conditions)] #[allow(clippy::same_item_push)] // TODO: Pending review in #587 -pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::InventoryManip) { +pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::SlotManip) { let state = server.state_mut(); let mut dropped_items = Vec::new(); let mut thrown_items = Vec::new(); @@ -60,7 +60,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv }; match manip { - comp::InventoryManip::Pickup(uid) => { + comp::SlotManip::Pickup(uid) => { let picked_up_item: Option; let item_entity = if let (Some((item, item_entity)), Some(mut inv)) = ( state @@ -133,7 +133,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv state.write_component(entity, event); }, - comp::InventoryManip::Collect(pos) => { + comp::SlotManip::Collect(pos) => { let block = state.terrain().get(pos).ok().copied(); if let Some(block) = block { @@ -204,7 +204,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv } }, - comp::InventoryManip::Use(slot) => { + comp::SlotManip::Use(slot) => { let mut inventories = state.ecs().write_storage::(); let mut inventory = if let Some(inventory) = inventories.get_mut(entity) { inventory @@ -405,7 +405,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv } }, - comp::InventoryManip::Swap(a, b) => { + comp::SlotManip::Swap(a, b) => { let ecs = state.ecs(); if let Some(pos) = ecs.read_storage::().get(entity) { @@ -429,7 +429,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv ); }, - comp::InventoryManip::Drop(slot) => { + comp::SlotManip::Drop(slot) => { let item = match slot { Slot::Inventory(slot) => state .ecs() @@ -462,7 +462,7 @@ pub fn handle_inventory(server: &mut Server, entity: EcsEntity, manip: comp::Inv ); }, - comp::InventoryManip::CraftRecipe(recipe) => { + comp::SlotManip::CraftRecipe(recipe) => { if let Some(mut inv) = state .ecs() .write_storage::()