diff --git a/DeltaVFactionQuestLog/Data/Scripts/PersistentFactionObjectives.cs b/DeltaVFactionQuestLog/Data/Scripts/PersistentFactionObjectives.cs index cd6c343..c6fd6fe 100644 --- a/DeltaVFactionQuestLog/Data/Scripts/PersistentFactionObjectives.cs +++ b/DeltaVFactionQuestLog/Data/Scripts/PersistentFactionObjectives.cs @@ -6,365 +6,396 @@ using System; using VRage.Game; -[MySessionComponentDescriptor(MyUpdateOrder.AfterSimulation)] -public class PersistentFactionObjectives : MySessionComponentBase +namespace DeltaVQuestLog { - private const string FileName = "FactionObjectives.txt"; - private Dictionary> factionObjectives = new Dictionary>(); - private HashSet notificationsDisabled = new HashSet(); - private bool isServer; - private Dictionary questLogHideTimes = new Dictionary(); - private Dictionary questLogCountdowns = new Dictionary(); - - - public override void LoadData() + [MySessionComponentDescriptor(MyUpdateOrder.AfterSimulation)] + public class PersistentFactionObjectives : MySessionComponentBase { - isServer = MyAPIGateway.Multiplayer.IsServer; // True for server instances (single-player, listen server host, dedicated server) - bool isDedicated = MyAPIGateway.Utilities.IsDedicated; // True only for dedicated servers + private const string FileName = "FactionObjectives.txt"; - if (isServer && !isDedicated) - { - // Single-player or listen server host - LoadObjectives(); - } - else if (isDedicated) - { - // Dedicated server - LoadObjectives(); - } + private Dictionary> factionObjectives = new Dictionary>(); + private HashSet notificationsDisabled = new HashSet(); + private bool isServer; + private Dictionary questLogHideTimes = new Dictionary(); + private Dictionary questLogCountdowns = new Dictionary(); + private const ushort QuestLogMessageId = 16852; // Choose a unique ID - // Register chat commands for all instances - MyAPIGateway.Utilities.MessageEntered += OnMessageEntered; - // Register player connect events on the server side (for dedicated and listen servers) - if (isServer) + public override void LoadData() { - MyVisualScriptLogicProvider.PlayerConnected += OnPlayerConnected; - } - } + isServer = MyAPIGateway.Multiplayer.IsServer; // True for server instances (single-player, listen server host, dedicated server) + bool isDedicated = MyAPIGateway.Utilities.IsDedicated; // True only for dedicated servers - protected override void UnloadData() - { - MyAPIGateway.Utilities.MessageEntered -= OnMessageEntered; + if (isServer && !isDedicated) + { + // Single-player or listen server host + LoadObjectives(); + } + else if (isDedicated) + { + // Dedicated server + LoadObjectives(); + } - if (isServer) - { - SaveObjectives(); - MyVisualScriptLogicProvider.PlayerConnected -= OnPlayerConnected; - } - } + // Register chat commands for all instances + MyAPIGateway.Utilities.MessageEntered += OnMessageEntered; - public override void UpdateAfterSimulation() - { - if (!isServer) return; + // Register player connect events on the server side (for dedicated and listen servers) + if (isServer) + { + MyVisualScriptLogicProvider.PlayerConnected += OnPlayerConnected; + } + MyAPIGateway.Multiplayer.RegisterMessageHandler(QuestLogMessageId, OnQuestLogMessageReceived); + MyAPIGateway.Utilities.MessageEntered += OnMessageEntered; - var now = DateTime.UtcNow; - var playersToRemove = new List(); + } - foreach (var entry in questLogHideTimes) + protected override void UnloadData() { - if (entry.Value <= now) + MyAPIGateway.Utilities.MessageEntered -= OnMessageEntered; + MyAPIGateway.Multiplayer.UnregisterMessageHandler(QuestLogMessageId, OnQuestLogMessageReceived); + + if (isServer) { - MyVisualScriptLogicProvider.SetQuestlog(false, "", entry.Key); - playersToRemove.Add(entry.Key); + SaveObjectives(); + MyVisualScriptLogicProvider.PlayerConnected -= OnPlayerConnected; } } - foreach (var playerId in playersToRemove) + public override void UpdateAfterSimulation() { - questLogHideTimes.Remove(playerId); - } - } + if (!isServer) return; - private void OnPlayerConnected(long playerId) - { - if (!isServer) return; + var now = DateTime.UtcNow; + var playersToRemove = new List(); - var playerFaction = MyAPIGateway.Session.Factions.TryGetPlayerFaction(playerId); - if (playerFaction == null) return; + foreach (var entry in questLogHideTimes) + { + if (entry.Value <= now) + { + MyVisualScriptLogicProvider.SetQuestlog(false, "", entry.Key); + playersToRemove.Add(entry.Key); + } + } - var factionId = playerFaction.FactionId; - ShowQuestLogForPlayer(factionId, playerId, 30); - } + foreach (var playerId in playersToRemove) + { + questLogHideTimes.Remove(playerId); + } + } - private void OnMessageEntered(string messageText, ref bool sendToOthers) - { - if (!messageText.StartsWith("/obj") && !messageText.StartsWith("/objective")) return; + private void OnPlayerConnected(long playerId) + { + if (!isServer) return; - sendToOthers = false; + var playerFaction = MyAPIGateway.Session.Factions.TryGetPlayerFaction(playerId); + if (playerFaction == null) return; - var args = messageText.Split(' '); - if (args.Length < 2) - { - MyAPIGateway.Utilities.ShowMessage("Objectives", "Invalid command. Use '/obj help' for a list of valid commands."); - return; + var factionId = playerFaction.FactionId; + ShowQuestLogForPlayer(factionId, playerId, 30); } - var playerId = MyAPIGateway.Session.Player.IdentityId; - var playerFaction = MyAPIGateway.Session.Factions.TryGetPlayerFaction(playerId); - - if (args[1].ToLower() == "help") + private void OnQuestLogMessageReceived(byte[] data) { - ShowHelp(); - return; + try + { + var message = MyAPIGateway.Utilities.SerializeFromBinary(data); + if (message == null) return; + + if (isServer) + { + // Handle server-side logic + } + else + { + // Handle client-side display + HandleClientQuestLogDisplay(message); + } + } + catch (Exception e) + { + MyLog.Default.WriteLineAndConsole($"Error processing quest log message: {e}"); + } } - if (playerFaction == null && args[1].ToLower() != "notifications") + private void OnMessageEntered(string messageText, ref bool sendToOthers) { - MyAPIGateway.Utilities.ShowMessage("Objectives", "You are not in a faction."); - return; - } + if (!messageText.StartsWith("/obj") && !messageText.StartsWith("/objective")) return; - var factionId = playerFaction?.FactionId ?? -1; + sendToOthers = false; - switch (args[1].ToLower()) - { - case "add": - HandleAddObjective(args, factionId, playerId); - break; - case "list": - HandleListObjectives(factionId); - break; - case "show": - HandleShowObjectives(factionId, playerId); - break; - case "remove": - HandleRemoveObjective(args, factionId, playerId); - break; - case "broadcast": - HandleBroadcast(args, factionId); - break; - case "hide": - HandleHideQuestLog(playerId); - break; - case "notifications": - HandleNotifications(args, playerId); - break; - case "clear": - HandleClearObjectives(factionId, playerId); - break; - default: - MyAPIGateway.Utilities.ShowMessage("Objectives", $"Invalid command '{args[1]}'. Use '/obj help' for a list of valid commands."); - break; - } - } + var args = messageText.Split(' '); + if (args.Length < 2) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "Invalid command. Use '/obj help' for a list of valid commands."); + return; + } - private void HandleAddObjective(string[] args, long factionId, long playerId) - { - if (args.Length < 3) - { - MyAPIGateway.Utilities.ShowMessage("Objectives", "Please provide an objective description."); - return; - } + var playerId = MyAPIGateway.Session.Player.IdentityId; + var playerFaction = MyAPIGateway.Session.Factions.TryGetPlayerFaction(playerId); - var objectiveText = string.Join(" ", args, 2, args.Length - 2); + if (args[1].ToLower() == "help") + { + ShowHelp(); + return; + } - if (!IsFactionLeaderOrFounder(factionId, playerId)) - { - MyAPIGateway.Utilities.ShowMessage("Objectives", "Only faction leaders or founders can add objectives."); - return; - } + if (playerFaction == null && args[1].ToLower() != "notifications") + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "You are not in a faction."); + return; + } - if (!factionObjectives.ContainsKey(factionId)) - { - factionObjectives[factionId] = new List(); + var factionId = playerFaction?.FactionId ?? -1; + + switch (args[1].ToLower()) + { + case "add": + HandleAddObjective(args, factionId, playerId); + break; + case "list": + HandleListObjectives(factionId); + break; + case "show": + HandleShowObjectives(factionId, playerId); + break; + case "remove": + HandleRemoveObjective(args, factionId, playerId); + break; + case "broadcast": + HandleBroadcast(args, factionId); + break; + case "hide": + HandleHideQuestLog(playerId); + break; + case "notifications": + HandleNotifications(args, playerId); + break; + case "clear": + HandleClearObjectives(factionId, playerId); + break; + default: + MyAPIGateway.Utilities.ShowMessage("Objectives", $"Invalid command '{args[1]}'. Use '/obj help' for a list of valid commands."); + break; + } } - if (!factionObjectives[factionId].Contains(objectiveText)) // Avoid duplicates + private void HandleAddObjective(string[] args, long factionId, long playerId) { - factionObjectives[factionId].Add(objectiveText); - SaveObjectives(); + if (args.Length < 3) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "Please provide an objective description."); + return; + } - // Get the player's name - string playerName = MyAPIGateway.Session.Player?.DisplayName ?? "Unknown"; + var objectiveText = string.Join(" ", args, 2, args.Length - 2); - // Show updated quest log to all faction members - ShowQuestLogToFaction(factionId, 10, $"Faction Objectives [Added by {playerName}: {objectiveText}]"); + if (!IsFactionLeaderOrFounder(factionId, playerId)) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "Only faction leaders or founders can add objectives."); + return; + } - MyAPIGateway.Utilities.ShowMessage("Objectives", $"{playerName} added objective: {objectiveText}"); - } - else - { - MyAPIGateway.Utilities.ShowMessage("Objectives", "This objective already exists."); - } - } + if (!factionObjectives.ContainsKey(factionId)) + { + factionObjectives[factionId] = new List(); + } - private void HandleListObjectives(long factionId) - { - if (!factionObjectives.ContainsKey(factionId) || factionObjectives[factionId] == null) - { - MyAPIGateway.Utilities.ShowMessage("Objectives", "No objectives found."); - return; - } + if (!factionObjectives[factionId].Contains(objectiveText)) // Avoid duplicates + { + factionObjectives[factionId].Add(objectiveText); + SaveObjectives(); - var objectives = factionObjectives[factionId]; - if (objectives.Count == 0) - { - MyAPIGateway.Utilities.ShowMessage("Objectives", "No objectives found."); - return; - } + // Get the player's name + string playerName = MyAPIGateway.Session.Player?.DisplayName ?? "Unknown"; - MyAPIGateway.Utilities.ShowMessage("Objectives", "Faction Objectives:"); - for (int i = 0; i < objectives.Count; i++) - { - MyAPIGateway.Utilities.ShowMessage("Objectives", $"{i + 1}. {objectives[i]}"); + // Show updated quest log to all faction members + ShowQuestLogToFaction(factionId, 10, $"Faction Objectives [Added by {playerName}: {objectiveText}]"); + + MyAPIGateway.Utilities.ShowMessage("Objectives", $"{playerName} added objective: {objectiveText}"); + } + else + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "This objective already exists."); + } } - } - private void HandleShowObjectives(long factionId, long playerId) - { - if (!factionObjectives.ContainsKey(factionId) || factionObjectives[factionId].Count == 0) + private void HandleListObjectives(long factionId) { - MyVisualScriptLogicProvider.SetQuestlog(false, "", playerId); - MyAPIGateway.Utilities.ShowMessage("Objectives", "No objectives to display."); - return; - } + if (!factionObjectives.ContainsKey(factionId) || factionObjectives[factionId] == null) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "No objectives found."); + return; + } - MyVisualScriptLogicProvider.SetQuestlog(true, "Faction Objectives", playerId); - MyVisualScriptLogicProvider.RemoveQuestlogDetails(playerId); + var objectives = factionObjectives[factionId]; + if (objectives.Count == 0) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "No objectives found."); + return; + } - foreach (var objective in factionObjectives[factionId]) - { - MyVisualScriptLogicProvider.AddQuestlogObjective(objective, false, true, playerId); + MyAPIGateway.Utilities.ShowMessage("Objectives", "Faction Objectives:"); + for (int i = 0; i < objectives.Count; i++) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", $"{i + 1}. {objectives[i]}"); + } } - } - private void HandleRemoveObjective(string[] args, long factionId, long playerId) - { - int index; - if (args.Length < 3 || !int.TryParse(args[2], out index)) + private void HandleShowObjectives(long factionId, long playerId) { - MyAPIGateway.Utilities.ShowMessage("Objectives", "Please provide a valid objective index to remove."); - return; - } + if (!factionObjectives.ContainsKey(factionId) || factionObjectives[factionId].Count == 0) + { + MyVisualScriptLogicProvider.SetQuestlog(false, "", playerId); + MyAPIGateway.Utilities.ShowMessage("Objectives", "No objectives to display."); + return; + } - if (!factionObjectives.ContainsKey(factionId) || index < 1 || index > factionObjectives[factionId].Count) - { - MyAPIGateway.Utilities.ShowMessage("Objectives", "Invalid objective index."); - return; + MyVisualScriptLogicProvider.SetQuestlog(true, "Faction Objectives", playerId); + MyVisualScriptLogicProvider.RemoveQuestlogDetails(playerId); + + foreach (var objective in factionObjectives[factionId]) + { + MyVisualScriptLogicProvider.AddQuestlogObjective(objective, false, true, playerId); + } } - if (!IsFactionLeaderOrFounder(factionId, playerId)) + private void HandleRemoveObjective(string[] args, long factionId, long playerId) { - MyAPIGateway.Utilities.ShowMessage("Objectives", "Only faction leaders or founders can remove objectives."); - return; - } + int index; + if (args.Length < 3 || !int.TryParse(args[2], out index)) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "Please provide a valid objective index to remove."); + return; + } - var removedObjective = factionObjectives[factionId][index - 1]; - factionObjectives[factionId].RemoveAt(index - 1); - SaveObjectives(); + if (!factionObjectives.ContainsKey(factionId) || index < 1 || index > factionObjectives[factionId].Count) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "Invalid objective index."); + return; + } - // Get the player's name - string playerName = MyAPIGateway.Session.Player?.DisplayName ?? "Unknown"; + if (!IsFactionLeaderOrFounder(factionId, playerId)) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "Only faction leaders or founders can remove objectives."); + return; + } - // Show updated quest log to all faction members - ShowQuestLogToFaction(factionId, 10, $"Faction Objectives [Removed by {playerName}: {removedObjective}]"); + var removedObjective = factionObjectives[factionId][index - 1]; + factionObjectives[factionId].RemoveAt(index - 1); + SaveObjectives(); - MyAPIGateway.Utilities.ShowMessage("Objectives", $"{playerName} removed objective: {removedObjective}"); - } + // Get the player's name + string playerName = MyAPIGateway.Session.Player?.DisplayName ?? "Unknown"; - private void HandleBroadcast(string[] args, long factionId) - { - int duration; - if (args.Length < 3 || !int.TryParse(args[2], out duration)) - { - MyAPIGateway.Utilities.ShowMessage("Objectives", "Usage: /obj broadcast "); - return; + // Show updated quest log to all faction members + ShowQuestLogToFaction(factionId, 10, $"Faction Objectives [Removed by {playerName}: {removedObjective}]"); + + MyAPIGateway.Utilities.ShowMessage("Objectives", $"{playerName} removed objective: {removedObjective}"); } - if (!factionObjectives.ContainsKey(factionId) || factionObjectives[factionId].Count == 0) + private void HandleBroadcast(string[] args, long factionId) { - MyAPIGateway.Utilities.ShowMessage("Objectives", "No objectives to broadcast."); - return; - } + int duration; + if (args.Length < 3 || !int.TryParse(args[2], out duration)) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "Usage: /obj broadcast "); + return; + } - var playerName = MyAPIGateway.Session.Player?.DisplayName ?? "Unknown"; - string customTitle = $"Faction Objectives [Broadcasted by {playerName}, {duration}s]"; + if (!factionObjectives.ContainsKey(factionId) || factionObjectives[factionId].Count == 0) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "No objectives to broadcast."); + return; + } - // Broadcast to faction members - ShowQuestLogToFaction(factionId, duration, customTitle); + var playerName = MyAPIGateway.Session.Player?.DisplayName ?? "Unknown"; + string customTitle = $"Faction Objectives [Broadcasted by {playerName}, {duration}s]"; - MyAPIGateway.Utilities.ShowMessage("Objectives", $"Broadcasting objectives for {duration} seconds by {playerName}."); - } + // Broadcast to faction members + ShowQuestLogToFaction(factionId, duration, customTitle); - private void HandleHideQuestLog(long playerId) - { - MyVisualScriptLogicProvider.SetQuestlog(false, "", playerId); - MyAPIGateway.Utilities.ShowMessage("Objectives", "Quest log hidden."); - } + MyAPIGateway.Utilities.ShowMessage("Objectives", $"Broadcasting objectives for {duration} seconds by {playerName}."); + } - private void HandleNotifications(string[] args, long playerId) - { - if (args.Length < 3) + private void HandleHideQuestLog(long playerId) { - MyAPIGateway.Utilities.ShowMessage("Objectives", "Usage: /obj notifications "); - return; + MyVisualScriptLogicProvider.SetQuestlog(false, "", playerId); + MyAPIGateway.Utilities.ShowMessage("Objectives", "Quest log hidden."); } - string option = args[2].ToLower(); - if (option == "off") + private void HandleNotifications(string[] args, long playerId) { - if (notificationsDisabled.Contains(playerId)) + if (args.Length < 3) { - MyAPIGateway.Utilities.ShowMessage("Objectives", "Notifications are already turned off."); + MyAPIGateway.Utilities.ShowMessage("Objectives", "Usage: /obj notifications "); + return; } - else + + string option = args[2].ToLower(); + if (option == "off") { - notificationsDisabled.Add(playerId); - MyAPIGateway.Utilities.ShowMessage("Objectives", "Notifications turned off."); + if (notificationsDisabled.Contains(playerId)) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "Notifications are already turned off."); + } + else + { + notificationsDisabled.Add(playerId); + MyAPIGateway.Utilities.ShowMessage("Objectives", "Notifications turned off."); + } } - } - else if (option == "on") - { - if (!notificationsDisabled.Contains(playerId)) + else if (option == "on") { - MyAPIGateway.Utilities.ShowMessage("Objectives", "Notifications are already turned on."); + if (!notificationsDisabled.Contains(playerId)) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "Notifications are already turned on."); + } + else + { + notificationsDisabled.Remove(playerId); + MyAPIGateway.Utilities.ShowMessage("Objectives", "Notifications turned on."); + } } else { - notificationsDisabled.Remove(playerId); - MyAPIGateway.Utilities.ShowMessage("Objectives", "Notifications turned on."); + MyAPIGateway.Utilities.ShowMessage("Objectives", "Invalid option. Usage: /obj notifications "); } } - else - { - MyAPIGateway.Utilities.ShowMessage("Objectives", "Invalid option. Usage: /obj notifications "); - } - } - private void HandleClearObjectives(long factionId, long playerId) - { - if (!IsFactionLeaderOrFounder(factionId, playerId)) + private void HandleClearObjectives(long factionId, long playerId) { - MyAPIGateway.Utilities.ShowMessage("Objectives", "Only faction leaders or founders can clear all objectives."); - return; - } + if (!IsFactionLeaderOrFounder(factionId, playerId)) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "Only faction leaders or founders can clear all objectives."); + return; + } - if (!factionObjectives.ContainsKey(factionId) || factionObjectives[factionId].Count == 0) - { - MyAPIGateway.Utilities.ShowMessage("Objectives", "No objectives to clear."); - return; - } + if (!factionObjectives.ContainsKey(factionId) || factionObjectives[factionId].Count == 0) + { + MyAPIGateway.Utilities.ShowMessage("Objectives", "No objectives to clear."); + return; + } - // Clear all objectives for the faction - factionObjectives[factionId].Clear(); - SaveObjectives(); + // Clear all objectives for the faction + factionObjectives[factionId].Clear(); + SaveObjectives(); - // Get the player's name - string playerName = MyAPIGateway.Session.Player?.DisplayName ?? "Unknown"; + // Get the player's name + string playerName = MyAPIGateway.Session.Player?.DisplayName ?? "Unknown"; - // Notify all faction members about the cleared objectives - ShowQuestLogToFaction(factionId, 10, $"[Cleared by {playerName}]"); + // Notify all faction members about the cleared objectives + ShowQuestLogToFaction(factionId, 10, $"[Cleared by {playerName}]"); - MyAPIGateway.Utilities.ShowMessage("Objectives", $"{playerName} cleared all objectives."); - } + MyAPIGateway.Utilities.ShowMessage("Objectives", $"{playerName} cleared all objectives."); + } - private void ShowHelp() - { - string title = "PersistentFactionObjectives"; - string currentobjprefix = "Available Commands"; - string body = @" + private void ShowHelp() + { + string title = "PersistentFactionObjectives"; + string currentobjprefix = "Available Commands"; + string body = @" /obj add - Add a new objective (leaders only) /obj list - List all objectives /obj show - Show the quest log locally @@ -376,114 +407,147 @@ private void ShowHelp() /obj clear - Clear all objectives (leaders only) /obj help - Show this help message "; - string currentobj = ""; + string currentobj = ""; - MyAPIGateway.Utilities.ShowMissionScreen(title, currentobjprefix, currentobj, body); - } - - private void ShowQuestLogForPlayer(long factionId, long playerId, int duration, string customTitle = "Faction Objectives") - { - if (!factionObjectives.ContainsKey(factionId)) return; + MyAPIGateway.Utilities.ShowMissionScreen(title, currentobjprefix, currentobj, body); + } - var objectives = factionObjectives[factionId]; + private void ShowQuestLogForPlayer(long factionId, long playerId, int duration, string customTitle = "Faction Objectives") + { + if (!factionObjectives.ContainsKey(factionId)) return; - // Show the quest log with the custom title - MyVisualScriptLogicProvider.SetQuestlog(true, customTitle, playerId); - MyVisualScriptLogicProvider.RemoveQuestlogDetails(playerId); + if (isServer) + { + try + { + var message = new QuestLogMessage + { + FactionId = factionId, + Objectives = factionObjectives[factionId], + Title = customTitle, + Duration = duration + }; + var data = MyAPIGateway.Utilities.SerializeToBinary(message); + MyAPIGateway.Multiplayer.SendMessageTo(QuestLogMessageId, data, (ulong)playerId); + } + catch (Exception e) + { + MyLog.Default.WriteLineAndConsole($"Error sending quest log message: {e}"); + } + } + else + { + DisplayQuestLog(factionObjectives[factionId], customTitle, playerId); + } + } - // Add each objective - foreach (var objective in objectives) + private void DisplayQuestLog(List objectives, string title, long playerId) { - MyVisualScriptLogicProvider.AddQuestlogObjective(objective, false, true, playerId); + MyVisualScriptLogicProvider.SetQuestlog(true, title, playerId); + MyVisualScriptLogicProvider.RemoveQuestlogDetails(playerId); + foreach (var objective in objectives) + { + MyVisualScriptLogicProvider.AddQuestlogObjective(objective, false, true, playerId); + } } - // Set a timer to hide the quest log - questLogHideTimes[playerId] = DateTime.UtcNow.AddSeconds(duration); - } + private void HandleClientQuestLogDisplay(QuestLogMessage message) + { + if (MyAPIGateway.Session.Player == null) return; + var playerId = MyAPIGateway.Session.Player.IdentityId; + DisplayQuestLog(message.Objectives, message.Title, playerId); - private void ShowQuestLogToFaction(long factionId, int duration, string customTitle = "Faction Objectives") - { - var faction = MyAPIGateway.Session.Factions.TryGetFactionById(factionId); - if (faction == null) return; + if (message.Duration > 0) + { + questLogHideTimes[playerId] = DateTime.UtcNow.AddSeconds(message.Duration); + } + } - foreach (var memberId in faction.Members.Keys) + private void ShowQuestLogToFaction(long factionId, int duration, string customTitle = "Faction Objectives") { - if (notificationsDisabled.Contains(memberId)) continue; + var faction = MyAPIGateway.Session.Factions.TryGetFactionById(factionId); + if (faction == null) return; - ShowQuestLogForPlayer(factionId, memberId, duration, customTitle); - } - } + foreach (var memberId in faction.Members.Keys) + { + if (notificationsDisabled.Contains(memberId)) continue; - private bool IsFactionLeaderOrFounder(long factionId, long playerId) - { - var faction = MyAPIGateway.Session.Factions.TryGetFactionById(factionId); - if (faction == null) - { - MyLog.Default.WriteLineAndConsole($"Faction not found for ID {factionId}"); - return false; + ShowQuestLogForPlayer(factionId, memberId, duration, customTitle); + } } - MyFactionMember member; - if (faction.Members.TryGetValue(playerId, out member)) + private bool IsFactionLeaderOrFounder(long factionId, long playerId) { - return member.IsLeader || member.IsFounder; + var faction = MyAPIGateway.Session.Factions.TryGetFactionById(factionId); + if (faction == null) + { + MyLog.Default.WriteLineAndConsole($"Faction not found for ID {factionId}"); + return false; + } + + MyFactionMember member; + if (faction.Members.TryGetValue(playerId, out member)) + { + return member.IsLeader || member.IsFounder; + } + + MyLog.Default.WriteLineAndConsole($"Player {playerId} is not a member of faction {factionId}"); + return false; } - MyLog.Default.WriteLineAndConsole($"Player {playerId} is not a member of faction {factionId}"); - return false; - } - - private void SaveObjectives() - { - try + private void SaveObjectives() { - using (var writer = MyAPIGateway.Utilities.WriteFileInWorldStorage(FileName, typeof(PersistentFactionObjectives))) + try { - foreach (var kvp in factionObjectives) + using (var writer = MyAPIGateway.Utilities.WriteFileInWorldStorage(FileName, typeof(PersistentFactionObjectives))) { - foreach (var obj in kvp.Value) + foreach (var kvp in factionObjectives) { - writer.WriteLine($"{kvp.Key}:{obj}"); + foreach (var obj in kvp.Value) + { + writer.WriteLine($"{kvp.Key}:{obj}"); + } } } } + catch (Exception ex) + { + MyLog.Default.WriteLineAndConsole($"Error saving objectives: {ex.Message}"); + } } - catch (Exception ex) - { - MyLog.Default.WriteLineAndConsole($"Error saving objectives: {ex.Message}"); - } - } - private void LoadObjectives() - { - try + private void LoadObjectives() { - if (!MyAPIGateway.Utilities.FileExistsInWorldStorage(FileName, typeof(PersistentFactionObjectives))) - return; - - using (var reader = MyAPIGateway.Utilities.ReadFileInWorldStorage(FileName, typeof(PersistentFactionObjectives))) + try { - string line; - long factionId; - while ((line = reader.ReadLine()) != null) + if (!MyAPIGateway.Utilities.FileExistsInWorldStorage(FileName, typeof(PersistentFactionObjectives))) + return; + + using (var reader = MyAPIGateway.Utilities.ReadFileInWorldStorage(FileName, typeof(PersistentFactionObjectives))) { - var parts = line.Split(new[] { ':' }, 2); - if (parts.Length == 2 && long.TryParse(parts[0], out factionId)) + string line; + long factionId; + while ((line = reader.ReadLine()) != null) { - if (!factionObjectives.ContainsKey(factionId)) - factionObjectives[factionId] = new List(); - - if (!factionObjectives[factionId].Contains(parts[1])) - factionObjectives[factionId].Add(parts[1]); + var parts = line.Split(new[] { ':' }, 2); + if (parts.Length == 2 && long.TryParse(parts[0], out factionId)) + { + if (!factionObjectives.ContainsKey(factionId)) + factionObjectives[factionId] = new List(); + + if (!factionObjectives[factionId].Contains(parts[1])) + factionObjectives[factionId].Add(parts[1]); + } } } } + catch (Exception ex) + { + MyLog.Default.WriteLineAndConsole($"Error loading objectives: {ex.Message}"); + } } - catch (Exception ex) - { - MyLog.Default.WriteLineAndConsole($"Error loading objectives: {ex.Message}"); - } + } } diff --git a/DeltaVFactionQuestLog/Data/Scripts/QuestLogMessage.cs b/DeltaVFactionQuestLog/Data/Scripts/QuestLogMessage.cs new file mode 100644 index 0000000..8c689a0 --- /dev/null +++ b/DeltaVFactionQuestLog/Data/Scripts/QuestLogMessage.cs @@ -0,0 +1,22 @@ +using ProtoBuf; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DeltaVQuestLog +{ + [ProtoContract] + public class QuestLogMessage + { + [ProtoMember(1)] + public long FactionId { get; set; } + [ProtoMember(2)] + public List Objectives { get; set; } + [ProtoMember(3)] + public string Title { get; set; } + [ProtoMember(4)] + public int Duration { get; set; } + } +} \ No newline at end of file