ECS SolutionTransfer, move to shared (#14156)

Removes the last bit of logic from the comp, moves it to shared, and
fixes a bunch of deprecation warnings in the system.
This commit is contained in:
0x6273
2023-02-19 02:01:05 +01:00
committed by GitHub
parent 354d72270a
commit ffaa7a96fe
4 changed files with 85 additions and 126 deletions

View File

@@ -1,99 +0,0 @@
using System.Threading.Tasks;
using Content.Server.Chemistry.Components.SolutionManager;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.UserInterface;
using Content.Shared.Chemistry;
using Content.Shared.FixedPoint;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Robust.Server.GameObjects;
namespace Content.Server.Chemistry.Components
{
/// <summary>
/// Gives click behavior for transferring to/from other reagent containers.
/// </summary>
[RegisterComponent]
public sealed class SolutionTransferComponent : Component
{
/// <summary>
/// The amount of solution to be transferred from this solution when clicking on other solutions with it.
/// </summary>
[DataField("transferAmount")]
[ViewVariables(VVAccess.ReadWrite)]
public FixedPoint2 TransferAmount { get; set; } = FixedPoint2.New(5);
/// <summary>
/// The minimum amount of solution that can be transferred at once from this solution.
/// </summary>
[DataField("minTransferAmount")]
[ViewVariables(VVAccess.ReadWrite)]
public FixedPoint2 MinimumTransferAmount { get; set; } = FixedPoint2.New(5);
/// <summary>
/// The maximum amount of solution that can be transferred at once from this solution.
/// </summary>
[DataField("maxTransferAmount")]
[ViewVariables(VVAccess.ReadWrite)]
public FixedPoint2 MaximumTransferAmount { get; set; } = FixedPoint2.New(50);
/// <summary>
/// Can this entity take reagent from reagent tanks?
/// </summary>
[DataField("canReceive")]
[ViewVariables(VVAccess.ReadWrite)]
public bool CanReceive { get; set; } = true;
/// <summary>
/// Can this entity give reagent to other reagent containers?
/// </summary>
[DataField("canSend")]
[ViewVariables(VVAccess.ReadWrite)]
public bool CanSend { get; set; } = true;
/// <summary>
/// Whether you're allowed to change the transfer amount.
/// </summary>
[DataField("canChangeTransferAmount")]
[ViewVariables(VVAccess.ReadWrite)]
public bool CanChangeTransferAmount { get; set; } = false;
[ViewVariables] public BoundUserInterface? UserInterface => Owner.GetUIOrNull(TransferAmountUiKey.Key);
protected override void Initialize()
{
base.Initialize();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
}
}
public void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage serverMsg)
{
if (serverMsg.Session.AttachedEntity == null)
return;
switch (serverMsg.Message)
{
case TransferAmountSetValueMessage svm:
var sval = svm.Value.Float();
var amount = Math.Clamp(sval, MinimumTransferAmount.Float(),
MaximumTransferAmount.Float());
serverMsg.Session.AttachedEntity.Value.PopupMessage(Loc.GetString("comp-solution-transfer-set-amount",
("amount", amount)));
SetTransferAmount(FixedPoint2.New(amount));
break;
}
}
public void SetTransferAmount(FixedPoint2 amount)
{
amount = FixedPoint2.New(Math.Clamp(amount.Int(), MinimumTransferAmount.Int(),
MaximumTransferAmount.Int()));
TransferAmount = amount;
}
}
}

View File

@@ -1,9 +1,9 @@
using Content.Server.Administration.Logs;
using Content.Shared.Verbs;
using Content.Server.Chemistry.Components;
using Content.Server.Chemistry.Components.SolutionManager;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Components;
using Content.Shared.Database;
using Content.Shared.FixedPoint;
@@ -15,7 +15,9 @@ namespace Content.Server.Chemistry.EntitySystems
[UsedImplicitly]
public sealed class SolutionTransferSystem : EntitySystem
{
[Dependency] private readonly SolutionContainerSystem _solutionContainer = default!;
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
/// <summary>
@@ -29,6 +31,16 @@ namespace Content.Server.Chemistry.EntitySystems
SubscribeLocalEvent<SolutionTransferComponent, GetVerbsEvent<AlternativeVerb>>(AddSetTransferVerbs);
SubscribeLocalEvent<SolutionTransferComponent, AfterInteractEvent>(OnAfterInteract);
SubscribeLocalEvent<SolutionTransferComponent, TransferAmountSetValueMessage>(OnTransferAmountSetValueMessage);
}
private void OnTransferAmountSetValueMessage(EntityUid uid, SolutionTransferComponent solutionTransfer, TransferAmountSetValueMessage message)
{
var newTransferAmount = FixedPoint2.Clamp(message.Value, solutionTransfer.MinimumTransferAmount, solutionTransfer.MaximumTransferAmount);
solutionTransfer.TransferAmount = newTransferAmount;
if (message.Session.AttachedEntity is {Valid: true} user)
_popupSystem.PopupEntity(Loc.GetString("comp-solution-transfer-set-amount", ("amount", newTransferAmount)), uid, user);
}
private void AddSetTransferVerbs(EntityUid uid, SolutionTransferComponent component, GetVerbsEvent<AlternativeVerb> args)
@@ -43,7 +55,7 @@ namespace Content.Server.Chemistry.EntitySystems
AlternativeVerb custom = new();
custom.Text = Loc.GetString("comp-solution-transfer-verb-custom-amount");
custom.Category = VerbCategory.SetTransferAmount;
custom.Act = () => component.UserInterface?.Open(actor.PlayerSession);
custom.Act = () => _userInterfaceSystem.TryOpen(args.Target, TransferAmountUiKey.Key, actor.PlayerSession);
custom.Priority = 1;
args.Verbs.Add(custom);
@@ -60,7 +72,7 @@ namespace Content.Server.Chemistry.EntitySystems
verb.Act = () =>
{
component.TransferAmount = FixedPoint2.New(amount);
args.User.PopupMessage(Loc.GetString("comp-solution-transfer-set-amount", ("amount", amount)));
_popupSystem.PopupEntity(Loc.GetString("comp-solution-transfer-set-amount", ("amount", amount)), uid, args.User);
};
// we want to sort by size, not alphabetically by the verb text.
@@ -80,9 +92,9 @@ namespace Content.Server.Chemistry.EntitySystems
//Special case for reagent tanks, because normally clicking another container will give solution, not take it.
if (component.CanReceive && !EntityManager.HasComponent<RefillableSolutionComponent>(target) // target must not be refillable (e.g. Reagent Tanks)
&& _solutionContainer.TryGetDrainableSolution(target, out var targetDrain) // target must be drainable
&& _solutionContainerSystem.TryGetDrainableSolution(target, out var targetDrain) // target must be drainable
&& EntityManager.TryGetComponent(uid, out RefillableSolutionComponent? refillComp)
&& _solutionContainer.TryGetRefillableSolution(uid, out var ownerRefill, refillable: refillComp))
&& _solutionContainerSystem.TryGetRefillableSolution(uid, out var ownerRefill, refillable: refillComp))
{
@@ -101,8 +113,7 @@ namespace Content.Server.Chemistry.EntitySystems
? "comp-solution-transfer-fill-fully"
: "comp-solution-transfer-fill-normal";
target.PopupMessage(args.User,
Loc.GetString(msg, ("owner", args.Target), ("amount", transferred), ("target", uid)));
_popupSystem.PopupEntity(Loc.GetString(msg, ("owner", args.Target), ("amount", transferred), ("target", uid)), uid, args.User);
args.Handled = true;
return;
@@ -110,8 +121,8 @@ namespace Content.Server.Chemistry.EntitySystems
}
// if target is refillable, and owner is drainable
if (component.CanSend && _solutionContainer.TryGetRefillableSolution(target, out var targetRefill)
&& _solutionContainer.TryGetDrainableSolution(uid, out var ownerDrain))
if (component.CanSend && _solutionContainerSystem.TryGetRefillableSolution(target, out var targetRefill)
&& _solutionContainerSystem.TryGetDrainableSolution(uid, out var ownerDrain))
{
var transferAmount = component.TransferAmount;
@@ -124,10 +135,8 @@ namespace Content.Server.Chemistry.EntitySystems
if (transferred > 0)
{
uid.PopupMessage(args.User,
Loc.GetString("comp-solution-transfer-transfer-solution",
("amount", transferred),
("target", target)));
var message = Loc.GetString("comp-solution-transfer-transfer-solution", ("amount", transferred), ("target", target));
_popupSystem.PopupEntity(message, uid, args.User);
args.Handled = true;
}
@@ -148,40 +157,37 @@ namespace Content.Server.Chemistry.EntitySystems
var transferAttempt = new SolutionTransferAttemptEvent(sourceEntity, targetEntity);
// Check if the source is cancelling the transfer
RaiseLocalEvent(sourceEntity, transferAttempt, true);
RaiseLocalEvent(sourceEntity, transferAttempt, broadcast: true);
if (transferAttempt.Cancelled)
{
sourceEntity.PopupMessage(user, transferAttempt.CancelReason!);
_popupSystem.PopupEntity(transferAttempt.CancelReason!, sourceEntity, user);
return FixedPoint2.Zero;
}
if (source.Volume == 0)
{
sourceEntity.PopupMessage(user,
Loc.GetString("comp-solution-transfer-is-empty", ("target", sourceEntity)));
_popupSystem.PopupEntity(Loc.GetString("comp-solution-transfer-is-empty", ("target", sourceEntity)), sourceEntity, user);
return FixedPoint2.Zero;
}
// Check if the target is cancelling the transfer
RaiseLocalEvent(targetEntity, transferAttempt, true);
RaiseLocalEvent(targetEntity, transferAttempt, broadcast: true);
if (transferAttempt.Cancelled)
{
sourceEntity.PopupMessage(user, transferAttempt.CancelReason!);
_popupSystem.PopupEntity(transferAttempt.CancelReason!, sourceEntity, user);
return FixedPoint2.Zero;
}
if (target.AvailableVolume == 0)
{
targetEntity.PopupMessage(user,
Loc.GetString("comp-solution-transfer-is-full", ("target", targetEntity)));
_popupSystem.PopupEntity(Loc.GetString("comp-solution-transfer-is-full", ("target", targetEntity)), targetEntity, user);
return FixedPoint2.Zero;
}
var actualAmount = FixedPoint2.Min(amount, FixedPoint2.Min(source.Volume, target.AvailableVolume));
var solutionSystem = Get<SolutionContainerSystem>();
var solution = solutionSystem.Drain(sourceEntity, source, actualAmount);
solutionSystem.Refill(targetEntity, target, solution);
var solution = _solutionContainerSystem.Drain(sourceEntity, source, actualAmount);
_solutionContainerSystem.Refill(targetEntity, target, solution);
_adminLogger.Add(LogType.Action, LogImpact.Medium,
$"{EntityManager.ToPrettyString(user):player} transferred {string.Join(", ", solution.Contents)} to {EntityManager.ToPrettyString(targetEntity):entity}, which now contains {string.Join(", ", target.Contents)}");

View File

@@ -1,14 +1,13 @@
using Content.Server.Chemistry.Components;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.Fluids.EntitySystems;
using Content.Server.Popups;
using Content.Shared.Audio;
using Content.Shared.Chemistry.Components;
using Content.Shared.Extinguisher;
using Content.Shared.FixedPoint;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using Content.Shared.Verbs;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Player;

View File

@@ -0,0 +1,53 @@
using Content.Shared.FixedPoint;
using Robust.Shared.GameStates;
namespace Content.Shared.Chemistry.Components;
/// <summary>
/// Gives click behavior for transferring to/from other reagent containers.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class SolutionTransferComponent : Component
{
/// <summary>
/// The amount of solution to be transferred from this solution when clicking on other solutions with it.
/// </summary>
[DataField("transferAmount")]
[ViewVariables(VVAccess.ReadWrite)]
public FixedPoint2 TransferAmount { get; set; } = FixedPoint2.New(5);
/// <summary>
/// The minimum amount of solution that can be transferred at once from this solution.
/// </summary>
[DataField("minTransferAmount")]
[ViewVariables(VVAccess.ReadWrite)]
public FixedPoint2 MinimumTransferAmount { get; set; } = FixedPoint2.New(5);
/// <summary>
/// The maximum amount of solution that can be transferred at once from this solution.
/// </summary>
[DataField("maxTransferAmount")]
[ViewVariables(VVAccess.ReadWrite)]
public FixedPoint2 MaximumTransferAmount { get; set; } = FixedPoint2.New(50);
/// <summary>
/// Can this entity take reagent from reagent tanks?
/// </summary>
[DataField("canReceive")]
[ViewVariables(VVAccess.ReadWrite)]
public bool CanReceive { get; set; } = true;
/// <summary>
/// Can this entity give reagent to other reagent containers?
/// </summary>
[DataField("canSend")]
[ViewVariables(VVAccess.ReadWrite)]
public bool CanSend { get; set; } = true;
/// <summary>
/// Whether you're allowed to change the transfer amount.
/// </summary>
[DataField("canChangeTransferAmount")]
[ViewVariables(VVAccess.ReadWrite)]
public bool CanChangeTransferAmount { get; set; } = false;
}