Files
tbd-station-14/Content.Server/Storage/EntitySystems/SecretStashSystem.cs
Paul Ritter 512d6a38c3 get that crap outta here (completely rewrites inventorysystem) (#5807)
* some work

* equip: done
unequip: todo

* unequipping done & refactored events

* workin

* movin

* reee namespaces

* stun

* mobstate

* fixes

* some work on events

* removes serverside itemcomp & misc fixes

* work

* smol merge fix

* ports template to prototype & finishes ui

* moves relay & adds containerenumerator

* actions & cuffs

* my god what is actioncode

* more fixes

* im loosing my grasp on reality

* more fixes

* more work

* explosions

* yes

* more work

* more fixes

* merge master & misc fixed because i forgot to commit before merging master

* more fixes

* fixes

* moar

* more work

* moar fixes

* suffixmap

* more work on client

* motivation low

* no. no containers

* mirroring client to server

* fixes

* move serverinvcomp

* serverinventorycomponent is dead

* gaming

* only strippable & ai left...

* only ai and richtext left

* fixes ai

* fixes

* fixes sprite layers

* more fixes

* resolves optional

* yes

* stable™️

* fixes

* moar fixes

* moar

* fix some tests

* lmao

* no comment

* good to merge™️

* fixes build but for real

* adresses some reviews

* adresses some more reviews

* nullables, yo

* fixes lobbyscreen

* timid refactor to differentiate actor & target

* adresses more reviews

* more

* my god what a mess

* removed the rest of duplicates

* removed duplicate slotflags and renamed shoes to feet

* removes another unused one

* yes

* fixes lobby & makes tryunequip return unequipped item

* fixes

* some funny renames

* fixes

* misc improvements to attemptevents

* fixes

* merge fixes

Co-authored-by: Paul Ritter <ritter.paul1@gmail.com>
2021-12-30 22:56:10 +01:00

137 lines
5.2 KiB
C#

using Content.Server.Clothing.Components;
using Content.Server.Popups;
using Content.Server.Storage.Components;
using Content.Shared.Acts;
using Content.Shared.Hands.Components;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
namespace Content.Server.Storage.EntitySystems
{
public class SecretStashSystem : EntitySystem
{
[Dependency] private readonly PopupSystem _popupSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SecretStashComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<SecretStashComponent, DestructionEventArgs>(OnDestroyed);
}
private void OnInit(EntityUid uid, SecretStashComponent component, ComponentInit args)
{
// set default secret part name
if (component.SecretPartName == "")
{
var meta = EntityManager.GetComponent<MetaDataComponent>(uid);
var entityName = Loc.GetString("comp-secret-stash-secret-part-name", ("name", meta.EntityName));
component.SecretPartName = entityName;
}
component.ItemContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(uid, "stash", out _);
}
private void OnDestroyed(EntityUid uid, SecretStashComponent component, DestructionEventArgs args)
{
component.ItemContainer.EmptyContainer();
}
/// <summary>
/// Is there something inside secret stash item container?
/// </summary>
public bool HasItemInside(EntityUid uid, SecretStashComponent? component = null)
{
if (!Resolve(uid, ref component))
return false;
return component.ItemContainer.ContainedEntity != null;
}
/// <summary>
/// Tries to hide item inside secret stash from hands of user.
/// </summary>
/// <returns>True if item was hidden inside stash</returns>
public bool TryHideItem(EntityUid uid, EntityUid userUid, EntityUid itemToHideUid,
SecretStashComponent? component = null, ItemComponent? item = null,
MetaDataComponent? itemMeta = null, SharedHandsComponent? hands = null)
{
if (!Resolve(uid, ref component))
return false;
if (!Resolve(itemToHideUid, ref item, ref itemMeta))
return false;
if (!Resolve(userUid, ref hands))
return false;
// check if secret stash is already occupied
var container = component.ItemContainer;
if (container.ContainedEntity != null)
{
var msg = Loc.GetString("comp-secret-stash-action-hide-container-not-empty");
_popupSystem.PopupEntity(msg, uid, Filter.Entities(userUid));
return false;
}
// check if item is too big to fit into secret stash
var itemName = itemMeta.EntityName;
if (item.Size > component.MaxItemSize)
{
var msg = Loc.GetString("comp-secret-stash-action-hide-item-too-big",
("item", itemName), ("stash", component.SecretPartName));
_popupSystem.PopupEntity(msg, uid, Filter.Entities(userUid));
return false;
}
// try to move item from hands to stash container
if (!hands.Drop(itemToHideUid, container))
{
return false;
}
// all done, show success message
var successMsg = Loc.GetString("comp-secret-stash-action-hide-success",
("item", itemName), ("this", component.SecretPartName));
_popupSystem.PopupEntity(successMsg, uid, Filter.Entities(userUid));
return true;
}
/// <summary>
/// Try get item and place it in users hand.
/// If user can't take it by hands, will drop item from container.
/// </summary>
/// <returns>True if user received item</returns>
public bool TryGetItem(EntityUid uid, EntityUid userUid, SecretStashComponent? component = null,
SharedHandsComponent? hands = null)
{
if (!Resolve(uid, ref component))
return false;
if (!Resolve(userUid, ref hands))
return false;
// check if secret stash has something inside
var container = component.ItemContainer;
if (container.ContainedEntity == null)
{
return false;
}
// get item inside container
var itemUid = container.ContainedEntity;
if (!EntityManager.TryGetComponent(itemUid, out ItemComponent? item))
{
return false;
}
hands.PutInHandOrDrop(item);
// show success message
var successMsg = Loc.GetString("comp-secret-stash-action-get-item-found-something",
("stash", component.SecretPartName));
_popupSystem.PopupEntity(successMsg, uid, Filter.Entities(userUid));
return true;
}
}
}