mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-10-03 18:43:30 -04:00
Add event for when getting whether a player can see an entity
This commit is contained in:
parent
ea115da1f8
commit
3be4601ee5
@ -95,4 +95,19 @@ namespace MCGalaxy.Events.EntityEvents {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public delegate void OnGettingCanSeeEntity(Player p, ref bool canSee, Entity target);
|
||||
/// <summary> Called when code is checking if this player can see the given entity. </summary>
|
||||
/// <remarks> e.g. You can use this event to make a player invisible during a game. </remarks>
|
||||
public sealed class OnGettingCanSeeEntityEvent : IEvent<OnGettingCanSeeEntity> {
|
||||
|
||||
public static void Call(Player p, ref bool canSee, Entity target) {
|
||||
IEvent<OnGettingCanSeeEntity>[] 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, ref canSee, target); }
|
||||
catch (Exception ex) { LogHandlerException(ex, items[i]); }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -42,6 +42,7 @@ namespace MCGalaxy.Games {
|
||||
OnPlayerSpawningEvent.Register(HandlePlayerSpawning, Priority.High);
|
||||
OnJoinedLevelEvent.Register(HandleJoinedLevel, Priority.High);
|
||||
OnPlayerChatEvent.Register(HandlePlayerChat, Priority.High);
|
||||
OnGettingCanSeeEntityEvent.Register(HandleCanSeeEntity, Priority.High);
|
||||
|
||||
base.HookEventHandlers();
|
||||
}
|
||||
@ -58,17 +59,27 @@ namespace MCGalaxy.Games {
|
||||
OnPlayerSpawningEvent.Unregister(HandlePlayerSpawning);
|
||||
OnJoinedLevelEvent.Unregister(HandleJoinedLevel);
|
||||
OnPlayerChatEvent.Unregister(HandlePlayerChat);
|
||||
OnGettingCanSeeEntityEvent.Unregister(HandleCanSeeEntity);
|
||||
|
||||
base.UnhookEventHandlers();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void HandleCanSeeEntity(Player p, ref bool canSee, Entity other) {
|
||||
Player target = other as Player;
|
||||
if (!canSee || p.Game.Referee) return;
|
||||
|
||||
ZSData data = TryGet(target);
|
||||
if (data == null) return;
|
||||
canSee = !(target.Game.Referee || data.Invisible);
|
||||
}
|
||||
|
||||
void HandleSendingModel(Entity e, ref string model, Player dst) {
|
||||
Player p = e as Player;
|
||||
if (p == null || !Get(p).Infected) return;
|
||||
model = p == dst ? p.Model : Config.ZombieModel;
|
||||
}
|
||||
|
||||
|
||||
void HandleTabListEntryAdded(Entity e, ref string tabName, ref string tabGroup, Player dst) {
|
||||
Player p = e as Player;
|
||||
if (p == null || p.level != Map) return;
|
||||
|
@ -20,6 +20,7 @@ using System.Threading;
|
||||
using MCGalaxy.DB;
|
||||
using MCGalaxy.Drawing;
|
||||
using MCGalaxy.Events.EconomyEvents;
|
||||
using MCGalaxy.Events.EntityEvents;
|
||||
using MCGalaxy.Events.PlayerEvents;
|
||||
using MCGalaxy.Games;
|
||||
using MCGalaxy.Maths;
|
||||
@ -80,16 +81,11 @@ namespace MCGalaxy {
|
||||
|
||||
public override bool CanSeeEntity(Entity other) {
|
||||
Player target = other as Player;
|
||||
if (target == null) return true; // not a player
|
||||
if (target == this) return true; // always see self
|
||||
if (other == this) return true; // always see self
|
||||
|
||||
// hidden via /hide or /ohide
|
||||
// TODO: Just use Entities.CanSee
|
||||
if (target.hidden) return Rank >= target.hideRank;
|
||||
|
||||
if (!ZSGame.Instance.Running || Game.Referee) return true;
|
||||
ZSData data = ZSGame.TryGet(target);
|
||||
return data == null || !(target.Game.Referee || data.Invisible);
|
||||
bool canSee = CanSee(target, Rank);
|
||||
OnGettingCanSeeEntityEvent.Call(this, ref canSee, other);
|
||||
return canSee;
|
||||
}
|
||||
|
||||
public BlockID GetHeldBlock() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user