Skip to content

Commit

Permalink
Fix gas tank and other hand-interaction bugs (#9700)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectroJr authored Jul 31, 2022
1 parent e227f20 commit bae540a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private void UnitInsert(DisposalUnitComponent unit, bool result, params EntityUi
foreach (var entity in entities)
{
Assert.That(system.CanInsert(unit, entity), Is.EqualTo(result));
system.TryInsert(unit.Owner, entity, entity);
system.TryInsert(unit.Owner, entity, null);
}
}

Expand Down
12 changes: 11 additions & 1 deletion Content.Server/Atmos/Components/GasTankComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ public sealed class GasTankComponent : Component, IGasMixtureHolder
/// <summary>
/// Tank is connected to internals.
/// </summary>
[ViewVariables] public bool IsConnected { get; set; }
[ViewVariables] public bool IsConnected => User != null;

[ViewVariables]
public EntityUid? User;

/// <summary>
/// True if this entity was recently moved out of a container. This might have been a hand -> inventory
/// transfer, or it might have been the user dropping the tank. This indicates the tank needs to be checked.
/// </summary>
[ViewVariables]
public bool CheckUser;

/// <summary>
/// Pressure at which tanks start leaking.
Expand Down
27 changes: 21 additions & 6 deletions Content.Server/Atmos/EntitySystems/GasTankSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public override void Initialize()
SubscribeLocalEvent<GasTankComponent, GetItemActionsEvent>(OnGetActions);
SubscribeLocalEvent<GasTankComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<GasTankComponent, ToggleActionEvent>(OnActionToggle);
SubscribeLocalEvent<GasTankComponent, DroppedEvent>(OnDropped);

SubscribeLocalEvent<GasTankComponent, EntParentChangedMessage>(OnParentChange);
SubscribeLocalEvent<GasTankComponent, GasTankSetPressureMessage>(OnGasTankSetPressure);
SubscribeLocalEvent<GasTankComponent, GasTankToggleInternalsMessage>(OnGasTankToggleInternals);
}
Expand Down Expand Up @@ -84,9 +83,12 @@ private void BeforeUiOpen(EntityUid uid, GasTankComponent component, BeforeActiv
UpdateUserInterface(component, true);
}

private void OnDropped(EntityUid uid, GasTankComponent component, DroppedEvent args)
private void OnParentChange(EntityUid uid, GasTankComponent component, ref EntParentChangedMessage args)
{
DisconnectFromInternals(component, args.User);
// When an item is moved from hands -> pockets, the container removal briefly dumps the item on the floor.
// So this is a shitty fix, where the parent check is just delayed. But this really needs to get fixed
// properly at some point.
component.CheckUser = true;
}

private void OnGetActions(EntityUid uid, GasTankComponent component, GetItemActionsEvent args)
Expand Down Expand Up @@ -122,6 +124,16 @@ public override void Update(float frameTime)

foreach (var gasTank in EntityManager.EntityQuery<GasTankComponent>())
{
if (gasTank.CheckUser)
{
gasTank.CheckUser = false;
if (Transform(gasTank.Owner).ParentUid != gasTank.User)
{
DisconnectFromInternals(gasTank);
continue;
}
}

_atmosphereSystem.React(gasTank.Air, gasTank);
CheckStatus(gasTank);
if (_ui.IsUiOpen(gasTank.Owner, SharedGasTankUiKey.Key))
Expand Down Expand Up @@ -184,7 +196,10 @@ public void ConnectToInternals(GasTankComponent component)
if (!CanConnectToInternals(component)) return;
var internals = GetInternalsComponent(component);
if (internals == null) return;
component.IsConnected = _internals.TryConnectTank(internals, component.Owner);

if (_internals.TryConnectTank(internals, component.Owner))
component.User = internals.Owner;

_actions.SetToggled(component.ToggleAction, component.IsConnected);

// Couldn't toggle!
Expand All @@ -201,7 +216,7 @@ public void ConnectToInternals(GasTankComponent component)
public void DisconnectFromInternals(GasTankComponent component, EntityUid? owner = null)
{
if (!component.IsConnected) return;
component.IsConnected = false;
component.User = null;
_actions.SetToggled(component.ToggleAction, false);

_internals.DisconnectTank(GetInternalsComponent(component, owner));
Expand Down
14 changes: 7 additions & 7 deletions Content.Server/Disposal/Unit/EntitySystems/DisposalUnitSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ private void DoInsertDisposalUnit(DoInsertDisposalUnitEvent ev)
return;
}

if (!unit.Container.Insert(toInsert))
{
if (ev.User != null && !_handsSystem.TryDropIntoContainer(ev.User.Value, ev.ToInsert, unit.Container))
return;
}

else
unit.Container.Insert(toInsert);

AfterInsert(unit, toInsert);
}

Expand Down Expand Up @@ -466,7 +466,7 @@ private bool Update(DisposalUnitComponent component, float frameTime)
return state == SharedDisposalUnitComponent.PressureState.Ready && component.RecentlyEjected.Count == 0;
}

public bool TryInsert(EntityUid unitId, EntityUid toInsertId, EntityUid userId, DisposalUnitComponent? unit = null)
public bool TryInsert(EntityUid unitId, EntityUid toInsertId, EntityUid? userId, DisposalUnitComponent? unit = null)
{
if (!Resolve(unitId, ref unit))
return false;
Expand All @@ -477,15 +477,15 @@ public bool TryInsert(EntityUid unitId, EntityUid toInsertId, EntityUid userId,
var delay = userId == toInsertId ? unit.EntryDelay : unit.DraggedEntryDelay;
var ev = new DoInsertDisposalUnitEvent(userId, toInsertId, unitId);

if (delay <= 0)
if (delay <= 0 || userId == null)
{
DoInsertDisposalUnit(ev);
return true;
}

// Can't check if our target AND disposals moves currently so we'll just check target.
// if you really want to check if disposals moves then add a predicate.
var doAfterArgs = new DoAfterEventArgs(userId, delay, default, toInsertId)
var doAfterArgs = new DoAfterEventArgs(userId.Value, delay, default, toInsertId)
{
BreakOnDamage = true,
BreakOnStun = true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Content.Server.Disposal.Unit.EntitySystems
namespace Content.Server.Disposal.Unit.EntitySystems
{
public record DoInsertDisposalUnitEvent(EntityUid User, EntityUid ToInsert, EntityUid Unit);
public record DoInsertDisposalUnitEvent(EntityUid? User, EntityUid ToInsert, EntityUid Unit);
}
6 changes: 4 additions & 2 deletions Content.Server/Kitchen/EntitySystems/MicrowaveSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
using Content.Shared.Body.Components;
using Content.Shared.Body.Part;
using Content.Shared.Popups;
using Content.Server.Hands.Systems;

namespace Content.Server.Kitchen.EntitySystems
{
[UsedImplicitly]
internal sealed class MicrowaveSystem : EntitySystem
{
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly HandsSystem _handsSystem = default!;

public override void Initialize()
{
base.Initialize();
Expand Down Expand Up @@ -114,8 +117,7 @@ private void OnInteractUsing(EntityUid uid, MicrowaveComponent component, Intera
}

args.Handled = true;

component.Storage.Insert(args.Used);
_handsSystem.TryDropIntoContainer(args.User, args.Used, component.Storage);
component.DirtyUi();
}

Expand Down

0 comments on commit bae540a

Please sign in to comment.