private static void Multiplayer_PeerContextReceived(object sender, StardewModdingAPI.Events.PeerContextReceivedEventArgs e) { // Whenever a new client connects to the host, force the NetStrings that store bag data to get re-loaded on the other clients if (Context.IsMainPlayer) { ItemBag.GetAllBags(false).ForEach(x => x.Resync()); // I have no idea why, but it seems like the items in the connected farmhand's inventory are getting replaced with new instances after I've synchronized the current instances. // So to maintain bags in their inventory, send the data to the peer using IMultiplayerEvents.SendMessage/IMultiplayerEvents.ModMessageReceived. // This will force the connected client to overwrite their inventory with the correct data. Farmer ConnectedFarmer = Game1.getAllFarmers().First(x => x.UniqueMultiplayerID == e.Peer.PlayerID); Dictionary <int, string> PendingSync = new Dictionary <int, string>(); for (int i = 0; i < ConnectedFarmer.Items.Count; i++) { Item Item = ConnectedFarmer.Items[i]; if (Item != null && Item is ItemBag Bag && Bag.TrySerializeToString(out string DataString, out Exception Error)) { PendingSync.Add(i, DataString); } } if (PendingSync.Any()) { Helper.Multiplayer.SendMessage(PendingSync, ForceResyncCommandType, new string[] { ItemBagsMod.ModUniqueId }, new long[] { ConnectedFarmer.UniqueMultiplayerID }); } } }
private void Multiplayer_PeerContextReceived(object sender, StardewModdingAPI.Events.PeerContextReceivedEventArgs e) { if (SerializationManager == null) { return; } if (Game1.eventUp) { SerializationManager.restoreAllModObjects(SerializationManager.trackedObjectList); //Force a restore and then a serialize save to prevent deletions. } //ModMonitor.Log("Got peer context. Serialize/remove all custom objects really quick to prevent loading errors."); SerializationManager.cleanUpInventory(); //SerializationManager.cleanUpWorld(); //SerializationManager.cleanUpStorageContainers(); }