Convert sus events to eventbus (#5044)

This commit is contained in:
metalgearsloth
2021-10-27 19:53:10 +11:00
committed by GitHub
parent 3b508b041b
commit a12eb29464
8 changed files with 41 additions and 40 deletions

View File

@@ -166,8 +166,8 @@ namespace Content.Server.Mind
_roles.Add(role); _roles.Add(role);
role.Greet(); role.Greet();
var message = new RoleAddedMessage(role); var message = new RoleAddedEvent(role);
OwnedEntity?.SendMessage(OwnedComponent, message); OwnedEntity?.EntityManager.EventBus.RaiseLocalEvent(OwnedEntity.Uid, message);
return role; return role;
} }
@@ -188,8 +188,8 @@ namespace Content.Server.Mind
_roles.Remove(role); _roles.Remove(role);
var message = new RoleRemovedMessage(role); var message = new RoleRemovedEvent(role);
OwnedEntity?.SendMessage(OwnedComponent, message); OwnedEntity?.EntityManager.EventBus.RaiseLocalEvent(OwnedEntity.Uid, message);
} }
public bool HasRole<T>() where T : Role public bool HasRole<T>() where T : Role
@@ -278,7 +278,7 @@ namespace Content.Server.Mind
if (IsVisitingEntity if (IsVisitingEntity
&& (ghostCheckOverride // to force mind transfer, for example from ControlMobVerb && (ghostCheckOverride // to force mind transfer, for example from ControlMobVerb
|| !VisitingEntity!.TryGetComponent(out GhostComponent? ghostComponent) // visiting entity is not a Ghost || !VisitingEntity!.TryGetComponent(out GhostComponent? ghostComponent) // visiting entity is not a Ghost
|| !ghostComponent.CanReturnToBody)) // it is a ghost, but cannot return to body anyway, so it's okay || !ghostComponent.CanReturnToBody)) // it is a ghost, but cannot return to body anyway, so it's okay
{ {
VisitingEntity = null; VisitingEntity = null;
} }

View File

@@ -0,0 +1,7 @@
namespace Content.Server.Roles
{
public sealed class RoleAddedEvent : RoleEvent
{
public RoleAddedEvent(Role role) : base(role) { }
}
}

View File

@@ -1,7 +0,0 @@
namespace Content.Server.Roles
{
public class RoleAddedMessage : RoleMessage
{
public RoleAddedMessage(Role role) : base(role) { }
}
}

View File

@@ -2,11 +2,11 @@
namespace Content.Server.Roles namespace Content.Server.Roles
{ {
public class RoleMessage : ComponentMessage public class RoleEvent : EntityEventArgs
{ {
public readonly Role Role; public readonly Role Role;
public RoleMessage(Role role) public RoleEvent(Role role)
{ {
Role = role; Role = role;
} }

View File

@@ -0,0 +1,7 @@
namespace Content.Server.Roles
{
public sealed class RoleRemovedEvent : RoleEvent
{
public RoleRemovedEvent(Role role) : base(role) { }
}
}

View File

@@ -1,7 +0,0 @@
namespace Content.Server.Roles
{
public class RoleRemovedMessage : RoleMessage
{
public RoleRemovedMessage(Role role) : base(role) { }
}
}

View File

@@ -1,4 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using Content.Server.Roles;
using Content.Server.Suspicion.Roles;
using Content.Shared.GameTicking; using Content.Shared.GameTicking;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
@@ -20,20 +22,34 @@ namespace Content.Server.Suspicion.EntitySystems
base.Initialize(); base.Initialize();
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset); SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
SubscribeLocalEvent<SuspicionRoleComponent, PlayerAttachedEvent>((HandlePlayerAttached)); SubscribeLocalEvent<SuspicionRoleComponent, PlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<SuspicionRoleComponent, PlayerDetachedEvent>((HandlePlayerDetached)); SubscribeLocalEvent<SuspicionRoleComponent, PlayerDetachedEvent>(OnPlayerDetached);
SubscribeLocalEvent<SuspicionRoleComponent, RoleAddedEvent>(OnRoleAdded);
SubscribeLocalEvent<SuspicionRoleComponent, RoleRemovedEvent>(OnRoleRemoved);
} }
private void HandlePlayerDetached(EntityUid uid, SuspicionRoleComponent component, PlayerDetachedEvent args) private void OnPlayerDetached(EntityUid uid, SuspicionRoleComponent component, PlayerDetachedEvent args)
{ {
component.SyncRoles(); component.SyncRoles();
} }
private void HandlePlayerAttached(EntityUid uid, SuspicionRoleComponent component, PlayerAttachedEvent args) private void OnPlayerAttached(EntityUid uid, SuspicionRoleComponent component, PlayerAttachedEvent args)
{ {
component.SyncRoles(); component.SyncRoles();
} }
private void OnRoleAdded(EntityUid uid, SuspicionRoleComponent component, RoleAddedEvent args)
{
if (args.Role is not SuspicionRole role) return;
component.Role = role;
}
private void OnRoleRemoved(EntityUid uid, SuspicionRoleComponent component, RoleRemovedEvent args)
{
if (args.Role is not SuspicionRole) return;
component.Role = null;
}
#endregion #endregion
public void AddTraitor(SuspicionRoleComponent role) public void AddTraitor(SuspicionRoleComponent role)

View File

@@ -162,20 +162,5 @@ namespace Content.Server.Suspicion
return new SuspicionRoleComponentState(Role?.Name, Role?.Antagonist, allies.ToArray()); return new SuspicionRoleComponentState(Role?.Name, Role?.Antagonist, allies.ToArray());
} }
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case RoleAddedMessage {Role: SuspicionRole role}:
Role = role;
break;
case RoleRemovedMessage {Role: SuspicionRole}:
Role = null;
break;
}
}
} }
} }