mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-24 05:03:34 -04:00
Add EntitySpawned/EntityDespawned events.
This commit is contained in:
parent
ab7ba194fd
commit
58cbef023e
@ -14,6 +14,7 @@ permissions and limitations under the Licenses.
|
|||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using MCGalaxy.Events.EntityEvents;
|
||||||
using MCGalaxy.Games;
|
using MCGalaxy.Games;
|
||||||
using MCGalaxy.Network;
|
using MCGalaxy.Network;
|
||||||
using MCGalaxy.Maths;
|
using MCGalaxy.Maths;
|
||||||
@ -84,20 +85,12 @@ namespace MCGalaxy {
|
|||||||
Orientation rot, string possession = "") {
|
Orientation rot, string possession = "") {
|
||||||
byte id = p == dst ? Entities.SelfID : p.id;
|
byte id = p == dst ? Entities.SelfID : p.id;
|
||||||
|
|
||||||
if (!ServerConfig.TablistGlobal)
|
string name = p.color + p.truename + possession;
|
||||||
TabList.Add(dst, p, id);
|
string skin = p.SkinName, model = p.Model;
|
||||||
if (!Server.zombie.Running || !p.Game.Infected) {
|
OnEntitySpawnedEvent.Call(p, ref name, ref skin, ref model, dst);
|
||||||
SpawnRaw(dst, id, p.SkinName, p.color + p.truename + possession, p.Model, p.Pos, p.Rot);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
string name = p.truename, skinName = p.SkinName;
|
SpawnRaw(dst, id, skin, name, model, p.Pos, p.Rot);
|
||||||
if (ZSConfig.ZombieName.Length > 0 && !dst.Game.Aka) {
|
if (!ServerConfig.TablistGlobal) TabList.Add(dst, p, id);
|
||||||
name = ZSConfig.ZombieName; skinName = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
string model = p == dst ? p.Model : ZSConfig.ZombieModel;
|
|
||||||
SpawnRaw(dst, id, skinName, Colors.red + name + possession, model, p.Pos, p.Rot);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Spawns this player to all other players, and spawns all others players to this player. </summary>
|
/// <summary> Spawns this player to all other players, and spawns all others players to this player. </summary>
|
||||||
@ -133,14 +126,14 @@ namespace MCGalaxy {
|
|||||||
internal static void Spawn(Player dst, PlayerBot b) {
|
internal static void Spawn(Player dst, PlayerBot b) {
|
||||||
string name = Chat.Format(b.color + b.DisplayName, dst, true, false);
|
string name = Chat.Format(b.color + b.DisplayName, dst, true, false);
|
||||||
if (b.DisplayName.CaselessEq("empty")) name = "";
|
if (b.DisplayName.CaselessEq("empty")) name = "";
|
||||||
string skin = Chat.Format(b.SkinName, dst, true, false);
|
string skin = Chat.Format(b.SkinName, dst, true, false), model = b.Model;
|
||||||
|
|
||||||
SpawnRaw(dst, b.id, skin, name, b.Model, b.Pos, b.Rot);
|
OnEntitySpawnedEvent.Call(b, ref name, ref skin, ref model, dst);
|
||||||
if (ServerConfig.TablistBots)
|
SpawnRaw(dst, b.id, skin, name, model, b.Pos, b.Rot);
|
||||||
TabList.Add(dst, b);
|
if (ServerConfig.TablistBots) TabList.Add(dst, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void SpawnRaw(Player dst, byte id, string skin, string name,
|
static void SpawnRaw(Player dst, byte id, string skin, string name,
|
||||||
string model, Position pos, Orientation rot) {
|
string model, Position pos, Orientation rot) {
|
||||||
// NOTE: Fix for standard clients
|
// NOTE: Fix for standard clients
|
||||||
if (id == Entities.SelfID) pos.Y -= 22;
|
if (id == Entities.SelfID) pos.Y -= 22;
|
||||||
@ -163,16 +156,16 @@ namespace MCGalaxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal static void Despawn(Player dst, Player other) {
|
internal static void Despawn(Player dst, Player other) {
|
||||||
|
OnEntityDespawnedEvent.Call(other, dst);
|
||||||
byte id = other == dst ? SelfID : other.id;
|
byte id = other == dst ? SelfID : other.id;
|
||||||
dst.Send(Packet.RemoveEntity(id));
|
dst.Send(Packet.RemoveEntity(id));
|
||||||
if (!ServerConfig.TablistGlobal)
|
if (!ServerConfig.TablistGlobal) TabList.Remove(dst, other);
|
||||||
TabList.Remove(dst, other);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void Despawn(Player dst, PlayerBot b) {
|
internal static void Despawn(Player dst, PlayerBot b) {
|
||||||
|
OnEntityDespawnedEvent.Call(b, dst);
|
||||||
dst.Send(Packet.RemoveEntity(b.id));
|
dst.Send(Packet.RemoveEntity(b.id));
|
||||||
if (ServerConfig.TablistBots)
|
if (ServerConfig.TablistBots) TabList.Remove(dst, b);
|
||||||
TabList.Remove(dst, b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -42,8 +42,43 @@ namespace MCGalaxy.Events.EntityEvents {
|
|||||||
public sealed class OnTabListEntryRemovedEvent : IEvent<OnTabListEntryRemoved> {
|
public sealed class OnTabListEntryRemovedEvent : IEvent<OnTabListEntryRemoved> {
|
||||||
|
|
||||||
public static void Call(Entity entity, Player dst) {
|
public static void Call(Entity entity, Player dst) {
|
||||||
if (handlers.Count == 0) return;
|
IEvent<OnTabListEntryRemoved>[] items = handlers.Items;
|
||||||
CallCommon(pl => pl(entity, dst));
|
// Don't use CallCommon, because this event is called very frequently
|
||||||
|
// and want to avoid lots of pointless temp mem allocations
|
||||||
|
for (int i = 0; i < items.Length; i++) {
|
||||||
|
try { items[i].method(entity, dst); }
|
||||||
|
catch (Exception ex) { LogHandlerException(ex, items[i]); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public delegate void OnEntitySpawned(Entity entity, ref string name, ref string skin, ref string model, Player dst);
|
||||||
|
/// <summary> Called when an entity is being spawned to someone. </summary>
|
||||||
|
public sealed class OnEntitySpawnedEvent : IEvent<OnEntitySpawned> {
|
||||||
|
|
||||||
|
public static void Call(Entity entity, ref string name, ref string skin, ref string model, Player dst) {
|
||||||
|
IEvent<OnEntitySpawned>[] 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(entity, ref name, ref skin, ref model, dst);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
LogHandlerException(ex, items[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public delegate void OnEntityDespawned(Entity entity, Player dst);
|
||||||
|
/// <summary> Called when an entity is being despawned from someone. </summary>
|
||||||
|
public sealed class OnEntityDespawnedEvent : IEvent<OnEntityDespawned> {
|
||||||
|
|
||||||
|
public static void Call(Entity entity, Player dst) {
|
||||||
|
IEvent<OnEntityDespawned>[] items = handlers.Items;
|
||||||
|
for (int i = 0; i < items.Length; i++) {
|
||||||
|
try { items[i].method(entity, dst); }
|
||||||
|
catch (Exception ex) { LogHandlerException(ex, items[i]); }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ namespace MCGalaxy.Games.ZS {
|
|||||||
public ZSGame Game;
|
public ZSGame Game;
|
||||||
|
|
||||||
public override void Load(bool startup) {
|
public override void Load(bool startup) {
|
||||||
|
OnEntitySpawnedEvent.Register(HandleEntitySpawned, Priority.High);
|
||||||
OnTabListEntryAddedEvent.Register(HandleTabListEntryAdded, Priority.High);
|
OnTabListEntryAddedEvent.Register(HandleTabListEntryAdded, Priority.High);
|
||||||
OnMoneyChangedEvent.Register(HandleMoneyChanged, Priority.High);
|
OnMoneyChangedEvent.Register(HandleMoneyChanged, Priority.High);
|
||||||
OnPlayerConnectEvent.Register(HandlePlayerConnect, Priority.High);
|
OnPlayerConnectEvent.Register(HandlePlayerConnect, Priority.High);
|
||||||
@ -41,6 +42,7 @@ namespace MCGalaxy.Games.ZS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override void Unload(bool shutdown) {
|
public override void Unload(bool shutdown) {
|
||||||
|
OnEntitySpawnedEvent.Unregister(HandleEntitySpawned);
|
||||||
OnTabListEntryAddedEvent.Unregister(HandleTabListEntryAdded);
|
OnTabListEntryAddedEvent.Unregister(HandleTabListEntryAdded);
|
||||||
OnMoneyChangedEvent.Unregister(HandleMoneyChanged);
|
OnMoneyChangedEvent.Unregister(HandleMoneyChanged);
|
||||||
OnPlayerConnectEvent.Unregister(HandlePlayerConnect);
|
OnPlayerConnectEvent.Unregister(HandlePlayerConnect);
|
||||||
@ -74,6 +76,18 @@ namespace MCGalaxy.Games.ZS {
|
|||||||
HUD.UpdateTertiary(p);
|
HUD.UpdateTertiary(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HandleEntitySpawned(Entity entity, ref string name, ref string skin, ref string model, Player dst) {
|
||||||
|
Player p = entity as Player;
|
||||||
|
if (p == null || !p.Game.Infected) return;
|
||||||
|
|
||||||
|
name = p.truename;
|
||||||
|
if (ZSConfig.ZombieName.Length > 0 && !dst.Game.Aka) {
|
||||||
|
name = ZSConfig.ZombieName; skin = name;
|
||||||
|
}
|
||||||
|
name = Colors.red + name;
|
||||||
|
model = p == dst ? p.Model : ZSConfig.ZombieModel;
|
||||||
|
}
|
||||||
|
|
||||||
void HandlePlayerConnect(Player p) {
|
void HandlePlayerConnect(Player p) {
|
||||||
if (!ZSConfig.SetMainLevel) return;
|
if (!ZSConfig.SetMainLevel) return;
|
||||||
Player.Message(p, "Zombie Survival is running! Type %T/ZS go %Sto join.");
|
Player.Message(p, "Zombie Survival is running! Type %T/ZS go %Sto join.");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user