ReagentDispenser ECS (#11418)
This commit is contained in:
@@ -1,281 +1,34 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Administration.Logs;
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.Chemistry;
|
||||
using Content.Shared.Chemistry.Dispenser;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.FixedPoint;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Server.Chemistry.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains all the server-side logic for reagent dispensers. See also <see cref="SharedReagentDispenserComponent"/>.
|
||||
/// This includes initializing the component based on prototype data, and sending and receiving messages from the client.
|
||||
/// Messages sent to the client are used to update update the user interface for a component instance.
|
||||
/// Messages sent from the client are used to handle ui button presses.
|
||||
/// A machine that dispenses reagents into a solution container.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedReagentDispenserComponent))]
|
||||
public sealed class ReagentDispenserComponent : SharedReagentDispenserComponent
|
||||
[Access(typeof(ReagentDispenserSystem))]
|
||||
public sealed class ReagentDispenserComponent : Component
|
||||
{
|
||||
private static ReagentInventoryComparer _comparer = new();
|
||||
public static string SolutionName = "reagent";
|
||||
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IEntityManager _entities = default!;
|
||||
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
|
||||
[DataField("pack", customTypeSerializer:typeof(PrototypeIdSerializer<ReagentDispenserInventoryPrototype>))]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public string? PackPrototypeId = default!;
|
||||
|
||||
[ViewVariables] [DataField("pack", customTypeSerializer:typeof(PrototypeIdSerializer<ReagentDispenserInventoryPrototype>))] private string _packPrototypeId = "";
|
||||
[DataField("emagPack", customTypeSerializer:typeof(PrototypeIdSerializer<ReagentDispenserInventoryPrototype>))]
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public string? EmagPackPrototypeId = default!;
|
||||
|
||||
[ViewVariables] [DataField("emagPack", customTypeSerializer:typeof(PrototypeIdSerializer<ReagentDispenserInventoryPrototype>))] public string EmagPackPrototypeId = "";
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool IsEmagged = false;
|
||||
|
||||
public bool AlreadyEmagged = false;
|
||||
[DataField("clickSound"), ViewVariables(VVAccess.ReadWrite)]
|
||||
public SoundSpecifier ClickSound = new SoundPathSpecifier("/Audio/Machines/machine_switch.ogg");
|
||||
|
||||
[DataField("clickSound")]
|
||||
private SoundSpecifier _clickSound = new SoundPathSpecifier("/Audio/Machines/machine_switch.ogg");
|
||||
|
||||
[ViewVariables] private FixedPoint2 _dispenseAmount = FixedPoint2.New(10);
|
||||
|
||||
[UsedImplicitly]
|
||||
[ViewVariables]
|
||||
private Solution? Solution
|
||||
{
|
||||
get
|
||||
{
|
||||
EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(Owner, SolutionName, out var solution);
|
||||
return solution;
|
||||
}
|
||||
}
|
||||
|
||||
[ViewVariables]
|
||||
private bool Powered => !_entities.TryGetComponent(Owner, out ApcPowerReceiverComponent? receiver) || receiver.Powered;
|
||||
|
||||
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(ReagentDispenserUiKey.Key);
|
||||
|
||||
/// <summary>
|
||||
/// Called once per instance of this component. Gets references to any other components needed
|
||||
/// by this component and initializes it's UI and other data.
|
||||
/// </summary>
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += OnUiReceiveMessage;
|
||||
}
|
||||
|
||||
InitializeFromPrototype();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks to see if the <c>pack</c> defined in this components yaml prototype
|
||||
/// exists. If so, it fills the reagent inventory list.
|
||||
/// </summary>
|
||||
private void InitializeFromPrototype()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_packPrototypeId)) return;
|
||||
|
||||
if (!_prototypeManager.TryIndex(_packPrototypeId, out ReagentDispenserInventoryPrototype? packPrototype))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var entry in packPrototype.Inventory)
|
||||
{
|
||||
Inventory.Add(new ReagentDispenserInventoryEntry(entry));
|
||||
}
|
||||
|
||||
Inventory.Sort(_comparer);
|
||||
}
|
||||
|
||||
public void AddFromPrototype(string pack)
|
||||
{
|
||||
if (string.IsNullOrEmpty(pack)) return;
|
||||
|
||||
if (!_prototypeManager.TryIndex(pack, out ReagentDispenserInventoryPrototype? packPrototype))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var entry in packPrototype.Inventory)
|
||||
{
|
||||
Inventory.Add(new ReagentDispenserInventoryEntry(entry));
|
||||
}
|
||||
|
||||
Inventory.Sort(_comparer);
|
||||
}
|
||||
|
||||
|
||||
public void OnPowerChanged()
|
||||
{
|
||||
UpdateUserInterface();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles ui messages from the client. For things such as button presses
|
||||
/// which interact with the world and require server action.
|
||||
/// </summary>
|
||||
/// <param name="obj">A user interface message from the client.</param>
|
||||
private void OnUiReceiveMessage(ServerBoundUserInterfaceMessage obj)
|
||||
{
|
||||
if (obj.Session.AttachedEntity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (obj.Message is not UiButtonPressedMessage msg )
|
||||
return;
|
||||
|
||||
if (!PlayerCanUseDispenser(obj.Session.AttachedEntity, true))
|
||||
return;
|
||||
|
||||
switch (msg.Button)
|
||||
{
|
||||
case UiButton.Clear:
|
||||
TryClear();
|
||||
break;
|
||||
case UiButton.SetDispenseAmount1:
|
||||
_dispenseAmount = FixedPoint2.New(1);
|
||||
break;
|
||||
case UiButton.SetDispenseAmount5:
|
||||
_dispenseAmount = FixedPoint2.New(5);
|
||||
break;
|
||||
case UiButton.SetDispenseAmount10:
|
||||
_dispenseAmount = FixedPoint2.New(10);
|
||||
break;
|
||||
case UiButton.SetDispenseAmount15:
|
||||
_dispenseAmount = FixedPoint2.New(15);
|
||||
break;
|
||||
case UiButton.SetDispenseAmount20:
|
||||
_dispenseAmount = FixedPoint2.New(20);
|
||||
break;
|
||||
case UiButton.SetDispenseAmount25:
|
||||
_dispenseAmount = FixedPoint2.New(25);
|
||||
break;
|
||||
case UiButton.SetDispenseAmount30:
|
||||
_dispenseAmount = FixedPoint2.New(30);
|
||||
break;
|
||||
case UiButton.SetDispenseAmount50:
|
||||
_dispenseAmount = FixedPoint2.New(50);
|
||||
break;
|
||||
case UiButton.SetDispenseAmount100:
|
||||
_dispenseAmount = FixedPoint2.New(100);
|
||||
break;
|
||||
case UiButton.Dispense:
|
||||
if (BeakerSlot.HasItem)
|
||||
{
|
||||
TryDispense(msg.DispenseIndex);
|
||||
// Ew
|
||||
if (BeakerSlot.Item != null)
|
||||
_adminLogger.Add(LogType.ChemicalReaction, LogImpact.Medium,
|
||||
$"{_entities.ToPrettyString(obj.Session.AttachedEntity.Value):player} dispensed {_dispenseAmount}u of {Inventory[msg.DispenseIndex].ID} into {_entities.ToPrettyString(BeakerSlot.Item.Value):entity}");
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
ClickSound();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the player entity is able to use the chem dispenser.
|
||||
/// </summary>
|
||||
/// <param name="playerEntity">The player entity.</param>
|
||||
/// <returns>Returns true if the entity can use the dispenser, and false if it cannot.</returns>
|
||||
private bool PlayerCanUseDispenser(EntityUid? playerEntity, bool needsPower = true)
|
||||
{
|
||||
//Need player entity to check if they are still able to use the dispenser
|
||||
if (playerEntity == null)
|
||||
return false;
|
||||
|
||||
//Check if device is powered
|
||||
if (needsPower && !Powered)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets component data to be used to update the user interface client-side.
|
||||
/// </summary>
|
||||
/// <returns>Returns a <see cref="SharedReagentDispenserComponent.ReagentDispenserBoundUserInterfaceState"/></returns>
|
||||
private ReagentDispenserBoundUserInterfaceState GetUserInterfaceState()
|
||||
{
|
||||
if (BeakerSlot.Item is not {Valid: true} beaker ||
|
||||
!_entities.TryGetComponent(beaker, out FitsInDispenserComponent? fits) ||
|
||||
!EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(beaker, fits.Solution, out var solution))
|
||||
{
|
||||
return new ReagentDispenserBoundUserInterfaceState(Powered, false, FixedPoint2.New(0),
|
||||
FixedPoint2.New(0),
|
||||
string.Empty, Inventory, _entities.GetComponent<MetaDataComponent>(Owner).EntityName, null, _dispenseAmount);
|
||||
}
|
||||
|
||||
return new ReagentDispenserBoundUserInterfaceState(Powered, true, solution.CurrentVolume,
|
||||
solution.MaxVolume,
|
||||
_entities.GetComponent<MetaDataComponent>(beaker).EntityName, Inventory, _entities.GetComponent<MetaDataComponent>(Owner).EntityName, solution.Contents.ToList(), _dispenseAmount);
|
||||
}
|
||||
|
||||
public void UpdateUserInterface()
|
||||
{
|
||||
if (!Initialized) return;
|
||||
|
||||
var state = GetUserInterfaceState();
|
||||
UserInterface?.SetState(state);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If this component contains an entity with a <see cref="SolutionHolder"/>, remove all of it's reagents / solutions.
|
||||
/// </summary>
|
||||
private void TryClear()
|
||||
{
|
||||
if (BeakerSlot.Item is not {Valid: true} beaker ||
|
||||
!_entities.TryGetComponent(beaker, out FitsInDispenserComponent? fits) ||
|
||||
!EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(beaker, fits.Solution, out var solution))
|
||||
return;
|
||||
|
||||
EntitySystem.Get<SolutionContainerSystem>().RemoveAllSolution(beaker, solution);
|
||||
|
||||
UpdateUserInterface();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If this component contains an entity with a <see cref="SolutionHolder"/>, attempt to dispense the specified reagent to it.
|
||||
/// </summary>
|
||||
/// <param name="dispenseIndex">The index of the reagent in <c>Inventory</c>.</param>
|
||||
private void TryDispense(int dispenseIndex)
|
||||
{
|
||||
if (BeakerSlot.Item is not {Valid: true} beaker ||
|
||||
!_entities.TryGetComponent(beaker, out FitsInDispenserComponent? fits) ||
|
||||
!EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(beaker, fits.Solution, out var solution)) return;
|
||||
|
||||
EntitySystem.Get<SolutionContainerSystem>()
|
||||
.TryAddReagent(beaker, solution, Inventory[dispenseIndex].ID, _dispenseAmount, out _);
|
||||
|
||||
UpdateUserInterface();
|
||||
}
|
||||
|
||||
private void ClickSound()
|
||||
{
|
||||
SoundSystem.Play(_clickSound.GetSound(), Filter.Pvs(Owner), Owner, AudioParams.Default.WithVolume(-2f));
|
||||
}
|
||||
|
||||
private sealed class ReagentInventoryComparer : Comparer<ReagentDispenserInventoryEntry>
|
||||
{
|
||||
public override int Compare(ReagentDispenserInventoryEntry x, ReagentDispenserInventoryEntry y)
|
||||
{
|
||||
return string.Compare(x.ID, y.ID, StringComparison.InvariantCultureIgnoreCase);
|
||||
}
|
||||
}
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public ReagentDispenserDispenseAmount DispenseAmount = ReagentDispenserDispenseAmount.U10;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
|
||||
[Dependency] private readonly StorageSystem _storageSystem = default!;
|
||||
[Dependency] private readonly LabelSystem _labelSystem = default!;
|
||||
|
||||
|
||||
private const string PillPrototypeId = "Pill";
|
||||
|
||||
public override void Initialize()
|
||||
@@ -59,18 +59,15 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
{
|
||||
if (!_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out var bufferSolution))
|
||||
return;
|
||||
var inputContainer = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.InputSlotName);
|
||||
var outputContainer = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.OutputSlotName);
|
||||
var inputContainer = _itemSlotsSystem.GetItemOrNull(chemMaster.Owner, SharedChemMaster.InputSlotName);
|
||||
var outputContainer = _itemSlotsSystem.GetItemOrNull(chemMaster.Owner, SharedChemMaster.OutputSlotName);
|
||||
|
||||
var dispenserName = Name(chemMaster.Owner);
|
||||
var bufferReagents = bufferSolution.Contents;
|
||||
var bufferCurrentVolume = bufferSolution.CurrentVolume;
|
||||
|
||||
var state = new ChemMasterBoundUserInterfaceState(
|
||||
chemMaster.Mode, dispenserName,
|
||||
BuildInputContainerInfo(inputContainer), BuildOutputContainerInfo(outputContainer),
|
||||
bufferReagents, bufferCurrentVolume,
|
||||
chemMaster.PillType, chemMaster.PillDosageLimit, updateLabel);
|
||||
chemMaster.Mode, BuildInputContainerInfo(inputContainer), BuildOutputContainerInfo(outputContainer),
|
||||
bufferReagents, bufferCurrentVolume, chemMaster.PillType, chemMaster.PillDosageLimit, updateLabel);
|
||||
|
||||
_userInterfaceSystem.TrySetUiState(chemMaster.Owner, ChemMasterUiKey.Key, state);
|
||||
}
|
||||
@@ -121,7 +118,7 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
|
||||
private void TransferReagents(ChemMasterComponent chemMaster, string reagentId, FixedPoint2 amount, bool fromBuffer)
|
||||
{
|
||||
var container = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.InputSlotName);
|
||||
var container = _itemSlotsSystem.GetItemOrNull(chemMaster.Owner, SharedChemMaster.InputSlotName);
|
||||
if (container is null ||
|
||||
!_solutionContainerSystem.TryGetFitsInDispenser(container.Value, out var containerSolution) ||
|
||||
!_solutionContainerSystem.TryGetSolution(chemMaster.Owner, SharedChemMaster.BufferSolutionName, out var bufferSolution))
|
||||
@@ -157,7 +154,7 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
}
|
||||
else
|
||||
{
|
||||
var container = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.InputSlotName);
|
||||
var container = _itemSlotsSystem.GetItemOrNull(chemMaster.Owner, SharedChemMaster.InputSlotName);
|
||||
if (container is not null &&
|
||||
_solutionContainerSystem.TryGetFitsInDispenser(container.Value, out var containerSolution))
|
||||
{
|
||||
@@ -173,18 +170,18 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
private void OnCreatePillsMessage(EntityUid uid, ChemMasterComponent chemMaster, ChemMasterCreatePillsMessage message)
|
||||
{
|
||||
var user = message.Session.AttachedEntity;
|
||||
var maybeContainer = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.OutputSlotName);
|
||||
var maybeContainer = _itemSlotsSystem.GetItemOrNull(chemMaster.Owner, SharedChemMaster.OutputSlotName);
|
||||
if (maybeContainer is not { Valid: true } container
|
||||
|| !TryComp(container, out ServerStorageComponent? storage)
|
||||
|| storage.Storage is null)
|
||||
{
|
||||
return; // output can't fit pills
|
||||
}
|
||||
|
||||
|
||||
// Ensure the number is valid.
|
||||
if (message.Number == 0 || message.Number > storage.StorageCapacityMax - storage.StorageUsed)
|
||||
return;
|
||||
|
||||
|
||||
// Ensure the amount is valid.
|
||||
if (message.Dosage == 0 || message.Dosage > chemMaster.PillDosageLimit)
|
||||
return;
|
||||
@@ -192,9 +189,9 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
var needed = message.Dosage * message.Number;
|
||||
if (!WithdrawFromBuffer(chemMaster, needed, user, out var withdrawal))
|
||||
return;
|
||||
|
||||
|
||||
_labelSystem.Label(container, message.Label);
|
||||
|
||||
|
||||
for (var i = 0; i < message.Number; i++)
|
||||
{
|
||||
var item = Spawn(PillPrototypeId, Transform(container).Coordinates);
|
||||
@@ -202,10 +199,10 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
_labelSystem.Label(item, message.Label);
|
||||
|
||||
var itemSolution = _solutionContainerSystem.EnsureSolution(item, SharedChemMaster.PillSolutionName);
|
||||
|
||||
|
||||
_solutionContainerSystem.TryAddSolution(
|
||||
item, itemSolution, withdrawal.SplitSolution(message.Dosage));
|
||||
|
||||
|
||||
if (TryComp<SpriteComponent>(item, out var spriteComp))
|
||||
spriteComp.LayerSetState(0, "pill" + (chemMaster.PillType + 1));
|
||||
}
|
||||
@@ -218,7 +215,7 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
EntityUid uid, ChemMasterComponent chemMaster, ChemMasterOutputToBottleMessage message)
|
||||
{
|
||||
var user = message.Session.AttachedEntity;
|
||||
var maybeContainer = _itemSlotsSystem.GetItem(chemMaster.Owner, SharedChemMaster.OutputSlotName);
|
||||
var maybeContainer = _itemSlotsSystem.GetItemOrNull(chemMaster.Owner, SharedChemMaster.OutputSlotName);
|
||||
if (maybeContainer is not { Valid: true } container
|
||||
|| !_solutionContainerSystem.TryGetSolution(
|
||||
container, SharedChemMaster.BottleSolutionName, out var solution))
|
||||
@@ -229,7 +226,7 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
// Ensure the amount is valid.
|
||||
if (message.Dosage == 0 || message.Dosage > solution.AvailableVolume)
|
||||
return;
|
||||
|
||||
|
||||
if (!WithdrawFromBuffer(chemMaster, message.Dosage, user, out var withdrawal))
|
||||
return;
|
||||
|
||||
@@ -247,7 +244,7 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
[NotNullWhen(returnValue: true)] out Solution? outputSolution)
|
||||
{
|
||||
outputSolution = null;
|
||||
|
||||
|
||||
if (!_solutionContainerSystem.TryGetSolution(
|
||||
chemMaster.Owner, SharedChemMaster.BufferSolutionName, out var solution))
|
||||
{
|
||||
@@ -276,7 +273,7 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
{
|
||||
_audioSystem.Play(chemMaster.ClickSound, Filter.Pvs(chemMaster.Owner), chemMaster.Owner, AudioParams.Default.WithVolume(-2f));
|
||||
}
|
||||
|
||||
|
||||
private ContainerInfo? BuildInputContainerInfo(EntityUid? container)
|
||||
{
|
||||
if (container is not { Valid: true })
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
using Content.Server.Chemistry.Components;
|
||||
using Content.Server.Power.Components;
|
||||
|
||||
namespace Content.Server.Chemistry.EntitySystems;
|
||||
|
||||
public sealed partial class ChemistrySystem
|
||||
{
|
||||
private void InitializeReagentDispenser()
|
||||
{
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, PowerChangedEvent>(OnReagentDispenserPower);
|
||||
}
|
||||
|
||||
private static void OnReagentDispenserPower(EntityUid uid, ReagentDispenserComponent component, PowerChangedEvent args)
|
||||
{
|
||||
component.OnPowerChanged();
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,5 @@ public sealed partial class ChemistrySystem : EntitySystem
|
||||
// Why ChemMaster duplicates reagentdispenser nobody knows.
|
||||
InitializeHypospray();
|
||||
InitializeInjector();
|
||||
InitializeReagentDispenser();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,145 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Administration.Logs;
|
||||
using Content.Server.Chemistry.Components;
|
||||
using Content.Shared.Chemistry.EntitySystems;
|
||||
using Content.Shared.Chemistry;
|
||||
using Content.Shared.Chemistry.Dispenser;
|
||||
using Content.Shared.Containers.ItemSlots;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Emag.Systems;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Chemistry.EntitySystems
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains all the server-side logic for reagent dispensers.
|
||||
/// <seealso cref="ReagentDispenserComponent"/>
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public sealed class ReagentDispenserSystem : SharedReagentDispenserSystem
|
||||
public sealed class ReagentDispenserSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly AudioSystem _audioSystem = default!;
|
||||
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
|
||||
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
|
||||
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, ComponentStartup>((_, comp, _) => comp.UpdateUserInterface());
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, SolutionChangedEvent>((_, comp, _) => comp.UpdateUserInterface());
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, EntInsertedIntoContainerMessage>((_, comp, _) => comp.UpdateUserInterface());
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, EntRemovedFromContainerMessage>((_, comp, _) => comp.UpdateUserInterface());
|
||||
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, ComponentStartup>((_, comp, _) => UpdateUiState(comp));
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, SolutionChangedEvent>((_, comp, _) => UpdateUiState(comp));
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, EntInsertedIntoContainerMessage>((_, comp, _) => UpdateUiState(comp));
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, EntRemovedFromContainerMessage>((_, comp, _) => UpdateUiState(comp));
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, BoundUIOpenedEvent>((_, comp, _) => UpdateUiState(comp));
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, GotEmaggedEvent>(OnEmagged);
|
||||
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, ReagentDispenserSetDispenseAmountMessage>(OnSetDispenseAmountMessage);
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, ReagentDispenserDispenseReagentMessage>(OnDispenseReagentMessage);
|
||||
SubscribeLocalEvent<ReagentDispenserComponent, ReagentDispenserClearContainerSolutionMessage>(OnClearContainerSolutionMessage);
|
||||
}
|
||||
|
||||
private void OnEmagged(EntityUid uid, ReagentDispenserComponent comp, GotEmaggedEvent args)
|
||||
private void UpdateUiState(ReagentDispenserComponent reagentDispenser)
|
||||
{
|
||||
if (!comp.AlreadyEmagged)
|
||||
var outputContainer = _itemSlotsSystem.GetItemOrNull(reagentDispenser.Owner, SharedReagentDispenser.OutputSlotName);
|
||||
var outputContainerInfo = BuildOutputContainerInfo(outputContainer);
|
||||
|
||||
var inventory = GetInventory(reagentDispenser);
|
||||
|
||||
var state = new ReagentDispenserBoundUserInterfaceState(outputContainerInfo, inventory, reagentDispenser.DispenseAmount);
|
||||
_userInterfaceSystem.TrySetUiState(reagentDispenser.Owner, ReagentDispenserUiKey.Key, state);
|
||||
}
|
||||
|
||||
private ContainerInfo? BuildOutputContainerInfo(EntityUid? container)
|
||||
{
|
||||
if (container is not { Valid: true })
|
||||
return null;
|
||||
|
||||
if (_solutionContainerSystem.TryGetFitsInDispenser(container.Value, out var solution))
|
||||
{
|
||||
comp.AddFromPrototype(comp.EmagPackPrototypeId);
|
||||
comp.AlreadyEmagged = true;
|
||||
args.Handled = true;
|
||||
var reagents = solution.Contents.Select(reagent => (reagent.ReagentId, reagent.Quantity)).ToList();
|
||||
return new ContainerInfo(Name(container.Value), true, solution.CurrentVolume, solution.MaxVolume, reagents);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<string> GetInventory(ReagentDispenserComponent reagentDispenser)
|
||||
{
|
||||
var inventory = new List<string>();
|
||||
|
||||
if (reagentDispenser.PackPrototypeId is not null
|
||||
&& _prototypeManager.TryIndex(reagentDispenser.PackPrototypeId, out ReagentDispenserInventoryPrototype? packPrototype))
|
||||
{
|
||||
inventory.AddRange(packPrototype.Inventory);
|
||||
}
|
||||
|
||||
if (reagentDispenser.IsEmagged
|
||||
&& reagentDispenser.EmagPackPrototypeId is not null
|
||||
&& _prototypeManager.TryIndex(reagentDispenser.EmagPackPrototypeId, out ReagentDispenserInventoryPrototype? emagPackPrototype))
|
||||
{
|
||||
inventory.AddRange(emagPackPrototype.Inventory);
|
||||
}
|
||||
|
||||
return inventory;
|
||||
}
|
||||
|
||||
private void OnEmagged(EntityUid uid, ReagentDispenserComponent reagentDispenser, GotEmaggedEvent args)
|
||||
{
|
||||
if (!reagentDispenser.IsEmagged)
|
||||
{
|
||||
reagentDispenser.IsEmagged = true;
|
||||
args.Handled = true;
|
||||
UpdateUiState(reagentDispenser);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSetDispenseAmountMessage(EntityUid uid, ReagentDispenserComponent reagentDispenser, ReagentDispenserSetDispenseAmountMessage message)
|
||||
{
|
||||
reagentDispenser.DispenseAmount = message.ReagentDispenserDispenseAmount;
|
||||
UpdateUiState(reagentDispenser);
|
||||
ClickSound(reagentDispenser);
|
||||
}
|
||||
|
||||
private void OnDispenseReagentMessage(EntityUid uid, ReagentDispenserComponent reagentDispenser, ReagentDispenserDispenseReagentMessage message)
|
||||
{
|
||||
// Ensure that the reagent is something this reagent dispenser can dispense.
|
||||
if (!GetInventory(reagentDispenser).Contains(message.ReagentId))
|
||||
return;
|
||||
|
||||
var outputContainer = _itemSlotsSystem.GetItemOrNull(reagentDispenser.Owner, SharedReagentDispenser.OutputSlotName);
|
||||
if (outputContainer is not {Valid: true} || !_solutionContainerSystem.TryGetFitsInDispenser(outputContainer.Value, out var solution))
|
||||
return;
|
||||
|
||||
if (_solutionContainerSystem.TryAddReagent(outputContainer.Value, solution, message.ReagentId, (int)reagentDispenser.DispenseAmount, out var dispensedAmount)
|
||||
&& message.Session.AttachedEntity is not null)
|
||||
{
|
||||
_adminLogger.Add(LogType.ChemicalReaction, LogImpact.Medium,
|
||||
$"{ToPrettyString(message.Session.AttachedEntity.Value):player} dispensed {dispensedAmount}u of {message.ReagentId} into {ToPrettyString(outputContainer.Value):entity}");
|
||||
}
|
||||
|
||||
UpdateUiState(reagentDispenser);
|
||||
ClickSound(reagentDispenser);
|
||||
}
|
||||
|
||||
private void OnClearContainerSolutionMessage(EntityUid uid, ReagentDispenserComponent reagentDispenser, ReagentDispenserClearContainerSolutionMessage message)
|
||||
{
|
||||
var outputContainer = _itemSlotsSystem.GetItemOrNull(reagentDispenser.Owner, SharedReagentDispenser.OutputSlotName);
|
||||
if (outputContainer is not {Valid: true} || !_solutionContainerSystem.TryGetFitsInDispenser(outputContainer.Value, out var solution))
|
||||
return;
|
||||
|
||||
_solutionContainerSystem.RemoveAllSolution(outputContainer.Value, solution);
|
||||
UpdateUiState(reagentDispenser);
|
||||
ClickSound(reagentDispenser);
|
||||
}
|
||||
|
||||
private void ClickSound(ReagentDispenserComponent reagentDispenser)
|
||||
{
|
||||
_audioSystem.PlayPvs(reagentDispenser.ClickSound, reagentDispenser.Owner, AudioParams.Default.WithVolume(-2f));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user