using Content.Server.Chat.Systems;
using Content.Shared.Chat;
using Content.Shared.Popups;
using Content.Shared.Teleportation;
using Content.Shared.Teleportation.Components;
using Content.Shared.Teleportation.Systems;
using Content.Shared.UserInterface;
using Content.Shared.Warps;
using Content.Shared.Whitelist;
namespace Content.Server.Teleportation;
///
///
///
public sealed partial class TeleportLocationsSystem : SharedTeleportLocationsSystem
{
[Dependency] private ChatSystem _chat = default!;
[Dependency] private SharedPopupSystem _popup = default!;
[Dependency] private EntityWhitelistSystem _whitelist = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnMapInit);
SubscribeLocalEvent(OnBeforeUiOpen);
}
private void OnMapInit(Entity ent, ref MapInitEvent args)
{
UpdateTeleportPoints(ent);
}
private void OnBeforeUiOpen(Entity ent, ref BeforeActivatableUIOpenEvent args)
{
UpdateTeleportPoints(ent);
}
protected override void OnTeleportToLocationRequest(Entity ent, ref TeleportLocationDestinationMessage args)
{
if (IsDelayed(ent))
return;
if (!string.IsNullOrWhiteSpace(ent.Comp.Speech))
{
var msg = Loc.GetString(ent.Comp.Speech, ("location", args.PointName));
_chat.TrySendInGameICMessage(args.Actor, msg, InGameICChatType.Speak, ChatTransmitRange.Normal);
}
base.OnTeleportToLocationRequest(ent, ref args);
}
protected override void HandleTeleportDestinationObstructed(EntityUid userUid)
{
// Client can't know if the destination is obstructed if it's not loaded.
var msg = Loc.GetString("teleportation-menu-destination-obstructed");
_popup.PopupEntity(msg, userUid, userUid, PopupType.MediumCaution);
}
// If it's in shared this doesn't populate the points on the UI
///
/// Gets the teleport points to send to the BUI
///
private void UpdateTeleportPoints(Entity ent)
{
ent.Comp.AvailableWarps.Clear();
var allEnts = AllEntityQuery();
// Selecting suitable entities with the warpPoint component using whitelist
while (allEnts.MoveNext(out var warpEnt, out var warpPointComp))
{
if (string.IsNullOrWhiteSpace(warpPointComp.Location))
continue;
if (!_whitelist.CheckBoth(warpEnt, ent.Comp.Blacklist, ent.Comp.Whitelist))
continue;
ent.Comp.AvailableWarps.Add(new TeleportPoint(Loc.GetString(warpPointComp.Location), GetNetEntity(warpEnt)));
}
Dirty(ent);
}
}