2023-06-29 08:35:54 -04:00
|
|
|
using Content.Shared.Coordinates.Helpers;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Engineering.Components;
|
|
|
|
|
using Content.Server.Stack;
|
2023-02-24 19:01:25 -05:00
|
|
|
using Content.Shared.DoAfter;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Interaction;
|
2022-06-03 22:15:46 -04:00
|
|
|
using Content.Shared.Maps;
|
2021-06-12 11:24:34 +02:00
|
|
|
using Content.Shared.Stacks;
|
2021-04-01 00:04:56 -07:00
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using Robust.Shared.Map;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Engineering.EntitySystems
|
2021-04-01 00:04:56 -07:00
|
|
|
{
|
|
|
|
|
[UsedImplicitly]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class SpawnAfterInteractSystem : EntitySystem
|
2021-04-01 00:04:56 -07:00
|
|
|
{
|
|
|
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
2023-04-03 13:13:48 +12:00
|
|
|
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
|
2021-07-26 12:58:17 +02:00
|
|
|
[Dependency] private readonly StackSystem _stackSystem = default!;
|
2021-04-01 00:04:56 -07:00
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
2021-05-22 21:06:40 -07:00
|
|
|
SubscribeLocalEvent<SpawnAfterInteractComponent, AfterInteractEvent>(HandleAfterInteract);
|
2021-04-01 00:04:56 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-22 21:06:40 -07:00
|
|
|
private async void HandleAfterInteract(EntityUid uid, SpawnAfterInteractComponent component, AfterInteractEvent args)
|
2021-04-01 00:04:56 -07:00
|
|
|
{
|
2022-05-24 19:34:39 -07:00
|
|
|
if (!args.CanReach && !component.IgnoreDistance)
|
|
|
|
|
return;
|
2021-04-01 00:04:56 -07:00
|
|
|
if (string.IsNullOrEmpty(component.Prototype))
|
|
|
|
|
return;
|
2022-06-20 12:14:35 +12:00
|
|
|
if (!_mapManager.TryGetGrid(args.ClickLocation.GetGridUid(EntityManager), out var grid))
|
2021-04-01 14:34:35 -07:00
|
|
|
return;
|
|
|
|
|
if (!grid.TryGetTileRef(args.ClickLocation, out var tileRef))
|
2021-04-01 00:04:56 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
bool IsTileClear()
|
|
|
|
|
{
|
2022-06-03 22:15:46 -04:00
|
|
|
return tileRef.Tile.IsEmpty == false && !tileRef.IsBlockedTurf(true);
|
2021-04-01 00:04:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!IsTileClear())
|
|
|
|
|
return;
|
|
|
|
|
|
2021-07-26 12:58:17 +02:00
|
|
|
if (component.DoAfterTime > 0)
|
2021-04-01 00:04:56 -07:00
|
|
|
{
|
2023-04-03 13:13:48 +12:00
|
|
|
var doAfterArgs = new DoAfterArgs(args.User, component.DoAfterTime, new AwaitedDoAfterEvent(), null)
|
2021-04-01 00:04:56 -07:00
|
|
|
{
|
|
|
|
|
BreakOnUserMove = true,
|
|
|
|
|
};
|
2021-07-26 12:58:17 +02:00
|
|
|
var result = await _doAfterSystem.WaitDoAfter(doAfterArgs);
|
2021-04-01 00:04:56 -07:00
|
|
|
|
|
|
|
|
if (result != DoAfterStatus.Finished)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-03 13:13:48 +12:00
|
|
|
if (component.Deleted || !IsTileClear())
|
2021-04-01 00:04:56 -07:00
|
|
|
return;
|
|
|
|
|
|
2022-12-24 23:28:21 -05:00
|
|
|
if (EntityManager.TryGetComponent<StackComponent?>(component.Owner, out var stackComp)
|
2021-09-20 13:39:05 +02:00
|
|
|
&& component.RemoveOnInteract && !_stackSystem.Use(uid, 1, stackComp))
|
2021-05-26 10:20:57 +02:00
|
|
|
{
|
2021-06-12 11:24:34 +02:00
|
|
|
return;
|
2021-05-26 10:20:57 +02:00
|
|
|
}
|
2021-04-01 00:04:56 -07:00
|
|
|
|
2021-04-28 10:49:37 -07:00
|
|
|
EntityManager.SpawnEntity(component.Prototype, args.ClickLocation.SnapToGrid(grid));
|
2021-04-01 00:04:56 -07:00
|
|
|
|
2021-12-08 13:00:43 +01:00
|
|
|
if (component.RemoveOnInteract && stackComp == null && !((!EntityManager.EntityExists(component.Owner) ? EntityLifeStage.Deleted : EntityManager.GetComponent<MetaDataComponent>(component.Owner).EntityLifeStage) >= EntityLifeStage.Deleted))
|
|
|
|
|
EntityManager.DeleteEntity(component.Owner);
|
2021-04-01 00:04:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|