#4420 Replaced SecureEntityStorageComp with LockComponent (new ECS) (#4228)

* #4420 moved sound components to a better spot in the project

* MWF-4420 - ported SecureEntityStorageComponent to new ECS system, as a LockComponent

* MWF-4420 - removed unused usings

* #4420 removed dumb ToggleLockVerb override workaround

* #4420 added SoundSpecifier to LockComponent
This commit is contained in:
Galactic Chimp
2021-07-30 19:22:06 +02:00
committed by GitHub
parent e256cb7bb0
commit 898bdc9491
10 changed files with 215 additions and 170 deletions

View File

@@ -0,0 +1,64 @@
using Content.Server.Lock;
using Content.Shared.ActionBlocker;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Helpers;
using Content.Shared.Sound;
using Content.Shared.Verbs;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Storage.Components
{
/// <summary>
/// Allows locking/unlocking, with access determined by AccessReader
/// </summary>
[RegisterComponent]
public class LockComponent : Component
{
public override string Name => "Lock";
[ViewVariables(VVAccess.ReadWrite)] [DataField("locked")] public bool Locked { get; set; } = true;
[ViewVariables(VVAccess.ReadWrite)] [DataField("unlockingSound")] public SoundSpecifier? UnlockSound { get; set; } = new SoundPathSpecifier("/Audio/Machines/door_lock_off.ogg");
[ViewVariables(VVAccess.ReadWrite)] [DataField("lockingSound")] public SoundSpecifier? LockSound { get; set; } = new SoundPathSpecifier("/Audio/Machines/door_lock_off.ogg");
[Verb]
private sealed class ToggleLockVerb : Verb<LockComponent>
{
protected override void GetData(IEntity user, LockComponent component, VerbData data)
{
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(user) ||
component.Owner.TryGetComponent(out EntityStorageComponent? entityStorageComponent) && entityStorageComponent.Open)
{
data.Visibility = VerbVisibility.Invisible;
return;
}
data.Text = Loc.GetString(component.Locked ? "toggle-lock-verb-unlock" : "toggle-lock-verb-lock");
}
protected override void Activate(IEntity user, LockComponent component)
{
// Do checks
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(user) ||
!user.InRangeUnobstructed(component))
{
return;
}
// Call relevant entity system
var lockSystem = user.EntityManager.EntitySysManager.GetEntitySystem<LockSystem>();
var eventData = new ActivateInWorldEvent(user, component.Owner);
if (component.Locked)
{
lockSystem.DoUnlock(component, eventData);
}
else
{
lockSystem.DoLock(component, eventData);
}
}
}
}
}

View File

@@ -0,0 +1,116 @@
using Content.Server.Access.Components;
using Content.Server.Storage.Components;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Notification.Managers;
using Content.Shared.Storage;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Player;
namespace Content.Server.Lock
{
/// <summary>
/// Handles (un)locking and examining of Lock components
/// </summary>
[UsedImplicitly]
public class LockSystem : EntitySystem
{
/// <inheritdoc />
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<LockComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<LockComponent, ActivateInWorldEvent>(OnActivated);
SubscribeLocalEvent<LockComponent, ExaminedEvent>(OnExamined);
}
private void OnStartup(EntityUid eUI, LockComponent lockComp, ComponentStartup args)
{
if (lockComp.Owner.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(StorageVisuals.CanLock, true);
}
}
private void OnActivated(EntityUid eUI, LockComponent lockComp, ActivateInWorldEvent args)
{
// Only attempt an unlock by default on Activate
if (lockComp.Locked)
{
DoUnlock(lockComp, args);
}
}
private void OnExamined(EntityUid eUI, LockComponent lockComp, ExaminedEvent args)
{
args.Message.AddText("\n");
args.Message.AddText(Loc.GetString(lockComp.Locked
? "lock-comp-on-examined-is-locked"
: "lock-comp-on-examined-is-unlocked",
("entityName", lockComp.Owner.Name)));
}
public void DoLock(LockComponent lockComp, ActivateInWorldEvent args)
{
if (!HasUserAccess(lockComp, args.User))
{
return;
}
lockComp.Owner.PopupMessage(args.User, Loc.GetString("lock-comp-do-lock-success", ("entityName",lockComp.Owner.Name)));
lockComp.Locked = true;
if(lockComp.LockSound != null)
{
SoundSystem.Play(Filter.Pvs(lockComp.Owner), lockComp.LockSound.GetSound(), lockComp.Owner, AudioParams.Default.WithVolume(-5));
}
if (lockComp.Owner.TryGetComponent(out AppearanceComponent? appearanceComp))
{
appearanceComp.SetData(StorageVisuals.Locked, true);
}
args.Handled = true;
}
public void DoUnlock(LockComponent lockComp, ActivateInWorldEvent args )
{
if (!HasUserAccess(lockComp, args.User))
{
return;
}
lockComp.Owner.PopupMessage(args.User, Loc.GetString("lock-comp-do-unlock-success", ("entityName", lockComp.Owner.Name)));
lockComp.Locked = false;
if(lockComp.UnlockSound != null)
{
SoundSystem.Play(Filter.Pvs(lockComp.Owner), lockComp.UnlockSound.GetSound(), lockComp.Owner, AudioParams.Default.WithVolume(-5));
}
if (lockComp.Owner.TryGetComponent(out AppearanceComponent? appearanceComp))
{
appearanceComp.SetData(StorageVisuals.Locked, false);
}
// To stop EntityStorageComponent from opening right after the container gets unlocked
args.Handled = true;
}
private static bool HasUserAccess(LockComponent lockComp, IEntity user)
{
if (lockComp.Owner.TryGetComponent(out AccessReader? reader))
{
if (!reader.IsAllowed(user))
{
lockComp.Owner.PopupMessage(user, Loc.GetString("lock-comp-has-user-access-fail"));
return false;
}
}
return true;
}
}
}