add chat method + event to api

This commit is contained in:
_nebula 2023-09-08 11:23:01 +01:00
parent 93b8c00240
commit 7a1a7deb2e
6 changed files with 87 additions and 1 deletions

View File

@ -23,6 +23,7 @@ public class APITestMod : ModBehaviour
qsbAPI.OnPlayerJoin().AddListener((uint playerId) => ModHelper.Console.WriteLine($"{playerId} joined the game!", MessageType.Success)); qsbAPI.OnPlayerJoin().AddListener((uint playerId) => ModHelper.Console.WriteLine($"{playerId} joined the game!", MessageType.Success));
qsbAPI.OnPlayerLeave().AddListener((uint playerId) => ModHelper.Console.WriteLine($"{playerId} left the game!", MessageType.Success)); qsbAPI.OnPlayerLeave().AddListener((uint playerId) => ModHelper.Console.WriteLine($"{playerId} left the game!", MessageType.Success));
qsbAPI.OnChatMessage().AddListener((string message, uint from) => ModHelper.Console.WriteLine($"Chat message \"{message}\" from {from} ({(from == uint.MaxValue ? "QSB" : qsbAPI.GetPlayerName(from))})"));
button.onClick.AddListener(() => button.onClick.AddListener(() =>
{ {
@ -52,6 +53,9 @@ public class APITestMod : ModBehaviour
ModHelper.Console.WriteLine("Sending float message test..."); ModHelper.Console.WriteLine("Sending float message test...");
qsbAPI.RegisterHandler<float>("apitest-float", MessageHandler); qsbAPI.RegisterHandler<float>("apitest-float", MessageHandler);
qsbAPI.SendMessage("apitest-float", 3.14f, receiveLocally: true); qsbAPI.SendMessage("apitest-float", 3.14f, receiveLocally: true);
qsbAPI.SendChatMessage("Non-system chat message", false, Color.white);
qsbAPI.SendChatMessage("System chat message", true, Color.cyan);
}); });
}; };
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using OWML.Common; using OWML.Common;
using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
public interface IQSBAPI public interface IQSBAPI
@ -96,4 +97,23 @@ public interface IQSBAPI
void RegisterHandler<T>(string messageType, Action<uint, T> handler); void RegisterHandler<T>(string messageType, Action<uint, T> handler);
#endregion #endregion
#region Chat
/// <summary>
/// Invoked when a chat message is received.
/// The string is the message body.
/// The uint is the player who sent the message. If it's a system message, this is uint.MaxValue.
/// </summary>
UnityEvent<string, uint> OnChatMessage();
/// <summary>
/// Sends a message in chat.
/// </summary>
/// <param name="message">The text of the message.</param>
/// <param name="systemMessage">If false, the message is sent as if the local player wrote it manually. If true, the message has no player attached to it, like the player join messages.</param>
/// <param name="color">The color of the message.</param>
void SendChatMessage(string message, bool systemMessage, Color color);
#endregion
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using OWML.Common; using OWML.Common;
using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
public interface IQSBAPI public interface IQSBAPI
@ -96,4 +97,23 @@ public interface IQSBAPI
void RegisterHandler<T>(string messageType, Action<uint, T> handler); void RegisterHandler<T>(string messageType, Action<uint, T> handler);
#endregion #endregion
#region Chat
/// <summary>
/// Invoked when a chat message is received.
/// The string is the message body.
/// The uint is the player who sent the message. If it's a system message, this is uint.MaxValue.
/// </summary>
UnityEvent<string, uint> OnChatMessage();
/// <summary>
/// Sends a message in chat.
/// </summary>
/// <param name="message">The text of the message.</param>
/// <param name="systemMessage">If false, the message is sent as if the local player wrote it manually. If true, the message has no player attached to it, like the player join messages.</param>
/// <param name="color">The color of the message.</param>
void SendChatMessage(string message, bool systemMessage, Color color);
#endregion
} }

View File

@ -5,7 +5,10 @@ using QSB.Messaging;
using QSB.Player; using QSB.Player;
using System; using System;
using System.Linq; using System.Linq;
using QSB.HUD;
using QSB.HUD.Messages;
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine;
namespace QSB.API; namespace QSB.API;
@ -35,6 +38,17 @@ public class QSBAPI : IQSBAPI
public void RegisterHandler<T>(string messageType, Action<uint, T> handler) public void RegisterHandler<T>(string messageType, Action<uint, T> handler)
=> AddonDataManager.RegisterHandler(messageType.GetStableHashCode(), handler); => AddonDataManager.RegisterHandler(messageType.GetStableHashCode(), handler);
public UnityEvent<string, uint> OnChatMessage() => MultiplayerHUDManager.OnChatMessageEvent;
public void SendChatMessage(string message, bool systemMessage, Color color)
{
var fromName = systemMessage
? "QSB"
: QSBPlayerManager.LocalPlayer.Name;
new ChatMessage($"{fromName}: {message}", color).Send();
}
} }
internal static class QSBAPIEvents internal static class QSBAPIEvents

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using QSB.Player;
using UnityEngine; using UnityEngine;
namespace QSB.HUD.Messages; namespace QSB.HUD.Messages;
@ -17,5 +18,27 @@ public class ChatMessage : QSBMessage<(string message, Color color)>
public override void OnReceiveRemote() public override void OnReceiveRemote()
{ {
MultiplayerHUDManager.Instance.WriteMessage(Data.message, Data.color); MultiplayerHUDManager.Instance.WriteMessage(Data.message, Data.color);
var fromPlayer = QSBPlayerManager.GetPlayer(From);
var qsb = false;
string name;
if (Data.message.StartsWith("QSB: "))
{
name = "QSB: ";
qsb = true;
}
else if (Data.message.StartsWith($"{fromPlayer.Name}: "))
{
name = $"{fromPlayer.Name}: ";
}
else
{
// uhhh idk what happened
MultiplayerHUDManager.OnChatMessageEvent.Invoke(Data.message, From);
return;
}
var messageWithoutName = Data.message.Remove(Data.message.IndexOf(name), name.Length);
MultiplayerHUDManager.OnChatMessageEvent.Invoke(messageWithoutName, qsb ? uint.MaxValue : From);
} }
} }

View File

@ -9,6 +9,7 @@ using QSB.WorldSync;
using System; using System;
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
using UnityEngine.UI; using UnityEngine.UI;
@ -42,6 +43,9 @@ public class MultiplayerHUDManager : MonoBehaviour, IAddComponentOnStart
public static readonly ListStack<HUDIcon> HUDIconStack = new(true); public static readonly ListStack<HUDIcon> HUDIconStack = new(true);
public class ChatEvent : UnityEvent<string, uint> { }
public static readonly ChatEvent OnChatMessageEvent = new();
private void Start() private void Start()
{ {
Instance = this; Instance = this;
@ -87,7 +91,8 @@ public class MultiplayerHUDManager : MonoBehaviour, IAddComponentOnStart
// perks of being a qsb dev :-) // perks of being a qsb dev :-)
public void WriteSystemMessage(string message, Color color) public void WriteSystemMessage(string message, Color color)
{ {
WriteMessage(message, color); WriteMessage($"QSB: {message}", color);
OnChatMessageEvent.Invoke(message, uint.MaxValue);
} }
public void WriteMessage(string message, Color color) public void WriteMessage(string message, Color color)