Add OnSettingColorEvent, make changing p.color consistent

Fixes /pe [user] color not immediately appear in tablist/entity
This commit is contained in:
UnknownShadow200 2020-06-06 19:05:36 +10:00
parent 36af5f0495
commit d7313bf840
10 changed files with 46 additions and 17 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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();
}

View File

@ -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);

View File

@ -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;

View File

@ -225,6 +225,21 @@ namespace MCGalaxy.Events.PlayerEvents {
}
}
public delegate void OnSettingColor(Player p, ref string color);
/// <summary> Called when color is being updated for a player. </summary>
/// <remarks> e.g. You can use this to ensure player's color remains fixed to red while in a game. </remarks>
public sealed class OnSettingColorEvent : IEvent<OnSettingColor> {
public static void Call(Player p, ref string color) {
IEvent<OnSettingColor>[] 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);
/// <summary> Called when MOTD is being retrieved for a player. </summary>
/// <remarks> e.g. You can use this event to make one player always have +hax motd. </remarks>

View File

@ -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();
}

View File

@ -121,6 +121,20 @@ namespace MCGalaxy {
if (title.Length > 0) prefix += MakeTitle(title, titlecolor);
}
/// <summary> Raises OnSettingColorEvent then sets color. </summary>
public void SetColor(string col) {
OnSettingColorEvent.Call(this, ref col);
color = col;
}
/// <summary> Raises OnSettingColorEvent then sets color. </summary>
/// <remarks> If color differs from previous, respawns this player. </remarks>
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 + "] ";
}

View File

@ -38,6 +38,12 @@ namespace MCGalaxy {
: Group.GroupIn(name).Color + name.RemoveLastPlus();
}
/// <summary> Calculates default color for the given player. </summary>
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;