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

51 lines
1.7 KiB
C#
Raw Normal View History

2023-07-29 00:26:12 +00:00
using System;
2023-07-31 19:51:20 +00:00
using System.Linq;
2023-07-31 20:22:54 +00:00
using OWML.Common;
2023-07-29 00:26:12 +00:00
using QSB.API.Messages;
using QSB.Messaging;
using QSB.Player;
2023-07-31 19:51:20 +00:00
using UnityEngine.Events;
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-07-29 00:26:12 +00:00
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
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);
public void SendMessage<T>(string messageType, T data, bool receiveLocally = false)
2023-07-31 19:51:20 +00:00
=> new AddonDataMessage(messageType, data, receiveLocally).Send();
2023-07-29 00:26:12 +00:00
public void RegisterHandler<T>(string messageType, Action<uint, T> handler)
2023-07-31 19:51:20 +00:00
=> AddonDataManager.RegisterHandler(messageType, handler);
}
internal static class QSBAPIEvents
{
static QSBAPIEvents()
2023-07-29 00:26:12 +00:00
{
2023-07-31 19:51:20 +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
public static UnityEvent<uint> OnPlayerJoinEvent = new PlayerEvent();
public static UnityEvent<uint> OnPlayerLeaveEvent = new PlayerEvent();
2023-07-29 00:26:12 +00:00
}
2023-07-31 19:51:20 +00:00
// i hate OOP sometimes
internal class PlayerEvent : UnityEvent<uint> { }