Prevent inventory events from being relayed to pockets (#6074)

Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
This commit is contained in:
Leon Friedrich
2022-01-10 17:37:20 +13:00
committed by GitHub
parent 19b1c003e0
commit 013edcc545
9 changed files with 63 additions and 20 deletions

View File

@@ -1,8 +1,9 @@
using Robust.Shared.GameObjects;
using Content.Shared.Inventory;
using Robust.Shared.GameObjects;
namespace Content.Server.Atmos
{
public abstract class PressureEvent : EntityEventArgs
public abstract class PressureEvent : EntityEventArgs, IInventoryRelayEvent
{
/// <summary>
/// The environment pressure.
@@ -28,6 +29,11 @@ namespace Content.Server.Atmos
/// </remarks>
public float Multiplier { get; set; } = 1f;
/// <summary>
/// The inventory slots that should be checked for pressure protecting equipment.
/// </summary>
public SlotFlags TargetSlots { get; } = ~SlotFlags.POCKET;
protected PressureEvent(float pressure)
{
Pressure = pressure;

View File

@@ -8,6 +8,7 @@ using Content.Server.Temperature.Components;
using Content.Shared.Alert;
using Content.Shared.Damage;
using Content.Shared.Database;
using Content.Shared.Inventory;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
@@ -212,8 +213,10 @@ namespace Content.Server.Temperature.Systems
}
}
public class ModifyChangedTemperatureEvent : EntityEventArgs
public class ModifyChangedTemperatureEvent : EntityEventArgs, IInventoryRelayEvent
{
public SlotFlags TargetSlots { get; } = ~SlotFlags.POCKET;
public float TemperatureDelta;
public ModifyChangedTemperatureEvent(float temperature)

View File

@@ -1,6 +1,7 @@
using System.Linq;
using Content.Shared.Damage.Prototypes;
using Content.Shared.FixedPoint;
using Content.Shared.Inventory;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
@@ -216,8 +217,11 @@ namespace Content.Shared.Damage
///
/// For example, armor.
/// </summary>
public class DamageModifyEvent : EntityEventArgs
public class DamageModifyEvent : EntityEventArgs, IInventoryRelayEvent
{
// Whenever locational damage is a thing, this should just check only that bit of armour.
public SlotFlags TargetSlots { get; } = ~SlotFlags.POCKET;
public DamageSpecifier Damage;
public DamageModifyEvent(DamageSpecifier damage)

View File

@@ -1,9 +1,12 @@
using Content.Shared.Inventory;
using Robust.Shared.GameObjects;
namespace Content.Shared.Electrocution
{
public class ElectrocutionAttemptEvent : CancellableEntityEventArgs
public class ElectrocutionAttemptEvent : CancellableEntityEventArgs, IInventoryRelayEvent
{
public SlotFlags TargetSlots { get; } = ~SlotFlags.POCKET;
public readonly EntityUid TargetUid;
public readonly EntityUid? SourceUid;
public float SiemensCoefficient = 1f;

View File

@@ -16,9 +16,9 @@ public partial class InventorySystem
SubscribeLocalEvent<InventoryComponent, RefreshMovementSpeedModifiersEvent>(RelayInventoryEvent);
}
protected void RelayInventoryEvent<T>(EntityUid uid, InventoryComponent component, T args) where T : EntityEventArgs
protected void RelayInventoryEvent<T>(EntityUid uid, InventoryComponent component, T args) where T : EntityEventArgs, IInventoryRelayEvent
{
var containerEnumerator = new ContainerSlotEnumerator(uid, component.TemplateId, _prototypeManager, this);
var containerEnumerator = new ContainerSlotEnumerator(uid, component.TemplateId, _prototypeManager, this, args.TargetSlots);
while(containerEnumerator.MoveNext(out var container))
{
if(!container.ContainedEntity.HasValue) continue;
@@ -26,3 +26,18 @@ public partial class InventorySystem
}
}
}
/// <summary>
/// Events that should be relayed to inventory slots should implement this interface.
/// </summary>
public interface IInventoryRelayEvent
{
/// <summary>
/// What inventory slots should this event be relayed to, if any?
/// </summary>
/// <remarks>
/// In general you may want to exclude <see cref="SlotFlags.POCKET"/>, given that those items are not truly
/// "equipped" by the user.
/// </remarks>
public SlotFlags TargetSlots { get; }
}

View File

@@ -86,32 +86,38 @@ public partial class InventorySystem : EntitySystem
private readonly InventorySystem _inventorySystem;
private readonly EntityUid _uid;
private readonly SlotDefinition[] _slots;
private int _nextIdx = int.MaxValue;
private readonly SlotFlags _flags;
private int _nextIdx = 0;
public ContainerSlotEnumerator(EntityUid uid, string prototypeId, IPrototypeManager prototypeManager, InventorySystem inventorySystem)
public ContainerSlotEnumerator(EntityUid uid, string prototypeId, IPrototypeManager prototypeManager, InventorySystem inventorySystem, SlotFlags flags = SlotFlags.All)
{
_uid = uid;
_inventorySystem = inventorySystem;
_flags = flags;
if (prototypeManager.TryIndex<InventoryTemplatePrototype>(prototypeId, out var prototype))
{
_slots = prototype.Slots;
if(_slots.Length > 0)
_nextIdx = 0;
}
else
{
_slots = Array.Empty<SlotDefinition>();
}
}
public bool MoveNext([NotNullWhen(true)] out ContainerSlot? container)
{
container = null;
if (_nextIdx >= _slots.Length) return false;
while (_nextIdx < _slots.Length && !_inventorySystem.TryGetSlotContainer(_uid, _slots[_nextIdx++].Name, out container, out _)) { }
while (_nextIdx < _slots.Length)
{
var slot = _slots[_nextIdx];
_nextIdx++;
return container != null;
if ((slot.SlotFlags & _flags) == 0)
continue;
if (_inventorySystem.TryGetSlotContainer(_uid, slot.Name, out container, out _))
return true;
}
return false;
}
}
}

View File

@@ -26,4 +26,5 @@ public enum SlotFlags
POCKET = 1 << 12,
LEGS = 1 << 13,
FEET = 1 << 14,
All = ~NONE,
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Shared.Inventory;
using Content.Shared.Movement.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
@@ -84,8 +85,10 @@ namespace Content.Shared.Movement.EntitySystems
/// should hook into this event and set it then. If you want this event to be raised,
/// call <see cref="MovementSpeedModifierSystem.RefreshMovementSpeedModifiers"/>.
/// </summary>
public class RefreshMovementSpeedModifiersEvent : EntityEventArgs
public class RefreshMovementSpeedModifiersEvent : EntityEventArgs, IInventoryRelayEvent
{
public SlotFlags TargetSlots { get; } = ~SlotFlags.POCKET;
public float WalkSpeedModifier { get; private set; } = 1.0f;
public float SprintSpeedModifier { get; private set; } = 1.0f;

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.Inventory;
using Content.Shared.StatusEffect;
using Content.Shared.Stunnable;
using JetBrains.Annotations;
@@ -158,7 +159,8 @@ namespace Content.Shared.Slippery
/// <summary>
/// Raised on an entity to determine if it can slip or not.
/// </summary>
public class SlipAttemptEvent : CancellableEntityEventArgs
public class SlipAttemptEvent : CancellableEntityEventArgs, IInventoryRelayEvent
{
public SlotFlags TargetSlots { get; } = SlotFlags.FEET;
}
}