Drink fixes (#605)

* Fix max_volume in cup YAML

Fixes #563

Added parsing of max_volume from YAML. Before it would get max volume only from drink content total.

* Fix drink owner getting deleted early

_contents.SplitSolution causes _contents.SolutionChanged to call Finish early causing a crash when try to get SoundComponent from the deleted Entity.
This commit is contained in:
ShadowCommander
2020-02-07 09:13:11 -08:00
committed by GitHub
parent 34bbca5cc2
commit cf78f8aa65
2 changed files with 26 additions and 15 deletions

View File

@@ -43,9 +43,12 @@ namespace Content.Server.GameObjects.Components.Nutrition
}
private Solution _initialContents; // This is just for loading from yaml
private int _maxVolume;
private bool _despawnOnFinish;
private bool _drinking;
public int UsesLeft()
{
// In case transfer amount exceeds volume left
@@ -61,6 +64,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
{
base.ExposeData(serializer);
serializer.DataField(ref _initialContents, "contents", null);
serializer.DataField(ref _maxVolume, "max_volume", 0);
serializer.DataField(ref _useSound, "use_sound", "/Audio/items/drink.ogg");
// E.g. cola can when done or clear bottle, whatever
// Currently this will enforce it has the same volume but this may change.
@@ -90,7 +94,11 @@ namespace Content.Server.GameObjects.Components.Nutrition
}
}
_contents.MaxVolume = _initialContents.TotalVolume;
_drinking = false;
if (_maxVolume != 0)
_contents.MaxVolume = _maxVolume;
else
_contents.MaxVolume = _initialContents.TotalVolume;
_contents.SolutionChanged += HandleSolutionChangedEvent;
}
@@ -140,6 +148,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
if (user.TryGetComponent(out StomachComponent stomachComponent))
{
_drinking = true;
var transferAmount = Math.Min(_transferAmount, _contents.CurrentVolume);
var split = _contents.SplitSolution(transferAmount);
if (stomachComponent.TryTransferSolution(split))
@@ -156,6 +165,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
_contents.TryAddSolution(split);
user.PopupMessage(user, _localizationManager.GetString("Can't drink"));
}
_drinking = false;
}
Finish(user);
@@ -170,7 +180,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
public void Finish(IEntity user)
{
// Drink containers are mostly transient.
if (!_despawnOnFinish || UsesLeft() > 0)
if (_drinking || !_despawnOnFinish || UsesLeft() > 0)
return;
var gridPos = Owner.Transform.GridPosition;