using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Framework; using StardewValley; using SFarmer = StardewValley.Farmer; #pragma warning disable 618 // Suppress obsolete-symbol errors in this file. Since several events are marked obsolete, this produces unnecessary warnings. namespace StardewModdingAPI.Events { /// Events raised when the player data changes. public static class PlayerEvents { /********* ** Properties *********/ #if SMAPI_1_x /// Manages deprecation warnings. private static DeprecationManager DeprecationManager; /// The backing field for . [SuppressMessage("ReSharper", "InconsistentNaming")] private static event EventHandler _LoadedGame; /// The backing field for . [SuppressMessage("ReSharper", "InconsistentNaming")] private static event EventHandler _FarmerChanged; #endif /********* ** Events *********/ #if SMAPI_1_x /// Raised after the player loads a saved game. [Obsolete("Use " + nameof(SaveEvents) + "." + nameof(SaveEvents.AfterLoad) + " instead")] public static event EventHandler LoadedGame { add { PlayerEvents.DeprecationManager.Warn($"{nameof(PlayerEvents)}.{nameof(PlayerEvents.LoadedGame)}", "1.6", DeprecationLevel.PendingRemoval); PlayerEvents._LoadedGame += value; } remove => PlayerEvents._LoadedGame -= value; } /// Raised after the game assigns a new player character. This happens just before ; it's unclear how this would happen any other time. [Obsolete("should no longer be used")] public static event EventHandler FarmerChanged { add { PlayerEvents.DeprecationManager.Warn($"{nameof(PlayerEvents)}.{nameof(PlayerEvents.FarmerChanged)}", "1.6", DeprecationLevel.PendingRemoval); PlayerEvents._FarmerChanged += value; } remove => PlayerEvents._FarmerChanged -= value; } #endif /// Raised after the player's inventory changes in any way (added or removed item, sorted, etc). public static event EventHandler InventoryChanged; /// Raised after the player levels up a skill. This happens as soon as they level up, not when the game notifies the player after their character goes to bed. public static event EventHandler LeveledUp; /********* ** Internal methods *********/ #if SMAPI_1_x /// Injects types required for backwards compatibility. /// Manages deprecation warnings. internal static void Shim(DeprecationManager deprecationManager) { PlayerEvents.DeprecationManager = deprecationManager; } /// Raise a event. /// Encapsulates monitoring and logging. /// Whether the save has been loaded. This is always true. internal static void InvokeLoadedGame(IMonitor monitor, EventArgsLoadedGameChanged loaded) { monitor.SafelyRaiseGenericEvent($"{nameof(PlayerEvents)}.{nameof(PlayerEvents.LoadedGame)}", PlayerEvents._LoadedGame?.GetInvocationList(), null, loaded); } /// Raise a event. /// Encapsulates monitoring and logging. /// The previous player character. /// The new player character. internal static void InvokeFarmerChanged(IMonitor monitor, SFarmer priorFarmer, SFarmer newFarmer) { monitor.SafelyRaiseGenericEvent($"{nameof(PlayerEvents)}.{nameof(PlayerEvents.FarmerChanged)}", PlayerEvents._FarmerChanged?.GetInvocationList(), null, new EventArgsFarmerChanged(priorFarmer, newFarmer)); } #endif /// Raise an event. /// Encapsulates monitoring and logging. /// The player's inventory. /// The inventory changes. internal static void InvokeInventoryChanged(IMonitor monitor, List inventory, IEnumerable changedItems) { monitor.SafelyRaiseGenericEvent($"{nameof(PlayerEvents)}.{nameof(PlayerEvents.InventoryChanged)}", PlayerEvents.InventoryChanged?.GetInvocationList(), null, new EventArgsInventoryChanged(inventory, changedItems.ToList())); } /// Rase a event. /// Encapsulates monitoring and logging. /// The player skill that leveled up. /// The new skill level. internal static void InvokeLeveledUp(IMonitor monitor, EventArgsLevelUp.LevelType type, int newLevel) { monitor.SafelyRaiseGenericEvent($"{nameof(PlayerEvents)}.{nameof(PlayerEvents.LeveledUp)}", PlayerEvents.LeveledUp?.GetInvocationList(), null, new EventArgsLevelUp(type, newLevel)); } } }