Introduce a priority for persistent CPE message lines

This commit is contained in:
Goodlyay 2023-05-13 13:08:21 -04:00
parent 180b66eb52
commit 6bc29e7b58
3 changed files with 58 additions and 3 deletions

View File

@ -14,6 +14,7 @@ permissions and limitations under the Licenses.
*/
using System;
using System.Text;
using System.Collections.Generic;
using MCGalaxy.Commands;
using MCGalaxy.Events.ServerEvents;
@ -238,5 +239,57 @@ namespace MCGalaxy {
isCommand = true;
return text.Substring(1);
}
public class PersistentMessage {
public enum Priority { Lowest, Low, Default, High, Highest }
public string message;
public Priority priority;
public PersistentMessage(string message, Priority priority) {
this.message = message; this.priority = priority;
}
/// <summary>
/// Returns false if no persistent message was handled, otherwise true.
/// </summary>
internal static bool Handle(Player p, CpeMessageType type, string message, Priority priority) {
if (!IsPersistent(type)) return false;
if (!p.fields.ContainsKey(type)) {
p.fields[type] = new List<PersistentMessage>();
}
var field = p.fields[type];
PersistentMessage thisMsg = null;
foreach (var persMsg in field) { //Find an existing message with same priority
if (persMsg.priority == priority) { thisMsg = persMsg; break; }
}
if (string.IsNullOrEmpty(message)) { //Clearing the message case
if (thisMsg == null) { return true; } //No message exists with this priority, meaning no action needs to be taken when clearing it
field.Remove(thisMsg);
PersistentMessage highestRemainingMsg = null;
foreach (var persMsg in field) {
if (highestRemainingMsg == null || persMsg.priority > highestRemainingMsg.priority) { highestRemainingMsg = persMsg; }
}
if (highestRemainingMsg == null) { p.Session.SendMessage(type, ""); return true; } //No messages remain, clear field and quit
p.Session.SendMessage(type, highestRemainingMsg.message); //reveal highest remaining message
return true;
}
if (thisMsg == null) { thisMsg = new PersistentMessage(message, priority); field.Add(thisMsg); } else { thisMsg.message = message; }
foreach (var persMsg in field) {
if (persMsg.priority > priority) { return true; } //If any other message in this field has higher priority, do not send to client
}
p.Session.SendMessage(type, message);
return true;
}
static bool IsPersistent(CpeMessageType type) {
return type == CpeMessageType.Status1 || type == CpeMessageType.Status2 || type == CpeMessageType.Status3 ||
type == CpeMessageType.BottomRight1 || type == CpeMessageType.BottomRight2 || type == CpeMessageType.BottomRight3;
}
}
}
}

View File

@ -56,13 +56,14 @@ namespace MCGalaxy
}
}
public void SendCpeMessage(CpeMessageType type, string message) {
public void SendCpeMessage(CpeMessageType type, string message, Chat.PersistentMessage.Priority priority = Chat.PersistentMessage.Priority.Default) {
if (type != CpeMessageType.Normal && !Supports(CpeExt.MessageTypes)) {
if (type == CpeMessageType.Announcement) type = CpeMessageType.Normal;
if (type >= CpeMessageType.Announcement) type = CpeMessageType.Normal;
else return;
}
message = Chat.Format(message, this);
if (Chat.PersistentMessage.Handle(this, type, message, priority)) { return; }
Session.SendMessage(type, message);
}

View File

@ -31,6 +31,7 @@ namespace MCGalaxy {
public PlayerIgnores Ignores = new PlayerIgnores();
public static string lastMSG = "";
internal Dictionary<CpeMessageType, List<Chat.PersistentMessage>> fields = new Dictionary<CpeMessageType, List<Chat.PersistentMessage>>();
public Zone ZoneIn;
//TpA