From dd7884ed40114fab1584852647dbb39c24f091e7 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Mon, 23 Sep 2024 12:10:22 +1000 Subject: [PATCH] Predict vending machine BUI (#32376) --- .../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 f8313882a2..28b1b25ade 100644 --- a/Content.Client/VendingMachines/VendingMachineBoundUserInterface.cs +++ b/Content.Client/VendingMachines/VendingMachineBoundUserInterface.cs @@ -23,29 +23,17 @@ namespace Content.Client.VendingMachines { 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 922a75d24a..1b1dde2b67 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 sealed class VendingMachineSystem : SharedVendingMachineSystem 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 a265ce2d43..38407a98c7 100644 --- a/Content.Server/VendingMachines/VendingMachineSystem.cs +++ b/Content.Server/VendingMachines/VendingMachineSystem.cs @@ -34,10 +34,8 @@ namespace Content.Server.VendingMachines [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 @@ namespace Content.Server.VendingMachines { base.Initialize(); - SubscribeLocalEvent(OnComponentMapInit); SubscribeLocalEvent(OnPowerChanged); SubscribeLocalEvent(OnBreak); SubscribeLocalEvent(OnEmagged); @@ -59,7 +56,6 @@ namespace Content.Server.VendingMachines Subs.BuiEvents(VendingMachineUiKey.Key, subs => { - subs.Event(OnBoundUIOpened); subs.Event(OnInventoryEjectMessage); }); @@ -70,12 +66,6 @@ namespace Content.Server.VendingMachines 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 @@ namespace Content.Server.VendingMachines 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 @@ namespace Content.Server.VendingMachines 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 @@ namespace Content.Server.VendingMachines _speakOnUIClosed.TrySetFlag((uid, speakComponent)); entry.Amount--; - UpdateVendingMachineInterfaceState(uid, vendComponent); + Dirty(uid, vendComponent); TryUpdateVisualState(uid, vendComponent); Audio.PlayPvs(vendComponent.SoundVend, uid); } @@ -493,7 +471,7 @@ namespace Content.Server.VendingMachines 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 59f8489ac6..94562ce8d1 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 abstract partial class SharedVendingMachineSystem : EntitySystem 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 23130bb8f3..a3c7949600 100644 --- a/Content.Shared/VendingMachines/VendingMachineComponent.cs +++ b/Content.Shared/VendingMachines/VendingMachineComponent.cs @@ -8,7 +8,7 @@ using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototy namespace Content.Shared.VendingMachines { - [RegisterComponent, NetworkedComponent, AutoGenerateComponentState] + [RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)] public sealed partial class VendingMachineComponent : Component { /// @@ -21,7 +21,7 @@ namespace Content.Shared.VendingMachines /// 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 @@ namespace Content.Shared.VendingMachines /// 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 @@ namespace Content.Shared.VendingMachines // 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 82758b17f6..27651bb1cb 100644 --- a/Content.Shared/VendingMachines/VendingMachineInterfaceState.cs +++ b/Content.Shared/VendingMachines/VendingMachineInterfaceState.cs @@ -2,17 +2,6 @@ using Robust.Shared.Serialization; 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 a6cf9ef0e3..99879cfbc9 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