diff --git a/MCGalaxy/Player/TabList.cs b/MCGalaxy/Entity/TabList.cs similarity index 88% rename from MCGalaxy/Player/TabList.cs rename to MCGalaxy/Entity/TabList.cs index f40162279..78e178063 100644 --- a/MCGalaxy/Player/TabList.cs +++ b/MCGalaxy/Entity/TabList.cs @@ -17,6 +17,7 @@ */ using System; using MCGalaxy.Games; +using MCGalaxy.Events.EntityEvents; using MCGalaxy.Network; namespace MCGalaxy { @@ -37,14 +38,12 @@ namespace MCGalaxy { dst.Send(Packet.ExtAddPlayerName(id, p.truename, name, group, grpPerm, dst.hasCP437)); } - /// Gets the name and the group name for the given player. - public static void GetEntry(Player p, Player dst, out string name, out string group) { + static void GetEntry(Player p, Player dst, out string name, out string group) { string col = Entities.GetSupportedCol(dst, p.color); group = ServerConfig.TablistGlobal ? "On " + p.level.name : "&fPlayers"; name = col + p.truename; - IGame game = p.level.CurrentGame(); - if (game != null) game.GetTabName(p, dst, ref name, ref group); - + OnTabListEntryAddedEvent.Call(p, ref name, ref group, dst); + if (p.hidden && p.IsAfk) { name += " &f(Hid, &7AFK)"; return; } if (p.hidden) name += " &f(Hid)"; if (p.IsAfk) name += " &7(AFK)"; @@ -53,7 +52,10 @@ namespace MCGalaxy { /// Adds the given bot to that player's tab list (if their client support it). public static void Add(Player dst, PlayerBot b) { if (!dst.hasExtList) return; - dst.Send(Packet.ExtAddPlayerName(b.id, b.SkinName, b.color + b.name, "Bots", 0, dst.hasCP437)); + + string name = b.color + b.name, group = "Bots"; + OnTabListEntryAddedEvent.Call(b, ref name, ref group, dst); + dst.Send(Packet.ExtAddPlayerName(b.id, b.SkinName, name, group, 0, dst.hasCP437)); } /// Removes the given player from player's tab list (if their client supports it). @@ -62,6 +64,7 @@ namespace MCGalaxy { dst.Send(Packet.ExtRemovePlayerName(id)); } + /// Updates the tab list entry for this player to all other players /// (whose clients support it) who can see the player in the tab list. internal static void Update(Player p, bool self) { diff --git a/MCGalaxy/Events/EntityEvents.cs b/MCGalaxy/Events/EntityEvents.cs new file mode 100644 index 000000000..efb861d9c --- /dev/null +++ b/MCGalaxy/Events/EntityEvents.cs @@ -0,0 +1,51 @@ +/* + Copyright 2015 MCGalaxy + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; +using System.Collections.Generic; + +namespace MCGalaxy.Events.EntityEvents { + + public delegate void OnTabListEntryAdded(Entity entity, ref string tabName, ref string tabGroup, Player dst); + /// Called when a tab list entry is being sent to a user. + public sealed class OnTabListEntryAddedEvent : IEvent { + + public static void Call(Entity entity, ref string tabName, ref string tabGroup, Player dst) { + IEvent[] items = handlers.Items; + // Can't use CallCommon because we need to pass arguments by ref + for (int i = 0; i < items.Length; i++) { + IEvent handler = items[i]; + + try { + handler.method(entity, ref tabName, ref tabGroup, dst); + } catch (Exception ex) { + LogHandlerException(ex, handler); + } + } + } + } + + public delegate void OnTabListEntryRemoved(Entity entity, Player dst); + /// Called when a tab list entry is being removed from a user. + public sealed class OnTabListEntryRemovedEvent : IEvent { + + public static void Call(Entity entity, Player dst) { + if (handlers.Count == 0) return; + CallCommon(pl => pl(entity, dst)); + } + } +} diff --git a/MCGalaxy/MCGalaxy_.csproj b/MCGalaxy/MCGalaxy_.csproj index c3e13d940..2fefd4c6a 100644 --- a/MCGalaxy/MCGalaxy_.csproj +++ b/MCGalaxy/MCGalaxy_.csproj @@ -479,7 +479,9 @@ + + @@ -597,7 +599,6 @@ -