diff --git a/Content.Client/Entry/IgnoredComponents.cs b/Content.Client/Entry/IgnoredComponents.cs index 805c620ea0..f5d976796d 100644 --- a/Content.Client/Entry/IgnoredComponents.cs +++ b/Content.Client/Entry/IgnoredComponents.cs @@ -92,6 +92,7 @@ namespace Content.Client.Entry "ExaminableBattery", "PottedPlantHide", "SecureEntityStorage", + "Lock", "PresetIdCard", "SolarControlConsole", "FlashOnTrigger", diff --git a/Content.Client/Storage/Visualizers/StorageVisualizer.cs b/Content.Client/Storage/Visualizers/StorageVisualizer.cs index 7b7aaf28c4..43a8df5684 100644 --- a/Content.Client/Storage/Visualizers/StorageVisualizer.cs +++ b/Content.Client/Storage/Visualizers/StorageVisualizer.cs @@ -1,4 +1,4 @@ -using Content.Shared.Storage; +using Content.Shared.Storage; using JetBrains.Annotations; using Robust.Client.GameObjects; using Robust.Shared.GameObjects; diff --git a/Content.Server/Lock/LockComponent.cs b/Content.Server/Lock/LockComponent.cs new file mode 100644 index 0000000000..966548e607 --- /dev/null +++ b/Content.Server/Lock/LockComponent.cs @@ -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 +{ + /// + /// Allows locking/unlocking, with access determined by AccessReader + /// + [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 + { + protected override void GetData(IEntity user, LockComponent component, VerbData data) + { + if (!EntitySystem.Get().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().CanInteract(user) || + !user.InRangeUnobstructed(component)) + { + return; + } + + // Call relevant entity system + var lockSystem = user.EntityManager.EntitySysManager.GetEntitySystem(); + var eventData = new ActivateInWorldEvent(user, component.Owner); + if (component.Locked) + { + lockSystem.DoUnlock(component, eventData); + } + else + { + lockSystem.DoLock(component, eventData); + } + } + } + } +} diff --git a/Content.Server/Lock/LockSystem.cs b/Content.Server/Lock/LockSystem.cs new file mode 100644 index 0000000000..063ac1656a --- /dev/null +++ b/Content.Server/Lock/LockSystem.cs @@ -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 +{ + /// + /// Handles (un)locking and examining of Lock components + /// + [UsedImplicitly] + public class LockSystem : EntitySystem + { + /// + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent(OnStartup); + SubscribeLocalEvent(OnActivated); + SubscribeLocalEvent(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; + } + } +} diff --git a/Content.Server/Storage/Components/EntityStorageComponent.cs b/Content.Server/Storage/Components/EntityStorageComponent.cs index 5face20f31..cbe6f9affd 100644 --- a/Content.Server/Storage/Components/EntityStorageComponent.cs +++ b/Content.Server/Storage/Components/EntityStorageComponent.cs @@ -24,7 +24,6 @@ using Robust.Shared.IoC; using Robust.Shared.Localization; using Robust.Shared.Maths; using Robust.Shared.Physics; -using Robust.Shared.Physics.Broadphase; using Robust.Shared.Player; using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Timing; @@ -131,7 +130,8 @@ namespace Content.Server.Storage.Components private bool _beingWelded; [ViewVariables(VVAccess.ReadWrite)] - public bool CanWeldShut { + public bool CanWeldShut + { get => _canWeldShut; set { @@ -160,6 +160,13 @@ namespace Content.Server.Storage.Components public virtual void Activate(ActivateEventArgs eventArgs) { + // HACK until EntityStorageComponent gets refactored to the new ECS system + if (Owner.TryGetComponent(out var @lock) && @lock.Locked) + { + // Do nothing, LockSystem is responsible for handling this case + return; + } + ToggleOpen(eventArgs.User); } @@ -167,7 +174,7 @@ namespace Content.Server.Storage.Components { if (IsWeldedShut) { - if(!silent) Owner.PopupMessage(user, Loc.GetString("entity-storage-component-welded-shut-message")); + if (!silent) Owner.PopupMessage(user, Loc.GetString("entity-storage-component-welded-shut-message")); return false; } return true; @@ -465,7 +472,8 @@ namespace Content.Server.Storage.Components protected virtual void OpenVerbGetData(IEntity user, EntityStorageComponent component, VerbData data) { - if (!EntitySystem.Get().CanInteract(user)) + if (!EntitySystem.Get().CanInteract(user) || + component.Owner.TryGetComponent(out LockComponent? lockComponent) && lockComponent.Locked) // HACK extra check, until EntityStorage gets refactored { data.Visibility = VerbVisibility.Invisible; return; @@ -475,7 +483,7 @@ namespace Content.Server.Storage.Components { data.Visibility = VerbVisibility.Disabled; var verb = Loc.GetString(component.Open ? "open-toggle-verb-close" : "open-toggle-verb-open"); - data.Text = Loc.GetString("open-toggle-verb-welded-shut-message",("verb", verb)); + data.Text = Loc.GetString("open-toggle-verb-welded-shut-message", ("verb", verb)); return; } diff --git a/Content.Server/Storage/Components/SecureEntityStorageComponent.cs b/Content.Server/Storage/Components/SecureEntityStorageComponent.cs deleted file mode 100644 index 5dcaf78cf4..0000000000 --- a/Content.Server/Storage/Components/SecureEntityStorageComponent.cs +++ /dev/null @@ -1,148 +0,0 @@ -using Content.Server.Access.Components; -using Content.Shared.ActionBlocker; -using Content.Shared.Interaction; -using Content.Shared.Interaction.Events; -using Content.Shared.Notification.Managers; -using Content.Shared.Storage; -using Content.Shared.Verbs; -using Robust.Server.GameObjects; -using Robust.Shared.Audio; -using Robust.Shared.GameObjects; -using Robust.Shared.Localization; -using Robust.Shared.Player; -using Robust.Shared.Serialization.Manager.Attributes; -using Robust.Shared.ViewVariables; - -namespace Content.Server.Storage.Components -{ - [RegisterComponent] - [ComponentReference(typeof(EntityStorageComponent))] - [ComponentReference(typeof(IActivate))] - [ComponentReference(typeof(IStorageComponent))] - public class SecureEntityStorageComponent : EntityStorageComponent - { - public override string Name => "SecureEntityStorage"; - [DataField("locked")] - private bool _locked = true; - - [ViewVariables(VVAccess.ReadWrite)] - public bool Locked - { - get => _locked; - set - { - _locked = value; - - if (Owner.TryGetComponent(out AppearanceComponent? appearance)) - { - appearance.SetData(StorageVisuals.Locked, _locked); - } - } - } - - protected override void Startup() - { - base.Startup(); - - if (Owner.TryGetComponent(out AppearanceComponent? appearance)) - { - appearance.SetData(StorageVisuals.CanLock, true); - } - } - - public override void Activate(ActivateEventArgs eventArgs) - { - if (Locked) - { - DoToggleLock(eventArgs.User); - return; - } - - base.Activate(eventArgs); - } - - public override bool CanOpen(IEntity user, bool silent = false) - { - if (Locked) - { - Owner.PopupMessage(user, "It's locked!"); - return false; - } - return base.CanOpen(user, silent); - } - - protected override void OpenVerbGetData(IEntity user, EntityStorageComponent component, VerbData data) - { - if (Locked) - { - data.Visibility = VerbVisibility.Invisible; - - return; - } - - base.OpenVerbGetData(user, component, data); - } - - private void DoToggleLock(IEntity user) - { - if (Locked) - { - DoUnlock(user); - } - else - { - DoLock(user); - } - } - - private void DoUnlock(IEntity user) - { - if (!CheckAccess(user)) return; - - Locked = false; - SoundSystem.Play(Filter.Pvs(Owner), "/Audio/Machines/door_lock_off.ogg", Owner, AudioParams.Default.WithVolume(-5)); - } - - private void DoLock(IEntity user) - { - if (!CheckAccess(user)) return; - - Locked = true; - SoundSystem.Play(Filter.Pvs(Owner), "/Audio/Machines/door_lock_on.ogg", Owner, AudioParams.Default.WithVolume(-5)); - } - - private bool CheckAccess(IEntity user) - { - if (Owner.TryGetComponent(out AccessReader? reader)) - { - if (!reader.IsAllowed(user)) - { - Owner.PopupMessage(user, Loc.GetString("secure-entity-storage-component-not-allowed-message")); - return false; - } - } - - return true; - } - - [Verb] - private sealed class ToggleLockVerb : Verb - { - protected override void GetData(IEntity user, SecureEntityStorageComponent component, VerbData data) - { - if (!EntitySystem.Get().CanInteract(user) || component.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, SecureEntityStorageComponent component) - { - component.DoToggleLock(user); - } - } - } -} diff --git a/Resources/Locale/en-US/lock/lock-component.ftl b/Resources/Locale/en-US/lock/lock-component.ftl new file mode 100644 index 0000000000..f9f975c96e --- /dev/null +++ b/Resources/Locale/en-US/lock/lock-component.ftl @@ -0,0 +1,10 @@ +lock-comp-on-examined-is-locked = The {$entityName} seems to be locked. +lock-comp-on-examined-is-unlocked = The {$entityName} seems to be unlocked. +lock-comp-do-lock-success = You lock the {$entityName}. +lock-comp-do-unlock-success = You unlock the {$entityName}. +lock-comp-has-user-access-fail = Access denied + +## ToggleLockVerb + +toggle-lock-verb-unlock = Unlock +toggle-lock-verb-lock = Lock \ No newline at end of file diff --git a/Resources/Locale/en-US/storage/components/secure-entity-storage-component.ftl b/Resources/Locale/en-US/storage/components/secure-entity-storage-component.ftl deleted file mode 100644 index 02082f2dcc..0000000000 --- a/Resources/Locale/en-US/storage/components/secure-entity-storage-component.ftl +++ /dev/null @@ -1,6 +0,0 @@ -secure-entity-storage-component-not-allowed-message = Access denied - -## ToggleLockVerb - -toggle-lock-verb-unlock = Unlock -toggle-lock-verb-lock = Lock \ No newline at end of file diff --git a/Resources/Prototypes/Entities/Structures/Storage/Closets/Lockers/base.yml b/Resources/Prototypes/Entities/Structures/Storage/Closets/Lockers/base.yml index 1b33400a9f..a8fe9b1a7e 100644 --- a/Resources/Prototypes/Entities/Structures/Storage/Closets/Lockers/base.yml +++ b/Resources/Prototypes/Entities/Structures/Storage/Closets/Lockers/base.yml @@ -4,7 +4,7 @@ abstract: true components: - type: AccessReader - - type: SecureEntityStorage + - type: Lock - type: Sprite netsync: false sprite: Structures/Storage/closet.rsi diff --git a/Resources/Prototypes/Entities/Structures/Storage/Crates/crates.yml b/Resources/Prototypes/Entities/Structures/Storage/Crates/crates.yml index dae7bc4c1c..70e4f09b5d 100644 --- a/Resources/Prototypes/Entities/Structures/Storage/Crates/crates.yml +++ b/Resources/Prototypes/Entities/Structures/Storage/Crates/crates.yml @@ -261,7 +261,7 @@ components: - type: AccessReader access: [["Security"]] - - type: SecureEntityStorage + - type: Lock - type: Sprite sprite: Structures/Storage/Crates/sec_gear.rsi layers: @@ -290,7 +290,7 @@ components: - type: AccessReader access: [["Engineering"]] - - type: SecureEntityStorage + - type: Lock - type: Sprite sprite: Structures/Storage/Crates/engicrate_secure.rsi layers: @@ -319,7 +319,7 @@ components: - type: AccessReader access: [["Medical"]] - - type: SecureEntityStorage + - type: Lock - type: Sprite sprite: Structures/Storage/Crates/medicalcrate_secure.rsi layers: @@ -347,7 +347,7 @@ parent: CrateGeneric components: - type: AccessReader - - type: SecureEntityStorage + - type: Lock - type: Sprite sprite: Structures/Storage/Crates/privatecrate_secure.rsi layers: @@ -376,7 +376,7 @@ components: - type: AccessReader access: [["Research"]] - - type: SecureEntityStorage + - type: Lock - type: Sprite sprite: Structures/Storage/Crates/scicrate_secure.rsi layers: @@ -405,7 +405,7 @@ components: - type: AccessReader access: [["Engineering"]] - - type: SecureEntityStorage + - type: Lock - type: Sprite sprite: Structures/Storage/Crates/plasma.rsi layers: @@ -433,7 +433,7 @@ parent: CrateGeneric components: - type: AccessReader - - type: SecureEntityStorage + - type: Lock - type: Sprite sprite: Structures/Storage/Crates/secure.rsi layers: @@ -462,7 +462,7 @@ components: - type: AccessReader access: [["Service"]] - - type: SecureEntityStorage + - type: Lock - type: Sprite sprite: Structures/Storage/Crates/hydro_secure.rsi layers: @@ -491,7 +491,7 @@ components: - type: AccessReader access: [["Security"]] - - type: SecureEntityStorage + - type: Lock - type: Sprite sprite: Structures/Storage/Crates/weapon.rsi layers: