From 63c17075813d4961c706722c5fa782e78a8567aa Mon Sep 17 00:00:00 2001 From: Vera Aguilera Puerto <6766154+Zumorica@users.noreply.github.com> Date: Tue, 24 Aug 2021 16:11:38 +0200 Subject: [PATCH] Adds new helpful filter methods. (#1962) - AddWhereAttachedEntity and RemoveWhereAttachedEntity methods - AddInGrid and AddInMap methods - BroadcastGrid and BroadcastMap static methods --- Robust.Shared/Player/Filter.cs | 55 ++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/Robust.Shared/Player/Filter.cs b/Robust.Shared/Player/Filter.cs index dd69d8f7e5..e9cfe8cf07 100644 --- a/Robust.Shared/Player/Filter.cs +++ b/Robust.Shared/Player/Filter.cs @@ -1,13 +1,11 @@ using System; using System.Collections.Generic; -using System.Linq; using JetBrains.Annotations; using Robust.Shared.Configuration; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Players; -using Robust.Shared.Utility; namespace Robust.Shared.Player { @@ -123,6 +121,31 @@ namespace Robust.Shared.Player return this; } + /// + /// Add all players whose attached entity match a predicate. + /// Doesn't consider players without an attached entity. + /// + public Filter AddWhereAttachedEntity(Predicate predicate) + { + return AddWhere(session => session.AttachedEntity is { } entity && predicate(entity)); + } + + /// + /// Add all players whose entity is on a certain grid. + /// + public Filter AddInGrid(GridId gridId) + { + return AddWhereAttachedEntity(entity => entity.Transform.GridID == gridId); + } + + /// + /// Add all players whose entity is on a certain map. + /// + public Filter AddInMap(MapId mapId) + { + return AddWhereAttachedEntity(entity => entity.Transform.MapID == mapId); + } + /// /// Adds all players in range of a position. /// @@ -162,6 +185,18 @@ namespace Robust.Shared.Player return this; } + /// + /// Removes all players whose attached entity match a predicate. + /// Doesn't consider players without an attached entity. + /// + /// + /// + public Filter RemoveWhereAttachedEntity(Predicate predicate) + { + _recipients.RemoveWhere(session => session.AttachedEntity is { } entity && predicate(entity)); + return this; + } + /// /// Removes all players in range of a position. /// @@ -229,6 +264,22 @@ namespace Robust.Shared.Player return Empty().AddAllPlayers(); } + /// + /// A new filter with all players whose attached entity is on a certain grid. + /// + public static Filter BroadcastGrid(GridId grid) + { + return Empty().AddInGrid(grid); + } + + /// + /// A new filter with all players whose attached entity is on a certain map. + /// + public static Filter BroadcastMap(MapId map) + { + return Empty().AddInMap(map); + } + /// /// A filter with every player who's PVS overlaps this entity. ///