Fix being able to spill closed drink cans (#3100)

This commit is contained in:
DrSmugleaf
2021-02-06 14:31:44 +01:00
committed by GitHub
parent 8ec4f1fb3d
commit 9db47249cd

View File

@@ -1,15 +1,17 @@
using System.Linq; #nullable enable
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Content.Server.GameObjects.Components.Body.Behavior; using Content.Server.GameObjects.Components.Body.Behavior;
using Content.Server.GameObjects.Components.Chemistry;
using Content.Server.GameObjects.Components.Fluids; using Content.Server.GameObjects.Components.Fluids;
using Content.Shared.Audio; using Content.Shared.Audio;
using Content.Shared.Chemistry; using Content.Shared.Chemistry;
using Content.Shared.GameObjects.Components.Body; using Content.Shared.GameObjects.Components.Body;
using Content.Shared.GameObjects.Components.Chemistry;
using Content.Shared.GameObjects.Components.Nutrition; using Content.Shared.GameObjects.Components.Nutrition;
using Content.Shared.GameObjects.EntitySystems; using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Interfaces; using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components; using Content.Shared.Interfaces.GameObjects.Components;
using JetBrains.Annotations;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems; using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.Audio; using Robust.Shared.Audio;
@@ -36,22 +38,39 @@ namespace Content.Server.GameObjects.Components.Nutrition
public override string Name => "Drink"; public override string Name => "Drink";
[ViewVariables] [ViewVariables]
private SolutionContainerComponent _contents; private bool _opened;
[ViewVariables] [ViewVariables]
private string _useSound; private string _useSound = string.Empty;
[ViewVariables] [ViewVariables]
private bool _defaultToOpened; private bool _defaultToOpened;
[ViewVariables(VVAccess.ReadWrite)]
public ReagentUnit TransferAmount { get; private set; } = ReagentUnit.New(2);
[ViewVariables(VVAccess.ReadWrite)]
public bool Opened { get; protected set; }
[ViewVariables]
public bool Empty => _contents.CurrentVolume.Float() <= 0;
private AppearanceComponent _appearanceComponent; [ViewVariables(VVAccess.ReadWrite)]
private string _soundCollection; public ReagentUnit TransferAmount { get; [UsedImplicitly] private set; } = ReagentUnit.New(2);
[ViewVariables(VVAccess.ReadWrite)]
public bool Opened
{
get => _opened;
set
{
if (_opened == value)
{
return;
}
_opened = value;
OpenedChanged();
}
}
[ViewVariables]
public bool Empty => Owner.GetComponentOrNull<ISolutionInteractionsComponent>()?.DrainAvailable <= 0;
private string _soundCollection = string.Empty;
private bool _pressurized; private bool _pressurized;
private string _burstSound; private string _burstSound = string.Empty;
public override void ExposeData(ObjectSerializer serializer) public override void ExposeData(ObjectSerializer serializer)
{ {
@@ -66,18 +85,28 @@ namespace Content.Server.GameObjects.Components.Nutrition
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
Owner.TryGetComponent(out _appearanceComponent);
if (!Owner.TryGetComponent(out _contents))
{
_contents = Owner.AddComponent<SolutionContainerComponent>();
}
_contents.Capabilities = SolutionContainerCaps.Refillable | SolutionContainerCaps.Drainable;
Opened = _defaultToOpened; Opened = _defaultToOpened;
UpdateAppearance(); UpdateAppearance();
} }
private void OpenedChanged()
{
if (!Owner.TryGetComponent(out SharedSolutionContainerComponent? contents))
{
return;
}
if (Opened)
{
contents.Capabilities |= SolutionContainerCaps.Refillable | SolutionContainerCaps.Drainable;
}
else
{
contents.Capabilities &= ~(SolutionContainerCaps.Refillable | SolutionContainerCaps.Drainable);
}
}
void ISolutionChange.SolutionChanged(SolutionChangeEventArgs eventArgs) void ISolutionChange.SolutionChanged(SolutionChangeEventArgs eventArgs)
{ {
UpdateAppearance(); UpdateAppearance();
@@ -85,7 +114,13 @@ namespace Content.Server.GameObjects.Components.Nutrition
private void UpdateAppearance() private void UpdateAppearance()
{ {
_appearanceComponent?.SetData(SharedFoodComponent.FoodVisuals.Visual, _contents.CurrentVolume.Float()); if (!Owner.TryGetComponent(out AppearanceComponent? appearance) ||
!Owner.TryGetComponent(out ISolutionInteractionsComponent? contents))
{
return;
}
appearance.SetData(SharedFoodComponent.FoodVisuals.Visual, contents.DrainAvailable.Float());
} }
bool IUse.UseEntity(UseEntityEventArgs args) bool IUse.UseEntity(UseEntityEventArgs args)
@@ -101,7 +136,8 @@ namespace Content.Server.GameObjects.Components.Nutrition
return false; return false;
} }
if (_contents.CurrentVolume.Float() <= 0) if (!Owner.TryGetComponent(out ISolutionInteractionsComponent? contents) ||
contents.DrainAvailable <= 0)
{ {
args.User.PopupMessage(Loc.GetString("{0:theName} is empty!", Owner)); args.User.PopupMessage(Loc.GetString("{0:theName} is empty!", Owner));
return true; return true;
@@ -113,7 +149,13 @@ namespace Content.Server.GameObjects.Components.Nutrition
//Force feeding a drink to someone. //Force feeding a drink to someone.
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs) async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
{ {
TryUseDrink(eventArgs.Target, forced: true); if (eventArgs.Target == null)
{
return false;
}
TryUseDrink(eventArgs.Target, true);
return true; return true;
} }
@@ -130,18 +172,15 @@ namespace Content.Server.GameObjects.Components.Nutrition
private bool TryUseDrink(IEntity target, bool forced = false) private bool TryUseDrink(IEntity target, bool forced = false)
{ {
if (target == null || !_contents.CanDrain)
{
return false;
}
if (!Opened) if (!Opened)
{ {
target.PopupMessage(Loc.GetString("Open {0:theName} first!", Owner)); target.PopupMessage(Loc.GetString("Open {0:theName} first!", Owner));
return false; return false;
} }
if (_contents.CurrentVolume.Float() <= 0) if (!Owner.TryGetComponent(out ISolutionInteractionsComponent? interactions) ||
!interactions.CanDrain ||
interactions.DrainAvailable <= 0)
{ {
if (!forced) if (!forced)
{ {
@@ -151,26 +190,33 @@ namespace Content.Server.GameObjects.Components.Nutrition
return false; return false;
} }
if (!target.TryGetComponent(out IBody body) || if (!target.TryGetComponent(out IBody? body) ||
!body.TryGetMechanismBehaviors<StomachBehavior>(out var stomachs)) !body.TryGetMechanismBehaviors<StomachBehavior>(out var stomachs))
{ {
target.PopupMessage(Loc.GetString("You can't drink {0:theName}!", Owner));
return false; return false;
} }
var transferAmount = ReagentUnit.Min(TransferAmount, _contents.CurrentVolume); var transferAmount = ReagentUnit.Min(TransferAmount, interactions.DrainAvailable);
var split = _contents.SplitSolution(transferAmount); var drain = interactions.Drain(transferAmount);
var firstStomach = stomachs.FirstOrDefault(stomach => stomach.CanTransferSolution(split)); var firstStomach = stomachs.FirstOrDefault(stomach => stomach.CanTransferSolution(drain));
// All stomach are full or can't handle whatever solution we have. // All stomach are full or can't handle whatever solution we have.
if (firstStomach == null) if (firstStomach == null)
{ {
_contents.TryAddSolution(split);
target.PopupMessage(Loc.GetString("You've had enough {0:theName}!", Owner)); target.PopupMessage(Loc.GetString("You've had enough {0:theName}!", Owner));
if (!interactions.CanRefill)
{
drain.SpillAt(target, "PuddleSmear");
return false;
}
interactions.Refill(drain);
return false; return false;
} }
if (_useSound != null) if (!string.IsNullOrEmpty(_useSound))
{ {
EntitySystem.Get<AudioSystem>().PlayFromEntity(_useSound, target, AudioParams.Default.WithVolume(-2f)); EntitySystem.Get<AudioSystem>().PlayFromEntity(_useSound, target, AudioParams.Default.WithVolume(-2f));
} }
@@ -180,9 +226,9 @@ namespace Content.Server.GameObjects.Components.Nutrition
// TODO: Account for partial transfer. // TODO: Account for partial transfer.
split.DoEntityReaction(target, ReactionMethod.Ingestion); drain.DoEntityReaction(target, ReactionMethod.Ingestion);
firstStomach.TryTransferSolution(split); firstStomach.TryTransferSolution(drain);
return true; return true;
} }
@@ -192,11 +238,16 @@ namespace Content.Server.GameObjects.Components.Nutrition
if (_pressurized && if (_pressurized &&
!Opened && !Opened &&
_random.Prob(0.25f) && _random.Prob(0.25f) &&
Owner.TryGetComponent(out SolutionContainerComponent component)) Owner.TryGetComponent(out ISolutionInteractionsComponent? interactions))
{ {
Opened = true; Opened = true;
var solution = component.SplitSolution(component.CurrentVolume); if (!interactions.CanDrain)
{
return;
}
var solution = interactions.Drain(interactions.DrainAvailable);
solution.SpillAt(Owner, "PuddleSmear"); solution.SpillAt(Owner, "PuddleSmear");
EntitySystem.Get<AudioSystem>().PlayFromEntity(_burstSound, Owner, EntitySystem.Get<AudioSystem>().PlayFromEntity(_burstSound, Owner,