From 0d760f5cfe2c0df790d2b6815e2167c3117b6336 Mon Sep 17 00:00:00 2001 From: JoshuaNavarro Date: Sat, 31 Aug 2019 18:23:47 -0700 Subject: [PATCH] Got farmhand inventory to be reconstructed even when farmhand is offline. --- .../Framework/Objects/ObjectManager.cs | 101 +++++++++++++- .../Objects/Resources/OreVeins/OreVeinTile.cs | 2 + .../Utilities/Serialization/Serialization.cs | 34 +++++ GeneralMods/Revitalize/ModCore.cs | 127 ++++++++++-------- 4 files changed, 208 insertions(+), 56 deletions(-) diff --git a/GeneralMods/Revitalize/Framework/Objects/ObjectManager.cs b/GeneralMods/Revitalize/Framework/Objects/ObjectManager.cs index f3e8afcd..b4672647 100644 --- a/GeneralMods/Revitalize/Framework/Objects/ObjectManager.cs +++ b/GeneralMods/Revitalize/Framework/Objects/ObjectManager.cs @@ -87,6 +87,8 @@ namespace Revitalize.Framework.Objects this.resources = new ResourceManager(); this.ItemsByName = new Dictionary(); + + ChairMultiTiledObject s = new ChairMultiTiledObject(); } /// @@ -268,7 +270,104 @@ namespace Revitalize.Framework.Objects { } - + + /// + /// Scans all mod items to try to find a match. + /// + /// + /// + /// + public Item getItemByIDAndType(string ID,Type T) + { + + foreach(var v in this.chairs) + { + if(v.Value.GetType()==T && v.Value.info.id == ID) + { + Item I= v.Value.getOne(); + return I; + } + } + + foreach(var v in this.furnitureStorage) + { + if (v.Value.GetType() == T && v.Value.info.id == ID) + { + Item I = v.Value.getOne(); + return I; + } + } + + foreach(var v in this.generic) + { + if (v.Value.GetType() == T && v.Value.info.id == ID) + { + Item I = v.Value.getOne(); + return I; + } + } + + foreach(var v in this.ItemsByName) + { + if (v.Value.GetType() == T && v.Value.info.id == ID) + { + Item I = v.Value.getOne(); + return I; + } + } + foreach(var v in this.lamps) + { + if (v.Value.GetType() == T && v.Value.info.id == ID) + { + Item I = v.Value.getOne(); + return I; + } + } + foreach(var v in this.miscellaneous) + { + if (v.Value.GetType() == T && v.Value.info.id == ID) + { + Item I = v.Value.getOne(); + return I; + } + } + foreach(var v in this.rugs) + { + if (v.Value.GetType() == T && v.Value.info.id == ID) + { + Item I = v.Value.getOne(); + return I; + } + } + foreach(var v in this.tables) + { + if (v.Value.GetType() == T && v.Value.info.id == ID) + { + Item I = v.Value.getOne(); + return I; + } + } + + foreach(var v in this.resources.ores) + { + if (v.Value.GetType() == T && v.Value.info.id == ID) + { + Item I = v.Value.getOne(); + return I; + } + + } + foreach(var v in this.resources.oreVeins) + { + if (v.Value.GetType() == T && v.Value.info.id == ID) + { + Item I = v.Value.getOne(); + return I; + } + } + + return null; + } } } diff --git a/GeneralMods/Revitalize/Framework/Objects/Resources/OreVeins/OreVeinTile.cs b/GeneralMods/Revitalize/Framework/Objects/Resources/OreVeins/OreVeinTile.cs index b4f37016..d3d716e8 100644 --- a/GeneralMods/Revitalize/Framework/Objects/Resources/OreVeins/OreVeinTile.cs +++ b/GeneralMods/Revitalize/Framework/Objects/Resources/OreVeins/OreVeinTile.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; +using Newtonsoft.Json; using PyTK.CustomElementHandler; using Revitalize.Framework.Objects.InformationFiles; using Revitalize.Framework.Utilities; @@ -40,6 +41,7 @@ namespace Revitalize.Framework.Objects.Resources.OreVeins } + [JsonIgnore] public override string ItemInfo { get diff --git a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs index faa54ebf..b4341ed1 100644 --- a/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs +++ b/GeneralMods/Revitalize/Framework/Utilities/Serialization/Serialization.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; +using PyTK.CustomElementHandler; using Revitalize.Framework.Objects; using Revitalize.Framework.Objects.Furniture; using Revitalize.Framework.Utilities.Serialization.ContractResolvers; @@ -305,6 +306,39 @@ namespace Revitalize.Framework.Utilities return I; } + public Item DeserializeFromFarmhandInventory(string JsonName) + { + //ModCore.log("Found a custom object in a chest!"); + string jsonString = JsonName; + //ModCore.log(JsonName); + string dataSplit = jsonString.Split(new string[] { "<" }, StringSplitOptions.None)[2]; + string backUpGUID = dataSplit.Split('|')[0]; + string[] guidArr = jsonString.Split(new string[] { "|" }, StringSplitOptions.None); + + string infoStr = jsonString.Split(new string[] { "<" }, StringSplitOptions.None)[0]; + string guidStr= jsonString.Split(new string[] { "<" }, StringSplitOptions.None)[1]; + + CustomObjectData pyTkData = ModCore.Serializer.DeserializeFromJSONString(dataSplit); + Type t = Type.GetType(pyTkData.type); + string id = pyTkData.id; + //Need Item info + + string guidName = backUpGUID; + string infoSplit = infoStr.Split('|')[3]; + infoSplit = infoSplit.Substring(3); + BasicItemInformation info = ModCore.Serializer.DeserializeFromJSONString(infoSplit); + + CustomObject clone = (CustomObject)ModCore.ObjectManager.getItemByIDAndType(id, t); + if (clone != null) + { + clone.info = info; + ModCore.log("Guid is????:"+guidStr); + clone.guid = Guid.Parse(guidStr); + return clone; + } + return null; + } + public void returnToTitle() { //this.gatherAllFilesForCleanup(); diff --git a/GeneralMods/Revitalize/ModCore.cs b/GeneralMods/Revitalize/ModCore.cs index 13dee0c8..34045f16 100644 --- a/GeneralMods/Revitalize/ModCore.cs +++ b/GeneralMods/Revitalize/ModCore.cs @@ -24,17 +24,18 @@ using Revitalize.Framework.Minigame.SeasideScrambleMinigame; using Revitalize.Framework.Objects.Items.Resources; using Revitalize.Framework.Hacks; using Revitalize.Framework.Configs; +using StardewValley.Locations; +using System.Linq; namespace Revitalize { //Bugs: // -Chair tops cut off objects - // -load content MUST be enabled for the table to be placed?????? WTF + // -load content MUST be enabled for the table to be placed?????? // TODO: /* Add in crafting menu. * Add in crafting table. - * Find way to hack vanilla furnace for more recipes. * * // -Make this mod able to load content packs for easier future modding @@ -85,15 +86,17 @@ namespace Revitalize // -Auto Preserves // -Auto Keg // -Auto Cask - // -Calcinator (oil+stone) + // -Calcinator (oil+stone: produces titanum?) // -Materials - // -Tin/Bronze/Alluminum/Silver?Platinum/Etc + // -Tin/Bronze/Alluminum/Silver?Platinum/Etc (all but platinum: may add in at a later date) -titanium -Alloys! - -Brass - -Electrum + -Brass (done) + -Electrum (done) + -Steel (done) + -Bronze (done) -Mythrill - -Steel + -Star Metal -Star Steel -Cobalt @@ -189,7 +192,7 @@ namespace Revitalize Accessories (recover hp/stamina,max hp,more friendship ,run faster, take less damage, etc) - -NEckalces + -Neckalces -Broaches -Earings -Pendants @@ -261,33 +264,79 @@ namespace Revitalize ModHelper.Events.GameLoop.DayEnding += this.GameLoop_DayEnding; ModHelper.Events.GameLoop.Saving += this.GameLoop_Saving; - //Adds in recipes to the mod. VanillaRecipeBook = new VanillaRecipeBook(); - /* - foreach(var v in Game1.objectInformation) - { - string name = v.Value.Split('/')[0]; - ModCore.log(name + "="+v.Key+","+Environment.NewLine,false); - } - */ + ModHelper.Events.Display.MenuChanged += this.Display_MenuChanged; } + private void Display_MenuChanged(object sender, StardewModdingAPI.Events.MenuChangedEventArgs e) + { + if (e.NewMenu != null) + { + ModCore.log(e.NewMenu.GetType()); + + if (e.NewMenu.GetType() == typeof(StardewValley.Menus.ItemGrabMenu)) + { + if (Game1.player.currentLocation is Cabin) + { + ModCore.log("Let's get processing!"); + List> addition = new List>(); + for (int i = 0; i < (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.actualInventory.Count; i++) + { + Item I = (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.actualInventory[i]; + if (I is Chest && I.Name != "Chest") + { + ModCore.log("Found a custom object!"); + Item cObj= ModCore.Serializer.DeserializeFromFarmhandInventory(I.Name); + if (cObj == null) + { + ModCore.log("NULL OBJ"); + } + else + { + ModCore.log("Not null!"); + } + if (cObj == null) continue; + addition.Add(new KeyValuePair(i,cObj)); + } + } + + /* + for(int I=0; I< (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.actualInventory.Count; I++) + { + if((Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.actualInventory[I] == null) + { + if (addition.Count > 0) { + (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.actualInventory[I] = addition[0].Value; + addition.RemoveAt(0); + } + } + } + */ + + foreach(KeyValuePair pair in addition) + { + (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.actualInventory[pair.Key] = pair.Value; + } + + (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu = new InventoryMenu((Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.xPositionOnScreen, (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.yPositionOnScreen, true, (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.actualInventory, (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.highlightMethod, (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.capacity, (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.rows, (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.horizontalGap, (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.verticalGap, (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.drawSlots); + (Game1.activeClickableMenu as ItemGrabMenu).populateClickableComponentList(); + (Game1.activeClickableMenu as ItemGrabMenu).ItemsToGrabMenu.populateClickableComponentList(); + } + } + } + } + private void GameLoop_Saving(object sender, StardewModdingAPI.Events.SavingEventArgs e) { - /* - foreach(var v in CustomObjects) - { - v.Value.updateInfo(); - } - */ + + } private void GameLoop_DayEnding(object sender, StardewModdingAPI.Events.DayEndingEventArgs e) { - //MultiplayerUtilities.RequestALLGuidObjects(); } /// @@ -437,13 +486,6 @@ namespace Revitalize private void GameLoop_SaveLoaded(object sender, StardewModdingAPI.Events.SaveLoadedEventArgs e) { this.loadContent(); - - /* - if (Game1.IsServer || Game1.IsMultiplayer || Game1.IsClient) - { - throw new Exception("Can't run Revitalize in multiplayer due to lack of current support!"); - } - */ Serializer.afterLoad(); ShopHacks.AddOreToClintsShop(); ObjectInteractionHacks.AfterLoad_RestoreTrackedMachines(); @@ -463,32 +505,7 @@ namespace Revitalize axe =(StardewValley.Tools.Axe)Serializer.Deserialize(Path.Combine(this.Helper.DirectoryPath, "AXE.json"),typeof(StardewValley.Tools.Axe)); //Game1.player.addItemToInventory(axe); */ - //Game1.player.addItemToInventory(ObjectManager.resources.ores["Test"].getOne()); - - //Game1.player.addItemToInventory(ObjectManager.resources.getOre("Tin", 19)); - //Ore tin = ObjectManager.resources.getOre("Tin", 19); - //Game1.player.addItemToInventory(ObjectManager.GetItem("TinIngot", 1)); - //Game1.player.addItemToInventory(new StardewValley.Object(388, 100)); - /* - Game1.player.addItemsByMenuIfNecessary(new List() - { - new StardewValley.Object(Vector2.Zero, (int)Enums.SDVBigCraftable.Furnace), - new StardewValley.Object((int)Enums.SDVObject.Coal,10), - new StardewValley.Object((int)Enums.SDVObject.PrismaticShard,5), - new StardewValley.Object((int)Enums.SDVObject.Emerald,1), - new StardewValley.Object((int)Enums.SDVObject.Aquamarine,1), - new StardewValley.Object((int)Enums.SDVObject.Ruby,1), - new StardewValley.Object((int)Enums.SDVObject.Amethyst,1), - new StardewValley.Object((int)Enums.SDVObject.Topaz,1), - new StardewValley.Object((int)Enums.SDVObject.Jade,1), - new StardewValley.Object((int)Enums.SDVObject.Diamond,1), - new StardewValley.Object((int)Enums.SDVObject.IronBar,1), - }); - */ - //ModCore.log("Tin sells for: " + tin.sellToStorePrice()); - - //ObjectManager.resources.spawnOreVein("Omegasis.Revitalize.Resources.Ore.Test", new Vector2(8, 7)); } /*