diff --git a/Revitalize/Revitalize/Revitalize/Class1.cs b/Revitalize/Revitalize/Revitalize/Class1.cs index 894d8d4b..39a6337d 100644 --- a/Revitalize/Revitalize/Revitalize/Class1.cs +++ b/Revitalize/Revitalize/Revitalize/Class1.cs @@ -58,7 +58,7 @@ namespace Revitalize StardewModdingAPI.Events.GameEvents.GameLoaded += GameEvents_GameLoaded; StardewModdingAPI.Events.GameEvents.OneSecondTick += MapWipe; StardewModdingAPI.Events.TimeEvents.DayOfMonthChanged += Util.ResetAllDailyBooleans; - + StardewModdingAPI.Events.SaveEvents.AfterLoad += SaveEvents_AfterLoad; StardewModdingAPI.Events.SaveEvents.BeforeSave += SaveEvents_BeforeSave; StardewModdingAPI.Events.SaveEvents.AfterSave += SaveEvents_AfterSave; @@ -72,20 +72,25 @@ namespace Revitalize newLoc = new List(); } + private void SaveEvents_AfterLoad(object sender, EventArgs e) + { + + } + private void SaveEvents_AfterSave(object sender, EventArgs e) { Serialize.createDirectories(); - Serialize.restoreInventory(); + Serialize.restoreAllModObjects(); } private void SaveEvents_BeforeSave(object sender, EventArgs e) { - Serialize.cleanUpInventory(); + Serialize.cleanUpInventory(); foreach(var v in Lists.trackedObjectList) { Log.AsyncC(v.name); } - Serialize.cleanUpWorld(); + Serialize.cleanUpWorld(); //grabs all of the items that im tracking and serializes them } private void GameEvents_UpdateTick(object sender, EventArgs e) diff --git a/Revitalize/Revitalize/Revitalize/Objects/Decoration.cs b/Revitalize/Revitalize/Revitalize/Objects/Decoration.cs index 177ab72b..837d9f00 100644 --- a/Revitalize/Revitalize/Revitalize/Objects/Decoration.cs +++ b/Revitalize/Revitalize/Revitalize/Objects/Decoration.cs @@ -226,8 +226,8 @@ namespace Revitalize.Objects if (Game1.player.currentLocation is FarmHouse) { // Game1.showRedMessage("Why2?"); - this.heldObject = new Decoration(parentSheetIndex, Vector2.Zero); - Game1.player.addItemByMenuIfNecessary((Item)this.heldObject); + // this.heldObject = new Decoration(parentSheetIndex, Vector2.Zero); + Util.addItemToInventoryAndCleanTrackedList(this); this.flaggedForPickUp = true; this.thisLocation = null; return true; @@ -242,8 +242,8 @@ namespace Revitalize.Objects this.heldObject = new TV(parentSheetIndex, Vector2.Zero); } else { - this.heldObject = new Decoration(parentSheetIndex, Vector2.Zero); - Game1.player.addItemByMenuIfNecessary((Item)this.heldObject); + // this.heldObject = new Decoration(parentSheetIndex, Vector2.Zero); + Util.addItemToInventoryAndCleanTrackedList(this); // this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); // this.heldObject = null; Game1.playSound("coin"); @@ -258,7 +258,7 @@ namespace Revitalize.Objects // Game1.showRedMessage("Why3?"); this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); this.heldObject = null; - Game1.player.addItemByMenuIfNecessary((Item)this); + Util.addItemToInventoryAndCleanTrackedList(this); Game1.playSound("coin"); this.thisLocation = null; return true; diff --git a/Revitalize/Revitalize/Revitalize/Objects/ExtraSeeds.cs b/Revitalize/Revitalize/Revitalize/Objects/ExtraSeeds.cs index 1b31c3dd..be565723 100644 --- a/Revitalize/Revitalize/Revitalize/Objects/ExtraSeeds.cs +++ b/Revitalize/Revitalize/Revitalize/Objects/ExtraSeeds.cs @@ -197,7 +197,7 @@ namespace Revitalize.Objects if (Game1.player.currentLocation is FarmHouse) { // - Game1.player.addItemByMenuIfNecessary(this); + Util.addItemToInventoryAndCleanTrackedList(this); removeLights(this.thisLocation); this.lightsOn = false; Game1.playSound("coin"); @@ -209,7 +209,7 @@ namespace Revitalize.Objects { // return true; // this.heldObject = new ExtraSeeds(parentSheetIndex, Vector2.Zero, this.lightColor, this.inventoryMaxSize); - Game1.player.addItemByMenuIfNecessary(this); + Util.addItemToInventoryAndCleanTrackedList(this); removeLights(this.thisLocation); this.lightsOn = false; Game1.playSound("coin"); @@ -222,7 +222,7 @@ namespace Revitalize.Objects { // Game1.showRedMessage("Why3?"); // if(this.heldObject!=null) Game1.player.addItemByMenuIfNecessary((Item)this.heldObject); - Util.addItemToInventoryElseDrop(this); + Util.addItemToInventoryAndCleanTrackedList(this); this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); this.heldObject = null; Game1.playSound("coin"); diff --git a/Revitalize/Revitalize/Revitalize/Objects/GiftPackage.cs b/Revitalize/Revitalize/Revitalize/Objects/GiftPackage.cs index 1df70dd4..a5935414 100644 --- a/Revitalize/Revitalize/Revitalize/Objects/GiftPackage.cs +++ b/Revitalize/Revitalize/Revitalize/Objects/GiftPackage.cs @@ -194,7 +194,7 @@ namespace Revitalize.Objects if (Game1.player.currentLocation is FarmHouse) { // - Game1.player.addItemByMenuIfNecessary(this); + Util.addItemToInventoryAndCleanTrackedList(this); removeLights(this.thisLocation); this.lightsOn = false; Game1.playSound("coin"); @@ -206,7 +206,7 @@ namespace Revitalize.Objects { // return true; // this.heldObject = new GiftPackage(parentSheetIndex, Vector2.Zero, this.lightColor, this.inventoryMaxSize); - Game1.player.addItemByMenuIfNecessary(this); + Util.addItemToInventoryAndCleanTrackedList(this); removeLights(this.thisLocation); this.lightsOn = false; Game1.playSound("coin"); @@ -219,7 +219,7 @@ namespace Revitalize.Objects { // Game1.showRedMessage("Why3?"); // if(this.heldObject!=null) Game1.player.addItemByMenuIfNecessary((Item)this.heldObject); - Util.addItemToInventoryElseDrop(this); + Util.addItemToInventoryAndCleanTrackedList(this); this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); this.heldObject = null; Game1.playSound("coin"); diff --git a/Revitalize/Revitalize/Revitalize/Objects/Light.cs b/Revitalize/Revitalize/Revitalize/Objects/Light.cs index 5c807df8..1315509b 100644 --- a/Revitalize/Revitalize/Revitalize/Objects/Light.cs +++ b/Revitalize/Revitalize/Revitalize/Objects/Light.cs @@ -188,7 +188,7 @@ namespace Revitalize.Objects if (Game1.player.currentLocation is FarmHouse) { // - Game1.player.addItemByMenuIfNecessary(this); + Util.addItemToInventoryAndCleanTrackedList(this); removeLights(this.thisLocation); this.lightsOn = false; Game1.playSound("coin"); @@ -199,9 +199,9 @@ namespace Revitalize.Objects else { // return true; - // this.heldObject = new Light(parentSheetIndex, Vector2.Zero, this.lightColor, this.inventoryMaxSize); - Game1.player.addItemByMenuIfNecessary(this); - removeLights(this.thisLocation); + // this.heldObject = new Light(parentSheetIndex, Vector2.Zero, this.lightColor, this.inventoryMaxSize); + Util.addItemToInventoryAndCleanTrackedList(this); + removeLights(this.thisLocation); this.lightsOn = false; Game1.playSound("coin"); thisLocation = null; @@ -213,8 +213,8 @@ namespace Revitalize.Objects { // Game1.showRedMessage("Why3?"); // if(this.heldObject!=null) Game1.player.addItemByMenuIfNecessary((Item)this.heldObject); - Util.addItemToInventoryElseDrop(this); - this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); + Util.addItemToInventoryAndCleanTrackedList(this); + this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); this.heldObject = null; Game1.playSound("coin"); removeLights(this.thisLocation); diff --git a/Revitalize/Revitalize/Revitalize/Objects/Machines/Machine.cs b/Revitalize/Revitalize/Revitalize/Objects/Machines/Machine.cs index d82833cd..3f01589c 100644 --- a/Revitalize/Revitalize/Revitalize/Objects/Machines/Machine.cs +++ b/Revitalize/Revitalize/Revitalize/Objects/Machines/Machine.cs @@ -271,7 +271,7 @@ namespace Revitalize.Objects.Machines if (this.heldObject != null) Util.addItemToInventoryElseDrop(this.heldObject.getOne()); this.heldObject = new Machine(parentSheetIndex, Vector2.Zero, this.inventoryMaxSize); - Util.addItemToInventoryElseDrop(this.heldObject.getOne()); + Util.addItemToInventoryAndCleanTrackedList(this); this.heldObject = null; this.flaggedForPickUp = true; this.thisLocation = null; @@ -291,7 +291,7 @@ namespace Revitalize.Objects.Machines // Util.addItemToInventoryElseDrop(this.heldObject); var obj = new Machine(parentSheetIndex, Vector2.Zero, this.inventoryMaxSize); - Util.addItemToInventoryElseDrop(obj); + Util.addItemToInventoryAndCleanTrackedList(this); // this.spillInventoryEverywhere(); if (this.heldObject != null) this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); @@ -309,7 +309,7 @@ namespace Revitalize.Objects.Machines // if(this.heldObject!=null) Game1.player.addItemByMenuIfNecessary((Item)this.heldObject); // this.spillInventoryEverywhere(); var obj = new Machine(parentSheetIndex, Vector2.Zero, this.inventoryMaxSize); - Util.addItemToInventoryElseDrop(obj); + Util.addItemToInventoryAndCleanTrackedList(this); if (this.heldObject != null) this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); this.heldObject = null; Game1.playSound("coin"); diff --git a/Revitalize/Revitalize/Revitalize/Objects/Machines/Quarry.cs b/Revitalize/Revitalize/Revitalize/Objects/Machines/Quarry.cs index 853e8f6d..b43511ec 100644 --- a/Revitalize/Revitalize/Revitalize/Objects/Machines/Quarry.cs +++ b/Revitalize/Revitalize/Revitalize/Objects/Machines/Quarry.cs @@ -336,9 +336,9 @@ namespace Revitalize.Objects.Machines if (clear == true) { inventory.Clear(); - // if (this.output != null) Util.addItemToInventoryElseDrop(this.output.getOne()); + // if (this.output != null) Util.addItemToInventoryElseDrop(this.output.getOne()); //this.output = new Quarry(parentSheetIndex, Vector2.Zero, this.inventoryMaxSize); - Util.addItemToInventoryElseDrop(this); + Util.addItemToInventoryAndCleanTrackedList(this); this.output = null; this.flaggedForPickUp = true; this.thisLocation = null; @@ -356,7 +356,7 @@ namespace Revitalize.Objects.Machines //if (this.output != null) Util.addItemToInventoryElseDrop(this.output.getOne()); this.output = this; - Util.addItemToInventoryElseDrop(this); + Util.addItemToInventoryAndCleanTrackedList(this); this.output = null; this.flaggedForPickUp = true; this.thisLocation = null; @@ -376,9 +376,9 @@ namespace Revitalize.Objects.Machines // Util.addItemToInventoryElseDrop(this.output); var obj = this; - Util.addItemToInventoryElseDrop(obj); + Util.addItemToInventoryAndCleanTrackedList(this); // this.showUI(); - // if (this.output != null) this.output.performRemoveAction(this.tileLocation, who.currentLocation); + // if (this.output != null) this.output.performRemoveAction(this.tileLocation, who.currentLocation); this.output = null; Game1.playSound("coin"); @@ -394,8 +394,8 @@ namespace Revitalize.Objects.Machines // if(this.output!=null) Game1.player.addItemByMenuIfNecessary((Item)this.output); // this.showUI(); var obj = this; - Util.addItemToInventoryElseDrop(obj); - // if (this.output != null) this.output.performRemoveAction(this.tileLocation, who.currentLocation); + Util.addItemToInventoryAndCleanTrackedList(this); + // if (this.output != null) this.output.performRemoveAction(this.tileLocation, who.currentLocation); this.output = null; Game1.playSound("coin"); this.thisLocation = null; diff --git a/Revitalize/Revitalize/Revitalize/Objects/Machines/Spawner.cs b/Revitalize/Revitalize/Revitalize/Objects/Machines/Spawner.cs index a4706d49..eb92f664 100644 --- a/Revitalize/Revitalize/Revitalize/Objects/Machines/Spawner.cs +++ b/Revitalize/Revitalize/Revitalize/Objects/Machines/Spawner.cs @@ -288,7 +288,7 @@ namespace Revitalize.Objects.Machines if(this.heldObject!=null) Util.addItemToInventoryElseDrop(this.heldObject.getOne()); this.heldObject = new Spawner(parentSheetIndex, Vector2.Zero, this.inventoryMaxSize); - Util.addItemToInventoryElseDrop(this.heldObject.getOne()); + Util.addItemToInventoryAndCleanTrackedList(this); this.heldObject = null; this.flaggedForPickUp = true; this.thisLocation = null; @@ -308,9 +308,9 @@ namespace Revitalize.Objects.Machines // Util.addItemToInventoryElseDrop(this.heldObject); var obj = new Spawner(parentSheetIndex, Vector2.Zero,this.inventoryMaxSize); - Util.addItemToInventoryElseDrop(obj); - // this.spillInventoryEverywhere(); - if(this.heldObject!=null) this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); + Util.addItemToInventoryAndCleanTrackedList(this); + // this.spillInventoryEverywhere(); + if (this.heldObject!=null) this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); this.heldObject = null; Game1.playSound("coin"); @@ -326,8 +326,8 @@ namespace Revitalize.Objects.Machines // if(this.heldObject!=null) Game1.player.addItemByMenuIfNecessary((Item)this.heldObject); // this.spillInventoryEverywhere(); var obj = new Spawner(parentSheetIndex, Vector2.Zero, this.inventoryMaxSize); - Util.addItemToInventoryElseDrop(obj); - if(this.heldObject!=null) this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); + Util.addItemToInventoryAndCleanTrackedList(this); + if (this.heldObject!=null) this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); this.heldObject = null; Game1.playSound("coin"); this.thisLocation = null; diff --git a/Revitalize/Revitalize/Revitalize/Objects/shopObject.cs b/Revitalize/Revitalize/Revitalize/Objects/shopObject.cs index 5ef21808..cfc3af52 100644 --- a/Revitalize/Revitalize/Revitalize/Objects/shopObject.cs +++ b/Revitalize/Revitalize/Revitalize/Objects/shopObject.cs @@ -180,7 +180,7 @@ namespace Revitalize.Objects { // Game1.showRedMessage("Why2?"); //this.heldObject = new shopObject(parentSheetIndex, Vector2.Zero); - Game1.player.addItemByMenuIfNecessary(this); + Util.addItemToInventoryAndCleanTrackedList(this); this.flaggedForPickUp = true; this.thisLocation = null; return true; @@ -196,7 +196,7 @@ namespace Revitalize.Objects } else { // this.heldObject = new shopObject(parentSheetIndex, Vector2.Zero); - Game1.player.addItemByMenuIfNecessary(this); + Util.addItemToInventoryAndCleanTrackedList(this); // this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); // this.heldObject = null; Game1.playSound("coin"); @@ -211,7 +211,7 @@ namespace Revitalize.Objects // Game1.showRedMessage("Why3?"); this.heldObject.performRemoveAction(this.tileLocation, who.currentLocation); this.heldObject = null; - Game1.player.addItemByMenuIfNecessary((Item)this); + Util.addItemToInventoryAndCleanTrackedList(this); Game1.playSound("coin"); this.thisLocation = null; return true; diff --git a/Revitalize/Revitalize/Revitalize/Serialize.cs b/Revitalize/Revitalize/Revitalize/Serialize.cs index 0bfc8dcb..09343679 100644 --- a/Revitalize/Revitalize/Revitalize/Serialize.cs +++ b/Revitalize/Revitalize/Revitalize/Serialize.cs @@ -160,14 +160,18 @@ namespace Revitalize Log.AsyncM("Done cleaning inventory!"); } - public static void restoreInventory() + /// + /// Restore all of the serialized mod objects from the mod's player's data folders to either the player's inventory or the game world depending where the object was located when it was serialized. + /// + public static void restoreAllModObjects() { // Log.AsyncG(InvPath); ProcessDirectoryForCleanUp(InvPath); try { - ProcessDirectoryForCleanUp(objectsInWorldPath); + Lists.trackedObjectList.Clear(); //clear whatever mod objects I'm tracking + ProcessDirectoryForCleanUp(objectsInWorldPath); //restore whatever I'm tracking here when I replace the object back into the world. This also works when loading up the game, not just when saving/loading } catch(Exception e) { @@ -175,6 +179,10 @@ namespace Revitalize } } + /// + /// Cleans up the mod files by deleting possible old files for objects that may/may not exist. Aka delete everything before serializing everything again. + /// + /// public static void ProcessDirectoryForDeletion(string targetDirectory) { // Process the list of files found in the directory. @@ -188,7 +196,7 @@ namespace Revitalize // Recurse into subdirectories of this directory. string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory); foreach (string subdirectory in subdirectoryEntries) - ProcessDirectoryForCleanUp(subdirectory); + ProcessDirectoryForDeletion(subdirectory); } @@ -245,15 +253,15 @@ namespace Revitalize } else { - cObj.thisLocation.objects.Add(cObj.tileLocation, cObj); + cObj.thisLocation.objects.Add(cObj.tileLocation, cObj); + Lists.trackedObjectList.Add(cObj); + //Util.placementAction(cObj, cObj.thisLocation,(int)cObj.tileLocation.X,(int) cObj.tileLocation.Y,null,false); } } } } - - } diff --git a/Revitalize/Revitalize/Revitalize/Util.cs b/Revitalize/Revitalize/Revitalize/Util.cs index 2d91c29d..ee2255e4 100644 --- a/Revitalize/Revitalize/Revitalize/Util.cs +++ b/Revitalize/Revitalize/Revitalize/Util.cs @@ -66,6 +66,25 @@ namespace Revitalize } } + public static bool addItemToInventoryAndCleanTrackedList(CoreObject I) + { + if (Game1.player.isInventoryFull() == false) + { + Game1.player.addItemToInventoryBool(I, false); + Lists.trackedObjectList.Remove(I); + return true; + } + else + { + Random random = new Random(129); + int i = random.Next(); + i = i % 4; + Vector2 v2 = new Vector2(Game1.player.getTileX() * Game1.tileSize, Game1.player.getTileY() * Game1.tileSize); + Game1.createItemDebris(I, v2, i); + return false; + } + } + public static bool addItemToInventoryElseUseMenu (List I) { @@ -91,7 +110,7 @@ namespace Revitalize } - public static bool placementAction(CoreObject cObj, GameLocation location, int x, int y, Farmer who = null) + public static bool placementAction(CoreObject cObj, GameLocation location, int x, int y, Farmer who = null, bool playSound=true) { Vector2 vector = new Vector2((float)(x / Game1.tileSize), (float)(y / Game1.tileSize)); // cObj.health = 10; @@ -659,7 +678,11 @@ namespace Revitalize } @object.initializeLightSource(vector); } - Game1.playSound("woodyStep"); + if(playSound==true) Game1.playSound("woodyStep"); + else + { + Log.AsyncG("restoring item from file"); + } //Log.AsyncM("Placed and object"); cObj.locationsName = location.name; Lists.trackedObjectList.Add(cObj);