From 56d25ba0f640dbdc80e3c08e0c19d445787483fb Mon Sep 17 00:00:00 2001 From: metalgearsloth Date: Sun, 22 Sep 2024 20:35:16 +1000 Subject: [PATCH] Predict vending machine BUI --- .../VendingMachineBoundUserInterface.cs | 20 +++---------- .../VendingMachines/VendingMachineSystem.cs | 10 +++++++ .../VendingMachines/VendingMachineSystem.cs | 30 +++---------------- .../SharedVendingMachineSystem.cs | 5 ++-- .../VendingMachineComponent.cs | 23 ++++---------- .../VendingMachineInterfaceState.cs | 11 ------- .../Structures/Machines/vending_machines.yml | 3 ++ 7 files changed, 30 insertions(+), 72 deletions(-) diff --git a/Content.Client/VendingMachines/VendingMachineBoundUserInterface.cs b/Content.Client/VendingMachines/VendingMachineBoundUserInterface.cs index f8313882a2fee0..28b1b25adef03f 100644 --- a/Content.Client/VendingMachines/VendingMachineBoundUserInterface.cs +++ b/Content.Client/VendingMachines/VendingMachineBoundUserInterface.cs @@ -23,29 +23,17 @@ protected override void Open() { base.Open(); - var vendingMachineSys = EntMan.System(); - - _cachedInventory = vendingMachineSys.GetAllInventory(Owner); - _menu = this.CreateWindow(); _menu.OpenCenteredLeft(); _menu.Title = EntMan.GetComponent(Owner).EntityName; - _menu.OnItemSelected += OnItemSelected; - - _menu.Populate(_cachedInventory); - - _menu.OpenCenteredLeft(); + Refresh(); } - protected override void UpdateState(BoundUserInterfaceState state) + public void Refresh() { - base.UpdateState(state); - - if (state is not VendingMachineInterfaceState newState) - return; - - _cachedInventory = newState.Inventory; + var system = EntMan.System(); + _cachedInventory = system.GetAllInventory(Owner); _menu?.Populate(_cachedInventory); } diff --git a/Content.Client/VendingMachines/VendingMachineSystem.cs b/Content.Client/VendingMachines/VendingMachineSystem.cs index 922a75d24a2686..1b1dde2b67ef5b 100644 --- a/Content.Client/VendingMachines/VendingMachineSystem.cs +++ b/Content.Client/VendingMachines/VendingMachineSystem.cs @@ -8,6 +8,7 @@ public sealed class VendingMachineSystem : SharedVendingMachineSystem { [Dependency] private readonly AnimationPlayerSystem _animationPlayer = default!; [Dependency] private readonly SharedAppearanceSystem _appearanceSystem = default!; + [Dependency] private readonly SharedUserInterfaceSystem _uiSystem = default!; public override void Initialize() { @@ -15,6 +16,15 @@ public override void Initialize() SubscribeLocalEvent(OnAppearanceChange); SubscribeLocalEvent(OnAnimationCompleted); + SubscribeLocalEvent(OnVendingAfterState); + } + + private void OnVendingAfterState(EntityUid uid, VendingMachineComponent component, ref AfterAutoHandleStateEvent args) + { + if (_uiSystem.TryGetOpenUi(uid, VendingMachineUiKey.Key, out var bui)) + { + bui.Refresh(); + } } private void OnAnimationCompleted(EntityUid uid, VendingMachineComponent component, AnimationCompletedEvent args) diff --git a/Content.Server/VendingMachines/VendingMachineSystem.cs b/Content.Server/VendingMachines/VendingMachineSystem.cs index a265ce2d431bf1..38407a98c72943 100644 --- a/Content.Server/VendingMachines/VendingMachineSystem.cs +++ b/Content.Server/VendingMachines/VendingMachineSystem.cs @@ -34,10 +34,8 @@ public sealed class VendingMachineSystem : SharedVendingMachineSystem [Dependency] private readonly IRobustRandom _random = default!; [Dependency] private readonly AccessReaderSystem _accessReader = default!; [Dependency] private readonly AppearanceSystem _appearanceSystem = default!; - [Dependency] private readonly SharedActionsSystem _action = default!; [Dependency] private readonly PricingSystem _pricing = default!; [Dependency] private readonly ThrowingSystem _throwingSystem = default!; - [Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!; [Dependency] private readonly IGameTiming _timing = default!; [Dependency] private readonly SpeakOnUIClosedSystem _speakOnUIClosed = default!; @@ -47,7 +45,6 @@ public override void Initialize() { base.Initialize(); - SubscribeLocalEvent(OnComponentMapInit); SubscribeLocalEvent(OnPowerChanged); SubscribeLocalEvent(OnBreak); SubscribeLocalEvent(OnEmagged); @@ -59,7 +56,6 @@ public override void Initialize() Subs.BuiEvents(VendingMachineUiKey.Key, subs => { - subs.Event(OnBoundUIOpened); subs.Event(OnInventoryEjectMessage); }); @@ -70,12 +66,6 @@ public override void Initialize() SubscribeLocalEvent(OnPriceCalculation); } - private void OnComponentMapInit(EntityUid uid, VendingMachineComponent component, MapInitEvent args) - { - _action.AddAction(uid, ref component.ActionEntity, component.Action, uid); - Dirty(uid, component); - } - private void OnVendingPrice(EntityUid uid, VendingMachineComponent component, ref PriceCalculationEvent args) { var price = 0.0; @@ -94,9 +84,9 @@ private void OnVendingPrice(EntityUid uid, VendingMachineComponent component, re args.Price += price; } - protected override void OnComponentInit(EntityUid uid, VendingMachineComponent component, ComponentInit args) + protected override void OnMapInit(EntityUid uid, VendingMachineComponent component, MapInitEvent args) { - base.OnComponentInit(uid, component, args); + base.OnMapInit(uid, component, args); if (HasComp(uid)) { @@ -110,18 +100,6 @@ private void OnActivatableUIOpenAttempt(EntityUid uid, VendingMachineComponent c args.Cancel(); } - private void OnBoundUIOpened(EntityUid uid, VendingMachineComponent component, BoundUIOpenedEvent args) - { - UpdateVendingMachineInterfaceState(uid, component); - } - - private void UpdateVendingMachineInterfaceState(EntityUid uid, VendingMachineComponent component) - { - var state = new VendingMachineInterfaceState(GetAllInventory(uid, component)); - - _userInterfaceSystem.SetUiState(uid, VendingMachineUiKey.Key, state); - } - private void OnInventoryEjectMessage(EntityUid uid, VendingMachineComponent component, VendingMachineEjectMessage args) { if (!this.IsPowered(uid, EntityManager)) @@ -297,7 +275,7 @@ public void TryEjectVendorItem(EntityUid uid, InventoryType type, string itemId, _speakOnUIClosed.TrySetFlag((uid, speakComponent)); entry.Amount--; - UpdateVendingMachineInterfaceState(uid, vendComponent); + Dirty(uid, vendComponent); TryUpdateVisualState(uid, vendComponent); Audio.PlayPvs(vendComponent.SoundVend, uid); } @@ -493,7 +471,7 @@ public void TryRestockInventory(EntityUid uid, VendingMachineComponent? vendComp RestockInventoryFromPrototype(uid, vendComponent); - UpdateVendingMachineInterfaceState(uid, vendComponent); + Dirty(uid, vendComponent); TryUpdateVisualState(uid, vendComponent); } diff --git a/Content.Shared/VendingMachines/SharedVendingMachineSystem.cs b/Content.Shared/VendingMachines/SharedVendingMachineSystem.cs index 59f8489ac625fb..94562ce8d1bf5d 100644 --- a/Content.Shared/VendingMachines/SharedVendingMachineSystem.cs +++ b/Content.Shared/VendingMachines/SharedVendingMachineSystem.cs @@ -23,11 +23,11 @@ public abstract partial class SharedVendingMachineSystem : EntitySystem public override void Initialize() { base.Initialize(); - SubscribeLocalEvent(OnComponentInit); + SubscribeLocalEvent(OnMapInit); SubscribeLocalEvent(OnAfterInteract); } - protected virtual void OnComponentInit(EntityUid uid, VendingMachineComponent component, ComponentInit args) + protected virtual void OnMapInit(EntityUid uid, VendingMachineComponent component, MapInitEvent args) { RestockInventoryFromPrototype(uid, component, component.InitialStockQuality); } @@ -46,6 +46,7 @@ public void RestockInventoryFromPrototype(EntityUid uid, AddInventoryFromPrototype(uid, packPrototype.StartingInventory, InventoryType.Regular, component, restockQuality); AddInventoryFromPrototype(uid, packPrototype.EmaggedInventory, InventoryType.Emagged, component, restockQuality); AddInventoryFromPrototype(uid, packPrototype.ContrabandInventory, InventoryType.Contraband, component, restockQuality); + Dirty(uid, component); } /// diff --git a/Content.Shared/VendingMachines/VendingMachineComponent.cs b/Content.Shared/VendingMachines/VendingMachineComponent.cs index 23130bb8f39345..a3c7949600a189 100644 --- a/Content.Shared/VendingMachines/VendingMachineComponent.cs +++ b/Content.Shared/VendingMachines/VendingMachineComponent.cs @@ -8,7 +8,7 @@ namespace Content.Shared.VendingMachines { - [RegisterComponent, NetworkedComponent, AutoGenerateComponentState] + [RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)] public sealed partial class VendingMachineComponent : Component { /// @@ -21,7 +21,7 @@ public sealed partial class VendingMachineComponent : Component /// Used by the server to determine how long the vending machine stays in the "Deny" state. /// Used by the client to determine how long the deny animation should be played. /// - [DataField("denyDelay")] + [DataField] public float DenyDelay = 2.0f; /// @@ -29,16 +29,16 @@ public sealed partial class VendingMachineComponent : Component /// The selected item is dispensed afer this delay. /// Used by the client to determine how long the deny animation should be played. /// - [DataField("ejectDelay")] + [DataField] public float EjectDelay = 1.2f; - [ViewVariables] + [DataField, AutoNetworkedField] public Dictionary Inventory = new(); - [ViewVariables] + [DataField, AutoNetworkedField] public Dictionary EmaggedInventory = new(); - [ViewVariables] + [DataField, AutoNetworkedField] public Dictionary ContrabandInventory = new(); public bool Contraband; @@ -102,17 +102,6 @@ public sealed partial class VendingMachineComponent : Component // Yoinked from: https://github.com/discordia-space/CEV-Eris/blob/35bbad6764b14e15c03a816e3e89aa1751660ba9/sound/machines/Custom_deny.ogg public SoundSpecifier SoundDeny = new SoundPathSpecifier("/Audio/Machines/custom_deny.ogg"); - /// - /// The action available to the player controlling the vending machine - /// - [DataField("action", customTypeSerializer: typeof(PrototypeIdSerializer))] - [AutoNetworkedField] - public string? Action = "ActionVendingThrow"; - - [DataField("actionEntity")] - [AutoNetworkedField] - public EntityUid? ActionEntity; - public float NonLimitedEjectForce = 7.5f; public float NonLimitedEjectRange = 5f; diff --git a/Content.Shared/VendingMachines/VendingMachineInterfaceState.cs b/Content.Shared/VendingMachines/VendingMachineInterfaceState.cs index 82758b17f60e2f..27651bb1cb4a80 100644 --- a/Content.Shared/VendingMachines/VendingMachineInterfaceState.cs +++ b/Content.Shared/VendingMachines/VendingMachineInterfaceState.cs @@ -2,17 +2,6 @@ namespace Content.Shared.VendingMachines { - [NetSerializable, Serializable] - public sealed class VendingMachineInterfaceState : BoundUserInterfaceState - { - public List Inventory; - - public VendingMachineInterfaceState(List inventory) - { - Inventory = inventory; - } - } - [Serializable, NetSerializable] public sealed class VendingMachineEjectMessage : BoundUserInterfaceMessage { diff --git a/Resources/Prototypes/Entities/Structures/Machines/vending_machines.yml b/Resources/Prototypes/Entities/Structures/Machines/vending_machines.yml index a6cf9ef0e308cf..99879cfbc9450b 100644 --- a/Resources/Prototypes/Entities/Structures/Machines/vending_machines.yml +++ b/Resources/Prototypes/Entities/Structures/Machines/vending_machines.yml @@ -5,6 +5,9 @@ description: Just add capitalism! abstract: true components: + - type: ActionGrant + actions: + - ActionVendingThrow - type: StationAiWhitelist - type: AmbientOnPowered - type: AmbientSound