Files
tbd-station-14/Content.Server/Players/JobWhitelist/JobWhitelistSystem.cs
Errant b692b6e33e Antag Rolebans (#35966)
Co-authored-by: beck-thompson <beck314159@hotmail.com>
Co-authored-by: Hannah Giovanna Dawson <karakkaraz@gmail.com>
2025-09-17 23:59:07 +02:00

91 lines
2.7 KiB
C#

using System.Collections.Immutable;
using Content.Server.GameTicking.Events;
using Content.Server.Station.Events;
using Content.Shared.CCVar;
using Content.Shared.Roles;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Server.Players.JobWhitelist;
public sealed class JobWhitelistSystem : EntitySystem
{
[Dependency] private readonly IConfigurationManager _config = default!;
[Dependency] private readonly JobWhitelistManager _manager = default!;
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly IPrototypeManager _prototypes = default!;
private ImmutableArray<ProtoId<JobPrototype>> _whitelistedJobs = [];
public override void Initialize()
{
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypesReloaded);
SubscribeLocalEvent<StationJobsGetCandidatesEvent>(OnStationJobsGetCandidates);
SubscribeLocalEvent<IsRoleAllowedEvent>(OnIsRoleAllowed);
SubscribeLocalEvent<GetDisallowedJobsEvent>(OnGetDisallowedJobs);
CacheJobs();
}
private void OnPrototypesReloaded(PrototypesReloadedEventArgs ev)
{
if (ev.WasModified<JobPrototype>())
CacheJobs();
}
private void OnStationJobsGetCandidates(ref StationJobsGetCandidatesEvent ev)
{
if (!_config.GetCVar(CCVars.GameRoleWhitelist))
return;
for (var i = ev.Jobs.Count - 1; i >= 0; i--)
{
var jobId = ev.Jobs[i];
if (_player.TryGetSessionById(ev.Player, out var player) &&
!_manager.IsAllowed(player, jobId))
{
ev.Jobs.RemoveSwap(i);
}
}
}
private void OnIsRoleAllowed(ref IsRoleAllowedEvent ev)
{
if (ev.Jobs is null)
return;
foreach (var proto in ev.Jobs)
{
if (!_manager.IsAllowed(ev.Player, proto))
ev.Cancelled = true;
}
}
//TODO: Antagonist role whitelists?
private void OnGetDisallowedJobs(ref GetDisallowedJobsEvent ev)
{
if (!_config.GetCVar(CCVars.GameRoleWhitelist))
return;
foreach (var job in _whitelistedJobs)
{
if (!_manager.IsAllowed(ev.Player, job))
ev.Jobs.Add(job);
}
}
private void CacheJobs()
{
var builder = ImmutableArray.CreateBuilder<ProtoId<JobPrototype>>();
foreach (var job in _prototypes.EnumeratePrototypes<JobPrototype>())
{
if (job.Whitelisted)
builder.Add(job.ID);
}
_whitelistedJobs = builder.ToImmutable();
}
}