CatStation/Content.Server/Sandbox/SandboxSystem.cs
Princess Cheeseballs 2c6ffecf62
Move GameTicker to Shared (#45834)
* Scope creep my ass

* why was this shit not pushed????

* gone...

* fix that

* review plus buildd

* asfsafsafsafsafas

* grug

* burber

* I'll fix this later

* shrimply die

* also this

* it's snerf or snuthin

* assaads

* round snend

* aaaa

* fix this shit for real

* actually, don't start GameRules until MapInit now that I think aboot it.

* forgot u

* don't actually need this part nullable

* Deffer comp removal

* one liner

* do that too

* chungus

* Honestly makes more sense here, plus WHY are we doing all that shit on "GameRuleAdded" rather than GameRuleStarted(?)

* deferred removal :V

* aaaaaaaaaaaaaaa

* whhop

* Whoop

* better

* landmine that I placed for myself then forgot about lmao

* cleanup

* FUCKING `!`

* gotovo

* amogus

* please don't check by metadata I beg

* waawsasfdfsasfa

* sfafsasfafsas

* fdsaasffsasfa

* code

* fix

* fasasffasfasfsad

* chungaas

* I may be stupid

* gra

* graaaaaaaaaaaaaaaaa

* comment

* gra

* clearing is not predicted and would require major refactors, so don't cache on client.

* lame shit :P

* last holes

* nukeopsfsafsasafsfafsa

* why  was that shit public bruh

* bubrbgwahrghfaka

* dynamic

* revert change that did nothing lmao
2026-09-16 19:34:19 +00:00

213 lines
7.4 KiB
C#

using System.Linq;
using Content.Server.GameTicking;
using Content.Shared.Access;
using Content.Shared.Access.Components;
using Content.Shared.Access.Systems;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.GameTicking;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Inventory;
using Content.Shared.Overlays;
using Content.Shared.PDA;
using Content.Shared.Sandbox;
using Robust.Server.Console;
using Robust.Server.Placement;
using Robust.Server.Player;
using Robust.Shared.Enums;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
namespace Content.Server.Sandbox
{
public sealed partial class SandboxSystem : SharedSandboxSystem
{
[Dependency] private IPlayerManager _playerManager = default!;
[Dependency] private IPlacementManager _placementManager = default!;
[Dependency] private IConGroupController _conGroupController = default!;
[Dependency] private IServerConsoleHost _host = default!;
[Dependency] private SharedAccessSystem _access = default!;
[Dependency] private InventorySystem _inventory = default!;
[Dependency] private ItemSlotsSystem _slots = default!;
[Dependency] private ServerGameTicker _ticker = default!;
[Dependency] private SharedHandsSystem _handsSystem = default!;
private bool _isSandboxEnabled;
[ViewVariables(VVAccess.ReadWrite)]
public bool IsSandboxEnabled
{
get => _isSandboxEnabled;
set
{
_isSandboxEnabled = value;
UpdateSandboxStatusForAll();
}
}
public override void Initialize()
{
base.Initialize();
SubscribeNetworkEvent<MsgSandboxRespawn>(SandboxRespawnReceived);
SubscribeNetworkEvent<MsgSandboxGiveAccess>(SandboxGiveAccessReceived);
SubscribeNetworkEvent<MsgSandboxGiveAghost>(SandboxGiveAghostReceived);
SubscribeNetworkEvent<MsgSandboxSuicide>(SandboxSuicideReceived);
SubscribeNetworkEvent<MsgSandboxThermalVision>(UpdateSandboxThermalVision);
SubscribeLocalEvent<GameRunLevelChangedEvent>(GameTickerOnOnRunLevelChanged);
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
_placementManager.AllowPlacementFunc = placement =>
{
if (IsSandboxEnabled)
{
return true;
}
var channel = placement.MsgChannel;
var player = _playerManager.GetSessionByChannel(channel);
if (_conGroupController.CanAdminPlace(player))
{
return true;
}
return false;
};
}
public override void Shutdown()
{
base.Shutdown();
_placementManager.AllowPlacementFunc = null;
_playerManager.PlayerStatusChanged -= OnPlayerStatusChanged;
}
private void GameTickerOnOnRunLevelChanged(GameRunLevelChangedEvent obj)
{
// Automatically clear sandbox state when round resets.
if (obj.New == GameRunLevel.PreRoundLobby)
{
IsSandboxEnabled = false;
}
}
private void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e)
{
if (e.NewStatus != SessionStatus.Connected || e.OldStatus != SessionStatus.Connecting)
return;
RaiseNetworkEvent(new MsgSandboxStatus { SandboxAllowed = IsSandboxEnabled }, e.Session.Channel);
}
private void SandboxRespawnReceived(MsgSandboxRespawn message, EntitySessionEventArgs args)
{
if (!IsSandboxEnabled)
return;
var player = _playerManager.GetSessionByChannel(args.SenderSession.Channel);
if (player.AttachedEntity == null) return;
_ticker.Respawn(player);
}
private void SandboxGiveAccessReceived(MsgSandboxGiveAccess message, EntitySessionEventArgs args)
{
if (!IsSandboxEnabled)
return;
var player = _playerManager.GetSessionByChannel(args.SenderSession.Channel);
if (player.AttachedEntity is not { } attached)
{
return;
}
var allAccess = ProtoMan
.EnumeratePrototypes<AccessLevelPrototype>()
.Select(p => new ProtoId<AccessLevelPrototype>(p.ID)).ToList();
if (_inventory.TryGetSlotEntity(attached, "id", out var slotEntity))
{
if (HasComp<AccessComponent>(slotEntity))
{
UpgradeId(slotEntity.Value);
}
else if (TryComp<PdaComponent>(slotEntity, out var pda))
{
if (pda.ContainedId is null)
{
var newID = CreateFreshId();
if (TryComp<ItemSlotsComponent>(slotEntity, out var itemSlots))
{
_slots.TryInsert(slotEntity.Value, pda.IdSlot, newID, null);
}
}
else
{
UpgradeId(pda.ContainedId!.Value);
}
}
}
else if (TryComp<HandsComponent>(attached, out var hands))
{
var card = CreateFreshId();
if (!_inventory.TryEquip(attached, card, "id", true, true))
{
_handsSystem.PickupOrDrop(attached, card, handsComp: hands);
}
}
void UpgradeId(EntityUid id)
{
_access.TrySetTags(id, allAccess);
}
EntityUid CreateFreshId()
{
var card = Spawn("CaptainIDCard", Transform(attached).Coordinates);
UpgradeId(card);
Comp<IdCardComponent>(card).FullName = MetaData(attached).EntityName;
return card;
}
}
private void SandboxGiveAghostReceived(MsgSandboxGiveAghost message, EntitySessionEventArgs args)
{
if (!IsSandboxEnabled)
return;
var player = _playerManager.GetSessionByChannel(args.SenderSession.Channel);
_host.ExecuteCommand(player, _conGroupController.CanCommand(player, "aghost") ? "aghost" : "ghost");
}
private void SandboxSuicideReceived(MsgSandboxSuicide message, EntitySessionEventArgs args)
{
if (!IsSandboxEnabled)
return;
var player = _playerManager.GetSessionByChannel(args.SenderSession.Channel);
_host.ExecuteCommand(player, "suicide");
}
private void UpdateSandboxStatusForAll()
{
RaiseNetworkEvent(new MsgSandboxStatus { SandboxAllowed = IsSandboxEnabled });
}
private void UpdateSandboxThermalVision(MsgSandboxThermalVision message, EntitySessionEventArgs args)
{
if (!IsSandboxEnabled)
return;
var ent = args.SenderSession.AttachedEntity;
if (ent == null) return;
if (HasComp<ThermalSightComponent>(ent))
RemComp<ThermalSightComponent>(ent.Value);
else
EnsureComp<ThermalSightComponent>(ent.Value);
}
}
}