diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-11-04 23:18:55 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-11-04 23:18:55 -0500 |
commit | e8276166c3e8d1a0b3a976ef29a00f8e1569cc72 (patch) | |
tree | a78c44e929ff1de70d20b012385c418aea7e78a6 /src/SMAPI/Framework/ModHelpers | |
parent | 688ee69ee64e03aee7a693e6c15092daf229ac5e (diff) | |
parent | b4a5b3829f0f738e5b7e05048068eaec9d2d01d1 (diff) | |
download | SMAPI-e8276166c3e8d1a0b3a976ef29a00f8e1569cc72.tar.gz SMAPI-e8276166c3e8d1a0b3a976ef29a00f8e1569cc72.tar.bz2 SMAPI-e8276166c3e8d1a0b3a976ef29a00f8e1569cc72.zip |
Merge branch 'add-multiplayer-sync' into develop
Diffstat (limited to 'src/SMAPI/Framework/ModHelpers')
-rw-r--r-- | src/SMAPI/Framework/ModHelpers/MultiplayerHelper.cs | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/src/SMAPI/Framework/ModHelpers/MultiplayerHelper.cs b/src/SMAPI/Framework/ModHelpers/MultiplayerHelper.cs index c449a51b..eedad0bc 100644 --- a/src/SMAPI/Framework/ModHelpers/MultiplayerHelper.cs +++ b/src/SMAPI/Framework/ModHelpers/MultiplayerHelper.cs @@ -1,4 +1,6 @@ +using System; using System.Collections.Generic; +using StardewModdingAPI.Framework.Networking; using StardewValley; namespace StardewModdingAPI.Framework.ModHelpers @@ -25,16 +27,50 @@ namespace StardewModdingAPI.Framework.ModHelpers this.Multiplayer = multiplayer; } + /// <summary>Get a new multiplayer ID.</summary> + public long GetNewID() + { + return this.Multiplayer.getNewID(); + } + /// <summary>Get the locations which are being actively synced from the host.</summary> public IEnumerable<GameLocation> GetActiveLocations() { return this.Multiplayer.activeLocations(); } - /// <summary>Get a new multiplayer ID.</summary> - public long GetNewID() + /// <summary>Get a connected player.</summary> + /// <param name="id">The player's unique ID.</param> + /// <returns>Returns the connected player, or <c>null</c> if no such player is connected.</returns> + public IMultiplayerPeer GetConnectedPlayer(long id) { - return this.Multiplayer.getNewID(); + return this.Multiplayer.Peers.TryGetValue(id, out MultiplayerPeer peer) + ? peer + : null; + } + + /// <summary>Get all connected players.</summary> + public IEnumerable<IMultiplayerPeer> GetConnectedPlayers() + { + return this.Multiplayer.Peers.Values; + } + + /// <summary>Send a message to mods installed by connected players.</summary> + /// <typeparam name="TMessage">The data type. This can be a class with a default constructor, or a value type.</typeparam> + /// <param name="message">The data to send over the network.</param> + /// <param name="messageType">A message type which receiving mods can use to decide whether it's the one they want to handle, like <c>SetPlayerLocation</c>. This doesn't need to be globally unique, since mods should check the originating mod ID.</param> + /// <param name="modIDs">The mod IDs which should receive the message on the destination computers, or <c>null</c> for all mods. Specifying mod IDs is recommended to improve performance, unless it's a general-purpose broadcast.</param> + /// <param name="playerIDs">The <see cref="Farmer.UniqueMultiplayerID" /> values for the players who should receive the message, or <c>null</c> for all players. If you don't need to broadcast to all players, specifying player IDs is recommended to reduce latency.</param> + /// <exception cref="ArgumentNullException">The <paramref name="message"/> or <paramref name="messageType" /> is null.</exception> + 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 + ); } } } |