Stripping (#1668)
* Start work on stripping. * more strippable work * Stripping works * Nullable * MORE NULLABLE * nullable moment * life is pain * Interaction check. * Update Content.Client/GameObjects/Components/HUD/Inventory/StrippableBoundUserInterface.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Client/GameObjects/Components/HUD/Inventory/StrippableBoundUserInterface.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Server/GameObjects/Components/GUI/HandsComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Server/GameObjects/Components/GUI/HandsComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Server/GameObjects/Components/GUI/HandsComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Server/GameObjects/Components/GUI/StrippableComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Server/GameObjects/Components/GUI/StrippableComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Server/GameObjects/Components/GUI/HandsComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Server/GameObjects/Components/GUI/StrippableComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Server/GameObjects/Components/GUI/StrippableComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Shared/GameObjects/Components/GUI/SharedStrippableComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Update Content.Server/GameObjects/Components/GUI/StrippableComponent.cs Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Rename InventoryComponent and HandsComponent's OnChanged event to OnItemChanged * Apply suggestions from code review Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> * Use static EquipmentSlotDefines * Do not expose ContainerSlot on Inventory or Hands. Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
e047262289
commit
c3df108b27
@@ -42,6 +42,8 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
private string? _activeHand;
|
||||
private uint _nextHand;
|
||||
|
||||
public event Action? OnItemChanged;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public string? ActiveHand
|
||||
{
|
||||
@@ -60,6 +62,8 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
|
||||
[ViewVariables] private readonly List<Hand> _hands = new List<Hand>();
|
||||
|
||||
public IEnumerable<string> Hands => _hands.Select(h => h.Name);
|
||||
|
||||
// Mostly arbitrary.
|
||||
public const float PickupRange = 2;
|
||||
|
||||
@@ -105,6 +109,12 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
return GetHand(handName)?.Entity?.GetComponent<ItemComponent>();
|
||||
}
|
||||
|
||||
public bool TryGetItem(string handName, [MaybeNullWhen(false)] out ItemComponent item)
|
||||
{
|
||||
item = GetItem(handName);
|
||||
return item != null;
|
||||
}
|
||||
|
||||
public ItemComponent? GetActiveHand => ActiveHand == null
|
||||
? null
|
||||
: GetItem(ActiveHand);
|
||||
@@ -136,6 +146,8 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
{
|
||||
if (PutInHand(item, hand, false))
|
||||
{
|
||||
OnItemChanged?.Invoke();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -156,6 +168,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
if (success)
|
||||
{
|
||||
item.Owner.Transform.LocalPosition = Vector2.Zero;
|
||||
OnItemChanged?.Invoke();
|
||||
}
|
||||
|
||||
_entitySystemManager.GetEntitySystem<InteractionSystem>().HandSelectedInteraction(Owner, item.Owner);
|
||||
@@ -250,6 +263,8 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
container.Insert(item.Owner);
|
||||
}
|
||||
|
||||
OnItemChanged?.Invoke();
|
||||
|
||||
Dirty();
|
||||
return true;
|
||||
}
|
||||
@@ -300,6 +315,8 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
container.Insert(item.Owner);
|
||||
}
|
||||
|
||||
OnItemChanged?.Invoke();
|
||||
|
||||
Dirty();
|
||||
return true;
|
||||
}
|
||||
@@ -364,6 +381,8 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
OnItemChanged?.Invoke();
|
||||
|
||||
Dirty();
|
||||
return true;
|
||||
}
|
||||
@@ -415,6 +434,8 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
|
||||
ActiveHand ??= name;
|
||||
|
||||
OnItemChanged?.Invoke();
|
||||
|
||||
Dirty();
|
||||
}
|
||||
|
||||
@@ -435,6 +456,8 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
_activeHand = _hands.FirstOrDefault()?.Name;
|
||||
}
|
||||
|
||||
OnItemChanged?.Invoke();
|
||||
|
||||
Dirty();
|
||||
}
|
||||
|
||||
@@ -645,7 +668,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
|
||||
Dirty();
|
||||
|
||||
if (!message.Entity.TryGetComponent(out IPhysicsComponent physics))
|
||||
if (!message.Entity.TryGetComponent(out ICollidableComponent physics))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -33,9 +33,13 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
#pragma warning restore 649
|
||||
|
||||
[ViewVariables]
|
||||
private readonly Dictionary<Slots, ContainerSlot> SlotContainers = new Dictionary<Slots, ContainerSlot>();
|
||||
private readonly Dictionary<Slots, ContainerSlot> _slotContainers = new Dictionary<Slots, ContainerSlot>();
|
||||
|
||||
private KeyValuePair<Slots, (EntityUid entity, bool fits)>? HoverEntity;
|
||||
private KeyValuePair<Slots, (EntityUid entity, bool fits)>? _hoverEntity;
|
||||
|
||||
public IEnumerable<Slots> Slots => _slotContainers.Keys;
|
||||
|
||||
public event Action OnItemChanged;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -43,7 +47,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
|
||||
foreach (var slotName in InventoryInstance.SlotMasks)
|
||||
{
|
||||
if (slotName != Slots.NONE)
|
||||
if (slotName != EquipmentSlotDefines.Slots.NONE)
|
||||
{
|
||||
AddSlot(slotName);
|
||||
}
|
||||
@@ -58,7 +62,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
{
|
||||
var multiplier = 1f;
|
||||
|
||||
foreach (var (slot, containerSlot) in SlotContainers)
|
||||
foreach (var (slot, containerSlot) in _slotContainers)
|
||||
{
|
||||
foreach (var entity in containerSlot.ContainedEntities)
|
||||
{
|
||||
@@ -81,7 +85,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
{
|
||||
var multiplier = 1f;
|
||||
|
||||
foreach (var (slot, containerSlot) in SlotContainers)
|
||||
foreach (var (slot, containerSlot) in _slotContainers)
|
||||
{
|
||||
foreach (var entity in containerSlot.ContainedEntities)
|
||||
{
|
||||
@@ -99,7 +103,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
bool IEffectBlocker.CanSlip()
|
||||
{
|
||||
if(Owner.TryGetComponent(out InventoryComponent inventoryComponent) &&
|
||||
inventoryComponent.TryGetSlotItem(Slots.SHOES, out ItemComponent shoes)
|
||||
inventoryComponent.TryGetSlotItem(EquipmentSlotDefines.Slots.SHOES, out ItemComponent shoes)
|
||||
)
|
||||
{
|
||||
return EffectBlockerSystem.CanSlip(shoes.Owner);
|
||||
@@ -110,7 +114,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
|
||||
public override void OnRemove()
|
||||
{
|
||||
var slots = SlotContainers.Keys.ToList();
|
||||
var slots = _slotContainers.Keys.ToList();
|
||||
foreach (var slot in slots)
|
||||
{
|
||||
RemoveSlot(slot);
|
||||
@@ -140,15 +144,15 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
}
|
||||
public T GetSlotItem<T>(Slots slot) where T : ItemComponent
|
||||
{
|
||||
if (!SlotContainers.ContainsKey(slot))
|
||||
if (!_slotContainers.ContainsKey(slot))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var containedEntity = SlotContainers[slot].ContainedEntity;
|
||||
var containedEntity = _slotContainers[slot].ContainedEntity;
|
||||
if (containedEntity?.Deleted == true)
|
||||
{
|
||||
SlotContainers[slot] = null;
|
||||
_slotContainers[slot] = null;
|
||||
containedEntity = null;
|
||||
Dirty();
|
||||
}
|
||||
@@ -169,7 +173,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
/// </remarks>
|
||||
/// <param name="slot">The slot to put the item in.</param>
|
||||
/// <param name="item">The item to insert into the slot.</param>
|
||||
/// <param name="reason">The translated reason why the item cannot be equiped, if this function returns false. Can be null.</param>
|
||||
/// <param name="reason">The translated reason why the item cannot be equipped, if this function returns false. Can be null.</param>
|
||||
/// <returns>True if the item was successfully inserted, false otherwise.</returns>
|
||||
public bool Equip(Slots slot, ItemComponent item, out string reason)
|
||||
{
|
||||
@@ -184,7 +188,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
return false;
|
||||
}
|
||||
|
||||
var inventorySlot = SlotContainers[slot];
|
||||
var inventorySlot = _slotContainers[slot];
|
||||
if (!inventorySlot.Insert(item.Owner))
|
||||
{
|
||||
return false;
|
||||
@@ -192,6 +196,8 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
|
||||
_entitySystemManager.GetEntitySystem<InteractionSystem>().EquippedInteraction(Owner, item.Owner, slot);
|
||||
|
||||
OnItemChanged?.Invoke();
|
||||
|
||||
Dirty();
|
||||
|
||||
return true;
|
||||
@@ -239,7 +245,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
reason = Loc.GetString("You can't equip this!");
|
||||
}
|
||||
|
||||
return pass && SlotContainers[slot].CanInsert(item.Owner);
|
||||
return pass && _slotContainers[slot].CanInsert(item.Owner);
|
||||
}
|
||||
|
||||
public bool CanEquip(Slots slot, ItemComponent item) => CanEquip(slot, item, out var _);
|
||||
@@ -258,7 +264,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
return false;
|
||||
}
|
||||
|
||||
var inventorySlot = SlotContainers[slot];
|
||||
var inventorySlot = _slotContainers[slot];
|
||||
var item = inventorySlot.ContainedEntity.GetComponent<ItemComponent>();
|
||||
if (!inventorySlot.Remove(inventorySlot.ContainedEntity))
|
||||
{
|
||||
@@ -271,6 +277,8 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
|
||||
_entitySystemManager.GetEntitySystem<InteractionSystem>().UnequippedInteraction(Owner, item.Owner, slot);
|
||||
|
||||
OnItemChanged?.Invoke();
|
||||
|
||||
Dirty();
|
||||
|
||||
return true;
|
||||
@@ -288,7 +296,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
if (!ActionBlockerSystem.CanUnequip(Owner))
|
||||
return false;
|
||||
|
||||
var InventorySlot = SlotContainers[slot];
|
||||
var InventorySlot = _slotContainers[slot];
|
||||
return InventorySlot.ContainedEntity != null && InventorySlot.CanRemove(InventorySlot.ContainedEntity);
|
||||
}
|
||||
|
||||
@@ -307,7 +315,12 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
}
|
||||
|
||||
Dirty();
|
||||
return SlotContainers[slot] = ContainerManagerComponent.Create<ContainerSlot>(GetSlotString(slot), Owner);
|
||||
|
||||
_slotContainers[slot] = ContainerManagerComponent.Create<ContainerSlot>(GetSlotString(slot), Owner);
|
||||
|
||||
OnItemChanged?.Invoke();
|
||||
|
||||
return _slotContainers[slot];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -331,7 +344,10 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
"Unable to remove slot as the contained clothing could not be dropped");
|
||||
}
|
||||
|
||||
SlotContainers.Remove(slot);
|
||||
_slotContainers.Remove(slot);
|
||||
|
||||
OnItemChanged?.Invoke();
|
||||
|
||||
Dirty();
|
||||
}
|
||||
|
||||
@@ -342,7 +358,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
/// <returns>True if the slot exists, false otherwise.</returns>
|
||||
public bool HasSlot(Slots slot)
|
||||
{
|
||||
return SlotContainers.ContainsKey(slot);
|
||||
return _slotContainers.ContainsKey(slot);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -354,7 +370,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
// make sure this is one of our containers.
|
||||
// Technically the correct way would be to enumerate the possible slot names
|
||||
// comparing with this container, but I might as well put the dictionary to good use.
|
||||
if (!(container is ContainerSlot slot) || !SlotContainers.ContainsValue(slot))
|
||||
if (!(container is ContainerSlot slot) || !_slotContainers.ContainsValue(slot))
|
||||
return;
|
||||
|
||||
if (entity.TryGetComponent(out ItemComponent itemComp))
|
||||
@@ -362,6 +378,8 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
itemComp.RemovedFromSlot();
|
||||
}
|
||||
|
||||
OnItemChanged?.Invoke();
|
||||
|
||||
Dirty();
|
||||
}
|
||||
|
||||
@@ -417,7 +435,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
if (activeHand != null && GetSlotItem(msg.Inventoryslot) == null)
|
||||
{
|
||||
var canEquip = CanEquip(msg.Inventoryslot, activeHand, out var reason);
|
||||
HoverEntity = new KeyValuePair<Slots, (EntityUid entity, bool fits)>(msg.Inventoryslot, (activeHand.Owner.Uid, canEquip));
|
||||
_hoverEntity = new KeyValuePair<Slots, (EntityUid entity, bool fits)>(msg.Inventoryslot, (activeHand.Owner.Uid, canEquip));
|
||||
|
||||
Dirty();
|
||||
}
|
||||
@@ -476,7 +494,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
public override ComponentState GetComponentState()
|
||||
{
|
||||
var list = new List<KeyValuePair<Slots, EntityUid>>();
|
||||
foreach (var (slot, container) in SlotContainers)
|
||||
foreach (var (slot, container) in _slotContainers)
|
||||
{
|
||||
if (container.ContainedEntity != null)
|
||||
{
|
||||
@@ -484,8 +502,8 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
}
|
||||
}
|
||||
|
||||
var hover = HoverEntity;
|
||||
HoverEntity = null;
|
||||
var hover = _hoverEntity;
|
||||
_hoverEntity = null;
|
||||
|
||||
return new InventoryComponentState(list, hover);
|
||||
}
|
||||
@@ -497,7 +515,7 @@ namespace Content.Server.GameObjects.Components.GUI
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var slot in SlotContainers.Values.ToList())
|
||||
foreach (var slot in _slotContainers.Values.ToList())
|
||||
{
|
||||
foreach (var entity in slot.ContainedEntities)
|
||||
{
|
||||
|
||||
372
Content.Server/GameObjects/Components/GUI/StrippableComponent.cs
Normal file
372
Content.Server/GameObjects/Components/GUI/StrippableComponent.cs
Normal file
@@ -0,0 +1,372 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Content.Server.GameObjects.Components.Items.Storage;
|
||||
using Content.Server.GameObjects.EntitySystems.DoAfter;
|
||||
using Content.Server.Interfaces;
|
||||
using Content.Shared.GameObjects.Components.GUI;
|
||||
using Content.Shared.GameObjects.Components.Inventory;
|
||||
using Content.Shared.GameObjects.EntitySystems;
|
||||
using Content.Shared.Interfaces.GameObjects.Components;
|
||||
using Robust.Server.GameObjects.Components.UserInterface;
|
||||
using Robust.Server.Interfaces.GameObjects;
|
||||
using Robust.Server.Interfaces.Player;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Systems;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using static Content.Shared.GameObjects.Components.Inventory.EquipmentSlotDefines;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.GUI
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class StrippableComponent : SharedStrippableComponent, IDragDrop
|
||||
{
|
||||
[Dependency] private IServerNotifyManager _notifyManager = default!;
|
||||
|
||||
public const float StripDelay = 2f;
|
||||
|
||||
[ViewVariables]
|
||||
private BoundUserInterface _userInterface;
|
||||
|
||||
private InventoryComponent _inventoryComponent;
|
||||
private HandsComponent _handsComponent;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>().GetBoundUserInterface(StrippingUiKey.Key);
|
||||
_userInterface.OnReceiveMessage += HandleUserInterfaceMessage;
|
||||
|
||||
_inventoryComponent = Owner.GetComponent<InventoryComponent>();
|
||||
_handsComponent = Owner.GetComponent<HandsComponent>();
|
||||
|
||||
_inventoryComponent.OnItemChanged += UpdateSubscribed;
|
||||
|
||||
// Initial update.
|
||||
UpdateSubscribed();
|
||||
}
|
||||
|
||||
private void UpdateSubscribed()
|
||||
{
|
||||
var inventory = GetInventorySlots();
|
||||
var hands = GetHandSlots();
|
||||
|
||||
_userInterface.SetState(new StrippingBoundUserInterfaceState(inventory, hands));
|
||||
}
|
||||
|
||||
public bool CanDragDrop(DragDropEventArgs eventArgs)
|
||||
{
|
||||
return eventArgs.User.HasComponent<HandsComponent>()
|
||||
&& eventArgs.Target != eventArgs.Dropped && eventArgs.Target == eventArgs.User;
|
||||
}
|
||||
|
||||
public bool DragDrop(DragDropEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.User.TryGetComponent(out IActorComponent actor)) return false;
|
||||
|
||||
OpenUserInterface(actor.playerSession);
|
||||
return true;
|
||||
}
|
||||
|
||||
private Dictionary<Slots, string> GetInventorySlots()
|
||||
{
|
||||
var dictionary = new Dictionary<Slots, string>();
|
||||
|
||||
foreach (var slot in _inventoryComponent.Slots)
|
||||
{
|
||||
dictionary[slot] = _inventoryComponent.GetSlotItem(slot)?.Owner.Name ?? "None";
|
||||
}
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
private Dictionary<string, string> GetHandSlots()
|
||||
{
|
||||
var dictionary = new Dictionary<string, string>();
|
||||
|
||||
foreach (var hand in _handsComponent.Hands)
|
||||
{
|
||||
dictionary[hand] = _handsComponent.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 (!ActionBlockerSystem.CanInteract(user))
|
||||
return false;
|
||||
|
||||
if (item == null)
|
||||
{
|
||||
_notifyManager.PopupMessageCursor(user, Loc.GetString("You aren't holding anything!"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!userHands.CanDrop(userHands.ActiveHand!))
|
||||
{
|
||||
_notifyManager.PopupMessageCursor(user, Loc.GetString("You can't drop that!"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!inventory.HasSlot(slot))
|
||||
return false;
|
||||
|
||||
if (inventory.TryGetSlotItem(slot, out ItemComponent _))
|
||||
{
|
||||
_notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} already {0:have} something there!", Owner));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!inventory.CanEquip(slot, item))
|
||||
{
|
||||
_notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot equip that there!", 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);
|
||||
|
||||
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 (!ActionBlockerSystem.CanInteract(user))
|
||||
return false;
|
||||
|
||||
if (item == null)
|
||||
{
|
||||
_notifyManager.PopupMessageCursor(user, Loc.GetString("You aren't holding anything!"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!userHands.CanDrop(userHands.ActiveHand!))
|
||||
{
|
||||
_notifyManager.PopupMessageCursor(user, Loc.GetString("You can't drop that!"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!hands.HasHand(hand))
|
||||
return false;
|
||||
|
||||
if (hands.TryGetItem(hand, out var _))
|
||||
{
|
||||
_notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} already {0:have} something there!", Owner));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!hands.CanPutInHand(item, hand))
|
||||
{
|
||||
_notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot put that there!", 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, false);
|
||||
hands.PutInHand(item, hand, 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 (!ActionBlockerSystem.CanInteract(user))
|
||||
return false;
|
||||
|
||||
if (!inventory.HasSlot(slot))
|
||||
return false;
|
||||
|
||||
if (!inventory.TryGetSlotItem(slot, out ItemComponent itemToTake))
|
||||
{
|
||||
_notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} {0:have} nothing there!", Owner));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!inventory.CanUnequip(slot))
|
||||
{
|
||||
_notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot unequip that!", 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);
|
||||
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 (!ActionBlockerSystem.CanInteract(user))
|
||||
return false;
|
||||
|
||||
if (!hands.HasHand(hand))
|
||||
return false;
|
||||
|
||||
if (!hands.TryGetItem(hand, out var heldItem))
|
||||
{
|
||||
_notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} {0:have} nothing there!", Owner));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!hands.CanDrop(hand))
|
||||
{
|
||||
_notifyManager.PopupMessageCursor(user, Loc.GetString("{0:They} cannot drop that!", 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:
|
||||
var inventory = Owner.GetComponent<InventoryComponent>();
|
||||
|
||||
if (inventory.TryGetSlotItem(inventoryMessage.Slot, out ItemComponent _))
|
||||
placingItem = false;
|
||||
|
||||
if(placingItem)
|
||||
PlaceActiveHandItemInInventory(user, inventoryMessage.Slot);
|
||||
else
|
||||
TakeItemFromInventory(user, inventoryMessage.Slot);
|
||||
break;
|
||||
case StrippingHandButtonPressed handMessage:
|
||||
var hands = Owner.GetComponent<HandsComponent>();
|
||||
|
||||
if (hands.TryGetItem(handMessage.Hand, out _))
|
||||
placingItem = false;
|
||||
|
||||
if(placingItem)
|
||||
PlaceActiveHandItemInHands(user, handMessage.Hand);
|
||||
else
|
||||
TakeItemFromHands(user, handMessage.Hand);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user