using System;
using StardewModdingAPI.Framework.Networking;
using StardewModdingAPI.Toolkit.Serialization;
namespace StardewModdingAPI.Events
{
/// Event arguments for an event.
public class ModMessageReceivedEventArgs : EventArgs
{
/*********
** Fields
*********/
/// The underlying message model.
private readonly ModMessageModel Message;
/// The JSON helper used to deserialize models.
private readonly JsonHelper JsonHelper;
/*********
** Accessors
*********/
/// The unique ID of the player from whose computer the message was sent.
public long FromPlayerID => this.Message.FromPlayerID;
/// The unique ID of the mod which sent the message.
public string FromModID => this.Message.FromModID;
/// A message type which can be used to decide whether it's the one you want to handle, like SetPlayerLocation. This doesn't need to be globally unique, so mods should check the .
public string Type => this.Message.Type;
/*********
** Public methods
*********/
/// Construct an instance.
/// The received message.
/// The JSON helper used to deserialize models.
internal ModMessageReceivedEventArgs(ModMessageModel message, JsonHelper jsonHelper)
{
this.Message = message;
this.JsonHelper = jsonHelper;
}
/// Read the message data into the given model type.
/// The message model type.
public TModel ReadAs()
where TModel : notnull
{
return this.Message.Data.ToObject(this.JsonHelper.GetSerializer())
?? throw new InvalidOperationException($"Can't read empty mod message data as a {typeof(TModel).FullName} value.");
}
}
}