Prevent closed drinks from transferring reagents (#7329)

* Prevent closed drinks from transferring reagents

* Inherit from cancellable event
This commit is contained in:
Alain Siegrist
2022-04-15 23:17:48 +02:00
committed by GitHub
parent 2caccefd8b
commit 6c85e1b255
3 changed files with 100 additions and 46 deletions

View File

@@ -5,6 +5,7 @@ using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Server.GameObjects;
using System.Collections.Generic;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Content.Shared.Popups;
@@ -66,5 +67,86 @@ namespace Content.Server.Chemistry.EntitySystems
args.Verbs.Add(verb);
}
}
/// <summary>
/// Transfer from a solution to another.
/// </summary>
/// <returns>The actual amount transferred.</returns>
public FixedPoint2 Transfer(EntityUid user,
EntityUid sourceEntity,
Solution source,
EntityUid targetEntity,
Solution target,
FixedPoint2 amount)
{
var transferAttempt = new SolutionTransferAttemptEvent(sourceEntity, targetEntity);
// Check if the source is cancelling the transfer
RaiseLocalEvent(sourceEntity, transferAttempt);
if (transferAttempt.Cancelled)
{
sourceEntity.PopupMessage(user, transferAttempt.CancelReason!);
return FixedPoint2.Zero;
}
if (source.DrainAvailable == 0)
{
sourceEntity.PopupMessage(user,
Loc.GetString("comp-solution-transfer-is-empty", ("target", sourceEntity)));
return FixedPoint2.Zero;
}
// Check if the target is cancelling the transfer
RaiseLocalEvent(targetEntity, transferAttempt);
if (transferAttempt.Cancelled)
{
sourceEntity.PopupMessage(user, transferAttempt.CancelReason!);
return FixedPoint2.Zero;
}
if (target.AvailableVolume == 0)
{
targetEntity.PopupMessage(user,
Loc.GetString("comp-solution-transfer-is-full", ("target", targetEntity)));
return FixedPoint2.Zero;
}
var actualAmount = FixedPoint2.Min(amount, FixedPoint2.Min(source.DrainAvailable, target.AvailableVolume));
var solutionSystem = Get<SolutionContainerSystem>();
var solution = solutionSystem.Drain(sourceEntity, source, actualAmount);
solutionSystem.Refill(targetEntity, target, solution);
return actualAmount;
}
}
/// <summary>
/// Raised when attempting to transfer from one solution to another.
/// </summary>
public sealed class SolutionTransferAttemptEvent : CancellableEntityEventArgs
{
public SolutionTransferAttemptEvent(EntityUid from, EntityUid to)
{
From = from;
To = to;
}
public EntityUid From { get; }
public EntityUid To { get; }
/// <summary>
/// Why the transfer has been cancelled.
/// </summary>
public string? CancelReason { get; private set; }
/// <summary>
/// Cancels the transfer.
/// </summary>
public void Cancel(string reason)
{
base.Cancel();
CancelReason = reason;
}
}
}