Files
tbd-station-14/Content.Server/Strip/StrippableComponent.cs
collinlunn f2816e8081 Moves Hands to shared, some prediction (#3829)
* HandsGuiState

* Gui state setting methods

* code cleanup

* Removes TryGetHands

* ClientHand

* Gui Hands

* Refactor WIP 1

* Hand index

* refactors 2

* wip 3

* wip 4

* wiip 4

* wip 5

* wip 6

* wip 7

* wip 8

* wip 9

* wip 11

* Hand ui mostly looks fine

* hands gui cleanup 1

* cleanup 2

* wip 13

* hand enabled

* stuff

* Hands gui gap fix

* onpressed test

* hand gui buttons events work

* bag activation works

* fix item use

* todo comment

* hands activate fix

* Moves Client Hands back to using strings to identify active hand

* fixes action hand highlighting

* diff fix

* serverhand

* SharedHand

* SharedHand, IReadOnlyHand

* Client Hands only stores SharedHand

* cleanup server hands

* server hand container shutdown

* misc renames, refactors of serverhand

* stuff 1

* stuff 3

* server hand refactor 1

* Undo API changes to remove massive diff

* More API name fixes

* server hands cleanup 2

* cleanup 3

* dropping cleanup

* Cleanup 4

* MoveItemFromHand

* Stuff

* region sorting

* Hand Putter methods cleanup

* stuff 2

* Merges all of serverhand and clienthand into sharedhand

* Other hands systems, hack to make inhands update (gui state set every frame, visualzier updated every frame)

* GetFinalDropCoordinates cleanup

* SwapHands cleanup

* Moves server hands code to shared hands

* Fixed hand selected and deselected

* Naming fixes

* Server hands system cleanup

* Hands privacy fixes

* Client hand updates when containers are modified

* HeldItemVisualizer

* Fixes hand gui item status panel

* method name fix

* Swap hands prediction

* Dropping prediction

* Fixes pickup entity animation

* Fixes HeldItemsVisualizer

* moves item pickup to shared

* PR cleanup

* fixes hand enabling/disabling

* build fix

* Conflict fixes

* Fixes pickup animation

* Uses component directed message subscriptions

* event unsubscriptions in hand system

* unsubscribe fix

* CanInsertEntityIntoHand checks if hand is enabled

* Moving items from one hand to another checks if the hands can pick up and drop

* Fixes stop pulling not re-enabling hand

* Fixes pickup animation for entities containers on the floor

* Fixes using held items

* Fixes multiple hands guis appearing

* test fix

* removes obsolete system sunsubscribes

* Checks IsFirstTimePredicted before playing drop animation

* fixes hand item deleted crash

* Uses Get to get other system

* Replaces AppearanceComponent with SharedAppearanceComponent

* Replaces EnsureComponent with TryGetComponent

* Improves event class names

* Moves property up to top of class

* Moves code for determining the hand visualizer rsi state into the visualizer instead of being determined on hand component

* Eventbus todo comment

* Yaml fix for changed visualizer name

* Makes HandsVisuals a byte

* Removes state from HandsVisualizer

* Fixes hand using interaction method name

* Namespace changes fixes

* Fix for changed hand interaction method

* missing }

* conflict build fix

* Moves cleint HandsSystem to correct folder

* Moved namespace fix for interaction test

* Moves Handsvisualizer ot correct folder

* Moves SharedHandsSystem to correct folder

* Fixes errors from moving namespace of hand systems

* Fixes PDA component changes

* Fixes ActionsComponent diff

* Fixes inventory component diff

* fixes null ref

* Replaces obsolete Loc.GetString usage with fluent translations

* Fluent for hands disarming

* SwapHands and Drop user input specify to the server which hand

* Pickup animation WorldPosiiton todo

* Cleans up hands gui subscription handling

* Fixes change in ActionBlockerSystem access

* Namespace references fixes

* HandsComponent PlayerAttached/Detached messages are handled through eventbus

* Fixes GasCanisterSystem drop method usage

* Fix gameticker equipping method at new location

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2021-06-21 19:21:20 +10:00

463 lines
15 KiB
C#

#nullable enable
using System.Collections.Generic;
using System.Threading;
using Content.Server.Cuffs.Components;
using Content.Server.DoAfter;
using Content.Server.Hands.Components;
using Content.Server.Inventory.Components;
using Content.Server.Items;
using Content.Server.UserInterface;
using Content.Shared.ActionBlocker;
using Content.Shared.DragDrop;
using Content.Shared.Interaction.Events;
using Content.Shared.Notification.Managers;
using Content.Shared.Strip.Components;
using Content.Shared.Verbs;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.ViewVariables;
using static Content.Shared.Inventory.EquipmentSlotDefines;
namespace Content.Server.Strip
{
[RegisterComponent]
[ComponentReference(typeof(SharedStrippableComponent))]
public sealed class StrippableComponent : SharedStrippableComponent
{
public const float StripDelay = 2f;
[ViewVariables]
private BoundUserInterface? UserInterface => Owner.GetUIOrNull(StrippingUiKey.Key);
protected override void Initialize()
{
base.Initialize();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += HandleUserInterfaceMessage;
}
Owner.EnsureComponentWarn<InventoryComponent>();
Owner.EnsureComponentWarn<HandsComponent>();
Owner.EnsureComponentWarn<CuffableComponent>();
if (Owner.TryGetComponent(out CuffableComponent? cuffed))
{
cuffed.OnCuffedStateChanged += UpdateSubscribed;
}
if (Owner.TryGetComponent(out InventoryComponent? inventory))
{
inventory.OnItemChanged += UpdateSubscribed;
}
if (Owner.TryGetComponent(out HandsComponent? hands))
{
hands.OnItemChanged += UpdateSubscribed;
}
// Initial update.
UpdateSubscribed();
}
private void UpdateSubscribed()
{
if (UserInterface == null)
{
return;
}
var inventory = GetInventorySlots();
var hands = GetHandSlots();
var cuffs = GetHandcuffs();
UserInterface.SetState(new StrippingBoundUserInterfaceState(inventory, hands, cuffs));
}
public override bool Drop(DragDropEvent args)
{
if (!args.User.TryGetComponent(out ActorComponent? actor)) return false;
OpenUserInterface(actor.PlayerSession);
return true;
}
private Dictionary<EntityUid, string> GetHandcuffs()
{
var dictionary = new Dictionary<EntityUid, string>();
if (!Owner.TryGetComponent(out CuffableComponent? cuffed))
{
return dictionary;
}
foreach (IEntity entity in cuffed.StoredEntities)
{
dictionary.Add(entity.Uid, entity.Name);
}
return dictionary;
}
private Dictionary<Slots, string> GetInventorySlots()
{
var dictionary = new Dictionary<Slots, string>();
if (!Owner.TryGetComponent(out InventoryComponent? inventory))
{
return dictionary;
}
foreach (var slot in inventory.Slots)
{
dictionary[slot] = inventory.GetSlotItem(slot)?.Owner.Name ?? "None";
}
return dictionary;
}
private Dictionary<string, string> GetHandSlots()
{
var dictionary = new Dictionary<string, string>();
if (!Owner.TryGetComponent(out HandsComponent? hands))
{
return dictionary;
}
foreach (var hand in hands.HandNames)
{
dictionary[hand] = hands.GetItem(hand)?.Owner.Name ?? "None";
}
return dictionary;
}
private void OpenUserInterface(IPlayerSession session)
{
UserInterface?.Open(session);
}
/// <summary>
/// Places item in user's active hand to an inventory slot.
/// </summary>
private async void PlaceActiveHandItemInInventory(IEntity user, Slots slot)
{
var inventory = Owner.GetComponent<InventoryComponent>();
var userHands = user.GetComponent<HandsComponent>();
var item = userHands.GetActiveHand;
bool Check()
{
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(user))
return false;
if (item == null)
{
user.PopupMessageCursor(Loc.GetString("strippable-component-not-holding-anything"));
return false;
}
if (!userHands.CanDrop(userHands.ActiveHand!))
{
user.PopupMessageCursor(Loc.GetString("strippable-component-cannot-drop"));
return false;
}
if (!inventory.HasSlot(slot))
return false;
if (inventory.TryGetSlotItem(slot, out ItemComponent _))
{
user.PopupMessageCursor(Loc.GetString("strippable-component-item-slot-occupied",("owner", Owner)));
return false;
}
if (!inventory.CanEquip(slot, item, false))
{
user.PopupMessageCursor(Loc.GetString("strippable-component-cannot-equip-message",("owner", Owner)));
return false;
}
return true;
}
var doAfterSystem = EntitySystem.Get<DoAfterSystem>();
var doAfterArgs = new DoAfterEventArgs(user, StripDelay, CancellationToken.None, Owner)
{
ExtraCheck = Check,
BreakOnStun = true,
BreakOnDamage = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
NeedHand = true,
};
var result = await doAfterSystem.DoAfter(doAfterArgs);
if (result != DoAfterStatus.Finished) return;
userHands.Drop(item!.Owner, false);
inventory.Equip(slot, item!.Owner, false);
UpdateSubscribed();
}
/// <summary>
/// Places item in user's active hand in one of the entity's hands.
/// </summary>
private async void PlaceActiveHandItemInHands(IEntity user, string hand)
{
var hands = Owner.GetComponent<HandsComponent>();
var userHands = user.GetComponent<HandsComponent>();
var item = userHands.GetActiveHand;
bool Check()
{
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(user))
return false;
if (item == null)
{
user.PopupMessageCursor(Loc.GetString("strippable-component-not-holding-anything"));
return false;
}
if (!userHands.CanDrop(userHands.ActiveHand!))
{
user.PopupMessageCursor(Loc.GetString("strippable-component-cannot-drop"));
return false;
}
if (!hands.HasHand(hand))
{
return false;
}
if (hands.TryGetItem(hand, out var _))
{
user.PopupMessageCursor(Loc.GetString("strippable-component-item-slot-occupied-message", ("owner", Owner)));
return false;
}
if (!hands.CanPickupEntity(hand, item.Owner, checkActionBlocker: false))
{
user.PopupMessageCursor(Loc.GetString("strippable-component-cannot-put-message",("owner", Owner)));
return false;
}
return true;
}
var doAfterSystem = EntitySystem.Get<DoAfterSystem>();
var doAfterArgs = new DoAfterEventArgs(user, StripDelay, CancellationToken.None, Owner)
{
ExtraCheck = Check,
BreakOnStun = true,
BreakOnDamage = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
NeedHand = true,
};
var result = await doAfterSystem.DoAfter(doAfterArgs);
if (result != DoAfterStatus.Finished) return;
userHands.Drop(hand);
hands.TryPickupEntity(hand, item!.Owner, checkActionBlocker: false);
UpdateSubscribed();
}
/// <summary>
/// Takes an item from the inventory and places it in the user's active hand.
/// </summary>
private async void TakeItemFromInventory(IEntity user, Slots slot)
{
var inventory = Owner.GetComponent<InventoryComponent>();
var userHands = user.GetComponent<HandsComponent>();
bool Check()
{
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(user))
return false;
if (!inventory.HasSlot(slot))
return false;
if (!inventory.TryGetSlotItem(slot, out ItemComponent? itemToTake))
{
user.PopupMessageCursor(Loc.GetString("strippable-component-item-slot-free-message",("owner", Owner)));
return false;
}
if (!inventory.CanUnequip(slot, false))
{
user.PopupMessageCursor(Loc.GetString("strippable-component-cannot-unequip-message",("owner", Owner)));
return false;
}
return true;
}
var doAfterSystem = EntitySystem.Get<DoAfterSystem>();
var doAfterArgs = new DoAfterEventArgs(user, StripDelay, CancellationToken.None, Owner)
{
ExtraCheck = Check,
BreakOnStun = true,
BreakOnDamage = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
};
var result = await doAfterSystem.DoAfter(doAfterArgs);
if (result != DoAfterStatus.Finished) return;
var item = inventory.GetSlotItem(slot);
inventory.Unequip(slot, false);
if (item != null)
{
userHands.PutInHandOrDrop(item);
}
UpdateSubscribed();
}
/// <summary>
/// Takes an item from a hand and places it in the user's active hand.
/// </summary>
private async void TakeItemFromHands(IEntity user, string hand)
{
var hands = Owner.GetComponent<HandsComponent>();
var userHands = user.GetComponent<HandsComponent>();
bool Check()
{
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(user))
return false;
if (!hands.HasHand(hand))
return false;
if (!hands.TryGetItem(hand, out var heldItem))
{
user.PopupMessageCursor(Loc.GetString("strippable-component-item-slot-free-message",("owner", Owner)));
return false;
}
if (!hands.CanDrop(hand, false))
{
user.PopupMessageCursor(Loc.GetString("strippable-component-cannot-drop-message",("owner", Owner)));
return false;
}
return true;
}
var doAfterSystem = EntitySystem.Get<DoAfterSystem>();
var doAfterArgs = new DoAfterEventArgs(user, StripDelay, CancellationToken.None, Owner)
{
ExtraCheck = Check,
BreakOnStun = true,
BreakOnDamage = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
};
var result = await doAfterSystem.DoAfter(doAfterArgs);
if (result != DoAfterStatus.Finished) return;
var item = hands.GetItem(hand);
hands.Drop(hand, false);
userHands.PutInHandOrDrop(item!);
UpdateSubscribed();
}
private void HandleUserInterfaceMessage(ServerBoundUserInterfaceMessage obj)
{
var user = obj.Session.AttachedEntity;
if (user == null || !(user.TryGetComponent(out HandsComponent? userHands))) return;
var placingItem = userHands.GetActiveHand != null;
switch (obj.Message)
{
case StrippingInventoryButtonPressed inventoryMessage:
if (Owner.TryGetComponent<InventoryComponent>(out var inventory))
{
if (inventory.TryGetSlotItem(inventoryMessage.Slot, out ItemComponent _))
placingItem = false;
if (placingItem)
PlaceActiveHandItemInInventory(user, inventoryMessage.Slot);
else
TakeItemFromInventory(user, inventoryMessage.Slot);
}
break;
case StrippingHandButtonPressed handMessage:
if (Owner.TryGetComponent<HandsComponent>(out var hands))
{
if (hands.TryGetItem(handMessage.Hand, out _))
placingItem = false;
if (placingItem)
PlaceActiveHandItemInHands(user, handMessage.Hand);
else
TakeItemFromHands(user, handMessage.Hand);
}
break;
case StrippingHandcuffButtonPressed handcuffMessage:
if (Owner.TryGetComponent<CuffableComponent>(out var cuffed))
{
foreach (var entity in cuffed.StoredEntities)
{
if (entity.Uid == handcuffMessage.Handcuff)
{
cuffed.TryUncuff(user, entity);
return;
}
}
}
break;
}
}
[Verb]
private sealed class StripVerb : Verb<StrippableComponent>
{
protected override void GetData(IEntity user, StrippableComponent component, VerbData data)
{
if (!component.CanBeStripped(user))
{
data.Visibility = VerbVisibility.Invisible;
return;
}
data.Visibility = VerbVisibility.Visible;
data.Text = Loc.GetString("strip-verb-get-data-text");
}
protected override void Activate(IEntity user, StrippableComponent component)
{
if (!user.TryGetComponent(out ActorComponent? actor))
{
return;
}
component.OpenUserInterface(actor.PlayerSession);
}
}
}
}