From 3e8764d1411ec1576e5c2a110c53cf79681573ab Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 1 Aug 2020 22:45:33 +1000 Subject: [PATCH] Add OnGettingCanSee event too --- MCGalaxy/Entity/Entity.cs | 2 +- MCGalaxy/Events/EntityEvents.cs | 2 +- MCGalaxy/Events/PlayerEvents.cs | 14 ++++++++++++++ MCGalaxy/Player/Player.cs | 15 +++++++++------ MCGalaxy/Plugins/Plugin.cs | 2 +- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/MCGalaxy/Entity/Entity.cs b/MCGalaxy/Entity/Entity.cs index 8c3d5ec4a..38cc0a6e6 100644 --- a/MCGalaxy/Entity/Entity.cs +++ b/MCGalaxy/Entity/Entity.cs @@ -57,7 +57,7 @@ namespace MCGalaxy { Rot = rot; } - /// Whether this player can see the given entity as an entity in the map. + /// Whether this player can see the given entity as an entity in the level. public abstract bool CanSeeEntity(Entity other); public abstract byte EntityID { get; } /// The level this entity is currently on. diff --git a/MCGalaxy/Events/EntityEvents.cs b/MCGalaxy/Events/EntityEvents.cs index 9309f09ae..fa4b81367 100644 --- a/MCGalaxy/Events/EntityEvents.cs +++ b/MCGalaxy/Events/EntityEvents.cs @@ -97,7 +97,7 @@ namespace MCGalaxy.Events.EntityEvents { } public delegate void OnGettingCanSeeEntity(Player p, ref bool canSee, Entity target); - /// Called when code is checking if this player can see the given entity. + /// Called when checking if this player can see the given entity as an entity in the level. /// e.g. You can use this event to make a player invisible during a game. public sealed class OnGettingCanSeeEntityEvent : IEvent { diff --git a/MCGalaxy/Events/PlayerEvents.cs b/MCGalaxy/Events/PlayerEvents.cs index acff146a8..bb56129ba 100644 --- a/MCGalaxy/Events/PlayerEvents.cs +++ b/MCGalaxy/Events/PlayerEvents.cs @@ -296,5 +296,19 @@ namespace MCGalaxy.Events.PlayerEvents { if (handlers.Count == 0) return; CallCommon(pl => pl(p)); } + } + + public delegate void OnGettingCanSee(Player p, LevelPermission plRank, ref bool canSee, Player target); + /// Called when code is checking if this player can see the given player. + public sealed class OnGettingCanSeeEvent : IEvent { + + public static void Call(Player p, LevelPermission plRank, ref bool canSee, Player target) { + IEvent[] items = handlers.Items; + // Can't use CallCommon because we need to pass arguments by ref + for (int i = 0; i < items.Length; i++) { + try { items[i].method(p, plRank, ref canSee, target); } + catch (Exception ex) { LogHandlerException(ex, items[i]); } + } + } } } diff --git a/MCGalaxy/Player/Player.cs b/MCGalaxy/Player/Player.cs index 97c377f51..67c49028c 100644 --- a/MCGalaxy/Player/Player.cs +++ b/MCGalaxy/Player/Player.cs @@ -76,15 +76,18 @@ namespace MCGalaxy { public bool CanSee(Player target) { return CanSee(target, Rank); } /// Whether this player can see the given player, as if they were the given rank. public bool CanSee(Player target, LevelPermission plRank) { - return target == this || target == null || !target.hidden || plRank >= target.hideRank; + if (target == this || target == null) return true; + + bool canSee = !target.hidden || plRank >= target.hideRank; + OnGettingCanSeeEvent.Call(this, plRank, ref canSee, target); + return canSee; } - public override bool CanSeeEntity(Entity other) { - Player target = other as Player; - if (other == this) return true; // always see self + public override bool CanSeeEntity(Entity target) { + if (target == this) return true; // always see self - bool canSee = CanSee(target, Rank); - OnGettingCanSeeEntityEvent.Call(this, ref canSee, other); + bool canSee = CanSee(target as Player, Rank); + OnGettingCanSeeEntityEvent.Call(this, ref canSee, target); return canSee; } diff --git a/MCGalaxy/Plugins/Plugin.cs b/MCGalaxy/Plugins/Plugin.cs index 531a1e9fe..1b007ff18 100644 --- a/MCGalaxy/Plugins/Plugin.cs +++ b/MCGalaxy/Plugins/Plugin.cs @@ -56,7 +56,7 @@ namespace MCGalaxy { /// Message to display once plugin is loaded. public virtual string welcome { get { return ""; } } /// The creator/author of this plugin. (Your name) - public abstract string creator { get; } + public virtual string creator { get { return ""; } } /// Whether or not to auto load this plugin on server startup. public virtual bool LoadAtStartup { get { return true; } } }