Adds trash tag to more items. New component "TrashOnEmpty" (#10166)

This commit is contained in:
Jessica M
2022-08-04 01:52:08 -07:00
committed by GitHub
parent 2f4849eae1
commit 39a6c8afb3
9 changed files with 97 additions and 6 deletions

View File

@@ -0,0 +1,53 @@
using Content.Server.Chemistry.Components.SolutionManager;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.Nutrition.Components;
using Content.Shared.Chemistry.Components;
using Content.Shared.Tag;
namespace Content.Server.Nutrition.EntitySystems
{
public sealed class TrashOnEmptySystem : EntitySystem
{
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
[Dependency] private readonly TagSystem _tagSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<TrashOnEmptyComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<TrashOnEmptyComponent, SolutionChangedEvent>(OnSolutionChange);
}
public void OnStartup(EntityUid uid, TrashOnEmptyComponent component, ComponentStartup args)
{
CheckSolutions(component);
}
public void OnSolutionChange(EntityUid uid, TrashOnEmptyComponent component, SolutionChangedEvent args)
{
CheckSolutions(component);
}
public void CheckSolutions(TrashOnEmptyComponent component)
{
EntityManager.EnsureComponent<TagComponent>(component.Owner);
if (!EntityManager.HasComponent<SolutionContainerManagerComponent>((component).Owner))
return;
if (_solutionContainerSystem.TryGetSolution(component.Owner, component.Solution, out var solution))
UpdateTags(component, solution);
}
public void UpdateTags(TrashOnEmptyComponent component, Solution solution)
{
if (solution.DrainAvailable <= 0)
{
_tagSystem.AddTag(component.Owner, "Trash");
return;
}
if (_tagSystem.HasTag(component.Owner, "Trash"))
_tagSystem.RemoveTag(component.Owner, "Trash");
}
}
}