quantum-space-buddies/QSB/API/QSBAPI.cs

67 lines
2.3 KiB
C#
Raw Normal View History

using Mirror;
using OWML.Common;
2023-07-29 00:26:12 +00:00
using QSB.API.Messages;
using QSB.Messaging;
using QSB.Player;
2023-08-02 21:07:44 +00:00
using System;
using System.Linq;
2023-09-08 10:23:01 +00:00
using QSB.HUD;
using QSB.HUD.Messages;
2023-07-31 19:51:20 +00:00
using UnityEngine.Events;
2023-09-08 10:23:01 +00:00
using UnityEngine;
2023-07-29 00:26:12 +00:00
namespace QSB.API;
2023-07-29 23:34:46 +00:00
public class QSBAPI : IQSBAPI
2023-07-29 00:26:12 +00:00
{
2023-07-31 20:22:54 +00:00
public void RegisterRequiredForAllPlayers(IModBehaviour mod)
{
var uniqueName = mod.ModHelper.Manifest.UniqueName;
QSBCore.Addons.Add(uniqueName, mod);
}
2023-08-02 01:08:09 +00:00
public bool GetIsHost() => QSBCore.IsHost;
2023-08-02 03:42:27 +00:00
public bool GetIsInMultiplayer() => QSBCore.IsInMultiplayer;
public uint GetLocalPlayerID() => QSBPlayerManager.LocalPlayerId;
2023-07-31 19:51:20 +00:00
public string GetPlayerName(uint playerId) => QSBPlayerManager.GetPlayer(playerId).Name;
public uint[] GetPlayerIDs() => QSBPlayerManager.PlayerList.Select(x => x.PlayerId).ToArray();
public UnityEvent<uint> OnPlayerJoin() => QSBAPIEvents.OnPlayerJoinEvent;
public UnityEvent<uint> OnPlayerLeave() => QSBAPIEvents.OnPlayerLeaveEvent;
2023-07-29 00:26:12 +00:00
2023-08-02 21:07:44 +00:00
public void SetCustomData<T>(uint playerId, string key, T data) => QSBPlayerManager.GetPlayer(playerId).SetCustomData(key, data);
public T GetCustomData<T>(uint playerId, string key) => QSBPlayerManager.GetPlayer(playerId).GetCustomData<T>(key);
2023-07-29 00:26:12 +00:00
2023-07-31 21:44:42 +00:00
public void SendMessage<T>(string messageType, T data, uint to = uint.MaxValue, bool receiveLocally = false)
=> new AddonDataMessage(messageType.GetStableHashCode(), data, receiveLocally) { To = to }.Send();
2023-07-29 00:26:12 +00:00
public void RegisterHandler<T>(string messageType, Action<uint, T> handler)
=> AddonDataManager.RegisterHandler(messageType.GetStableHashCode(), handler);
2023-09-08 10:23:01 +00:00
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();
}
2023-07-31 19:51:20 +00:00
}
internal static class QSBAPIEvents
{
static QSBAPIEvents()
2023-07-29 00:26:12 +00:00
{
2023-08-02 19:25:04 +00:00
QSBPlayerManager.OnAddPlayer += player => OnPlayerJoinEvent.Invoke(player.PlayerId);
QSBPlayerManager.OnRemovePlayer += player => OnPlayerLeaveEvent.Invoke(player.PlayerId);
2023-07-29 00:26:12 +00:00
}
2023-07-31 19:51:20 +00:00
2023-07-31 20:34:59 +00:00
internal class PlayerEvent : UnityEvent<uint> { }
2023-08-02 10:36:09 +00:00
2023-08-02 19:03:10 +00:00
internal static readonly PlayerEvent OnPlayerJoinEvent = new();
internal static readonly PlayerEvent OnPlayerLeaveEvent = new();
2023-07-29 00:26:12 +00:00
}