Files
tbd-station-14/Content.Shared/VendingMachines/SharedVendingMachineSystem.cs
Leon Friedrich 19277a2276 More DoAfter Changes (#14609)
* DoAfters

* Compact Clone()

* Fix mice and cuffables

* Try generalize attempt events

* moves climbabledoafter event to shared, fixes issue with climbable target

* Fix merge (cuffing)

* Make all events netserializable

* handful of doafter events moved

* moves the rest of the events to their respective shared folders

* Changes all mentions of server doafter to shared

* stop stripping cancellation

* fix merge errors

* draw paused doafters

* handle unpausing

* missing netserializable ref

* removes break on stun reference

* removes cuffing state reference

* Fix tools

* Fix door prying.

* Fix construction

* Fix dumping

* Fix wielding assert

* fix rev

* Fix test

* more test fixes

---------

Co-authored-by: keronshb <keronshb@live.com>
2023-04-02 21:13:48 -04:00

120 lines
4.3 KiB
C#

using Content.Shared.Emag.Components;
using Robust.Shared.Prototypes;
using System.Linq;
using Content.Shared.DoAfter;
using Robust.Shared.Serialization;
namespace Content.Shared.VendingMachines;
public abstract class SharedVendingMachineSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<VendingMachineComponent, ComponentInit>(OnComponentInit);
}
protected virtual void OnComponentInit(EntityUid uid, VendingMachineComponent component, ComponentInit args)
{
RestockInventoryFromPrototype(uid, component);
}
public void RestockInventoryFromPrototype(EntityUid uid,
VendingMachineComponent? component = null)
{
if (!Resolve(uid, ref component))
{
return;
}
if (!_prototypeManager.TryIndex(component.PackPrototypeId, out VendingMachineInventoryPrototype? packPrototype))
return;
AddInventoryFromPrototype(uid, packPrototype.StartingInventory, InventoryType.Regular, component);
AddInventoryFromPrototype(uid, packPrototype.EmaggedInventory, InventoryType.Emagged, component);
AddInventoryFromPrototype(uid, packPrototype.ContrabandInventory, InventoryType.Contraband, component);
}
/// <summary>
/// Returns all of the vending machine's inventory. Only includes emagged and contraband inventories if
/// <see cref="EmaggedComponent"/> exists and <see cref="VendingMachineComponent.Contraband"/> is true
/// are <c>true</c> respectively.
/// </summary>
/// <param name="uid"></param>
/// <param name="component"></param>
/// <returns></returns>
public List<VendingMachineInventoryEntry> GetAllInventory(EntityUid uid, VendingMachineComponent? component = null)
{
if (!Resolve(uid, ref component))
return new();
var inventory = new List<VendingMachineInventoryEntry>(component.Inventory.Values);
if (HasComp<EmaggedComponent>(uid))
inventory.AddRange(component.EmaggedInventory.Values);
if (component.Contraband)
inventory.AddRange(component.ContrabandInventory.Values);
return inventory;
}
public List<VendingMachineInventoryEntry> GetAvailableInventory(EntityUid uid, VendingMachineComponent? component = null)
{
if (!Resolve(uid, ref component))
return new();
return GetAllInventory(uid, component).Where(_ => _.Amount > 0).ToList();
}
private void AddInventoryFromPrototype(EntityUid uid, Dictionary<string, uint>? entries,
InventoryType type,
VendingMachineComponent? component = null)
{
if (!Resolve(uid, ref component) || entries == null)
{
return;
}
Dictionary<string, VendingMachineInventoryEntry> inventory;
switch (type)
{
case InventoryType.Regular:
inventory = component.Inventory;
break;
case InventoryType.Emagged:
inventory = component.EmaggedInventory;
break;
case InventoryType.Contraband:
inventory = component.ContrabandInventory;
break;
default:
return;
}
foreach (var (id, amount) in entries)
{
if (_prototypeManager.HasIndex<EntityPrototype>(id))
{
if (inventory.TryGetValue(id, out VendingMachineInventoryEntry? entry))
// Prevent a machine's stock from going over three times
// the prototype's normal amount. This is an arbitrary
// number and meant to be a convenience for someone
// restocking a machine who doesn't want to force vend out
// all the items just to restock one empty slot without
// losing the rest of the restock.
entry.Amount = Math.Min(entry.Amount + amount, 3 * amount);
else
inventory.Add(id, new VendingMachineInventoryEntry(type, id, amount));
}
}
}
}
[Serializable, NetSerializable]
public sealed class RestockDoAfterEvent : SimpleDoAfterEvent
{
}