mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-03-10 16:14:45 +00:00
add chat method + event to api
This commit is contained in:
parent
93b8c00240
commit
7a1a7deb2e
@ -23,6 +23,7 @@ public class APITestMod : ModBehaviour
|
||||
|
||||
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.OnChatMessage().AddListener((string message, uint from) => ModHelper.Console.WriteLine($"Chat message \"{message}\" from {from} ({(from == uint.MaxValue ? "QSB" : qsbAPI.GetPlayerName(from))})"));
|
||||
|
||||
button.onClick.AddListener(() =>
|
||||
{
|
||||
@ -52,6 +53,9 @@ public class APITestMod : ModBehaviour
|
||||
ModHelper.Console.WriteLine("Sending float message test...");
|
||||
qsbAPI.RegisterHandler<float>("apitest-float", MessageHandler);
|
||||
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);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using OWML.Common;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public interface IQSBAPI
|
||||
@ -96,4 +97,23 @@ public interface IQSBAPI
|
||||
void RegisterHandler<T>(string messageType, Action<uint, T> handler);
|
||||
|
||||
#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
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using OWML.Common;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public interface IQSBAPI
|
||||
@ -96,4 +97,23 @@ public interface IQSBAPI
|
||||
void RegisterHandler<T>(string messageType, Action<uint, T> handler);
|
||||
|
||||
#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
|
||||
}
|
||||
|
@ -5,7 +5,10 @@ using QSB.Messaging;
|
||||
using QSB.Player;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using QSB.HUD;
|
||||
using QSB.HUD.Messages;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine;
|
||||
|
||||
namespace QSB.API;
|
||||
|
||||
@ -35,6 +38,17 @@ public class QSBAPI : IQSBAPI
|
||||
|
||||
public void RegisterHandler<T>(string messageType, Action<uint, T> 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
|
||||
|
@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using QSB.Player;
|
||||
using UnityEngine;
|
||||
|
||||
namespace QSB.HUD.Messages;
|
||||
@ -17,5 +18,27 @@ public class ChatMessage : QSBMessage<(string message, Color color)>
|
||||
public override void OnReceiveRemote()
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@ using QSB.WorldSync;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
@ -42,6 +43,9 @@ public class MultiplayerHUDManager : MonoBehaviour, IAddComponentOnStart
|
||||
|
||||
public static readonly ListStack<HUDIcon> HUDIconStack = new(true);
|
||||
|
||||
public class ChatEvent : UnityEvent<string, uint> { }
|
||||
public static readonly ChatEvent OnChatMessageEvent = new();
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Instance = this;
|
||||
@ -87,7 +91,8 @@ public class MultiplayerHUDManager : MonoBehaviour, IAddComponentOnStart
|
||||
// perks of being a qsb dev :-)
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user