Change gas canisters to use ItemSlots (#22561)

This commit is contained in:
Kara
2023-12-16 01:28:27 -07:00
committed by GitHub
parent d9f399417a
commit c97abe92d7
3 changed files with 43 additions and 57 deletions

View File

@@ -1,4 +1,5 @@
using Content.Shared.Atmos; using Content.Shared.Atmos;
using Content.Shared.Containers.ItemSlots;
using Robust.Shared.Audio; using Robust.Shared.Audio;
namespace Content.Server.Atmos.Piping.Unary.Components namespace Content.Server.Atmos.Piping.Unary.Components
@@ -15,7 +16,11 @@ namespace Content.Server.Atmos.Piping.Unary.Components
/// </summary> /// </summary>
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
[DataField("container")] [DataField("container")]
public string ContainerName { get; set; } = "GasCanisterTankHolder"; public string ContainerName { get; set; } = "tank_slot";
[ViewVariables(VVAccess.ReadWrite)]
[DataField]
public ItemSlot GasTankSlot = new();
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
[DataField("gasMixture")] [DataField("gasMixture")]

View File

@@ -11,6 +11,7 @@ using Content.Server.NodeContainer.Nodes;
using Content.Server.Popups; using Content.Server.Popups;
using Content.Shared.Atmos; using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping.Binary.Components; using Content.Shared.Atmos.Piping.Binary.Components;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Database; using Content.Shared.Database;
using Content.Shared.Hands.EntitySystems; using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction; using Content.Shared.Interaction;
@@ -34,6 +35,7 @@ public sealed class GasCanisterSystem : EntitySystem
[Dependency] private readonly PopupSystem _popup = default!; [Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly UserInterfaceSystem _ui = default!; [Dependency] private readonly UserInterfaceSystem _ui = default!;
[Dependency] private readonly NodeContainerSystem _nodeContainer = default!; [Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
[Dependency] private readonly ItemSlotsSystem _slots = default!;
public override void Initialize() public override void Initialize()
{ {
@@ -43,7 +45,7 @@ public sealed class GasCanisterSystem : EntitySystem
SubscribeLocalEvent<GasCanisterComponent, AtmosDeviceUpdateEvent>(OnCanisterUpdated); SubscribeLocalEvent<GasCanisterComponent, AtmosDeviceUpdateEvent>(OnCanisterUpdated);
SubscribeLocalEvent<GasCanisterComponent, ActivateInWorldEvent>(OnCanisterActivate, after: new[] { typeof(LockSystem) }); SubscribeLocalEvent<GasCanisterComponent, ActivateInWorldEvent>(OnCanisterActivate, after: new[] { typeof(LockSystem) });
SubscribeLocalEvent<GasCanisterComponent, InteractHandEvent>(OnCanisterInteractHand); SubscribeLocalEvent<GasCanisterComponent, InteractHandEvent>(OnCanisterInteractHand);
SubscribeLocalEvent<GasCanisterComponent, InteractUsingEvent>(OnCanisterInteractUsing); SubscribeLocalEvent<GasCanisterComponent, ItemSlotInsertAttemptEvent>(OnCanisterInsertAttempt);
SubscribeLocalEvent<GasCanisterComponent, EntInsertedIntoContainerMessage>(OnCanisterContainerInserted); SubscribeLocalEvent<GasCanisterComponent, EntInsertedIntoContainerMessage>(OnCanisterContainerInserted);
SubscribeLocalEvent<GasCanisterComponent, EntRemovedFromContainerMessage>(OnCanisterContainerRemoved); SubscribeLocalEvent<GasCanisterComponent, EntRemovedFromContainerMessage>(OnCanisterContainerRemoved);
SubscribeLocalEvent<GasCanisterComponent, PriceCalculationEvent>(CalculateCanisterPrice); SubscribeLocalEvent<GasCanisterComponent, PriceCalculationEvent>(CalculateCanisterPrice);
@@ -74,11 +76,8 @@ public sealed class GasCanisterSystem : EntitySystem
private void OnCanisterStartup(EntityUid uid, GasCanisterComponent comp, ComponentStartup args) private void OnCanisterStartup(EntityUid uid, GasCanisterComponent comp, ComponentStartup args)
{ {
// Ensure container manager. // Ensure container
var containerManager = EnsureComp<ContainerManagerComponent>(uid); _slots.AddItemSlot(uid, comp.ContainerName, comp.GasTankSlot);
// Ensure container.
_container.EnsureContainer<ContainerSlot>(uid, comp.ContainerName, containerManager);
if (TryComp<LockComponent>(uid, out var lockComp)) if (TryComp<LockComponent>(uid, out var lockComp))
{ {
@@ -87,10 +86,9 @@ public sealed class GasCanisterSystem : EntitySystem
} }
private void DirtyUI(EntityUid uid, private void DirtyUI(EntityUid uid,
GasCanisterComponent? canister = null, NodeContainerComponent? nodeContainer = null, GasCanisterComponent? canister = null, NodeContainerComponent? nodeContainer = null)
ContainerManagerComponent? containerManager = null)
{ {
if (!Resolve(uid, ref canister, ref nodeContainer, ref containerManager)) if (!Resolve(uid, ref canister, ref nodeContainer))
return; return;
var portStatus = false; var portStatus = false;
@@ -100,10 +98,9 @@ public sealed class GasCanisterSystem : EntitySystem
if (_nodeContainer.TryGetNode(nodeContainer, canister.PortName, out PipeNode? portNode) && portNode.NodeGroup?.Nodes.Count > 1) if (_nodeContainer.TryGetNode(nodeContainer, canister.PortName, out PipeNode? portNode) && portNode.NodeGroup?.Nodes.Count > 1)
portStatus = true; portStatus = true;
if (containerManager.TryGetContainer(canister.ContainerName, out var tankContainer) if (canister.GasTankSlot.Item != null)
&& tankContainer.ContainedEntities.Count > 0)
{ {
var tank = tankContainer.ContainedEntities[0]; var tank = canister.GasTankSlot.Item.Value;
var tankComponent = Comp<GasTankComponent>(tank); var tankComponent = Comp<GasTankComponent>(tank);
tankLabel = Name(tank); tankLabel = Name(tank);
tankPressure = tankComponent.Air.Pressure; tankPressure = tankComponent.Air.Pressure;
@@ -117,15 +114,12 @@ public sealed class GasCanisterSystem : EntitySystem
private void OnHoldingTankEjectMessage(EntityUid uid, GasCanisterComponent canister, GasCanisterHoldingTankEjectMessage args) private void OnHoldingTankEjectMessage(EntityUid uid, GasCanisterComponent canister, GasCanisterHoldingTankEjectMessage args)
{ {
if (!TryComp<ContainerManagerComponent>(uid, out var containerManager) if (canister.GasTankSlot.Item == null || args.Session.AttachedEntity == null)
|| !containerManager.TryGetContainer(canister.ContainerName, out var container))
return; return;
if (container.ContainedEntities.Count == 0) var item = canister.GasTankSlot.Item;
return; _slots.TryEjectToHands(uid, canister.GasTankSlot, args.Session.AttachedEntity);
_adminLogger.Add(LogType.CanisterTankEjected, LogImpact.Medium, $"Player {ToPrettyString(args.Session.AttachedEntity.GetValueOrDefault()):player} ejected tank {ToPrettyString(item):tank} from {ToPrettyString(uid):canister}");
_adminLogger.Add(LogType.CanisterTankEjected, LogImpact.Medium, $"Player {ToPrettyString(args.Session.AttachedEntity.GetValueOrDefault()):player} ejected tank {ToPrettyString(container.ContainedEntities[0]):tank} from {ToPrettyString(uid):canister}");
container.Remove(container.ContainedEntities[0]);
} }
private void OnCanisterChangeReleasePressure(EntityUid uid, GasCanisterComponent canister, GasCanisterChangeReleasePressureMessage args) private void OnCanisterChangeReleasePressure(EntityUid uid, GasCanisterComponent canister, GasCanisterChangeReleasePressureMessage args)
@@ -141,12 +135,8 @@ public sealed class GasCanisterSystem : EntitySystem
private void OnCanisterChangeReleaseValve(EntityUid uid, GasCanisterComponent canister, GasCanisterChangeReleaseValveMessage args) private void OnCanisterChangeReleaseValve(EntityUid uid, GasCanisterComponent canister, GasCanisterChangeReleaseValveMessage args)
{ {
var impact = LogImpact.High; var impact = LogImpact.High;
if (TryComp<ContainerManagerComponent>(uid, out var containerManager)
&& containerManager.TryGetContainer(canister.ContainerName, out var container))
{
// filling a jetpack with plasma is less important than filling a room with it // filling a jetpack with plasma is less important than filling a room with it
impact = container.ContainedEntities.Count != 0 ? LogImpact.Medium : LogImpact.High; impact = canister.GasTankSlot.HasItem ? LogImpact.Medium : LogImpact.High;
}
var containedGasDict = new Dictionary<Gas, float>(); var containedGasDict = new Dictionary<Gas, float>();
var containedGasArray = Gas.GetValues(typeof(Gas)); var containedGasArray = Gas.GetValues(typeof(Gas));
@@ -178,18 +168,12 @@ public sealed class GasCanisterSystem : EntitySystem
MixContainerWithPipeNet(canister.Air, net.Air); MixContainerWithPipeNet(canister.Air, net.Air);
} }
ContainerManagerComponent? containerManager = null;
// Release valve is open, release gas. // Release valve is open, release gas.
if (canister.ReleaseValve) if (canister.ReleaseValve)
{ {
if (!TryComp(uid, out containerManager) if (canister.GasTankSlot.Item != null)
|| !containerManager.TryGetContainer(canister.ContainerName, out var container))
return;
if (container.ContainedEntities.Count > 0)
{ {
var gasTank = Comp<GasTankComponent>(container.ContainedEntities[0]); var gasTank = Comp<GasTankComponent>(canister.GasTankSlot.Item.Value);
_atmos.ReleaseGasTo(canister.Air, gasTank.Air, canister.ReleasePressure); _atmos.ReleaseGasTo(canister.Air, gasTank.Air, canister.ReleasePressure);
} }
else else
@@ -203,7 +187,7 @@ public sealed class GasCanisterSystem : EntitySystem
if (MathHelper.CloseToPercent(canister.Air.Pressure, canister.LastPressure)) if (MathHelper.CloseToPercent(canister.Air.Pressure, canister.LastPressure))
return; return;
DirtyUI(uid, canister, nodeContainer, containerManager); DirtyUI(uid, canister, nodeContainer);
canister.LastPressure = canister.Air.Pressure; canister.LastPressure = canister.Air.Pressure;
@@ -254,28 +238,22 @@ public sealed class GasCanisterSystem : EntitySystem
args.Handled = true; args.Handled = true;
} }
private void OnCanisterInteractUsing(EntityUid uid, GasCanisterComponent component, InteractUsingEvent args) private void OnCanisterInsertAttempt(EntityUid uid, GasCanisterComponent component, ref ItemSlotInsertAttemptEvent args)
{ {
var container = _container.EnsureContainer<ContainerSlot>(uid, component.ContainerName); if (args.Slot.ID != component.ContainerName || args.User == null)
// Container full.
if (container.ContainedEntity != null)
return; return;
// Check the used item is valid... if (!TryComp<GasTankComponent>(args.Item, out var gasTank) || gasTank.IsValveOpen)
if (!TryComp<GasTankComponent>(args.Used, out var gasTank) || gasTank.IsValveOpen) {
args.Cancelled = true;
return; return;
}
// Preventing inserting a tank since if its locked you cant remove it. // Preventing inserting a tank since if its locked you cant remove it.
if (CheckLocked(uid, component, args.User)) if (!CheckLocked(uid, component, args.User.Value))
return; return;
if (!_hands.TryDropIntoContainer(args.User, args.Used, container)) args.Cancelled = true;
return;
_adminLogger.Add(LogType.CanisterTankInserted, LogImpact.Medium, $"Player {ToPrettyString(args.User):player} inserted tank {ToPrettyString(container.ContainedEntities[0]):tank} into {ToPrettyString(uid):canister}");
args.Handled = true;
} }
private void OnCanisterContainerInserted(EntityUid uid, GasCanisterComponent component, EntInsertedIntoContainerMessage args) private void OnCanisterContainerInserted(EntityUid uid, GasCanisterComponent component, EntInsertedIntoContainerMessage args)
@@ -347,10 +325,7 @@ public sealed class GasCanisterSystem : EntitySystem
if (TryComp<LockComponent>(uid, out var lockComp) && lockComp.Locked) if (TryComp<LockComponent>(uid, out var lockComp) && lockComp.Locked)
{ {
_popup.PopupEntity(Loc.GetString("gas-canister-popup-denied"), uid, user); _popup.PopupEntity(Loc.GetString("gas-canister-popup-denied"), uid, user);
if (comp.AccessDeniedSound != null)
{
_audio.PlayPvs(comp.AccessDeniedSound, uid); _audio.PlayPvs(comp.AccessDeniedSound, uid);
}
return true; return true;
} }

View File

@@ -80,7 +80,7 @@
joinSystem: true joinSystem: true
- type: ContainerContainer - type: ContainerContainer
containers: containers:
GasCanisterTankHolder: !type:ContainerSlot {} tank_slot: !type:ContainerSlot {}
- type: NodeContainer - type: NodeContainer
nodes: nodes:
port: port:
@@ -88,8 +88,14 @@
nodeGroupID: Pipe nodeGroupID: Pipe
rotationsEnabled: false rotationsEnabled: false
volume: 1 volume: 1
- type: ItemSlots
- type: GasPortable - type: GasPortable
- type: GasCanister - type: GasCanister
tank_slot:
name: Gas Tank
whitelist:
component:
- GasTank
- type: StaticPrice - type: StaticPrice
price: 1000 price: 1000
- type: AccessReader - type: AccessReader