Files
RAGECOOP-V/Server/Scripting/BaseScript.cs

101 lines
3.2 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System;
2022-06-22 14:18:20 +08:00
using System.Linq;
2022-10-23 19:02:39 +08:00
using RageCoop.Core.Scripting;
2022-06-22 14:18:20 +08:00
2022-10-23 19:02:39 +08:00
namespace RageCoop.Server.Scripting;
internal class BaseScript : ServerScript
2022-06-22 14:18:20 +08:00
{
2022-10-23 19:02:39 +08:00
private readonly Server Server;
public BaseScript(Server server)
2022-06-22 14:18:20 +08:00
{
2022-10-23 19:02:39 +08:00
Server = server;
}
public override void OnStart()
{
API.RegisterCustomEventHandler(CustomEvents.NativeResponse, NativeResponse);
API.RegisterCustomEventHandler(CustomEvents.OnVehicleDeleted,
e => { API.Entities.RemoveVehicle((int)e.Args[0]); });
API.RegisterCustomEventHandler(CustomEvents.OnPedDeleted, e => { API.Entities.RemovePed((int)e.Args[0]); });
API.RegisterCustomEventHandler(CustomEvents.WeatherTimeSync, e =>
2022-06-22 14:18:20 +08:00
{
2022-10-23 19:02:39 +08:00
if (Server.Settings.WeatherTimeSync)
{
2022-10-23 19:02:39 +08:00
if (e.Client != API.Host)
2022-07-10 16:13:08 +08:00
{
2022-10-23 19:02:39 +08:00
e.Client.SendCustomEvent(CustomEvents.IsHost, false);
return;
}
2022-08-21 12:37:37 +08:00
2022-10-23 19:02:39 +08:00
foreach (var c in API.GetAllClients().Values)
{
if (c == e.Client) continue;
c.SendCustomEventQueued(CustomEvents.WeatherTimeSync, e.Args);
2022-07-10 16:13:08 +08:00
}
}
2022-10-23 19:02:39 +08:00
});
API.RegisterCustomEventHandler(CustomEvents.OnPlayerDied,
e =>
2022-07-02 17:14:56 +08:00
{
2022-10-23 19:02:39 +08:00
API.SendCustomEventQueued(API.GetAllClients().Values.Where(x => x != e.Client).ToList(),
CustomEvents.OnPlayerDied, e.Client.Username);
});
API.Events.OnChatMessage += (s, e) =>
Server.Logger?.Info((e.Client?.Username ?? e.ClaimedSender ?? "Unknown") + ": " + e.Message);
}
public override void OnStop()
{
}
public static void SetAutoRespawn(Client c, bool toggle)
{
c.SendCustomEvent(CustomEvents.SetAutoRespawn, toggle);
}
public void SetNameTag(Client c, bool toggle)
{
foreach (var other in API.GetAllClients().Values)
2022-07-03 15:28:28 +08:00
{
2022-10-23 19:02:39 +08:00
if (c == other) continue;
other.SendCustomEvent(CustomEvents.SetDisplayNameTag, c.Player.ID, toggle);
2022-07-03 15:28:28 +08:00
}
2022-10-23 19:02:39 +08:00
}
public void SendServerPropsTo(List<ServerProp> objects, List<Client> clients = null)
{
foreach (var obj in objects)
API.SendCustomEventQueued(clients, CustomEvents.ServerPropSync, obj.ID, obj.Model, obj.Position,
obj.Rotation);
}
public void SendServerBlipsTo(List<ServerBlip> objects, List<Client> clients = null)
{
foreach (var obj in objects)
API.SendCustomEventQueued(clients, CustomEvents.ServerBlipSync, obj.ID, (ushort)obj.Sprite, (byte)obj.Color,
obj.Scale, obj.Position, obj.Rotation, obj.Name);
}
2022-09-08 12:41:56 -07:00
2022-10-23 19:02:39 +08:00
private void NativeResponse(CustomEventReceivedArgs e)
{
try
2022-06-23 09:46:38 +08:00
{
2022-10-23 19:02:39 +08:00
var id = (int)e.Args[0];
lock (e.Client.Callbacks)
2022-06-23 09:46:38 +08:00
{
2022-10-23 19:02:39 +08:00
if (e.Client.Callbacks.TryGetValue(id, out var callback))
2022-06-23 09:46:38 +08:00
{
2022-10-23 19:02:39 +08:00
callback(e.Args[1]);
e.Client.Callbacks.Remove(id);
2022-06-23 09:46:38 +08:00
}
}
2022-10-23 19:02:39 +08:00
}
catch (Exception ex)
{
API.Logger.Error("Failed to parse NativeResponse");
API.Logger.Error(ex);
2022-06-23 09:46:38 +08:00
}
2022-06-22 14:18:20 +08:00
}
2022-10-23 19:02:39 +08:00
}