From 75a70a9f78deb89386a6ae6186a2c724e9176e73 Mon Sep 17 00:00:00 2001 From: Joshua Navarro Date: Wed, 5 Dec 2018 18:06:11 -0800 Subject: [PATCH] Added in file loading for birthday gifts. --- .../Framework/MultiplayerSupport.cs | 3 +- GeneralMods/HappyBirthday/GiftManager.cs | 38 ++++++++++++++++--- GeneralMods/HappyBirthday/HappyBirthday.cs | 13 +++++++ .../HappyBirthday/HappyBirthday.csproj | 2 +- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/GeneralMods/HappyBirthday/Framework/MultiplayerSupport.cs b/GeneralMods/HappyBirthday/Framework/MultiplayerSupport.cs index c8a7c49b..50925633 100644 --- a/GeneralMods/HappyBirthday/Framework/MultiplayerSupport.cs +++ b/GeneralMods/HappyBirthday/Framework/MultiplayerSupport.cs @@ -1,5 +1,4 @@ - -using StardewValley; +using StardewValley; using System; using System.Collections.Generic; using System.Linq; diff --git a/GeneralMods/HappyBirthday/GiftManager.cs b/GeneralMods/HappyBirthday/GiftManager.cs index 7cee60e1..bdd816f2 100644 --- a/GeneralMods/HappyBirthday/GiftManager.cs +++ b/GeneralMods/HappyBirthday/GiftManager.cs @@ -89,20 +89,48 @@ namespace Omegasis.HappyBirthday public GiftManager() { this.BirthdayGifts = new List(); + this.loadVillagerBirthdayGifts(); } - public void loadVillagerBirthdayGifts() + /// + /// Load birthday gift information from disk. Preferably from BirthdayGift.json in the mod's directory. + /// + /// + public Dictionary loadVillagerBirthdayGifts() { - if (File.Exists("BirthdayGifts.json")) + if (HappyBirthday.Config.useLegacyBirthdayFiles == false) { - + if (File.Exists("BirthdayGifts.json")) + { + HappyBirthday.ModMonitor.Log("Load from BirthdayGifts.json"); + this.defaultBirthdayGifts = HappyBirthday.ModHelper.Data.ReadJsonFile>(Path.Combine(HappyBirthday.ModHelper.DirectoryPath, "BirthdayGifts.json")); + return this.defaultBirthdayGifts; + } + else + { + HappyBirthday.ModMonitor.Log("BirthdayGifts.json created from default birthday gift information and can be overridden."); + HappyBirthday.ModHelper.Data.WriteJsonFile>(Path.Combine(HappyBirthday.ModHelper.DirectoryPath, "BirthdayGifts.json"), this.defaultBirthdayGifts); + return defaultBirthdayGifts; + } } else { - HappyBirthday.ModHelper.Data.WriteJsonFile>(HappyBirthday.ModHelper.DirectoryPath, this.defaultBirthdayGifts); + + if (File.Exists(Path.Combine(Game1.content.RootDirectory, "Data", "PossibleBirthdayGifts.xnb"))){ + HappyBirthday.ModMonitor.Log("Legacy loading detected. Attempting to load from StardewValley/Content/Data/PossibleBirthdayGifts.xnb"); + this.defaultBirthdayGifts = Game1.content.Load>(Path.Combine("Data", "PossibleBirthdayGifts")); + return this.defaultBirthdayGifts; + } + else + { + HappyBirthday.ModMonitor.Log("No birthday gift information found. Loading from internal birthday list"); + return this.defaultBirthdayGifts; + } } } + + /// Get the default gift items. @@ -113,7 +141,7 @@ namespace Omegasis.HappyBirthday try { // read from birthday gifts file - IDictionary data = Game1.content.Load>("Data\\BirthdayGifts"); + IDictionary data = loadVillagerBirthdayGifts(); data.TryGetValue(name, out string text); if (text != null) { diff --git a/GeneralMods/HappyBirthday/HappyBirthday.cs b/GeneralMods/HappyBirthday/HappyBirthday.cs index c73c25ee..80ace5cf 100644 --- a/GeneralMods/HappyBirthday/HappyBirthday.cs +++ b/GeneralMods/HappyBirthday/HappyBirthday.cs @@ -64,8 +64,16 @@ namespace Omegasis.HappyBirthday public static IModHelper ModHelper; + public static IMonitor ModMonitor; + + /// + /// Class to handle all birthday messages for this mod. + /// public BirthdayMessages messages; + /// + /// Class to handle all birthday gifts for this mod. + /// public GiftManager giftManager; /********* @@ -89,6 +97,7 @@ namespace Omegasis.HappyBirthday StardewModdingAPI.Events.GraphicsEvents.OnPostRenderHudEvent += GraphicsEvents_OnPostRenderHudEvent; ; //MultiplayerSupport.initializeMultiplayerSupport(); ModHelper = Helper; + ModMonitor = Monitor; messages = new BirthdayMessages(); giftManager = new GiftManager(); @@ -137,6 +146,10 @@ namespace Omegasis.HappyBirthday private void GraphicsEvents_OnPostRenderGuiEvent(object sender, EventArgs e) { if (Game1.activeClickableMenu == null) return; + //Don't do anything if birthday has not been chosen yet. + if (PlayerData == null) return; + if (PlayerData.BirthdaySeason == null || PlayerData.BirthdayDay==0) return; + if (PlayerData.BirthdaySeason.ToLower() != Game1.currentSeason.ToLower()) return; if (Game1.activeClickableMenu is Billboard) { diff --git a/GeneralMods/HappyBirthday/HappyBirthday.csproj b/GeneralMods/HappyBirthday/HappyBirthday.csproj index 6c2773a8..2498969a 100644 --- a/GeneralMods/HappyBirthday/HappyBirthday.csproj +++ b/GeneralMods/HappyBirthday/HappyBirthday.csproj @@ -91,7 +91,7 @@ - +