summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModHelpers/MultiplayerHelper.cs
blob: 6900a1d29d9144bc9dc35841ec279d10c81e0f9f (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
using System.Collections.Generic;
using StardewModdingAPI.Framework.Networking;
using StardewValley;

namespace StardewModdingAPI.Framework.ModHelpers
{
    /// <summary>Provides multiplayer utilities.</summary>
    internal class MultiplayerHelper : BaseHelper, IMultiplayerHelper
    {
        /*********
        ** Fields
        *********/
        /// <summary>SMAPI's core multiplayer utility.</summary>
        private readonly SMultiplayer Multiplayer;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="mod">The mod using this instance.</param>
        /// <param name="multiplayer">SMAPI's core multiplayer utility.</param>
        public MultiplayerHelper(IModMetadata mod, SMultiplayer multiplayer)
            : base(mod)
        {
            this.Multiplayer = multiplayer;
        }

        /// <inheritdoc />
        public long GetNewID()
        {
            return this.Multiplayer.getNewID();
        }

        /// <inheritdoc />
        public IEnumerable<GameLocation> GetActiveLocations()
        {
            return this.Multiplayer.activeLocations();
        }

        /// <inheritdoc />
        public IMultiplayerPeer? GetConnectedPlayer(long id)
        {
            return this.Multiplayer.Peers.TryGetValue(id, out MultiplayerPeer? peer)
                ? peer
                : null;
        }

        /// <inheritdoc />
        public IEnumerable<IMultiplayerPeer> GetConnectedPlayers()
        {
            return this.Multiplayer.Peers.Values;
        }

        /// <inheritdoc />
        public void SendMessage<TMessage>(TMessage message, string messageType, string[]? modIDs = null, long[]? playerIDs = null)
        {
            this.Multiplayer.BroadcastModMessage(
                message: message,
                messageType: messageType,
                fromModID: this.ModID,
                toModIDs: modIDs,
                toPlayerIDs: playerIDs
            );
        }
    }
}