public static void SendMessageInMultiplayer <TMessage>(this IMultiplayerHelper self, Func <TMessage> message, string type, string[]?modIDs = null, long[]?playerIDs = null)
     where TMessage : notnull
 {
     if (GameExt.GetMultiplayerMode() != MultiplayerMode.SinglePlayer)
     {
         self.SendMessage(message(), type, modIDs, playerIDs);
     }
 }
 private void OnSaving(object?sender, SavingEventArgs e)
 {
     if (GameExt.GetMultiplayerMode() != MultiplayerMode.Client)
     {
         var serializedMails = new List <MailData <string> >(Mails.Select(m => m.Serialize()));
         Helper.Data.WriteSaveData(PersistedMailsSaveDataKey, serializedMails);
     }
     UpdateMails();
 }
 private void OnSaveLoaded(object?sender, SaveLoadedEventArgs e)
 {
     if (GameExt.GetMultiplayerMode() != MultiplayerMode.Client)
     {
         Mails.Clear();
         var serializedMails = Helper.Data.ReadSaveData <List <MailData <string> > >(PersistedMailsSaveDataKey) ?? new List <MailData <string> >();
         foreach (var serializedMail in serializedMails)
         {
             Mails.Add(serializedMail.Deserialize());
         }
     }
     UpdateMails();
 }
        private void OnPeerConnected(object?sender, PeerConnectedEventArgs e)
        {
            if (GameExt.GetMultiplayerMode() != MultiplayerMode.Server)
            {
                return;
            }
            if (e.Peer.GetMod(ModManifest.UniqueID) is null)
            {
                return;
            }

            Helper.Multiplayer.SendMessage(
                Mails.Select(m => m.Serialize()).ToList(),
                MailListMessage,
                new[] { ModManifest.UniqueID },
                new[] { e.Peer.PlayerID }
                );
        }
        private void OnModMessageReceived(object?sender, ModMessageReceivedEventArgs e)
        {
            if (e.FromModID != ModManifest.UniqueID)
            {
                return;
            }

            if (e.Type == MailListMessage)
            {
                Mails.Clear();
                var serializedMails = e.ReadAs <List <MailData <string> > >();
                foreach (var serializedMail in serializedMails)
                {
                    Mails.Add(serializedMail.Deserialize());
                }
                UpdateMails();
            }
            else if (e.Type == NewMailMessage)
            {
                var serializedMail = e.ReadAs <MailData <string> >();
                Mails.Add(serializedMail.Deserialize());

                if (GameExt.GetMultiplayerMode() == MultiplayerMode.Server)
                {
                    Helper.Multiplayer.SendMessage(
                        serializedMail,
                        NewMailMessage,
                        new[] { ModManifest.UniqueID },
                        Game1.getOnlineFarmers()
                        .Select(p => p.UniqueMultiplayerID)
                        .Where(id => id != GameExt.GetHostPlayer().UniqueMultiplayerID&& id != e.FromPlayerID)
                        .ToArray()
                        );
                }

                if (serializedMail.AddresseeID == Game1.player.UniqueMultiplayerID)
                {
                    UpdateMails();
                }
            }
            else if (e.Type == ReadMailMessage)
            {
                var(modUniqueID, mailID) = e.ReadAs <(string, string)>();
                var mail = Mails.FirstOrDefault(m => m.ModUniqueID == modUniqueID && m.ID == mailID);
                if (mail is not null && mail.Title is null)
                {
                    Mails.Remove(mail);

                    if (GameExt.GetMultiplayerMode() == MultiplayerMode.Server)
                    {
                        Helper.Multiplayer.SendMessage(
                            (modUniqueID, mailID),
                            ReadMailMessage,
                            new[] { ModManifest.UniqueID },
                            Game1.getOnlineFarmers()
                            .Select(p => p.UniqueMultiplayerID)
                            .Where(id => id != GameExt.GetHostPlayer().UniqueMultiplayerID&& id != e.FromPlayerID)
                            .ToArray()
                            );
                    }

                    if (mail.AddresseeID == Game1.player.UniqueMultiplayerID)
                    {
                        UpdateMails();
                    }
                }
            }
            else
            {
                Monitor.Log($"Received unknown message of type {e.Type}.", LogLevel.Trace);
            }
        }