Make chemistry machines and IdCardConsole use item slots (#5428)

* chemistry item slots

* item slots id card console
This commit is contained in:
Leon Friedrich
2021-11-24 20:03:07 +13:00
committed by GitHub
parent 355625bded
commit 3b29ffdfa0
26 changed files with 371 additions and 560 deletions

View File

@@ -1,16 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Content.Server.Chemistry.Components.SolutionManager;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.Hands.Components;
using Content.Server.Items;
using Content.Server.Power.Components;
using Content.Server.UserInterface;
using Content.Shared.ActionBlocker;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Dispenser;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.FixedPoint;
using Content.Shared.Interaction;
using Content.Shared.Popups;
@@ -18,7 +17,6 @@ using Content.Shared.Sound;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
@@ -38,21 +36,19 @@ namespace Content.Server.Chemistry.Components
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
[ComponentReference(typeof(IInteractUsing))]
public class ReagentDispenserComponent : SharedReagentDispenserComponent, IActivate, IInteractUsing
[ComponentReference(typeof(SharedReagentDispenserComponent))]
public class ReagentDispenserComponent : SharedReagentDispenserComponent, IActivate
{
private static ReagentInventoryComparer _comparer = new();
public static string SolutionName = "reagent";
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[ViewVariables] public ContainerSlot BeakerContainer = default!;
[ViewVariables] [DataField("pack")] private string _packPrototypeId = "";
[DataField("clickSound")]
private SoundSpecifier _clickSound = new SoundPathSpecifier("/Audio/Machines/machine_switch.ogg");
[ViewVariables] public bool HasBeaker => BeakerContainer.ContainedEntity != null;
[ViewVariables] private FixedPoint2 _dispenseAmount = FixedPoint2.New(10);
[UsedImplicitly]
@@ -84,12 +80,7 @@ namespace Content.Server.Chemistry.Components
UserInterface.OnReceiveMessage += OnUiReceiveMessage;
}
// Name relied upon by construction graph machine.yml to ensure beaker doesn't get deleted
BeakerContainer =
ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-reagentContainerContainer");
InitializeFromPrototype();
UpdateUserInterface();
}
[Obsolete("Component Messages are deprecated, use Entity Events instead.")]
@@ -157,7 +148,7 @@ namespace Content.Server.Chemistry.Components
switch (msg.Button)
{
case UiButton.Eject:
TryEject(obj.Session.AttachedEntity);
EntitySystem.Get<ItemSlotsSystem>().TryEjectToHands(OwnerUid, BeakerSlot, obj.Session.AttachedEntityUid);
break;
case UiButton.Clear:
TryClear();
@@ -190,7 +181,7 @@ namespace Content.Server.Chemistry.Components
_dispenseAmount = FixedPoint2.New(100);
break;
case UiButton.Dispense:
if (HasBeaker)
if (BeakerSlot.HasItem)
{
TryDispense(msg.DispenseIndex);
}
@@ -233,7 +224,7 @@ namespace Content.Server.Chemistry.Components
/// <returns>Returns a <see cref="SharedReagentDispenserComponent.ReagentDispenserBoundUserInterfaceState"/></returns>
private ReagentDispenserBoundUserInterfaceState GetUserInterfaceState()
{
var beaker = BeakerContainer.ContainedEntity;
var beaker = BeakerSlot.Item;
if (beaker == null || !beaker.TryGetComponent(out FitsInDispenserComponent? fits) ||
!EntitySystem.Get<SolutionContainerSystem>().TryGetSolution(beaker.Uid, fits.Solution, out var solution))
{
@@ -253,40 +244,19 @@ namespace Content.Server.Chemistry.Components
UserInterface?.SetState(state);
}
/// <summary>
/// If this component contains an entity with a <see cref="SolutionHolder"/>, eject it.
/// Tries to eject into user's hands first, then ejects onto dispenser if both hands are full.
/// </summary>
public void TryEject(IEntity user)
{
if (!HasBeaker)
return;
var beaker = BeakerContainer.ContainedEntity;
if (beaker is null)
return;
BeakerContainer.Remove(beaker);
UpdateUserInterface();
if (!user.TryGetComponent<HandsComponent>(out var hands) ||
!beaker.TryGetComponent<ItemComponent>(out var item))
return;
if (hands.CanPutInHand(item))
hands.PutInHand(item);
}
/// <summary>
/// If this component contains an entity with a <see cref="SolutionHolder"/>, remove all of it's reagents / solutions.
/// </summary>
private void TryClear()
{
if (!HasBeaker || !BeakerContainer.ContainedEntity!.TryGetComponent(out FitsInDispenserComponent? fits) ||
var beaker = BeakerSlot.Item;
if (beaker == null || !beaker.TryGetComponent(out FitsInDispenserComponent? fits) ||
!EntitySystem.Get<SolutionContainerSystem>()
.TryGetSolution(BeakerContainer.ContainedEntity.Uid, fits.Solution, out var solution))
.TryGetSolution(beaker.Uid, fits.Solution, out var solution))
return;
EntitySystem.Get<SolutionContainerSystem>().RemoveAllSolution(BeakerContainer.ContainedEntity!.Uid, solution);
EntitySystem.Get<SolutionContainerSystem>().RemoveAllSolution(beaker.Uid, solution);
UpdateUserInterface();
}
@@ -297,14 +267,14 @@ namespace Content.Server.Chemistry.Components
/// <param name="dispenseIndex">The index of the reagent in <c>Inventory</c>.</param>
private void TryDispense(int dispenseIndex)
{
if (!HasBeaker) return;
var beaker = BeakerSlot.Item;
if (BeakerContainer.ContainedEntity is not {} contained || !contained.TryGetComponent(out FitsInDispenserComponent? fits)
if (beaker is null || !beaker.TryGetComponent(out FitsInDispenserComponent? fits)
|| !EntitySystem.Get<SolutionContainerSystem>()
.TryGetSolution(BeakerContainer.ContainedEntity.Uid, fits.Solution, out var solution)) return;
.TryGetSolution(beaker.Uid, fits.Solution, out var solution)) return;
EntitySystem.Get<SolutionContainerSystem>()
.TryAddReagent(BeakerContainer.ContainedEntity.Uid, solution, Inventory[dispenseIndex].ID, _dispenseAmount, out _);
.TryAddReagent(beaker.Uid, solution, Inventory[dispenseIndex].ID, _dispenseAmount, out _);
UpdateUserInterface();
}
@@ -333,51 +303,6 @@ namespace Content.Server.Chemistry.Components
}
}
/// <summary>
/// Called when you click the owner entity with something in your active hand. If the entity in your hand
/// contains a <see cref="SolutionHolder"/>, if you have hands, and if the dispenser doesn't already
/// hold a container, it will be added to the dispenser.
/// </summary>
/// <param name="args">Data relevant to the event such as the actor which triggered it.</param>
/// <returns></returns>
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs args)
{
if (!args.User.TryGetComponent(out HandsComponent? hands))
{
Owner.PopupMessage(args.User, Loc.GetString("reagent-dispenser-component-interact-using-no-hands"));
return true;
}
if (hands.GetActiveHand == null)
{
Owner.PopupMessage(args.User,
Loc.GetString("reagent-dispenser-component-interact-using-nothing-in-hands"));
return false;
}
var activeHandEntity = hands.GetActiveHand.Owner;
if (activeHandEntity.HasComponent<FitsInDispenserComponent>())
{
if (HasBeaker)
{
Owner.PopupMessage(args.User,
Loc.GetString("reagent-dispenser-component-has-container-already-message"));
return false;
}
BeakerContainer.Insert(activeHandEntity);
UpdateUserInterface();
return true;
}
Owner.PopupMessage(args.User,
Loc.GetString("reagent-dispenser-component-cannot-put-entity-message",
("entity", activeHandEntity)));
return false;
}
private void ClickSound()
{
SoundSystem.Play(Filter.Pvs(Owner), _clickSound.GetSound(), Owner, AudioParams.Default.WithVolume(-2f));