From d7313bf8402d8e86531f08a8d624d30a4dc500a4 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 6 Jun 2020 19:05:36 +1000 Subject: [PATCH] Add OnSettingColorEvent, make changing p.color consistent Fixes /pe [user] color not immediately appear in tablist/entity --- MCGalaxy/Commands/Chat/CmdColor.cs | 5 ++--- MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs | 2 +- MCGalaxy/Commands/Moderation/ModActionCmd.cs | 4 +--- MCGalaxy/Commands/other/CmdFakerank.cs | 3 +-- MCGalaxy/CorePlugin/ModActionHandler.cs | 1 - MCGalaxy/Database/PlayerData.cs | 8 +++++--- MCGalaxy/Events/PlayerEvents.cs | 15 +++++++++++++++ MCGalaxy/Player/Group/Group.cs | 5 +---- MCGalaxy/Player/Player.cs | 14 ++++++++++++++ MCGalaxy/Player/PlayerInfo.cs | 6 ++++++ 10 files changed, 46 insertions(+), 17 deletions(-) diff --git a/MCGalaxy/Commands/Chat/CmdColor.cs b/MCGalaxy/Commands/Chat/CmdColor.cs index 9c2aa4e7c..72530112a 100644 --- a/MCGalaxy/Commands/Chat/CmdColor.cs +++ b/MCGalaxy/Commands/Chat/CmdColor.cs @@ -51,17 +51,16 @@ namespace MCGalaxy.Commands.Chatting { string color = ""; if (colName.Length == 0) { Chat.MessageFrom(who, "λNICK %Shad their color removed"); - who.color = who.group.Color; + who.UpdateColor(who.group.Color); } else { color = Matcher.FindColor(p, colName); if (color == null) return; if (color == who.color) { p.Message(who.ColoredName + " %Salready has that color."); return; } Chat.MessageFrom(who, "λNICK %Shad their color changed to " + color + Colors.Name(color)); - who.color = color; + who.UpdateColor(color); } - Entities.GlobalRespawn(who); who.SetPrefix(); PlayerDB.Update(who.name, PlayerData.ColumnColor, color); } diff --git a/MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs b/MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs index ec1d46edf..1df042fc2 100644 --- a/MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs +++ b/MCGalaxy/Commands/Maintenance/CmdPlayerEdit.cs @@ -112,7 +112,7 @@ namespace MCGalaxy.Commands.Maintenance { v => who.TotalTime = v); } else if (opt == "color") { SetColor(p, args, PlayerData.ColumnColor, who, - v => who.color = (v.Length == 0 ? who.group.Color : v)); + v => who.UpdateColor(v.Length == 0 ? who.group.Color : v)); } else if (opt == "titlecolor") { SetColor(p, args, PlayerData.ColumnTColor, who, v => who.titlecolor = v); diff --git a/MCGalaxy/Commands/Moderation/ModActionCmd.cs b/MCGalaxy/Commands/Moderation/ModActionCmd.cs index 8d0dbbe4d..625b98fe7 100644 --- a/MCGalaxy/Commands/Moderation/ModActionCmd.cs +++ b/MCGalaxy/Commands/Moderation/ModActionCmd.cs @@ -84,9 +84,7 @@ namespace MCGalaxy.Commands.Moderation { who.AllowBuild = who.level.BuildAccess.CheckAllowed(who); if (who.hidden && who.hideRank < who.Rank) who.hideRank = who.Rank; - // If player has explicit /color, don't change it - string dbCol = PlayerDB.FindColor(who); - if (dbCol.Length == 0) who.color = newRank.Color; + who.SetColor(PlayerInfo.DefaultColor(who)); who.SetPrefix(); Entities.DespawnEntities(who, false); diff --git a/MCGalaxy/Commands/other/CmdFakerank.cs b/MCGalaxy/Commands/other/CmdFakerank.cs index bf35fb7f3..882ef5654 100644 --- a/MCGalaxy/Commands/other/CmdFakerank.cs +++ b/MCGalaxy/Commands/other/CmdFakerank.cs @@ -46,8 +46,7 @@ namespace MCGalaxy.Commands.Misc { who.Message("You are now ranked {0}%S, type /Help for your new set of commands.", newRank.ColoredName); } - who.color = newRank.Color; - Entities.GlobalRespawn(who); + who.UpdateColor(newRank.Color); who.SetPrefix(); } diff --git a/MCGalaxy/CorePlugin/ModActionHandler.cs b/MCGalaxy/CorePlugin/ModActionHandler.cs index 44b264ac6..7fc671e52 100644 --- a/MCGalaxy/CorePlugin/ModActionHandler.cs +++ b/MCGalaxy/CorePlugin/ModActionHandler.cs @@ -112,7 +112,6 @@ namespace MCGalaxy.Core { if (who != null) who.Kick("Banned for " + e.Duration.Shorten(true) + "." + e.ReasonSuffixed); } else { - if (who != null) who.color = ""; Ban.DeleteBan(e.Target); Ban.BanPlayer(e.Actor, e.Target, e.Reason, !e.Announce, e.TargetGroup.Name); ModActionCmd.ChangeRank(e.Target, e.targetGroup, Group.BannedRank, who); diff --git a/MCGalaxy/Database/PlayerData.cs b/MCGalaxy/Database/PlayerData.cs index 341673b3a..415031bb7 100644 --- a/MCGalaxy/Database/PlayerData.cs +++ b/MCGalaxy/Database/PlayerData.cs @@ -54,7 +54,7 @@ namespace MCGalaxy.DB { static object ReadID(IDataRecord record, object arg) { return record.GetInt32(0); } internal static void Create(Player p) { p.prefix = ""; - p.color = p.group.Color; + p.SetColor(p.group.Color); p.FirstLogin = DateTime.Now; p.TimesVisited = 1; @@ -79,8 +79,10 @@ namespace MCGalaxy.DB { p.title = data.Title; p.titlecolor = data.TitleColor; - p.color = data.Color; - if (p.color.Length == 0) p.color = p.group.Color; + + string col = data.Color; + if (col.Length == 0) col = p.group.Color; + p.SetColor(col); p.TotalModified = data.TotalModified; p.TotalDrawn = data.TotalDrawn; diff --git a/MCGalaxy/Events/PlayerEvents.cs b/MCGalaxy/Events/PlayerEvents.cs index 4f1b95fe3..acff146a8 100644 --- a/MCGalaxy/Events/PlayerEvents.cs +++ b/MCGalaxy/Events/PlayerEvents.cs @@ -225,6 +225,21 @@ namespace MCGalaxy.Events.PlayerEvents { } } + public delegate void OnSettingColor(Player p, ref string color); + /// Called when color is being updated for a player. + /// e.g. You can use this to ensure player's color remains fixed to red while in a game. + public sealed class OnSettingColorEvent : IEvent { + + public static void Call(Player p, ref string color) { + 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, ref color); } + catch (Exception ex) { LogHandlerException(ex, items[i]); } + } + } + } + public delegate void OnGettingMotd(Player p, ref string motd); /// Called when MOTD is being retrieved for a player. /// e.g. You can use this event to make one player always have +hax motd. diff --git a/MCGalaxy/Player/Group/Group.cs b/MCGalaxy/Player/Group/Group.cs index c4d42be8b..fb9975296 100644 --- a/MCGalaxy/Player/Group/Group.cs +++ b/MCGalaxy/Player/Group/Group.cs @@ -216,10 +216,7 @@ namespace MCGalaxy { if (grp == null) grp = DefaultRank; p.group = grp; - if (PlayerDB.FindColor(p).Length == 0 && p.color != grp.Color) { - p.color = grp.Color; - Entities.GlobalRespawn(p); - } + p.UpdateColor(PlayerInfo.DefaultColor(p)); p.SetPrefix(); } diff --git a/MCGalaxy/Player/Player.cs b/MCGalaxy/Player/Player.cs index 98fd467a6..552146634 100644 --- a/MCGalaxy/Player/Player.cs +++ b/MCGalaxy/Player/Player.cs @@ -121,6 +121,20 @@ namespace MCGalaxy { if (title.Length > 0) prefix += MakeTitle(title, titlecolor); } + /// Raises OnSettingColorEvent then sets color. + public void SetColor(string col) { + OnSettingColorEvent.Call(this, ref col); + color = col; + } + + /// Raises OnSettingColorEvent then sets color. + /// If color differs from previous, respawns this player. + public void UpdateColor(string col) { + string prevCol = color; + SetColor(col); + if (prevCol != color) Entities.GlobalRespawn(this); + } + internal string MakeTitle(string title, string titleCol) { return color + "[" + titleCol + title + color + "] "; } diff --git a/MCGalaxy/Player/PlayerInfo.cs b/MCGalaxy/Player/PlayerInfo.cs index 982b4ccfd..9539ade47 100644 --- a/MCGalaxy/Player/PlayerInfo.cs +++ b/MCGalaxy/Player/PlayerInfo.cs @@ -38,6 +38,12 @@ namespace MCGalaxy { : Group.GroupIn(name).Color + name.RemoveLastPlus(); } + /// Calculates default color for the given player. + public static string DefaultColor(Player p) { + string col = PlayerDB.FindColor(p); + return col.Length > 0 ? col : p.group.Color; + } + public static int NonHiddenCount() { Player[] players = Online.Items; int count = 0;