Got items to be loaded back into player's inventory upon loading a save file.

This commit is contained in:
JoshuaNavarro 2019-08-21 20:17:27 -07:00
parent c7770cb8d7
commit 9d3be07daa
2 changed files with 114 additions and 70 deletions

View File

@ -5,6 +5,8 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using Revitalize.Framework.Objects;
using Revitalize.Framework.Objects.Furniture;
using Revitalize.Framework.Utilities.Serialization.ContractResolvers; using Revitalize.Framework.Utilities.Serialization.ContractResolvers;
using StardewValley; using StardewValley;
using StardewValley.Objects; using StardewValley.Objects;
@ -119,7 +121,111 @@ namespace Revitalize.Framework.Utilities
public void afterLoad() public void afterLoad()
{ {
this.deleteAllUnusedFiles(); this.deleteAllUnusedFiles();
this.removeNullObjects(); //this.removeNullObjects();
this.restoreModObjects();
}
public void restoreModObjects()
{
foreach (var v in ModCore.ObjectGroups)
{
foreach (var obj in v.Value.objects.Values)
{
(obj as CustomObject).replaceAfterLoad();
}
}
foreach (GameLocation loc in Game1.locations)
{
foreach (StardewValley.Object c in loc.Objects.Values)
{
if (c is Chest)
{
List<Item> toRemove = new List<Item>();
List<Item> toAdd = new List<Item>();
foreach (Item o in (c as Chest).items)
{
if (o == null) continue;
if (o is Chest && o.Name != "Chest")
{
ModCore.log("Found a custom object in a chest!");
string jsonString = o.Name;
string guidName = jsonString.Split(new string[] { "GUID=" }, StringSplitOptions.None)[1];
ModCore.log(jsonString);
string type = jsonString.Split('|')[2];
Item I = (Item)ModCore.Serializer.DeserializeGUID(guidName, Type.GetType(type));
if (I is MultiTiledObject)
{
(I as MultiTiledObject).recreate();
}
toAdd.Add(I);
toRemove.Add(o);
//Item i = Serializer.DeserializeFromJSONString<Item>(jsonString);
//ModCore.log("Deserialized item is: "+i.Name);
}
}
foreach (Item i in toRemove)
{
(c as Chest).items.Remove(i);
}
foreach (Item I in toAdd)
{
(c as Chest).items.Add(I);
}
}
else if (c is StorageFurnitureTile)
{
foreach (Item o in (c as StorageFurnitureTile).info.inventory.items)
{
if (o is Chest && o.Name != "Chest")
{
ModCore.log("Found a custom object in a chest!");
}
}
}
}
}
List<Item> toAdd2 = new List<Item>();
List<Item> toRemove2 = new List<Item>();
foreach (Item I in Game1.player.Items)
{
if (I == null) continue;
else
{
if (I is Chest && I.Name != "Chest")
{
ModCore.log("Found a custom object in a chest!");
string jsonString = I.Name;
string guidName = jsonString.Split(new string[] { "GUID=" }, StringSplitOptions.None)[1];
ModCore.log(jsonString);
string type = jsonString.Split('|')[2];
Item ret = (Item)ModCore.Serializer.DeserializeGUID(guidName, Type.GetType(type));
if (ret is MultiTiledObject)
{
(ret as MultiTiledObject).recreate();
}
toAdd2.Add(ret);
toRemove2.Add(I);
//Item i = Serializer.DeserializeFromJSONString<Item>(jsonString);
//ModCore.log("Deserialized item is: "+i.Name);
}
}
}
foreach (Item i in toRemove2)
{
Game1.player.Items.Remove(i);
}
foreach (Item I in toAdd2)
{
Game1.player.addItemToInventory(I);
}
} }
public void returnToTitle() public void returnToTitle()

View File

@ -380,74 +380,12 @@ namespace Revitalize
{ {
this.loadContent(); this.loadContent();
Serializer.afterLoad();
if (Game1.IsServer || Game1.IsMultiplayer || Game1.IsClient) if (Game1.IsServer || Game1.IsMultiplayer || Game1.IsClient)
{ {
throw new Exception("Can't run Revitalize in multiplayer due to lack of current support!"); throw new Exception("Can't run Revitalize in multiplayer due to lack of current support!");
} }
Serializer.afterLoad();
foreach(var v in ObjectGroups)
{
foreach(var obj in v.Value.objects.Values)
{
(obj as CustomObject).replaceAfterLoad();
}
}
foreach(GameLocation loc in Game1.locations)
{
foreach(StardewValley.Object c in loc.Objects.Values)
{
if (c is Chest)
{
List<Item> toRemove=new List<Item>();
List<Item> toAdd=new List<Item>();
foreach (Item o in (c as Chest).items)
{
if (o is Chest && o.Name != "Chest")
{
ModCore.log("Found a custom object in a chest!");
string jsonString = o.Name;
string guidName = jsonString.Split(new string[] { "GUID=" },StringSplitOptions.None)[1];
ModCore.log(jsonString);
string type = jsonString.Split('|')[2];
Item I=(Item)Serializer.DeserializeGUID(guidName, Type.GetType(type));
if(I is MultiTiledObject)
{
(I as MultiTiledObject).recreate();
}
toAdd.Add(I);
toRemove.Add(o);
//Item i = Serializer.DeserializeFromJSONString<Item>(jsonString);
//ModCore.log("Deserialized item is: "+i.Name);
}
}
foreach(Item i in toRemove)
{
(c as Chest).items.Remove(i);
}
foreach(Item I in toAdd)
{
(c as Chest).items.Add(I);
}
}
else if(c is StorageFurnitureTile)
{
foreach (Item o in (c as StorageFurnitureTile).info.inventory.items)
{
if (o is Chest&& o.Name != "Chest")
{
ModCore.log("Found a custom object in a chest!");
}
}
}
}
}
// Game1.player.addItemToInventory(GetObjectFromPool("Omegasis.BigTiledTest")); // Game1.player.addItemToInventory(GetObjectFromPool("Omegasis.BigTiledTest"));
Game1.player.addItemToInventory(ObjectManager.getChair("Omegasis.Revitalize.Furniture.Chairs.OakChair")); Game1.player.addItemToInventory(ObjectManager.getChair("Omegasis.Revitalize.Furniture.Chairs.OakChair"));