Remove 700 usages of Component.Owner (#21100)

This commit is contained in:
DrSmugleaf
2023-10-19 12:34:31 -07:00
committed by GitHub
parent 5825ffb95c
commit f560f88eb5
261 changed files with 2291 additions and 2036 deletions

View File

@@ -40,12 +40,12 @@ namespace Content.Server.Ghost.Roles
private uint _nextRoleIdentifier;
private bool _needsUpdateGhostRoleCount = true;
private readonly Dictionary<uint, GhostRoleComponent> _ghostRoles = new();
private readonly Dictionary<uint, Entity<GhostRoleComponent>> _ghostRoles = new();
private readonly Dictionary<ICommonSession, GhostRolesEui> _openUis = new();
private readonly Dictionary<ICommonSession, MakeGhostRoleEui> _openMakeGhostRoleUis = new();
[ViewVariables]
public IReadOnlyCollection<GhostRoleComponent> GhostRoles => _ghostRoles.Values;
public IReadOnlyCollection<Entity<GhostRoleComponent>> GhostRoles => _ghostRoles.Values;
public override void Initialize()
{
@@ -65,9 +65,9 @@ namespace Content.Server.Ghost.Roles
_playerManager.PlayerStatusChanged += PlayerStatusChanged;
}
private void OnMobStateChanged(EntityUid uid, GhostTakeoverAvailableComponent component, MobStateChangedEvent args)
private void OnMobStateChanged(Entity<GhostTakeoverAvailableComponent> component, ref MobStateChangedEvent args)
{
if (!TryComp(uid, out GhostRoleComponent? ghostRole))
if (!TryComp(component, out GhostRoleComponent? ghostRole))
return;
switch (args.NewMobState)
@@ -75,12 +75,12 @@ namespace Content.Server.Ghost.Roles
case MobState.Alive:
{
if (!ghostRole.Taken)
RegisterGhostRole(ghostRole);
RegisterGhostRole((component, ghostRole));
break;
}
case MobState.Critical:
case MobState.Dead:
UnregisterGhostRole(ghostRole);
UnregisterGhostRole((component, ghostRole));
break;
}
}
@@ -126,7 +126,8 @@ namespace Content.Server.Ghost.Roles
public void CloseEui(ICommonSession session)
{
if (!_openUis.ContainsKey(session)) return;
if (!_openUis.ContainsKey(session))
return;
_openUis.Remove(session, out var eui);
@@ -176,47 +177,57 @@ namespace Content.Server.Ghost.Roles
}
}
public void RegisterGhostRole(GhostRoleComponent role)
public void RegisterGhostRole(Entity<GhostRoleComponent> role)
{
if (_ghostRoles.ContainsValue(role)) return;
_ghostRoles[role.Identifier = GetNextRoleIdentifier()] = role;
UpdateAllEui();
if (_ghostRoles.ContainsValue(role))
return;
_ghostRoles[role.Comp.Identifier = GetNextRoleIdentifier()] = role;
UpdateAllEui();
}
public void UnregisterGhostRole(GhostRoleComponent role)
public void UnregisterGhostRole(Entity<GhostRoleComponent> role)
{
if (!_ghostRoles.ContainsKey(role.Identifier) || _ghostRoles[role.Identifier] != role) return;
_ghostRoles.Remove(role.Identifier);
var comp = role.Comp;
if (!_ghostRoles.ContainsKey(comp.Identifier) || _ghostRoles[comp.Identifier] != role)
return;
_ghostRoles.Remove(comp.Identifier);
UpdateAllEui();
}
public void Takeover(ICommonSession player, uint identifier)
{
if (!_ghostRoles.TryGetValue(identifier, out var role)) return;
if (!_ghostRoles.TryGetValue(identifier, out var role))
return;
var ev = new TakeGhostRoleEvent(player);
RaiseLocalEvent(role.Owner, ref ev);
RaiseLocalEvent(role, ref ev);
if (!ev.TookRole) return;
if (!ev.TookRole)
return;
if (player.AttachedEntity != null)
_adminLogger.Add(LogType.GhostRoleTaken, LogImpact.Low, $"{player:player} took the {role.RoleName:roleName} ghost role {ToPrettyString(player.AttachedEntity.Value):entity}");
_adminLogger.Add(LogType.GhostRoleTaken, LogImpact.Low, $"{player:player} took the {role.Comp.RoleName:roleName} ghost role {ToPrettyString(player.AttachedEntity.Value):entity}");
CloseEui(player);
}
public void Follow(ICommonSession player, uint identifier)
{
if (!_ghostRoles.TryGetValue(identifier, out var role)) return;
if (player.AttachedEntity == null) return;
if (!_ghostRoles.TryGetValue(identifier, out var role))
return;
_followerSystem.StartFollowingEntity(player.AttachedEntity.Value, role.Owner);
if (player.AttachedEntity == null)
return;
_followerSystem.StartFollowingEntity(player.AttachedEntity.Value, role);
}
public void GhostRoleInternalCreateMindAndTransfer(ICommonSession player, EntityUid roleUid, EntityUid mob, GhostRoleComponent? role = null)
{
if (!Resolve(roleUid, ref role)) return;
if (!Resolve(roleUid, ref role))
return;
DebugTools.AssertNotNull(player.ContentData());
@@ -233,10 +244,8 @@ namespace Content.Server.Ghost.Roles
var roles = new List<GhostRoleInfo>();
var metaQuery = GetEntityQuery<MetaDataComponent>();
foreach (var (id, role) in _ghostRoles)
foreach (var (id, (uid, role)) in _ghostRoles)
{
var uid = role.Owner;
if (metaQuery.GetComponent(uid).EntityPaused)
continue;
@@ -249,8 +258,12 @@ namespace Content.Server.Ghost.Roles
private void OnPlayerAttached(PlayerAttachedEvent message)
{
// Close the session of any player that has a ghost roles window open and isn't a ghost anymore.
if (!_openUis.ContainsKey(message.Player)) return;
if (EntityManager.HasComponent<GhostComponent>(message.Entity)) return;
if (!_openUis.ContainsKey(message.Player))
return;
if (HasComp<GhostComponent>(message.Entity))
return;
CloseEui(message.Player);
}
@@ -260,7 +273,7 @@ namespace Content.Server.Ghost.Roles
return;
ghostRole.Taken = true;
UnregisterGhostRole(ghostRole);
UnregisterGhostRole((uid, ghostRole));
}
private void OnMindRemoved(EntityUid uid, GhostTakeoverAvailableComponent component, MindRemovedMessage args)
@@ -273,7 +286,7 @@ namespace Content.Server.Ghost.Roles
return;
ghostRole.Taken = false;
RegisterGhostRole(ghostRole);
RegisterGhostRole((uid, ghostRole));
}
public void Reset(RoundRestartCleanupEvent ev)
@@ -304,20 +317,21 @@ namespace Content.Server.Ghost.Roles
UpdateAllEui();
}
private void OnInit(EntityUid uid, GhostRoleComponent role, ComponentInit args)
private void OnInit(Entity<GhostRoleComponent> ent, ref ComponentInit args)
{
var role = ent.Comp;
if (role.Probability < 1f && !_random.Prob(role.Probability))
{
RemComp<GhostRoleComponent>(uid);
RemComp<GhostRoleComponent>(ent);
return;
}
if (role.RoleRules == "")
role.RoleRules = Loc.GetString("ghost-role-component-default-rules");
RegisterGhostRole(role);
RegisterGhostRole(ent);
}
private void OnShutdown(EntityUid uid, GhostRoleComponent role, ComponentShutdown args)
private void OnShutdown(Entity<GhostRoleComponent> role, ref ComponentShutdown args)
{
UnregisterGhostRole(role);
}
@@ -391,7 +405,7 @@ namespace Content.Server.Ghost.Roles
MakeSentientCommand.MakeSentient(uid, EntityManager, ghostRole.AllowMovement, ghostRole.AllowSpeech);
GhostRoleInternalCreateMindAndTransfer(args.Player, uid, uid, ghostRole);
UnregisterGhostRole(ghostRole);
UnregisterGhostRole((uid, ghostRole));
args.TookRole = true;
}