Make vending machine restocks predicted (and its sound not spammable) (#38609)

* feat: make vending machine restocks predicted

* refactor: VendingMachineRestockComponent cleanup

* refactor: minor simplification

* revert: refactor: minor simplification; load bearing IsFirstTimePredicted

lol second guessed myself

* chore: unneeded VendingMachineSystem dep

* Update Content.Shared/VendingMachines/VendingMachineComponent.cs

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
This commit is contained in:
Perry Fraser
2025-09-11 14:27:54 -04:00
committed by GitHub
parent 77eca4a570
commit fbf65b7f74
5 changed files with 77 additions and 70 deletions

View File

@@ -8,20 +8,14 @@ using Content.Server.Vocalization.Systems;
using Content.Shared.Cargo;
using Content.Shared.Damage;
using Content.Shared.Destructible;
using Content.Shared.DoAfter;
using Content.Shared.Emp;
using Content.Shared.IdentityManagement;
using Content.Shared.Popups;
using Content.Shared.Power;
using Content.Shared.Throwing;
using Content.Shared.UserInterface;
using Content.Shared.VendingMachines;
using Content.Shared.Wall;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server.VendingMachines
{
@@ -30,7 +24,6 @@ namespace Content.Server.VendingMachines
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly PricingSystem _pricing = default!;
[Dependency] private readonly ThrowingSystem _throwingSystem = default!;
[Dependency] private readonly IGameTiming _timing = default!;
private const float WallVendEjectDistanceFromWall = 1f;
@@ -46,11 +39,8 @@ namespace Content.Server.VendingMachines
SubscribeLocalEvent<VendingMachineComponent, TryVocalizeEvent>(OnTryVocalize);
SubscribeLocalEvent<VendingMachineComponent, ActivatableUIOpenAttemptEvent>(OnActivatableUIOpenAttempt);
SubscribeLocalEvent<VendingMachineComponent, VendingMachineSelfDispenseEvent>(OnSelfDispense);
SubscribeLocalEvent<VendingMachineComponent, RestockDoAfterEvent>(OnDoAfter);
SubscribeLocalEvent<VendingMachineRestockComponent, PriceCalculationEvent>(OnPriceCalculation);
}
@@ -133,30 +123,6 @@ namespace Content.Server.VendingMachines
EjectRandom(uid, throwItem: true, forceEject: false, component);
}
private void OnDoAfter(EntityUid uid, VendingMachineComponent component, DoAfterEvent args)
{
if (args.Handled || args.Cancelled || args.Args.Used == null)
return;
if (!TryComp<VendingMachineRestockComponent>(args.Args.Used, out var restockComponent))
{
Log.Error($"{ToPrettyString(args.Args.User)} tried to restock {ToPrettyString(uid)} with {ToPrettyString(args.Args.Used.Value)} which did not have a VendingMachineRestockComponent.");
return;
}
TryRestockInventory(uid, component);
Popup.PopupEntity(Loc.GetString("vending-machine-restock-done-self", ("target", uid)), args.Args.User, args.Args.User, PopupType.Medium);
var othersFilter = Filter.PvsExcept(args.Args.User);
Popup.PopupEntity(Loc.GetString("vending-machine-restock-done-others", ("user", Identity.Entity(args.User, EntityManager)), ("target", uid)), args.Args.User, othersFilter, true, PopupType.Medium);
Audio.PlayPvs(restockComponent.SoundRestockDone, uid, AudioParams.Default.WithVolume(-2f).WithVariation(0.2f));
Del(args.Args.Used.Value);
args.Handled = true;
}
/// <summary>
/// Sets the <see cref="VendingMachineComponent.CanShoot"/> property of the vending machine.
/// </summary>
@@ -259,7 +225,7 @@ namespace Content.Server.VendingMachines
var disabled = EntityQueryEnumerator<EmpDisabledComponent, VendingMachineComponent>();
while (disabled.MoveNext(out var uid, out _, out var comp))
{
if (comp.NextEmpEject < _timing.CurTime)
if (comp.NextEmpEject < Timing.CurTime)
{
EjectRandom(uid, true, false, comp);
comp.NextEmpEject += (5 * comp.EjectDelay);
@@ -267,17 +233,6 @@ namespace Content.Server.VendingMachines
}
}
public void TryRestockInventory(EntityUid uid, VendingMachineComponent? vendComponent = null)
{
if (!Resolve(uid, ref vendComponent))
return;
RestockInventoryFromPrototype(uid, vendComponent);
Dirty(uid, vendComponent);
TryUpdateVisualState((uid, vendComponent));
}
private void OnPriceCalculation(EntityUid uid, VendingMachineRestockComponent component, ref PriceCalculationEvent args)
{
List<double> priceSets = new();
@@ -308,7 +263,7 @@ namespace Content.Server.VendingMachines
{
args.Affected = true;
args.Disabled = true;
component.NextEmpEject = _timing.CurTime;
component.NextEmpEject = Timing.CurTime;
}
}