quantum-space-buddies/APITestMod/APITestMod.cs

67 lines
2.6 KiB
C#
Raw Normal View History

2023-07-31 19:51:20 +00:00
using OWML.Common;
using OWML.ModHelper;
2023-07-31 18:55:17 +00:00
using UnityEngine;
using UnityEngine.SceneManagement;
namespace APITestMod;
public class APITestMod : ModBehaviour
{
public void Start()
{
var qsbAPI = ModHelper.Interaction.TryGetModApi<IQSBAPI>("Raicuparta.QuantumSpaceBuddies");
var menuFrameworkAPI = ModHelper.Interaction.TryGetModApi<IMenuAPI>("_nebula.MenuFramework");
LoadManager.OnCompleteSceneLoad += (oldScene, newScene) =>
{
if (newScene != OWScene.SolarSystem)
{
return;
}
var button = menuFrameworkAPI.PauseMenu_MakeSimpleButton("QSB Api Test");
2023-07-31 19:51:20 +00:00
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));
2023-09-08 10:23:01 +00:00
qsbAPI.OnChatMessage().AddListener((string message, uint from) => ModHelper.Console.WriteLine($"Chat message \"{message}\" from {from} ({(from == uint.MaxValue ? "QSB" : qsbAPI.GetPlayerName(from))})"));
2023-07-31 19:51:20 +00:00
qsbAPI.RegisterHandler<string>("apitest-string", MessageHandler);
qsbAPI.RegisterHandler<int>("apitest-int", MessageHandler);
qsbAPI.RegisterHandler<float>("apitest-float", MessageHandler);
2023-07-31 18:55:17 +00:00
button.onClick.AddListener(() =>
{
ModHelper.Console.WriteLine("TESTING QSB API!");
ModHelper.Console.WriteLine($"Local Player ID : {qsbAPI.GetLocalPlayerID()}");
2023-07-31 19:51:20 +00:00
ModHelper.Console.WriteLine("Player IDs :");
foreach (var playerID in qsbAPI.GetPlayerIDs())
{
ModHelper.Console.WriteLine($" - id:{playerID} name:{qsbAPI.GetPlayerName(playerID)}");
}
2023-07-31 18:55:17 +00:00
ModHelper.Console.WriteLine("Setting custom data as \"QSB TEST STRING\"");
qsbAPI.SetCustomData(qsbAPI.GetLocalPlayerID(), "APITEST.TESTSTRING", "QSB TEST STRING");
ModHelper.Console.WriteLine($"Retreiving custom data : {qsbAPI.GetCustomData<string>(qsbAPI.GetLocalPlayerID(), "APITEST.TESTSTRING")}");
ModHelper.Console.WriteLine("Sending string message test...");
2023-07-31 21:49:32 +00:00
qsbAPI.SendMessage("apitest-string", "STRING MESSAGE", receiveLocally: true);
2023-07-31 18:55:17 +00:00
ModHelper.Console.WriteLine("Sending int message test...");
2023-07-31 21:49:32 +00:00
qsbAPI.SendMessage("apitest-int", 123, receiveLocally: true);
2023-07-31 18:55:17 +00:00
ModHelper.Console.WriteLine("Sending float message test...");
2023-07-31 21:49:32 +00:00
qsbAPI.SendMessage("apitest-float", 3.14f, receiveLocally: true);
2023-09-08 10:23:01 +00:00
qsbAPI.SendChatMessage("Non-system chat message", false, Color.white);
qsbAPI.SendChatMessage("System chat message", true, Color.cyan);
2023-07-31 18:55:17 +00:00
});
};
}
private void MessageHandler<T>(uint from, T data)
=> ModHelper.Console.WriteLine($"Got : {data}");
}