Files
OldThink/Content.Server/Storage/EntitySystems/SpawnItemsOnUseSystem.cs

54 lines
1.6 KiB
C#
Raw Normal View History

using Content.Server.Storage.Components;
2022-03-17 20:13:31 +13:00
using Content.Shared.Hands.EntitySystems;
2022-03-13 01:33:23 +13:00
using Content.Shared.Interaction.Events;
using Content.Shared.Storage;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Random;
namespace Content.Server.Storage.EntitySystems
{
public sealed class SpawnItemsOnUseSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
2022-03-17 20:13:31 +13:00
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SpawnItemsOnUseComponent, UseInHandEvent>(OnUseInHand);
}
private void OnUseInHand(EntityUid uid, SpawnItemsOnUseComponent component, UseInHandEvent args)
{
if (args.Handled)
return;
var coords = Transform(args.User).Coordinates;
var spawnEntities = EntitySpawnCollection.GetSpawns(component.Items, _random);
2021-12-26 15:32:45 +13:00
EntityUid? entityToPlaceInHands = null;
foreach (var proto in spawnEntities)
{
entityToPlaceInHands = Spawn(proto, coords);
}
if (component.Sound != null)
SoundSystem.Play(Filter.Pvs(uid), component.Sound.GetSound(), uid);
component.Uses--;
if (component.Uses == 0)
{
args.Handled = true;
2021-12-05 18:09:01 +01:00
EntityManager.DeleteEntity(uid);
}
2022-03-17 20:13:31 +13:00
if (entityToPlaceInHands != null)
{
2022-03-17 20:13:31 +13:00
_handsSystem.PickupOrDrop(args.User, entityToPlaceInHands.Value);
}
}
}
}