Add error popup message on equip failure (#955)

This commit is contained in:
Hugal31
2020-05-23 17:27:57 +02:00
committed by GitHub
parent 1ad9a10050
commit 6c2ad53b68
4 changed files with 69 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ using Robust.Server.GameObjects.Components.Container;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network; using Robust.Shared.Interfaces.Network;
using Robust.Shared.Localization;
using Robust.Shared.Timers; using Robust.Shared.Timers;
using static Content.Shared.GameObjects.Components.Inventory.EquipmentSlotDefines; using static Content.Shared.GameObjects.Components.Inventory.EquipmentSlotDefines;
@@ -23,15 +24,17 @@ namespace Content.Server.GameObjects
_inventory = Owner.GetComponent<InventoryComponent>(); _inventory = Owner.GetComponent<InventoryComponent>();
} }
bool IInventoryController.CanEquip(Slots slot, IEntity entity, bool flagsCheck) bool IInventoryController.CanEquip(Slots slot, IEntity entity, bool flagsCheck, out string reason)
{ {
var slotMask = SlotMasks[slot]; var slotMask = SlotMasks[slot];
reason = null;
if ((slotMask & (SlotFlags.POCKET | SlotFlags.IDCARD)) != SlotFlags.NONE) if ((slotMask & (SlotFlags.POCKET | SlotFlags.IDCARD)) != SlotFlags.NONE)
{ {
// Can't wear stuff in ID card or pockets unless you have a uniform. // Can't wear stuff in ID card or pockets unless you have a uniform.
if (_inventory.GetSlotItem(Slots.INNERCLOTHING) == null) if (_inventory.GetSlotItem(Slots.INNERCLOTHING) == null)
{ {
reason = Loc.GetString("You need a uniform to store something in your pockets!");
return false; return false;
} }
@@ -44,6 +47,10 @@ namespace Content.Server.GameObjects
{ {
return true; return true;
} }
else if (!flagsCheck)
{
reason = Loc.GetString("This is too large!");
}
} }
} }

View File

@@ -14,7 +14,14 @@ namespace Content.Server.GameObjects
/// <param name="slot">The slot to be equipped into.</param> /// <param name="slot">The slot to be equipped into.</param>
/// <param name="entity">The entity to equip.</param> /// <param name="entity">The entity to equip.</param>
/// <param name="flagsCheck">Whether the entity passes default slot masks & flags checks.</param> /// <param name="flagsCheck">Whether the entity passes default slot masks & flags checks.</param>
/// <param name="reason">The translated reason why the item cannot be equiped, if this function returns false. Can be null.</param>
/// <returns>True if the entity can be equipped, false otherwise</returns> /// <returns>True if the entity can be equipped, false otherwise</returns>
bool CanEquip(Slots slot, IEntity entity, bool flagsCheck) => flagsCheck; bool CanEquip(Slots slot, IEntity entity, bool flagsCheck, out string reason)
{
reason = null;
return flagsCheck;
}
bool CanEquip(Slots slot, IEntity entity, bool flagsCheck) => CanEquip(slot, entity, flagsCheck, out var _);
} }
} }

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks.Dataflow; using System.Threading.Tasks.Dataflow;
using Content.Server.GameObjects.EntitySystems; using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces;
using Content.Shared.GameObjects; using Content.Shared.GameObjects;
using Robust.Server.GameObjects.Components.Container; using Robust.Server.GameObjects.Components.Container;
using Robust.Server.Interfaces.Player; using Robust.Server.Interfaces.Player;
@@ -11,6 +12,7 @@ using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components; using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Interfaces.Network; using Robust.Shared.Interfaces.Network;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Players; using Robust.Shared.Players;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
@@ -24,6 +26,7 @@ namespace Content.Server.GameObjects
{ {
#pragma warning disable 649 #pragma warning disable 649
[Dependency] private readonly IEntitySystemManager _entitySystemManager; [Dependency] private readonly IEntitySystemManager _entitySystemManager;
[Dependency] private readonly IServerNotifyManager _serverNotifyManager;
#pragma warning restore 649 #pragma warning restore 649
[ViewVariables] [ViewVariables]
@@ -91,8 +94,9 @@ namespace Content.Server.GameObjects
/// </remarks> /// </remarks>
/// <param name="slot">The slot to put the item in.</param> /// <param name="slot">The slot to put the item in.</param>
/// <param name="item">The item to insert into the slot.</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>
/// <returns>True if the item was successfully inserted, false otherwise.</returns> /// <returns>True if the item was successfully inserted, false otherwise.</returns>
public bool Equip(Slots slot, ItemComponent item) public bool Equip(Slots slot, ItemComponent item, out string reason)
{ {
if (item == null) if (item == null)
{ {
@@ -100,7 +104,7 @@ namespace Content.Server.GameObjects
"Clothing must be passed here. To remove some clothing from a slot, use Unequip()"); "Clothing must be passed here. To remove some clothing from a slot, use Unequip()");
} }
if (!CanEquip(slot, item)) if (!CanEquip(slot, item, out reason))
{ {
return false; return false;
} }
@@ -117,18 +121,21 @@ namespace Content.Server.GameObjects
return true; return true;
} }
public bool Equip(Slots slot, IEntity entity) => Equip(slot, entity.GetComponent<ItemComponent>()); public bool Equip(Slots slot, ItemComponent item) => Equip(slot, item, out var _);
public bool Equip(Slots slot, IEntity entity) => Equip(slot, entity.GetComponent<ItemComponent>());
/// <summary> /// <summary>
/// Checks whether an item can be put in the specified slot. /// Checks whether an item can be put in the specified slot.
/// </summary> /// </summary>
/// <param name="slot">The slot to check for.</param> /// <param name="slot">The slot to check for.</param>
/// <param name="item">The item to check for.</param> /// <param name="item">The item to check for.</param>
/// <param name="reason">The translated reason why the item cannot be equiped, if this function returns false. Can be null.</param>
/// <returns>True if the item can be inserted into the specified slot.</returns> /// <returns>True if the item can be inserted into the specified slot.</returns>
public bool CanEquip(Slots slot, ItemComponent item) public bool CanEquip(Slots slot, ItemComponent item, out string reason)
{ {
var pass = false; var pass = false;
reason = null;
if (!ActionBlockerSystem.CanEquip(Owner)) if (!ActionBlockerSystem.CanEquip(Owner))
return false; return false;
@@ -139,16 +146,23 @@ namespace Content.Server.GameObjects
{ {
pass = true; pass = true;
} }
else
{
reason = Loc.GetString("This doesn't fit.");
}
} }
if (Owner.TryGetComponent(out IInventoryController controller)) if (Owner.TryGetComponent(out IInventoryController controller))
{ {
pass = controller.CanEquip(slot, item.Owner, pass); pass = controller.CanEquip(slot, item.Owner, pass, out var controllerReason);
reason = controllerReason ?? reason;
} }
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 _);
public bool CanEquip(Slots slot, IEntity entity) => CanEquip(slot, entity.GetComponent<ItemComponent>()); public bool CanEquip(Slots slot, IEntity entity) => CanEquip(slot, entity.GetComponent<ItemComponent>());
/// <summary> /// <summary>
@@ -282,9 +296,12 @@ namespace Content.Server.GameObjects
if (activeHand != null && activeHand.Owner.TryGetComponent(out ItemComponent clothing)) if (activeHand != null && activeHand.Owner.TryGetComponent(out ItemComponent clothing))
{ {
hands.Drop(hands.ActiveIndex); hands.Drop(hands.ActiveIndex);
if (!Equip(msg.Inventoryslot, clothing)) if (!Equip(msg.Inventoryslot, clothing, out var reason))
{ {
hands.PutInHand(clothing); hands.PutInHand(clothing);
if (reason != null)
_serverNotifyManager.PopupMessage(Owner, Owner, reason);
} }
} }
break; break;

View File

@@ -4,11 +4,14 @@ using Robust.Shared.Utility;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Content.Server.GameObjects.EntitySystems; using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces;
using Content.Shared.GameObjects; using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Items; using Content.Shared.GameObjects.Components.Items;
using Robust.Shared.IoC;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using static Content.Shared.GameObjects.Components.Inventory.EquipmentSlotDefines; using static Content.Shared.GameObjects.Components.Inventory.EquipmentSlotDefines;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Server.GameObjects namespace Content.Server.GameObjects
{ {
@@ -17,6 +20,10 @@ namespace Content.Server.GameObjects
[ComponentReference(typeof(StoreableComponent))] [ComponentReference(typeof(StoreableComponent))]
public class ClothingComponent : ItemComponent, IUse public class ClothingComponent : ItemComponent, IUse
{ {
#pragma warning disable 649
[Dependency] private readonly IServerNotifyManager _serverNotifyManager;
#pragma warning restore 649
public override string Name => "Clothing"; public override string Name => "Clothing";
public override uint? NetID => ContentNetIDs.CLOTHING; public override uint? NetID => ContentNetIDs.CLOTHING;
@@ -82,16 +89,38 @@ namespace Content.Server.GameObjects
hands.Drop(Owner); hands.Drop(Owner);
inv.Unequip(slot); inv.Unequip(slot);
hands.PutInHand(item); hands.PutInHand(item);
if (!TryEquip(inv, slot, eventArgs.User))
{
hands.Drop(item.Owner);
inv.Equip(slot, item);
hands.PutInHand(Owner.GetComponent<ItemComponent>());
}
} }
else else
{ {
hands.Drop(Owner); hands.Drop(Owner);
if (!TryEquip(inv, slot, eventArgs.User))
hands.PutInHand(Owner.GetComponent<ItemComponent>());
} }
return inv.Equip(slot, this); return true;
} }
return false; return false;
} }
private bool TryEquip(InventoryComponent inv, Slots slot, IEntity user)
{
if (!inv.Equip(slot, this, out var reason))
{
if (reason != null)
_serverNotifyManager.PopupMessage(Owner, user, reason);
return false;
}
return true;
}
} }
} }