summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Events/PlayerEvents.cs
blob: 99bdac16b2c86dfa2d567ad5117d8819c099babb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Linq;
using StardewModdingAPI.Framework;
using StardewValley;
using SFarmer = StardewValley.Farmer;

namespace StardewModdingAPI.Events
{
    /// <summary>Events raised when the player data changes.</summary>
    public static class PlayerEvents
    {
        /*********
        ** Events
        *********/
        /// <summary>Raised after the player loads a saved game.</summary>
        [Obsolete("Use " + nameof(SaveEvents) + "." + nameof(SaveEvents.AfterLoad) + " instead")]
        public static event EventHandler<EventArgsLoadedGameChanged> LoadedGame;

        /// <summary>Raised after the game assigns a new player character. This happens just before <see cref="LoadedGame"/>; it's unclear how this would happen any other time.</summary>
        [Obsolete("should no longer be used")]
        public static event EventHandler<EventArgsFarmerChanged> FarmerChanged;

        /// <summary>Raised after the player's inventory changes in any way (added or removed item, sorted, etc).</summary>
        public static event EventHandler<EventArgsInventoryChanged> InventoryChanged;

        /// <summary> 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.</summary>
        public static event EventHandler<EventArgsLevelUp> LeveledUp;


        /*********
        ** Internal methods
        *********/
        /// <summary>Raise a <see cref="LoadedGame"/> event.</summary>
        /// <param name="monitor">Encapsulates monitoring and logging.</param>
        /// <param name="loaded">Whether the save has been loaded. This is always true.</param>
        internal static void InvokeLoadedGame(IMonitor monitor, EventArgsLoadedGameChanged loaded)
        {
            if (PlayerEvents.LoadedGame == null)
                return;

            string name = $"{nameof(PlayerEvents)}.{nameof(PlayerEvents.LoadedGame)}";
            Delegate[] handlers = PlayerEvents.LoadedGame.GetInvocationList();

            Program.DeprecationManager.WarnForEvent(handlers, name, "1.6", DeprecationLevel.Notice);
            monitor.SafelyRaiseGenericEvent(name, handlers, null, loaded);
        }

        /// <summary>Raise a <see cref="FarmerChanged"/> event.</summary>
        /// <param name="monitor">Encapsulates monitoring and logging.</param>
        /// <param name="priorFarmer">The previous player character.</param>
        /// <param name="newFarmer">The new player character.</param>
        internal static void InvokeFarmerChanged(IMonitor monitor, SFarmer priorFarmer, SFarmer newFarmer)
        {
            if (PlayerEvents.FarmerChanged == null)
                return;

            string name = $"{nameof(PlayerEvents)}.{nameof(PlayerEvents.FarmerChanged)}";
            Delegate[] handlers = PlayerEvents.FarmerChanged.GetInvocationList();

            Program.DeprecationManager.WarnForEvent(handlers, name, "1.6", DeprecationLevel.Notice);
            monitor.SafelyRaiseGenericEvent(name, handlers, null, new EventArgsFarmerChanged(priorFarmer, newFarmer));
        }

        /// <summary>Raise an <see cref="InventoryChanged"/> event.</summary>
        /// <param name="monitor">Encapsulates monitoring and logging.</param>
        /// <param name="inventory">The player's inventory.</param>
        /// <param name="changedItems">The inventory changes.</param>
        internal static void InvokeInventoryChanged(IMonitor monitor, List<Item> inventory, IEnumerable<ItemStackChange> changedItems)
        {
            monitor.SafelyRaiseGenericEvent($"{nameof(PlayerEvents)}.{nameof(PlayerEvents.InventoryChanged)}", PlayerEvents.InventoryChanged?.GetInvocationList(), null, new EventArgsInventoryChanged(inventory, changedItems.ToList()));
        }

        /// <summary>Rase a <see cref="LeveledUp"/> event.</summary>
        /// <param name="monitor">Encapsulates monitoring and logging.</param>
        /// <param name="type">The player skill that leveled up.</param>
        /// <param name="newLevel">The new skill level.</param>
        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));
        }
    }
}